Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit 8e168c5

Browse files
author
devfd
committed
Android JS services
1 parent fba57a5 commit 8e168c5

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package co.apptailor.Worker;
2+
3+
import android.app.Service;
4+
import android.content.Intent;
5+
import android.os.Handler;
6+
import android.os.IBinder;
7+
import android.os.Looper;
8+
import android.support.annotation.Nullable;
9+
import android.util.Log;
10+
11+
import com.facebook.react.bridge.ReactApplicationContext;
12+
import com.facebook.react.common.ApplicationHolder;
13+
import com.facebook.react.devsupport.DevInternalSettings;
14+
import com.facebook.react.devsupport.DevServerHelper;
15+
import com.facebook.soloader.SoLoader;
16+
17+
import java.io.File;
18+
19+
import co.apptailor.Worker.core.StubDevSupportManager;
20+
21+
public class JSService extends Service {
22+
private static final String TAG = "JSService";
23+
private JSWorker worker;
24+
25+
@Nullable
26+
@Override
27+
public IBinder onBind(Intent intent) {
28+
return null;
29+
}
30+
31+
@Override
32+
public void onCreate() {
33+
super.onCreate();
34+
SoLoader.init(this, /* native exopackage */ false);
35+
try {
36+
ApplicationHolder.getApplication();
37+
}
38+
catch (AssertionError err) {
39+
ApplicationHolder.setApplication(getApplication());
40+
}
41+
}
42+
43+
@Override
44+
public void onDestroy() {
45+
super.onDestroy();
46+
clean();
47+
}
48+
49+
@Override
50+
public int onStartCommand(Intent intent, int flags, int startId) {
51+
Log.d(TAG, "Starting background JS Service");
52+
53+
DevInternalSettings devInternalSettings = new DevInternalSettings(this, new StubDevSupportManager());
54+
devInternalSettings.setHotModuleReplacementEnabled(false);
55+
devInternalSettings.setElementInspectorEnabled(false);
56+
devInternalSettings.setReloadOnJSChangeEnabled(false);
57+
58+
DevServerHelper devServerHelper = new DevServerHelper(devInternalSettings);
59+
60+
String bundleName = "src/service.bundle";
61+
String bundleSlug = bundleName.replaceAll("/", "_");
62+
63+
final File bundleFile = new File(this.getFilesDir(), bundleSlug);
64+
worker = new JSWorker(bundleName, devServerHelper.getSourceUrl(bundleName), bundleFile.getAbsolutePath());
65+
66+
final Handler mainHandler = new Handler(Looper.getMainLooper());
67+
final ReactApplicationContext context = new ReactApplicationContext(getApplicationContext());
68+
69+
devServerHelper.downloadBundleFromURL(new DevServerHelper.BundleDownloadCallback() {
70+
@Override
71+
public void onSuccess() {
72+
mainHandler.post(new Runnable() {
73+
@Override
74+
public void run() {
75+
try {
76+
worker.runFromContext(context);
77+
} catch (Exception e) {
78+
Log.d(TAG, "Error while running service bundle");
79+
e.printStackTrace();
80+
}
81+
}
82+
});
83+
}
84+
85+
@Override
86+
public void onFailure(Exception cause) {
87+
Log.d(TAG, "Error while downloading service bundle");
88+
cause.printStackTrace();
89+
}
90+
}, bundleName, bundleFile);
91+
92+
return Service.START_STICKY;
93+
}
94+
95+
private void clean() {
96+
if (worker != null) {
97+
worker.terminate();
98+
worker = null;
99+
}
100+
}
101+
}

android/src/main/java/co/apptailor/Worker/WorkerModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package co.apptailor.Worker;
22

33
import android.app.Activity;
4+
import android.content.Intent;
45
import android.os.Handler;
56
import android.util.Log;
67

@@ -116,6 +117,14 @@ public void postWorkerMessage(int workerId, String message) {
116117
worker.postMessage(message);
117118
}
118119

120+
@ReactMethod
121+
public void startService() {
122+
Activity activity = getCurrentActivity();
123+
if (activity == null) { return; }
124+
Intent intent = new Intent(activity, JSService.class);
125+
activity.startService(intent);
126+
}
127+
119128
@Override
120129
public void onHostResume() {}
121130

0 commit comments

Comments
 (0)