16
16
import java .io .File ;
17
17
import java .util .HashMap ;
18
18
19
- public class NodeBaseApp extends LinearLayout implements NodeService . NodeMonitorEvent {
19
+ public class NodeBaseApp extends LinearLayout implements NodeMonitorEvent {
20
20
public NodeBaseApp (Context context , HashMap <String , Object > env ) {
21
21
super (context );
22
22
setOrientation (LinearLayout .VERTICAL );
23
- _context = context ;
24
23
_env = env ;
25
24
_appdir = (File )env .get ("appdir" );
26
25
@@ -93,7 +92,7 @@ public void prepareLayout() {
93
92
94
93
TextView label ;
95
94
label = new TextView (context );
96
- label .setText (String .format ("\n App: %s" , _appdir . getName ()));
95
+ label .setText (String .format ("\n App: %s" , getAppName ()));
97
96
label .setTextSize (TypedValue .COMPLEX_UNIT_SP , 24f );
98
97
contents .addView (label );
99
98
label = new TextView (context );
@@ -114,7 +113,7 @@ public void prepareLayout() {
114
113
tbl_r_t = new TableRow (context );
115
114
_listEntries = new Spinner (context );
116
115
_listEntries .setAdapter (
117
- new ArrayAdapter <String >(
116
+ new ArrayAdapter <>(
118
117
context , android .R .layout .simple_spinner_dropdown_item , _appentries ));
119
118
tbl_r_t .addView (_listEntries );
120
119
_txtParams = new EditText (context );
@@ -151,19 +150,39 @@ public void prepareEvents() {
151
150
_btnStart .setOnClickListener (new OnClickListener () {
152
151
@ Override
153
152
public void onClick (View view ) {
153
+ String appname = getAppName ();
154
154
_btnStart .setEnabled (false );
155
155
_btnStop .setEnabled (true );
156
156
_btnOpen .setEnabled (true );
157
157
_btnShare .setEnabled (true );
158
+ new Thread (new Runnable () {
159
+ @ Override
160
+ public void run () {
161
+ String appname = getAppName ();
162
+ long timestamp = System .currentTimeMillis ();
163
+ while (System .currentTimeMillis () - timestamp < 3000 /* 3s timeout */ ) {
164
+ if (NodeService .services .containsKey (appname )) {
165
+ NodeMonitor monitor = NodeService .services .get (appname );
166
+ if (monitor .isDead ()) {
167
+ // not guarantee but give `after` get chance to run
168
+ // if want to guarantee, `synchronized` isDead
169
+ NodeBaseApp .this .after (monitor .getCommand (), null );
170
+ } else {
171
+ monitor .setEvent (NodeBaseApp .this );
172
+ }
173
+ break ;
174
+ }
175
+ }
176
+ }
177
+ }).start ();
158
178
NodeService .touchService (
159
- _context ,
179
+ getContext () ,
160
180
new String []{
161
181
NodeService .AUTH_TOKEN ,
162
- "start" ,
163
- _appdir .getName (),
182
+ "start" , appname ,
164
183
String .format (
165
184
"%s/node/node %s/%s %s" ,
166
- ( String ) _env .get ("datadir" ),
185
+ _env .get ("datadir" ). toString ( ),
167
186
_appdir .getAbsolutePath (),
168
187
String .valueOf (_listEntries .getSelectedItem ()),
169
188
_txtParams .getText ().toString ()
@@ -179,76 +198,90 @@ public void onClick(View view) {
179
198
_btnStop .setEnabled (false );
180
199
_btnOpen .setEnabled (false );
181
200
_btnShare .setEnabled (false );
182
- NodeService .touchService (_context , new String []{
201
+ NodeService .touchService (getContext () , new String []{
183
202
NodeService .AUTH_TOKEN ,
184
- "stop" , _appdir . getName ()
203
+ "stop" , getAppName ()
185
204
});
186
205
}
187
206
});
188
207
189
208
_btnOpen .setOnClickListener (new OnClickListener () {
190
209
@ Override
191
210
public void onClick (View v ) {
192
- String name = null , protocol = null , port = null , index = null ;
193
- if (_config != null ) {
194
- name = _config .get (null , "name" );
195
- port = _config .get (null , "port" );
196
- protocol = _config .get (null , "protocol" );
197
- index = _config .get (null , "index" );
198
- }
199
- if (name == null ) name = "NodeBase Service" ;
200
- if (port == null ) port = "" ; else port = ":" + port ;
201
- if (protocol == null ) protocol = "http" ;
202
- if (index == null ) index = "" ;
203
- String url = String .format (
204
- "%s://%s%s%s" , protocol , Network .getWifiIpv4 (getContext ()), port , index
211
+ String app_url = String .format (
212
+ generateAppUrlTemplate (),
213
+ Network .getWifiIpv4 (getContext ())
205
214
);
206
- External .openBrowser (getContext (), url );
215
+ External .openBrowser (getContext (), app_url );
207
216
}
208
217
});
209
218
210
219
_btnShare .setOnClickListener (new OnClickListener () {
211
220
@ Override
212
221
public void onClick (View view ) {
213
- String name = null , protocol = null , port = null , index = null ;
214
- if (_config != null ) {
215
- name = _config .get (null , "name" );
216
- port = _config .get (null , "port" );
217
- protocol = _config .get (null , "protocol" );
218
- index = _config .get (null , "index" );
219
- }
220
- if (name == null ) name = "NodeBase Service" ;
221
- if (port == null ) port = "" ; else port = ":" + port ;
222
- if (protocol == null ) protocol = "http" ;
223
- if (index == null ) index = "" ;
222
+ String name = generateAppTitle ();
223
+ String app_url = String .format (
224
+ generateAppUrlTemplate (),
225
+ Network .getWifiIpv4 (getContext ())
226
+ );
224
227
External .shareInformation (
225
228
getContext (), "Share" , "NodeBase" ,
226
- String .format (
227
- "[%s] is running at %s://%s%s%s" ,
228
- name , protocol , Network .getWifiIpv4 (getContext ()), port , index
229
- ), null );
229
+ String .format ("[%s] is running at %s" , name , app_url ), null
230
+ );
230
231
}
231
232
});
232
233
}
233
234
235
+ private String generateAppUrlTemplate () {
236
+ String protocol = null , port = null , index = null ;
237
+ if (_config != null ) {
238
+ port = _config .get (null , "port" );
239
+ protocol = _config .get (null , "protocol" );
240
+ index = _config .get (null , "index" );
241
+ }
242
+ if (port == null ) port = "" ; else port = ":" + port ;
243
+ if (protocol == null ) protocol = "http" ;
244
+ if (index == null ) index = "" ;
245
+ return protocol + "://%s" + String .format ("%s%s" , port , index );
246
+ }
247
+
248
+ private String generateAppTitle () {
249
+ String name = null ;
250
+ if (_config != null ) {
251
+ name = _config .get (null , "name" );
252
+ }
253
+ if (name == null ) name = "NodeBase Service" ;
254
+ return name ;
255
+ }
256
+
234
257
public String getAppName () {
235
258
return _appdir .getName ();
236
259
}
237
260
238
261
@ Override
239
262
public void before (String [] cmd ) {
240
- _btnStart .setEnabled (false );
241
- _btnStop .setEnabled (false );
242
- _btnOpen .setEnabled (false );
243
- _btnShare .setEnabled (false );
263
+ UserInterface .run (new Runnable () {
264
+ @ Override
265
+ public void run () {
266
+ _btnStart .setEnabled (false );
267
+ _btnStop .setEnabled (false );
268
+ _btnOpen .setEnabled (false );
269
+ _btnShare .setEnabled (false );
270
+ }
271
+ });
244
272
}
245
273
246
274
@ Override
247
275
public void started (String [] cmd , Process process ) {
248
- _btnStart .setEnabled (false );
249
- _btnStop .setEnabled (true );
250
- _btnOpen .setEnabled (true );
251
- _btnShare .setEnabled (true );
276
+ UserInterface .run (new Runnable () {
277
+ @ Override
278
+ public void run () {
279
+ _btnStart .setEnabled (false );
280
+ _btnStop .setEnabled (true );
281
+ _btnOpen .setEnabled (true );
282
+ _btnShare .setEnabled (true );
283
+ }
284
+ });
252
285
}
253
286
254
287
@ Override
@@ -257,10 +290,19 @@ public void error(String[] cmd, Process process) {
257
290
258
291
@ Override
259
292
public void after (String [] cmd , Process process ) {
260
- _btnStart .setEnabled (true );
261
- _btnStop .setEnabled (false );
262
- _btnOpen .setEnabled (false );
263
- _btnShare .setEnabled (false );
293
+ UserInterface .run (new Runnable () {
294
+ @ Override
295
+ public void run () {
296
+ _btnStart .setEnabled (true );
297
+ _btnStop .setEnabled (false );
298
+ _btnOpen .setEnabled (false );
299
+ _btnShare .setEnabled (false );
300
+ Alarm .showToast (
301
+ NodeBaseApp .this .getContext (),
302
+ String .format ("\" %s\" stopped" , getAppName ())
303
+ );
304
+ }
305
+ });
264
306
}
265
307
266
308
private HashMap <String , Object > _env ;
@@ -271,5 +313,4 @@ public void after(String[] cmd, Process process) {
271
313
private EditText _txtParams ;
272
314
private String _readme ;
273
315
private NodeBaseAppConfigFile _config ;
274
- private Context _context ;
275
316
}
0 commit comments