diff options
author | Sooraj Sasindran <sasindran@google.com> | 2019-04-01 15:30:16 -0700 |
---|---|---|
committer | Sooraj Sasindran <sasindran@google.com> | 2019-04-01 17:04:42 -0700 |
commit | 3247a4b1a93c9ae47e2ce8faabd751bcf4ed2456 (patch) | |
tree | 2d75a6406d975061b02ee5f2ed7d83c9a2212e30 | |
parent | b3e6670d6cd4f81ba6d97b705e937afdc64cb4f0 (diff) | |
download | AlternativeNetworkAccess-3247a4b1a93c9ae47e2ce8faabd751bcf4ed2456.tar.gz |
Fix pending intent for subscription switching
Fix pending intent for subscription switching
Bug: 129693271
Test: manual
Change-Id: I2ca8b6dba1d01aa72061c5effda4d00e09730299
-rw-r--r-- | src/com/android/ons/ONSProfileSelector.java | 70 | ||||
-rw-r--r-- | src/com/android/ons/OpportunisticNetworkService.java | 23 | ||||
-rw-r--r-- | tests/src/com/android/ons/ONSProfileSelectorTest.java | 2 |
3 files changed, 53 insertions, 42 deletions
diff --git a/src/com/android/ons/ONSProfileSelector.java b/src/com/android/ons/ONSProfileSelector.java index 3f09b7e..0a3517e 100644 --- a/src/com/android/ons/ONSProfileSelector.java +++ b/src/com/android/ons/ONSProfileSelector.java @@ -70,6 +70,9 @@ public class ONSProfileSelector { /* message to indicate start of profile selection process */ private static final int MSG_START_PROFILE_SELECTION = 2; + /* message to indicate Subscription switch completion */ + private static final int MSG_SUB_SWITCH_COMPLETE = 3; + private boolean mIsEnabled = false; @VisibleForTesting @@ -100,39 +103,6 @@ public class ONSProfileSelector { protected Handler mHandler; /** - * Broadcast receiver to receive intents - */ - @VisibleForTesting - protected final BroadcastReceiver mProfileSelectorBroadcastReceiver = - new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - int sequenceId; - int subId; - String action = intent.getAction(); - logDebug("ACTION_SUB_SWITCH : " + action); - if (!mIsEnabled || action == null) { - return; - } - - switch (action) { - case ACTION_SUB_SWITCH: - sequenceId = intent.getIntExtra("sequenceId", INVALID_SEQUENCE_ID); - subId = intent.getIntExtra("subId", - SubscriptionManager.INVALID_SUBSCRIPTION_ID); - logDebug("ACTION_SUB_SWITCH sequenceId: " + sequenceId - + " mSequenceId: " + mSequenceId); - if (sequenceId != mSequenceId) { - return; - } - - onSubSwitchComplete(subId); - break; - } - } - }; - - /** * Network scan callback handler */ @VisibleForTesting @@ -331,16 +301,31 @@ public class ONSProfileSelector { private void switchToSubscription(int subId) { Intent callbackIntent = new Intent(ACTION_SUB_SWITCH); - callbackIntent.setClass(mContext, ONSProfileSelector.class); - callbackIntent.putExtra("sequenceId", getAndUpdateToken()); + callbackIntent.setClass(mContext, OpportunisticNetworkService.class); + updateToken(); + callbackIntent.putExtra("sequenceId", mSequenceId); callbackIntent.putExtra("subId", subId); - PendingIntent replyIntent = PendingIntent.getBroadcast(mContext, + PendingIntent replyIntent = PendingIntent.getService(mContext, 1, callbackIntent, Intent.FILL_IN_ACTION); mSubscriptionManager.switchToSubscription(subId, replyIntent); } + void onSubSwitchComplete(Intent intent) { + int sequenceId = intent.getIntExtra("sequenceId", INVALID_SEQUENCE_ID); + int subId = intent.getIntExtra("subId", + SubscriptionManager.INVALID_SUBSCRIPTION_ID); + logDebug("ACTION_SUB_SWITCH sequenceId: " + sequenceId + + " mSequenceId: " + mSequenceId); + if (sequenceId != mSequenceId) { + return; + } + + Message message = Message.obtain(mHandler, MSG_SUB_SWITCH_COMPLETE, subId); + message.sendToTarget(); + } + private void onSubSwitchComplete(int subId) { enableModem(subId, true); mProfileSelectionCallback.onProfileSelectionDone(); @@ -348,9 +333,9 @@ public class ONSProfileSelector { TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS); } - private int getAndUpdateToken() { + private void updateToken() { synchronized (mLock) { - return mSequenceId++; + mSequenceId++; } } @@ -665,6 +650,12 @@ public class ONSProfileSelector { checkProfileUpdate((Object[]) msg.obj); } break; + case MSG_SUB_SWITCH_COMPLETE: + logDebug("Msg received for sub switch"); + synchronized (mLock) { + onSubSwitchComplete((int) msg.obj); + } + break; default: log("invalid message"); break; @@ -674,9 +665,6 @@ public class ONSProfileSelector { /* register for profile update events */ mSubscriptionManager.addOnOpportunisticSubscriptionsChangedListener( AsyncTask.SERIAL_EXECUTOR, mProfileChangeListener); - /* register for subscription switch intent */ - mContext.registerReceiver(mProfileSelectorBroadcastReceiver, - new IntentFilter(ACTION_SUB_SWITCH)); } private void log(String msg) { diff --git a/src/com/android/ons/OpportunisticNetworkService.java b/src/com/android/ons/OpportunisticNetworkService.java index 3aee0c7..23385fd 100644 --- a/src/com/android/ons/OpportunisticNetworkService.java +++ b/src/com/android/ons/OpportunisticNetworkService.java @@ -317,6 +317,29 @@ public class OpportunisticNetworkService extends Service { } @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (intent == null) { + return START_STICKY; + } + + String action = intent.getAction(); + if (action == null) { + return START_STICKY; + } + + switch (action) { + case ONSProfileSelector.ACTION_SUB_SWITCH: { + if (mProfileSelector != null) { + mProfileSelector.onSubSwitchComplete(intent); + } + break; + } + } + + return START_STICKY; + } + + @Override public void onDestroy() { super.onDestroy(); log("Destroyed Successfully..."); diff --git a/tests/src/com/android/ons/ONSProfileSelectorTest.java b/tests/src/com/android/ons/ONSProfileSelectorTest.java index a7698ba..782a004 100644 --- a/tests/src/com/android/ons/ONSProfileSelectorTest.java +++ b/tests/src/com/android/ons/ONSProfileSelectorTest.java @@ -79,7 +79,7 @@ public class ONSProfileSelectorTest extends ONSBaseTest { super.init(c, aNSProfileSelectionCallback); this.mSubscriptionManager = ONSProfileSelectorTest.this.mSubscriptionManager; mProfileChngLstnrCpy = mProfileChangeListener; - mProfileSelectorBroadcastReceiverCpy = mProfileSelectorBroadcastReceiver; + mProfileSelectorBroadcastReceiverCpy = null; mNetworkAvailableCallBackCpy = mNetworkAvailableCallBack; mNetworkScanCtlr = mONSNetworkScanCtlr; } |