|
| 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 | +} |
0 commit comments