From 0fa8049fe87fea2a097e41946cab0db3f24836e6 Mon Sep 17 00:00:00 2001 From: Eugene Susla Date: Mon, 1 Jul 2019 11:06:06 -0700 Subject: Fix sms app changed broadcast Fixes: 136257475 Test: switch sms holder in settings, and ensure ceceived broadcasts for both becoming and leaving the sms app status Change-Id: I10dda3fd8ebd38ab12bdb8192ed4f3863f4238b8 --- .../android/server/role/RoleManagerService.java | 17 +++- .../com/android/server/role/RoleUserState.java | 7 +- .../android/internal/telephony/SmsApplication.java | 93 +++++++++++++--------- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/services/core/java/com/android/server/role/RoleManagerService.java b/services/core/java/com/android/server/role/RoleManagerService.java index 9cd6b0d32793..c6a1867fa1e9 100644 --- a/services/core/java/com/android/server/role/RoleManagerService.java +++ b/services/core/java/com/android/server/role/RoleManagerService.java @@ -49,6 +49,7 @@ import android.os.ResultReceiver; import android.os.ShellCallback; import android.os.UserHandle; import android.os.UserManagerInternal; +import android.provider.Telephony; import android.service.sms.FinancialSmsService; import android.telephony.IFinancialSmsCallback; import android.text.TextUtils; @@ -60,6 +61,7 @@ import android.util.SparseArray; import android.util.proto.ProtoOutputStream; import com.android.internal.annotations.GuardedBy; +import com.android.internal.telephony.SmsApplication; import com.android.internal.util.ArrayUtils; import com.android.internal.util.BitUtils; import com.android.internal.util.CollectionUtils; @@ -377,13 +379,16 @@ public class RoleManagerService extends SystemService implements RoleUserState.C } @Override - public void onRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId) { + public void onRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId, + @Nullable String removedHolder, @Nullable String addedHolder) { mListenerHandler.sendMessage(PooledLambda.obtainMessage( - RoleManagerService::notifyRoleHoldersChanged, this, roleName, userId)); + RoleManagerService::notifyRoleHoldersChanged, this, roleName, userId, + removedHolder, addedHolder)); } @WorkerThread - private void notifyRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId) { + private void notifyRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId, + @Nullable String removedHolder, @Nullable String addedHolder) { RemoteCallbackList listeners = getListeners(userId); if (listeners != null) { notifyRoleHoldersChangedForListeners(listeners, roleName, userId); @@ -394,6 +399,12 @@ public class RoleManagerService extends SystemService implements RoleUserState.C if (allUsersListeners != null) { notifyRoleHoldersChangedForListeners(allUsersListeners, roleName, userId); } + + // Legacy: sms app changed broadcasts + if (RoleManager.ROLE_SMS.equals(roleName)) { + SmsApplication.broadcastSmsAppChange(getContext(), UserHandle.of(userId), + removedHolder, addedHolder); + } } @WorkerThread diff --git a/services/core/java/com/android/server/role/RoleUserState.java b/services/core/java/com/android/server/role/RoleUserState.java index c7e3fa4f2074..6375b4851283 100644 --- a/services/core/java/com/android/server/role/RoleUserState.java +++ b/services/core/java/com/android/server/role/RoleUserState.java @@ -294,7 +294,7 @@ public class RoleUserState { } if (changed) { - mCallback.onRoleHoldersChanged(roleName, mUserId); + mCallback.onRoleHoldersChanged(roleName, mUserId, null, packageName); } return true; } @@ -328,7 +328,7 @@ public class RoleUserState { } if (changed) { - mCallback.onRoleHoldersChanged(roleName, mUserId); + mCallback.onRoleHoldersChanged(roleName, mUserId, packageName, null); } return true; } @@ -632,6 +632,7 @@ public class RoleUserState { * @param roleName the name of the role whose holders are changed * @param userId the user id for this role holder change */ - void onRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId); + void onRoleHoldersChanged(@NonNull String roleName, @UserIdInt int userId, + @Nullable String removedHolder, @Nullable String addedHolder); } } diff --git a/telephony/java/com/android/internal/telephony/SmsApplication.java b/telephony/java/com/android/internal/telephony/SmsApplication.java index 44dc24bf716e..98f52cbf93da 100644 --- a/telephony/java/com/android/internal/telephony/SmsApplication.java +++ b/telephony/java/com/android/internal/telephony/SmsApplication.java @@ -17,6 +17,7 @@ package com.android.internal.telephony; import android.Manifest.permission; +import android.annotation.Nullable; import android.app.AppOpsManager; import android.app.role.RoleManager; import android.content.ComponentName; @@ -662,49 +663,69 @@ public final class SmsApplication { } defaultSmsAppChanged(context); + } + } + + /** + * Sends broadcasts on sms app change: + * {@link Intent#ACTION_DEFAULT_SMS_PACKAGE_CHANGED} + * {@link Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL} + */ + public static void broadcastSmsAppChange(Context context, + UserHandle userHandle, @Nullable String oldPackage, @Nullable String newPackage) { + Collection apps = getApplicationCollection(context); + broadcastSmsAppChange(context, userHandle, + getApplicationForPackage(apps, oldPackage), + getApplicationForPackage(apps, newPackage)); + } + + private static void broadcastSmsAppChange(Context context, UserHandle userHandle, + @Nullable SmsApplicationData oldAppData, + @Nullable SmsApplicationData applicationData) { + if (DEBUG_MULTIUSER) { + Log.i(LOG_TAG, "setDefaultApplicationInternal oldAppData=" + oldAppData); + } + if (oldAppData != null && oldAppData.mSmsAppChangedReceiverClass != null) { + // Notify the old sms app that it's no longer the default + final Intent oldAppIntent = + new Intent(Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED); + final ComponentName component = new ComponentName(oldAppData.mPackageName, + oldAppData.mSmsAppChangedReceiverClass); + oldAppIntent.setComponent(component); + oldAppIntent.putExtra(Intents.EXTRA_IS_DEFAULT_SMS_APP, false); if (DEBUG_MULTIUSER) { - Log.i(LOG_TAG, "setDefaultApplicationInternal oldAppData=" + oldAppData); - } - if (oldAppData != null && oldAppData.mSmsAppChangedReceiverClass != null) { - // Notify the old sms app that it's no longer the default - final Intent oldAppIntent = - new Intent(Telephony.Sms.Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED); - final ComponentName component = new ComponentName(oldAppData.mPackageName, - oldAppData.mSmsAppChangedReceiverClass); - oldAppIntent.setComponent(component); - oldAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false); - if (DEBUG_MULTIUSER) { - Log.i(LOG_TAG, "setDefaultApplicationInternal old=" + oldAppData.mPackageName); - } - context.sendBroadcastAsUser(oldAppIntent, userHandle); + Log.i(LOG_TAG, "setDefaultApplicationInternal old=" + oldAppData.mPackageName); } - // Notify the new sms app that it's now the default (if the new sms app has a receiver - // to handle the changed default sms intent). + context.sendBroadcastAsUser(oldAppIntent, userHandle); + } + // Notify the new sms app that it's now the default (if the new sms app has a receiver + // to handle the changed default sms intent). + if (DEBUG_MULTIUSER) { + Log.i(LOG_TAG, "setDefaultApplicationInternal new applicationData=" + + applicationData); + } + if (applicationData != null && applicationData.mSmsAppChangedReceiverClass != null) { + final Intent intent = + new Intent(Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED); + final ComponentName component = new ComponentName(applicationData.mPackageName, + applicationData.mSmsAppChangedReceiverClass); + intent.setComponent(component); + intent.putExtra(Intents.EXTRA_IS_DEFAULT_SMS_APP, true); if (DEBUG_MULTIUSER) { - Log.i(LOG_TAG, "setDefaultApplicationInternal new applicationData=" + - applicationData); - } - if (applicationData.mSmsAppChangedReceiverClass != null) { - final Intent intent = - new Intent(Telephony.Sms.Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED); - final ComponentName component = new ComponentName(applicationData.mPackageName, - applicationData.mSmsAppChangedReceiverClass); - intent.setComponent(component); - intent.putExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, true); - if (DEBUG_MULTIUSER) { - Log.i(LOG_TAG, "setDefaultApplicationInternal new=" + packageName); - } - context.sendBroadcastAsUser(intent, userHandle); + Log.i(LOG_TAG, "setDefaultApplicationInternal new=" + applicationData.mPackageName); } + context.sendBroadcastAsUser(intent, userHandle); + } - // Send an implicit broadcast for the system server. - // (or anyone with MONITOR_DEFAULT_SMS_PACKAGE, really.) - final Intent intent = - new Intent(Telephony.Sms.Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL); - context.sendBroadcastAsUser(intent, userHandle, - permission.MONITOR_DEFAULT_SMS_PACKAGE); + // Send an implicit broadcast for the system server. + // (or anyone with MONITOR_DEFAULT_SMS_PACKAGE, really.) + final Intent intent = + new Intent(Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL); + context.sendBroadcastAsUser(intent, userHandle, + permission.MONITOR_DEFAULT_SMS_PACKAGE); + if (applicationData != null) { MetricsLogger.action(context, MetricsEvent.ACTION_DEFAULT_SMS_APP_CHANGED, applicationData.mPackageName); } -- cgit v1.2.3-59-g8ed1b