1616import java .io .File ;
1717import java .util .HashMap ;
1818
19- public class NodeBaseApp extends LinearLayout implements NodeService . NodeMonitorEvent {
19+ public class NodeBaseApp extends LinearLayout implements NodeMonitorEvent {
2020 public NodeBaseApp (Context context , HashMap <String , Object > env ) {
2121 super (context );
2222 setOrientation (LinearLayout .VERTICAL );
23- _context = context ;
2423 _env = env ;
2524 _appdir = (File )env .get ("appdir" );
2625
@@ -93,7 +92,7 @@ public void prepareLayout() {
9392
9493 TextView label ;
9594 label = new TextView (context );
96- label .setText (String .format ("\n App: %s" , _appdir . getName ()));
95+ label .setText (String .format ("\n App: %s" , getAppName ()));
9796 label .setTextSize (TypedValue .COMPLEX_UNIT_SP , 24f );
9897 contents .addView (label );
9998 label = new TextView (context );
@@ -114,7 +113,7 @@ public void prepareLayout() {
114113 tbl_r_t = new TableRow (context );
115114 _listEntries = new Spinner (context );
116115 _listEntries .setAdapter (
117- new ArrayAdapter <String >(
116+ new ArrayAdapter <>(
118117 context , android .R .layout .simple_spinner_dropdown_item , _appentries ));
119118 tbl_r_t .addView (_listEntries );
120119 _txtParams = new EditText (context );
@@ -151,19 +150,39 @@ public void prepareEvents() {
151150 _btnStart .setOnClickListener (new OnClickListener () {
152151 @ Override
153152 public void onClick (View view ) {
153+ String appname = getAppName ();
154154 _btnStart .setEnabled (false );
155155 _btnStop .setEnabled (true );
156156 _btnOpen .setEnabled (true );
157157 _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 ();
158178 NodeService .touchService (
159- _context ,
179+ getContext () ,
160180 new String []{
161181 NodeService .AUTH_TOKEN ,
162- "start" ,
163- _appdir .getName (),
182+ "start" , appname ,
164183 String .format (
165184 "%s/node/node %s/%s %s" ,
166- ( String ) _env .get ("datadir" ),
185+ _env .get ("datadir" ). toString ( ),
167186 _appdir .getAbsolutePath (),
168187 String .valueOf (_listEntries .getSelectedItem ()),
169188 _txtParams .getText ().toString ()
@@ -179,76 +198,90 @@ public void onClick(View view) {
179198 _btnStop .setEnabled (false );
180199 _btnOpen .setEnabled (false );
181200 _btnShare .setEnabled (false );
182- NodeService .touchService (_context , new String []{
201+ NodeService .touchService (getContext () , new String []{
183202 NodeService .AUTH_TOKEN ,
184- "stop" , _appdir . getName ()
203+ "stop" , getAppName ()
185204 });
186205 }
187206 });
188207
189208 _btnOpen .setOnClickListener (new OnClickListener () {
190209 @ Override
191210 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 ())
205214 );
206- External .openBrowser (getContext (), url );
215+ External .openBrowser (getContext (), app_url );
207216 }
208217 });
209218
210219 _btnShare .setOnClickListener (new OnClickListener () {
211220 @ Override
212221 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+ );
224227 External .shareInformation (
225228 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+ );
230231 }
231232 });
232233 }
233234
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+
234257 public String getAppName () {
235258 return _appdir .getName ();
236259 }
237260
238261 @ Override
239262 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+ });
244272 }
245273
246274 @ Override
247275 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+ });
252285 }
253286
254287 @ Override
@@ -257,10 +290,19 @@ public void error(String[] cmd, Process process) {
257290
258291 @ Override
259292 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+ });
264306 }
265307
266308 private HashMap <String , Object > _env ;
@@ -271,5 +313,4 @@ public void after(String[] cmd, Process process) {
271313 private EditText _txtParams ;
272314 private String _readme ;
273315 private NodeBaseAppConfigFile _config ;
274- private Context _context ;
275316}
0 commit comments