From 42d1d4be6b7916bf01882203d61e71b180d42fa1 Mon Sep 17 00:00:00 2001 From: Amit Mahajan Date: Fri, 24 Apr 2020 13:46:11 -0700 Subject: [PATCH] Look up CBS package name through PackageManager. Instead of hardcoding it. Test: manual Bug: 154436403 Merged-In: I0feb356e6cd4868f71198bdee49a7658b4b98b5d Change-Id: I9b3b3464614eac7394434e55eb142bc1053e8758 --- .../simstatus/SimStatusDialogController.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/com/android/settings/deviceinfo/simstatus/SimStatusDialogController.java b/src/com/android/settings/deviceinfo/simstatus/SimStatusDialogController.java index 7951b07521b..370bdfb686e 100644 --- a/src/com/android/settings/deviceinfo/simstatus/SimStatusDialogController.java +++ b/src/com/android/settings/deviceinfo/simstatus/SimStatusDialogController.java @@ -23,6 +23,8 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.os.IBinder; import android.os.PersistableBundle; @@ -70,8 +72,6 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O private final static String TAG = "SimStatusDialogCtrl"; - private static final String CELL_BROADCAST_SERVICE_PACKAGE = "com.android.cellbroadcastservice"; - @VisibleForTesting final static int NETWORK_PROVIDER_VALUE_ID = R.id.operator_name_value; @VisibleForTesting @@ -360,7 +360,9 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O private void bindCellBroadcastService() { mCellBroadcastServiceConnection = new CellBroadcastServiceConnection(); Intent intent = new Intent(CellBroadcastService.CELL_BROADCAST_SERVICE_INTERFACE); - intent.setPackage(CELL_BROADCAST_SERVICE_PACKAGE); + String cbsPackage = getCellBroadcastServicePackage(); + if (TextUtils.isEmpty(cbsPackage)) return; + intent.setPackage(cbsPackage); if (mCellBroadcastServiceConnection != null && mCellBroadcastServiceConnection.getService() == null) { if (!mContext.bindService(intent, mCellBroadcastServiceConnection, @@ -372,6 +374,38 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O } } + /** Returns the package name of the cell broadcast service, or null if there is none. */ + private String getCellBroadcastServicePackage() { + PackageManager packageManager = mContext.getPackageManager(); + List cbsPackages = packageManager.queryIntentServices( + new Intent(CellBroadcastService.CELL_BROADCAST_SERVICE_INTERFACE), + PackageManager.MATCH_SYSTEM_ONLY); + if (cbsPackages.size() != 1) { + Log.e(TAG, "getCellBroadcastServicePackageName: found " + cbsPackages.size() + + " CBS packages"); + } + for (ResolveInfo info : cbsPackages) { + if (info.serviceInfo == null) continue; + String packageName = info.serviceInfo.packageName; + if (!TextUtils.isEmpty(packageName)) { + if (packageManager.checkPermission( + android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, + packageName) == PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, "getCellBroadcastServicePackageName: " + packageName); + return packageName; + } else { + Log.e(TAG, "getCellBroadcastServicePackageName: " + packageName + + " does not have READ_PRIVILEGED_PHONE_STATE permission"); + } + } else { + Log.e(TAG, "getCellBroadcastServicePackageName: found a CBS package but " + + "packageName is null/empty"); + } + } + Log.e(TAG, "getCellBroadcastServicePackageName: package name not found"); + return null; + } + private void updateLatestAreaInfo() { mShowLatestAreaInfo = Resources.getSystem().getBoolean( com.android.internal.R.bool.config_showAreaUpdateInfoSettings)