Skip to content

Commit 9ccbae8

Browse files
committed
[platform] click "Open" to open app in browser
- add Open button for each app - add WakeLock to make sure backend app can run when phone is active
1 parent 85e852a commit 9ccbae8

File tree

7 files changed

+141
-78
lines changed

7 files changed

+141
-78
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package seven.drawalive.nodebase;
2+
3+
import android.content.Context;
4+
import android.support.v7.app.AlertDialog;
5+
import android.widget.Toast;
6+
7+
public class Alarm {
8+
public static void showMessage(Context context, String text, String title) {
9+
AlertDialog.Builder builder = new AlertDialog.Builder(context);
10+
builder.setMessage(text);
11+
if (title != null) builder.setTitle(title);
12+
builder.create().show();
13+
}
14+
public static void showMessage(Context context, String text) {
15+
showMessage(context, text, null);
16+
}
17+
18+
public static void showToast(Context context, String text) {
19+
Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT).show();
20+
}
21+
}

app/app/src/main/java/seven/drawalive/nodebase/Downloader.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import java.io.IOException;
1010
import java.io.InputStream;
1111
import java.io.OutputStream;
12+
import java.net.HttpURLConnection;
1213
import java.net.MalformedURLException;
1314
import java.net.URL;
14-
import java.net.URLConnection;
1515

