diff options
author | Raman Tenneti <rtenneti@google.com> | 2019-02-20 03:16:31 -0800 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-02-20 03:16:31 -0800 |
commit | b29b72ea05c95c6b4307f2fe5e2f33b00915e021 (patch) | |
tree | e9507677e66c1a50c3c08734c6a7950401b803f7 /src | |
parent | 8d12ad9be311c618cc572d91494e3783d46e101d (diff) | |
parent | 1d423c5fe5e418a7a393611330a3b42d09fd5d85 (diff) | |
download | android_packages_apps_Messaging-b29b72ea05c95c6b4307f2fe5e2f33b00915e021.tar.gz android_packages_apps_Messaging-b29b72ea05c95c6b4307f2fe5e2f33b00915e021.tar.bz2 android_packages_apps_Messaging-b29b72ea05c95c6b4307f2fe5e2f33b00915e021.zip |
AOSP/Messaging - update the Messaging version to target P (28) or higher. Use JobIntentService to start the Backgroundworkerservice and ActionServiceImpl services. am: cdf40bb061 am: b966c435c6
am: 1d423c5fe5
Change-Id: Ibc75e9508f7c82368e282624ba8a06f41a6ce5f8
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/messaging/datamodel/action/ActionServiceImpl.java | 105 | ||||
-rw-r--r-- | src/com/android/messaging/datamodel/action/BackgroundWorkerService.java | 60 |
2 files changed, 72 insertions, 93 deletions
diff --git a/src/com/android/messaging/datamodel/action/ActionServiceImpl.java b/src/com/android/messaging/datamodel/action/ActionServiceImpl.java index a408dac..6725dde 100644 --- a/src/com/android/messaging/datamodel/action/ActionServiceImpl.java +++ b/src/com/android/messaging/datamodel/action/ActionServiceImpl.java @@ -17,7 +17,6 @@ package com.android.messaging.datamodel.action; import android.app.AlarmManager; -import android.app.IntentService; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -25,22 +24,29 @@ import android.content.Intent; import android.os.Bundle; import android.os.SystemClock; +import androidx.core.app.JobIntentService; + import com.android.messaging.Factory; import com.android.messaging.datamodel.DataModel; +import com.android.messaging.util.ConnectivityUtil; import com.android.messaging.util.LogUtil; import com.android.messaging.util.LoggingTimer; -import com.android.messaging.util.WakeLockHelper; import com.google.common.annotations.VisibleForTesting; /** * ActionService used to perform background processing for data model */ -public class ActionServiceImpl extends IntentService { +public class ActionServiceImpl extends JobIntentService { private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; private static final boolean VERBOSE = false; + /** + * Unique job ID for this service. + */ + public static final int JOB_ID = 1000; + public ActionServiceImpl() { - super("ActionService"); + super(); } /** @@ -128,6 +134,7 @@ public class ActionServiceImpl extends IntentService { protected static final String BUNDLE_ACTION = "bundle_action"; private BackgroundWorker mBackgroundWorker; + private ConnectivityUtil mConnectivityUtil; /** * Allocate an intent with a specific opcode. @@ -206,90 +213,70 @@ public class ActionServiceImpl extends IntentService { public void onCreate() { super.onCreate(); mBackgroundWorker = DataModel.get().getBackgroundWorkerForActionService(); - DataModel.get().getConnectivityUtil().registerForSignalStrength(); + mConnectivityUtil = DataModel.get().getConnectivityUtil(); + mConnectivityUtil.registerForSignalStrength(); } @Override public void onDestroy() { super.onDestroy(); - DataModel.get().getConnectivityUtil().unregisterForSignalStrength(); + mConnectivityUtil.unregisterForSignalStrength(); } - private static final String WAKELOCK_ID = "bugle_datamodel_service_wakelock"; - @VisibleForTesting - static WakeLockHelper sWakeLock = new WakeLockHelper(WAKELOCK_ID); - /** - * Queue intent to the ActionService after acquiring wake lock + * Queue intent to the ActionService. */ private static void startServiceWithIntent(final Intent intent) { final Context context = Factory.get().getApplicationContext(); final int opcode = intent.getIntExtra(EXTRA_OP_CODE, 0); - // Increase refCount on wake lock - acquiring if necessary - if (VERBOSE) { - LogUtil.v(TAG, "acquiring wakelock for opcode " + opcode); - } - sWakeLock.acquire(context, intent, opcode); intent.setClass(context, ActionServiceImpl.class); + enqueueWork(context, intent); + } - // TODO: Note that intent will be quietly discarded if it exceeds available rpc - // memory (in total around 1MB). See this article for background - // http://developer.android.com/reference/android/os/TransactionTooLargeException.html - // Perhaps we should keep large structures in the action monitor? - if (context.startService(intent) == null) { - LogUtil.e(TAG, - "ActionService.startServiceWithIntent: failed to start service for intent " - + intent); - sWakeLock.release(intent, opcode); - } + public static void enqueueWork(Context context, Intent work) { + enqueueWork(context, ActionServiceImpl.class, JOB_ID, work); } /** * {@inheritDoc} */ @Override - protected void onHandleIntent(final Intent intent) { + protected void onHandleWork(final Intent intent) { if (intent == null) { // Shouldn't happen but sometimes does following another crash. LogUtil.w(TAG, "ActionService.onHandleIntent: Called with null intent"); return; } final int opcode = intent.getIntExtra(EXTRA_OP_CODE, 0); - sWakeLock.ensure(intent, opcode); - - try { - Action action; - final Bundle actionBundle = intent.getBundleExtra(EXTRA_ACTION_BUNDLE); - actionBundle.setClassLoader(getClassLoader()); - switch(opcode) { - case OP_START_ACTION: { - action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); - executeAction(action); - break; - } - - case OP_RECEIVE_BACKGROUND_RESPONSE: { - action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); - final Bundle response = intent.getBundleExtra(EXTRA_WORKER_RESPONSE); - processBackgroundResponse(action, response); - break; - } - - case OP_RECEIVE_BACKGROUND_FAILURE: { - action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); - processBackgroundFailure(action); - break; - } - - default: - throw new RuntimeException("Unrecognized opcode in ActionServiceImpl"); + + Action action; + final Bundle actionBundle = intent.getBundleExtra(EXTRA_ACTION_BUNDLE); + actionBundle.setClassLoader(getClassLoader()); + switch(opcode) { + case OP_START_ACTION: { + action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); + executeAction(action); + break; + } + + case OP_RECEIVE_BACKGROUND_RESPONSE: { + action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); + final Bundle response = intent.getBundleExtra(EXTRA_WORKER_RESPONSE); + processBackgroundResponse(action, response); + break; } - action.sendBackgroundActions(mBackgroundWorker); - } finally { - // Decrease refCount on wake lock - releasing if necessary - sWakeLock.release(intent, opcode); + case OP_RECEIVE_BACKGROUND_FAILURE: { + action = (Action) actionBundle.getParcelable(BUNDLE_ACTION); + processBackgroundFailure(action); + break; + } + + default: + throw new RuntimeException("Unrecognized opcode in ActionServiceImpl"); } + + action.sendBackgroundActions(mBackgroundWorker); } private static final long EXECUTION_TIME_WARN_LIMIT_MS = 1000; // 1 second diff --git a/src/com/android/messaging/datamodel/action/BackgroundWorkerService.java b/src/com/android/messaging/datamodel/action/BackgroundWorkerService.java index 4d4b150..6096345 100644 --- a/src/com/android/messaging/datamodel/action/BackgroundWorkerService.java +++ b/src/com/android/messaging/datamodel/action/BackgroundWorkerService.java @@ -16,18 +16,18 @@ package com.android.messaging.datamodel.action; -import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import androidx.core.app.JobIntentService; + import com.android.messaging.Factory; import com.android.messaging.datamodel.DataModel; import com.android.messaging.datamodel.DataModelException; import com.android.messaging.util.Assert; import com.android.messaging.util.LogUtil; import com.android.messaging.util.LoggingTimer; -import com.android.messaging.util.WakeLockHelper; import com.google.common.annotations.VisibleForTesting; import java.util.List; @@ -37,18 +37,19 @@ import java.util.List; * Used to actually "send" messages which may take some time and should not block ActionService * or UI */ -public class BackgroundWorkerService extends IntentService { +public class BackgroundWorkerService extends JobIntentService { private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; private static final boolean VERBOSE = false; - private static final String WAKELOCK_ID = "bugle_background_worker_wakelock"; - @VisibleForTesting - static WakeLockHelper sWakeLock = new WakeLockHelper(WAKELOCK_ID); + /** + * Unique job ID for this service. + */ + public static final int JOB_ID = 1001; private final ActionService mHost; public BackgroundWorkerService() { - super("BackgroundWorker"); + super(); mHost = DataModel.get().getActionService(); } @@ -74,7 +75,7 @@ public class BackgroundWorkerService extends IntentService { protected static final String EXTRA_ATTEMPT = "retry_attempt"; /** - * Queue action intent to the BackgroundWorkerService after acquiring wake lock + * Queue action intent to the BackgroundWorkerService. */ private static void startServiceWithAction(final Action action, final int retryCount) { @@ -85,50 +86,41 @@ public class BackgroundWorkerService extends IntentService { } /** - * Queue intent to the BackgroundWorkerService after acquiring wake lock + * Queue intent to the BackgroundWorkerService. */ private static void startServiceWithIntent(final int opcode, final Intent intent) { final Context context = Factory.get().getApplicationContext(); intent.setClass(context, BackgroundWorkerService.class); intent.putExtra(EXTRA_OP_CODE, opcode); - sWakeLock.acquire(context, intent, opcode); - if (VERBOSE) { - LogUtil.v(TAG, "acquiring wakelock for opcode " + opcode); - } - if (context.startService(intent) == null) { - LogUtil.e(TAG, - "BackgroundWorkerService.startServiceWithAction: failed to start service for " - + opcode); - sWakeLock.release(intent, opcode); - } + enqueueWork(context, intent); + } + + public static void enqueueWork(Context context, Intent work) { + enqueueWork(context, BackgroundWorkerService.class, JOB_ID, work); } @Override - protected void onHandleIntent(final Intent intent) { + protected void onHandleWork(final Intent intent) { if (intent == null) { // Shouldn't happen but sometimes does following another crash. LogUtil.w(TAG, "BackgroundWorkerService.onHandleIntent: Called with null intent"); return; } final int opcode = intent.getIntExtra(EXTRA_OP_CODE, 0); - sWakeLock.ensure(intent, opcode); - try { - switch(opcode) { - case OP_PROCESS_REQUEST: { - final Action action = intent.getParcelableExtra(EXTRA_ACTION); - final int attempt = intent.getIntExtra(EXTRA_ATTEMPT, -1); - doBackgroundWork(action, attempt); - break; - } - - default: - throw new RuntimeException("Unrecognized opcode in BackgroundWorkerService"); + switch(opcode) { + case OP_PROCESS_REQUEST: { + final Action action = intent.getParcelableExtra(EXTRA_ACTION); + final int attempt = intent.getIntExtra(EXTRA_ATTEMPT, -1); + doBackgroundWork(action, attempt); + break; } - } finally { - sWakeLock.release(intent, opcode); + + default: + LogUtil.w(TAG, "Unrecognized opcode in BackgroundWorkerService " + opcode); + throw new RuntimeException("Unrecognized opcode in BackgroundWorkerService"); } } |