diff options
author | cyBerta <cyberta@riseup.net> | 2020-06-11 02:05:35 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2020-06-11 02:05:35 +0200 |
commit | 69e11a98f00f04d9a6996c3f2c98f9b75b603f55 (patch) | |
tree | e75f9115a76d00802af4a6906e8457fdba8d86a2 /app/src/main/java/se | |
parent | d4c0f2d288aed4ec49a0eaa1e369a98bf34cc1dc (diff) |
ProviderAPI as JobIntentService
Diffstat (limited to 'app/src/main/java/se')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java | 37 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java | 15 |
2 files changed, 32 insertions, 20 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java index 57bea0d3..9d34b38f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java @@ -17,25 +17,32 @@ package se.leap.bitmaskclient; import android.annotation.SuppressLint; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.annotation.NonNull; +import androidx.core.app.JobIntentService; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; /** * Implements HTTP api methods (encapsulated in {{@link ProviderApiManager}}) * used to manage communications with the provider server. * <p/> - * It's an IntentService because it downloads data from the Internet, so it operates in the background. + * It's an JobIntentService because it downloads data from the Internet, so it operates in the background. * * @author parmegv * @author MeanderingCode * @author cyberta */ -public class ProviderAPI extends IntentService implements ProviderApiManagerBase.ProviderApiServiceCallback { +public class ProviderAPI extends JobIntentService implements ProviderApiManagerBase.ProviderApiServiceCallback { + + /** + * Unique job ID for this service. + */ + static final int JOB_ID = 161375; final public static String TAG = ProviderAPI.class.getSimpleName(), @@ -74,10 +81,6 @@ public class ProviderAPI extends IntentService implements ProviderApiManagerBase ProviderApiManager providerApiManager; - public ProviderAPI() { - super(TAG); - } - //TODO: refactor me, please! //used in insecure flavor only @SuppressLint("unused") @@ -91,14 +94,24 @@ public class ProviderAPI extends IntentService implements ProviderApiManagerBase providerApiManager = initApiManager(); } + /** + * Convenience method for enqueuing work in to this service. + */ + static void enqueueWork(Context context, Intent work) { + try { + enqueueWork(context, ProviderAPI.class, JOB_ID, work); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } @Override - public void broadcastEvent(Intent intent) { - LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + protected void onHandleWork(@NonNull Intent command) { + providerApiManager.handleIntent(command); } @Override - protected void onHandleIntent(Intent command) { - providerApiManager.handleIntent(command); + public void broadcastEvent(Intent intent) { + LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } private ProviderApiManager initApiManager() { diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java b/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java index 04a53a47..f3122376 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java @@ -1,9 +1,12 @@ package se.leap.bitmaskclient; -import android.content.*; -import android.os.*; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.ResultReceiver; -import org.jetbrains.annotations.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class ProviderAPICommand { private Context context; @@ -40,11 +43,7 @@ public class ProviderAPICommand { private void execute() { if (isInitialized()) { Intent intent = setUpIntent(); - try { - context.startService(intent); - } catch (IllegalStateException e) { - e.printStackTrace(); - } + ProviderAPI.enqueueWork(context, intent); } } |