1616
public class Downloader {
1717
public static class DownloadTask extends AsyncTask<String, String, String> {
@@ -25,20 +25,24 @@ protected String doInBackground(String... strings) {
2525
String outfile = strings[1];
2626
InputStream download_stream = null;
2727
OutputStream output_stream = null;
28+
HttpURLConnection conn = null;
2829
publishProgress("Starting ...");
2930
try {
3031
URL urlobj = new URL(url);
31-
URLConnection conn = urlobj.openConnection();
32+
conn = (HttpURLConnection)urlobj.openConnection();
33+
if (conn.getResponseCode()/200 != 2) {
34+
throw new IOException("server error: " + conn.getResponseCode());
35+
}
3236
int file_len = conn.getContentLength();
33-
byte[] buf = new byte[4096];
37+
byte[] buf = new byte[1024*1024];
3438
int read_len = 0, total_read_len = 0;
3539
download_stream = conn.getInputStream();
3640
Storage.unlink(outfile);
3741
Storage.touch(outfile);
3842
output_stream = new FileOutputStream(outfile);
3943
while ((read_len = download_stream.read(buf)) >= 0) {
4044
if (isCancelled()) {
41-
throw new IOException();
45+
throw new IOException("user cancelled");
4246
}
4347
total_read_len += read_len;
4448
output_stream.write(buf, 0, read_len);
@@ -52,14 +56,15 @@ protected String doInBackground(String... strings) {
5256
download_stream.close();
5357
publishProgress("Finishing ...");
5458
} catch (MalformedURLException e) {
55-
return null;
59+
return e.toString();
5660
} catch (IOException e) {
57-
return null;
61+
return e.toString();
5862
} finally {
5963
if (download_stream != null) try {download_stream.close();} catch (IOException e) {}
6064
if (output_stream != null) try {output_stream.close();} catch (IOException e) {}
65+
if (conn != null) conn.disconnect();
6166
}
62-
return outfile;
67+
return null;
6368
}
6469

6570
@Override
@@ -81,10 +86,10 @@ protected void onPostExecute(String result) {
8186
downloader.callback.run();
8287
}
8388
downloader.progress.dismiss();
84-
if (result != null) {
85-
External.showToast(downloader.context,"Download successful");
89+
if (result == null) {
90+
Alarm.showToast(downloader.context,"Download successful");
8691
} else {
87-
External.showToast(downloader.context,"Download failed");
92+
Alarm.showToast(downloader.context,"Download failed: " + result);
8893
}
8994
}
9095

app/app/src/main/java/seven/drawalive/nodebase/External.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.net.Uri;
6-
import android.support.v7.app.AlertDialog;
7-
import android.widget.Toast;
86

97
import java.io.File;
108

@@ -35,18 +33,4 @@ public static void shareInformation(
3533
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3634
context.startActivity(Intent.createChooser(intent, title));
3735
}
38-
39-
public static void showMessage(Context context, String text, String title) {
40-
AlertDialog.Builder builder = new AlertDialog.Builder(context);
41-
builder.setMessage(text);
42-
if (title != null) builder.setTitle(title);
43-
builder.create().show();
44-
}
45-
public static void showMessage(Context context, String text) {
46-
showMessage(context, text, null);
47-
}
48-
49-
public static void showToast(Context context, String text) {
50-
Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT).show();
51-
}
5236
}

app/app/src/main/java/seven/drawalive/nodebase/NodeBase.java

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package seven.drawalive.nodebase;
22

3-
import android.content.Intent;
43
import android.support.v7.app.AppCompatActivity;
54
import android.os.Bundle;
65
import android.text.Editable;
@@ -34,6 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
3433
prepareState();
3534
prepareEvents();
3635
Permission.request(this);
36+
Permission.keepScreen(this, true);
3737

3838
setContentView(view);
3939
if (!Storage.exists(config.nodeBin())) {
@@ -43,7 +43,8 @@ protected void onCreate(Bundle savedInstanceState) {
4343

4444
@Override
4545
protected void onDestroy() {
46-
stopNodeService();
46+
Permission.keepScreen(this, false);
47+
NodeService.stopService(this);
4748
super.onDestroy();
4849
}
4950

@@ -166,7 +167,7 @@ protected void refreshAppList() {
166167
File approot = new File(dirname);
167168
_panelAppList.removeAllViews();
168169
if (!approot.isDirectory()) {
169-
External.showToast(this, String.format("\"%s\" is not a directory", dirname));
170+
Alarm.showToast(this, String.format("\"%s\" is not a directory", dirname));
170171
return;
171172
}
172173
try {
@@ -181,7 +182,7 @@ protected void refreshAppList() {
181182
HashMap<String, Object> env = new HashMap<>();
182183
env.put("appdir", f);
183184
env.put("datadir", config.dataDir());
184-
NodeBaseApp app = new NodeBaseApp(this, new AppAction(this), env);
185+
NodeBaseApp app = new NodeBaseApp(this, env);
185186
_appList.add(app);
186187
_panelAppList.addView(app);
187188
}
@@ -194,49 +195,12 @@ protected void refreshAppList() {
194195
}
195196
}
196197

197-
public static class AppAction {
198-
AppAction(NodeBase nodebase) {
199-
_nodebase = nodebase;
200-
}
201-
202-
public void signal(String[] args) {
203-
_nodebase.sendNodeSignal(args);
204-
}
205-
206-
public void stop(String name) {
207-
if (name == null) {
208-
_nodebase.stopNodeService();
209-
} else {
210-
_nodebase.sendNodeSignal(new String[]{
211-
NodeService.AUTH_TOKEN,
212-
"stop", name
213-
});
214-
}
215-
}
216-
217-
private NodeBase _nodebase;
218-
}
219-
220-
protected void sendNodeSignal(String[] args) {
221-
Log.i("NodeBase:Signal", "Start Service");
222-
Log.i("NodeBase:Signal", String.format("Command - %s", args[1]));
223-
Intent intent = new Intent(this, NodeService.class);
224-
intent.putExtra(NodeService.ARGV, args);
225-
startService(intent);
226-
}
227-
228-
protected void stopNodeService() {
229-
Log.i("NodeBase:Signal", "Stop Service");
230-
Intent intent = new Intent(this, NodeService.class);
231-
stopService(intent);
232-
}
233-
234198
private void copyBinNodeFromNodebaseWorkdir() {
235199
String dirname = config.workDir();
236200
String upgrade_node_filename = String.format("%s/.bin/node", dirname);
237201
File f = new File(upgrade_node_filename);
238202
if (!f.exists()) {
239-
External.showMessage(
203+
Alarm.showMessage(
240204
this,
241205
String.format("%s does not exists.", upgrade_node_filename),
242206
"Upgrade Failed"
@@ -261,7 +225,7 @@ private void showNodeVersion() {
261225
} else {
262226
text = String.format("NodeJS: %s", version);
263227
}
264-
External.showMessage(this, text, "Node Version");
228+
Alarm.showMessage(this, text, "Node Version");
265229
}
266230

267231
private void showNicIps() {
@@ -279,7 +243,7 @@ private void showNicIps() {
279243
nic_list.append('\n');
280244
}
281245
String text = new String(nic_list);
282-
External.showMessage(this, text, "NetworkInterface(s)");
246+
Alarm.showMessage(this, text, "NetworkInterface(s)");
283247
}
284248

285249
private void resetNode() {

app/app/src/main/java/seven/drawalive/nodebase/NodeBaseApp.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import java.io.File;
1717
import java.util.HashMap;
1818

19-
public class NodeBaseApp extends LinearLayout {
20-
public NodeBaseApp(Context context, NodeBase.AppAction delegate, HashMap<String, Object> env) {
19+
public class NodeBaseApp extends LinearLayout implements NodeService.NodeMonitorEvent {
20+
public NodeBaseApp(Context context, HashMap<String, Object> env) {
2121
super(context);
2222
setOrientation(LinearLayout.VERTICAL);
23+
_context = context;
2324
_env = env;
24-
_delegate = delegate;
2525
_appdir = (File)env.get("appdir");
2626

2727
collectAppInformation();
@@ -133,6 +133,10 @@ public void prepareLayout() {
133133
_btnStop.setText("Stop");
134134
_btnStop.setEnabled(false);
135135
subview.addView(_btnStop);
136+
_btnOpen = new Button(context);
137+
_btnOpen.setText("Open");
138+
_btnOpen.setEnabled(false);
139+
subview.addView(_btnOpen);
136140
_btnShare = new Button(context);
137141
_btnShare.setText("Share");
138142
_btnShare.setEnabled(false);
@@ -149,8 +153,10 @@ public void prepareEvents() {
149153
public void onClick(View view) {
150154
_btnStart.setEnabled(false);
151155
_btnStop.setEnabled(true);
156+
_btnOpen.setEnabled(true);
152157
_btnShare.setEnabled(true);
153-
_delegate.signal(
158+
NodeService.touchService(
159+
_context,
154160
new String[]{
155161
NodeService.AUTH_TOKEN,
156162
"start",
@@ -171,14 +177,36 @@ public void onClick(View view) {
171177
public void onClick(View view) {
172178
_btnStart.setEnabled(true);
173179
_btnStop.setEnabled(false);
180+
_btnOpen.setEnabled(false);
174181
_btnShare.setEnabled(false);
175-
_delegate.signal(new String[]{
182+
NodeService.touchService(_context, new String[]{
176183
NodeService.AUTH_TOKEN,
177184
"stop", _appdir.getName()
178185
});
179186
}
180187
});
181188

189+
_btnOpen.setOnClickListener(new OnClickListener() {
190+
@Override
191+
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
205+
);
206+
External.openBrowser(getContext(), url);
207+
}
208+
});
209+
182210
_btnShare.setOnClickListener(new OnClickListener() {
183211
@Override
184212
public void onClick(View view) {
@@ -207,13 +235,41 @@ public String getAppName() {
207235
return _appdir.getName();
208236
}
209237

238+
@Override
239+
public void before(String[] cmd) {
240+
_btnStart.setEnabled(false);
241+
_btnStop.setEnabled(false);
242+
_btnOpen.setEnabled(false);
243+
_btnShare.setEnabled(false);
244+
}
245+
246+
@Override
247+
public void started(String[] cmd, Process process) {
248+
_btnStart.setEnabled(false);
249+
_btnStop.setEnabled(true);
250+
_btnOpen.setEnabled(true);
251+
_btnShare.setEnabled(true);
252+
}
253+
254+
@Override
255+
public void error(String[] cmd, Process process) {
256+
}
257+
258+
@Override
259+
public void after(String[] cmd, Process process) {
260+
_btnStart.setEnabled(true);
261+
_btnStop.setEnabled(false);
262+
_btnOpen.setEnabled(false);
263+
_btnShare.setEnabled(false);
264+
}
265+
210266
private HashMap<String, Object> _env;
211-
private NodeBase.AppAction _delegate;
212267
private File _appdir;
213268
private String[] _appentries;
214-
private Button _btnStart, _btnStop, _btnShare;
269+
private Button _btnStart, _btnStop, _btnOpen, _btnShare;
215270
private Spinner _listEntries;
216271
private EditText _txtParams;
217272
private String _readme;
218273
private NodeBaseAppConfigFile _config;
274+
private Context _context;
219275
}

app/app/src/main/java/seven/drawalive/nodebase/NodeService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import android.app.Service;
5+
import android.content.Context;
56
import android.content.Intent;
67
import android.os.IBinder;
78
import android.support.annotation.Nullable;
@@ -46,6 +47,21 @@ public static String checkOutput(String[] cmd) {
4647
}
4748
}
4849

50+
51+
public static void touchService(Context context, String[] args) {
52+
Log.i("NodeService:Signal", "Start Service");
53+
Log.i("NodeService:Signal", String.format("Command - %s", args[1]));
54+
Intent intent = new Intent(context, NodeService.class);
55+
intent.putExtra(NodeService.ARGV, args);
56+
context.startService(intent);
57+
}
58+
59+
public static void stopService(Context context) {
60+
Log.i("NodeService:Signal", "Stop Service");
61+
Intent intent = new Intent(context, NodeService.class);
62+
context.stopService(intent);
63+
}
64+
4965
public interface NodeMonitorEvent {
5066
void before(String[] cmd);
5167
void started(String[] cmd, Process process);

0 commit comments

Comments
 (0)