|
| 1 | +package com.socketmobile; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.ComponentName; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.pm.PackageManager; |
| 8 | +import android.net.Uri; |
| 9 | +import android.os.Build; |
| 10 | +import android.util.Log; |
| 11 | + |
| 12 | +import static android.content.Intent.FLAG_RECEIVER_FOREGROUND; |
| 13 | + |
| 14 | +import com.facebook.react.bridge.ReactContext; |
| 15 | + |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +final class CaptureService { |
| 19 | + private static final String BASE_PACKAGE = "com.socketmobile.capture"; |
| 20 | + private static final String SERVICE_APP_ID = "com.socketmobile.companion"; |
| 21 | + private static final String BROADCAST_RECEIVER = BASE_PACKAGE + ".StartService"; |
| 22 | + private static final String ACTION = BASE_PACKAGE + ".START_SERVICE"; |
| 23 | + private static final String SERVICE_NAME = BASE_PACKAGE + ".AndroidService"; |
| 24 | + |
| 25 | + private static final Logger log = Logger.getLogger(CaptureService.class.getName()); |
| 26 | + |
| 27 | + private CaptureService() { |
| 28 | + throw new UnsupportedOperationException( |
| 29 | + this.getClass().getName() + " is a utility class. Do not instantiate."); |
| 30 | + } |
| 31 | + |
| 32 | + static Intent getInstallIntent(Context context) { |
| 33 | + Uri marketUri = Uri.parse("market://details?id=" + SERVICE_APP_ID); |
| 34 | + Uri httpUri = Uri.parse("https://play.google.com/store/apps/details?id=" + SERVICE_APP_ID); |
| 35 | + |
| 36 | + Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri); |
| 37 | + if (systemCanHandleIntent(context, marketIntent, PackageManager.MATCH_DEFAULT_ONLY)) { |
| 38 | + return marketIntent; |
| 39 | + } else { |
| 40 | + return new Intent(Intent.ACTION_VIEW, httpUri); |
| 41 | + } |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @SuppressLint("InlinedApi") static Intent getStartIntent() { |
| 46 | + return new Intent(ACTION) |
| 47 | + .setFlags(FLAG_RECEIVER_FOREGROUND) |
| 48 | + .setComponent(new ComponentName(SERVICE_APP_ID, BROADCAST_RECEIVER)); |
| 49 | + } |
| 50 | + |
| 51 | + static boolean isInstalled(Context context) { |
| 52 | + return systemCanHandleBroadcast(context, getStartIntent(), 0); |
| 53 | + } |
| 54 | + |
| 55 | + static void install(Context context) { |
| 56 | + context.startActivity(getInstallIntent(context)); |
| 57 | + } |
| 58 | + |
| 59 | + public static String start(Context context) { |
| 60 | + try { |
| 61 | + Intent serviceIntent = getExplicitStartIntent(context); |
| 62 | + if (serviceIntent != null) { |
| 63 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 64 | + context.startForegroundService(serviceIntent); |
| 65 | + return "successfully started with startForegroundService"; |
| 66 | + } |
| 67 | + context.sendBroadcast(getStartIntent()); |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + Log.d("CaptureService", "Could not start with startForeground Service: " + e.getMessage()); |
| 71 | + e.printStackTrace(); |
| 72 | + context.sendBroadcast(getStartIntent()); |
| 73 | + return "successfully started with sendBroadcast"; |
| 74 | + } |
| 75 | + return "Error starting service: Unknown error"; |
| 76 | + } |
| 77 | + |
| 78 | + private static Intent getExplicitStartIntent(Context context) { |
| 79 | + return new Intent() |
| 80 | + .setComponent(new ComponentName(SERVICE_APP_ID, SERVICE_NAME)); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean systemCanHandleIntent(Context ctx, Intent i, int flags) { |
| 84 | + return ctx.getPackageManager().queryIntentActivities(i, flags).size() > 0; |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean systemCanHandleBroadcast(Context ctx, Intent i, int flags) { |
| 88 | + return ctx.getPackageManager().queryBroadcastReceivers(i, flags).size() > 0; |
| 89 | + } |
| 90 | +} |
| 91 | + |
0 commit comments