From e30d7d2e533f830ee638b1a727e537880b18bf66 Mon Sep 17 00:00:00 2001 From: twyen Date: Wed, 22 Nov 2017 12:16:10 -0800 Subject: Check Contacts support for preferred SIM Preferred SIM can only be enabled when both dialer and the system contact app supports it. This CL checks if the app for ACTION_QUICK_CONTACT has the metadata supports_per_number_preferred_account. Also added a flag to disable the whole preferred SIM feature. Bug: 69638458 Test: CallingAccountSelectorTest PiperOrigin-RevId: 176687062 Change-Id: Id00debe2393068a422907a9eff2ac4ef0fcf6f8e --- .../precall/impl/CallingAccountSelector.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'java/com/android/dialer/precall') diff --git a/java/com/android/dialer/precall/impl/CallingAccountSelector.java b/java/com/android/dialer/precall/impl/CallingAccountSelector.java index 4d2e6063a..8d3df2360 100644 --- a/java/com/android/dialer/precall/impl/CallingAccountSelector.java +++ b/java/com/android/dialer/precall/impl/CallingAccountSelector.java @@ -20,11 +20,16 @@ import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.database.Cursor; import android.net.Uri; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; +import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.PhoneLookup; +import android.provider.ContactsContract.QuickContact; import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -60,6 +65,9 @@ public class CallingAccountSelector implements PreCallAction { @VisibleForTesting static final String TAG_CALLING_ACCOUNT_SELECTOR = "CallingAccountSelector"; + @VisibleForTesting + static final String METADATA_SUPPORTS_PREFERRED_SIM = "supports_per_number_preferred_account"; + private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment; private boolean isDiscarding; @@ -246,6 +254,9 @@ public class CallingAccountSelector implements PreCallAction { @WorkerThread public PreferredAccountWorkerResult doInBackground(Context context) throws Throwable { PreferredAccountWorkerResult result = new PreferredAccountWorkerResult(); + if (!isPreferredSimEnabled(context)) { + return result; + } result.dataId = getDataId(context.getContentResolver(), phoneNumber); if (result.dataId.isPresent()) { result.phoneAccountHandle = getPreferredAccount(context, result.dataId.get()); @@ -435,4 +446,41 @@ public class CallingAccountSelector implements PreCallAction { return null; } } + + @WorkerThread + private static boolean isPreferredSimEnabled(Context context) { + Assert.isWorkerThread(); + if (!ConfigProviderBindings.get(context).getBoolean("preferred_sim_enabled", true)) { + return false; + } + + Intent quickContactIntent = getQuickContactIntent(); + ResolveInfo resolveInfo = + context + .getPackageManager() + .resolveActivity(quickContactIntent, PackageManager.GET_META_DATA); + if (resolveInfo == null + || resolveInfo.activityInfo == null + || resolveInfo.activityInfo.applicationInfo == null + || resolveInfo.activityInfo.applicationInfo.metaData == null) { + LogUtil.e("CallingAccountSelector.isPreferredSimEnabled", "cannot resolve quick contact app"); + return false; + } + if (!resolveInfo.activityInfo.applicationInfo.metaData.getBoolean( + METADATA_SUPPORTS_PREFERRED_SIM, false)) { + LogUtil.i( + "CallingAccountSelector.isPreferredSimEnabled", + "system contacts does not support preferred SIM"); + return false; + } + return true; + } + + @VisibleForTesting + static Intent getQuickContactIntent() { + Intent intent = new Intent(QuickContact.ACTION_QUICK_CONTACT); + intent.addCategory(Intent.CATEGORY_DEFAULT); + intent.setData(Contacts.CONTENT_URI.buildUpon().appendPath("1").build()); + return intent; + } } -- cgit v1.2.3