Skip to content

Commit 6ef7ec7

Browse files
committed
[platform] redesign and implementation for bakckend
- remove embeded node binary - backend redesign and make code clean - support multiple node js processes running - TODO - download node binary - process state monitoring - ui redesign and implementation
1 parent b512649 commit 6ef7ec7

16 files changed

+788
-565
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ local.properties
4646
.idea
4747
.gradle
4848
build
49+
local

app/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
</activity>
2727

2828
<service
29-
android:name=".NodeBaseServer"
29+
android:name=".NodeService"
3030
android:enabled="true"
3131
android:exported="true">
3232
</service>
3333
</application>
3434

35-
</manifest>
35+
</manifest>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package seven.drawalive.nodebase;
2+
3+
import android.content.Context;
4+
5+
import java.util.HashMap;
6+
7+
8+
public class Configuration {
9+
10+
public static final String NODE_URL = "https://raw.githubusercontent.com/wiki/dna2github/NodeBase/binary/v0/node";
11+
public static final String KEYVAL_NODEBASE_DIR = "nodebase_dir";
12+
13+
public static HashMap<String, String> parse(String text) {
14+
if (text == null) return null;
15+
HashMap<String, String> keyval = new HashMap<>();
16+
String[] lines = text.split("\n");
17+
String key, val;
18+
for (int i = 0, n = lines.length; i < n; i++) {
19+
key = lines[i].trim();
20+
if (key.length() == 0) continue;
21+
i ++;
22+
if (i >= n) break;
23+
char last = key.charAt(key.length()-1);
24+
boolean multiple_line = false;
25+
if (last == '+') {
26+
key = key.substring(0, key.length()-1).trim();
27+
multiple_line = true;
28+
}
29+
if (key.length() == 0) /* after all comments */ break;
30+
if (multiple_line) {
31+
val = "";
32+
for (int j = i; j < n; j++) {
33+
val += "\n" + lines[j];
34+
}
35+
i = n;
36+
} else {
37+
val = lines[i].trim();
38+
}
39+
keyval.put(key, val);
40+
}
41+
return keyval;
42+
}
43+
44+
public Configuration(Context context) {
45+
datadir = context.getApplicationInfo().dataDir;
46+
firstrun = false;
47+
load();
48+
}
49+
50+
public void load() {
51+
String infile = String.format("%s/config", datadir);
52+
keyval = parse(Storage.read(infile));
53+
if (keyval == null) {
54+
firstrun = true;
55+
keyval = new HashMap<>();
56+
}
57+
if (!keyval.containsKey(KEYVAL_NODEBASE_DIR)) {
58+
keyval.put(KEYVAL_NODEBASE_DIR, "/sdcard/.nodebase");
59+
}
60+
}
61+
62+
public void save() {
63+
String outfile = String.format("%s/config", datadir);
64+
StringBuffer buf = new StringBuffer();
65+
String val;
66+
for (String key : keyval.keySet()) {
67+
buf.append(key);
68+
buf.append('\n');
69+
buf.append(" ");
70+
val = keyval.get(key);
71+
if (val == null) val = "";
72+
if (val.indexOf('\n') >= 0) {
73+
val = val.replaceAll("\n", " ");
74+
}
75+
buf.append(val);
76+
}
77+
Storage.write(new String(buf), outfile);
78+
}
79+
80+
public String dataDir() {
81+
return datadir;
82+
}
83+
84+
public String workDir() {
85+
return keyval.get(KEYVAL_NODEBASE_DIR);
86+
}
87+
88+
public String nodeBin() {
89+
return String.format("%s/node/node", datadir);
90+
}
91+
92+
public boolean firstRun() {
93+
return firstrun;
94+
}
95+
96+
public void prepareEnvironment() {
97+
Storage.makeDirectory(String.format("%s/node", datadir));
98+
}
99+
100+
public String get(String key) {
101+
if (keyval.containsKey(key)) return keyval.get(key);
102+
return null;
103+
}
104+
105+
public void set(String key, String val) {
106+
keyval.put(key, val);
107+
}
108+
109+
private boolean firstrun;
110+
private String datadir;
111+
private HashMap<String, String> keyval;
112+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package seven.drawalive.nodebase;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.net.Uri;
6+
7+
import java.io.File;
8+
9+
public class External {
10+
public static void openBrowser(Context context, String url) {
11+
Intent intent = new Intent(Intent.ACTION_SEND);
12+
intent.setAction("android.intent.action.VIEW");
13+
intent.setData(Uri.parse(url));
14+
context.startActivity(intent);
15+
}
16+
17+
public static void shareInformation(
18+
Context context, String title,
19+
String label, String text, String imgFilePath) {
20+
Intent intent = new Intent(Intent.ACTION_SEND);
21+
if (imgFilePath == null || imgFilePath.equals("")) {
22+
intent.setType("text/plain");
23+
} else {
24+
File f = new File(imgFilePath);
25+
if (f != null && f.exists() && f.isFile()) {
26+
intent.setType("image/jpg");
27+
Uri u = Uri.fromFile(f);
28+
intent.putExtra(Intent.EXTRA_STREAM, u);
29+
}
30+
}
31+
intent.putExtra(Intent.EXTRA_SUBJECT, label);
32+
intent.putExtra(Intent.EXTRA_TEXT, text);
33+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
34+
context.startActivity(Intent.createChooser(intent, title));
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package seven.drawalive.nodebase;
2+
3+
import android.content.Context;
4+
import android.net.wifi.WifiInfo;
5+
import android.net.wifi.WifiManager;
6+
7+
import java.net.InterfaceAddress;
8+
import java.net.NetworkInterface;
9+
import java.net.SocketException;
10+
import java.util.Collections;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
14+
public class Network {
15+
public static HashMap<String, String[]> getNicIps() {
16+
HashMap<String, String[]> name_ip = new HashMap<>();
17+
try {
18+
for (NetworkInterface nic :
19+
Collections.list(NetworkInterface.getNetworkInterfaces())) {
20+
List<InterfaceAddress> nic_addr = nic.getInterfaceAddresses();
21+
if (nic_addr.size() == 0) continue;
22+
String[] ips = new String[nic_addr.size()];
23+
String name = nic.getName();
24+
int index = 0;
25+
for (InterfaceAddress ia : nic_addr) {
26+
String addr = ia.getAddress().getHostAddress();
27+
if (addr.indexOf('%') >= 0) {
28+
addr = addr.split("%")[0];
29+
}
30+
ips[index++] = addr;
31+
}
32+
name_ip.put(name, ips);
33+
}
34+
} catch (SocketException e) {
35+
}
36+
return name_ip;
37+
}
38+
39+
public static String getWifiIpv4(Context context) {
40+
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
41+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
42+
int ip = wifiInfo.getIpAddress();
43+
return String.format("%d.%d.%d.%d", ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff);
44+
}
45+
}

0 commit comments

Comments
 (0)