summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/preferredsim
diff options
context:
space:
mode:
authortwyen <twyen@google.com>2018-03-07 12:12:24 -0800
committerEric Erfanian <erfanian@google.com>2018-03-07 12:50:21 -0800
commit73a74c3c709c87d4ad9860c1fbda585c139235e0 (patch)
treea998b9dae213f71b989f533245ca399c06e08178 /java/com/android/dialer/preferredsim
parent28b252f6e0e4d42551a31d1b197f2eedf7b6a7d5 (diff)
Handle preferred SIM for ACTION_CALL
Previously preferred SIM is handled only by precall, which covers dialing with dialer or with the special receiver contacts uses. If a third party app uses ACTION_CALL or telecomManager.placeCall(), then the in call UI will be launched directly and the old account selection dialog will be used without preferred SIM support. In this CL logic from CallingAccountSelector is refactored out so InCallActivity can use it for the dialog. Bug: 73718976 Test: Unit tests, In call UI not covered. PiperOrigin-RevId: 188214007 Change-Id: Ifaacf982a3e98601dc362b649c3501d4ee96e63e
Diffstat (limited to 'java/com/android/dialer/preferredsim')
-rw-r--r--java/com/android/dialer/preferredsim/PreferredAccountRecorder.java145
-rw-r--r--java/com/android/dialer/preferredsim/PreferredAccountWorker.java273
-rw-r--r--java/com/android/dialer/preferredsim/suggestion/AndroidManifest.xml19
-rw-r--r--java/com/android/dialer/preferredsim/suggestion/SuggestionProvider.java37
-rw-r--r--java/com/android/dialer/preferredsim/suggestion/res/values/strings.xml25
5 files changed, 499 insertions, 0 deletions
diff --git a/java/com/android/dialer/preferredsim/PreferredAccountRecorder.java b/java/com/android/dialer/preferredsim/PreferredAccountRecorder.java
new file mode 100644
index 000000000..75f8ad71d
--- /dev/null
+++ b/java/com/android/dialer/preferredsim/PreferredAccountRecorder.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.dialer.preferredsim;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.WorkerThread;
+import android.telecom.PhoneAccountHandle;
+import com.android.dialer.common.Assert;
+import com.android.dialer.common.concurrent.DialerExecutor.Worker;
+import com.android.dialer.common.concurrent.DialerExecutorComponent;
+import com.android.dialer.logging.DialerImpression;
+import com.android.dialer.logging.Logger;
+import com.android.dialer.preferredsim.PreferredSimFallbackContract.PreferredSim;
+import com.android.dialer.preferredsim.suggestion.SimSuggestionComponent;
+import com.android.dialer.preferredsim.suggestion.SuggestionProvider.Suggestion;
+
+/** Records selected preferred SIM and reports related metric to the suggestion provider */
+public class PreferredAccountRecorder {
+
+ @Nullable private final String number;
+ @Nullable private final Suggestion suggestion;
+ @Nullable private final String dataId;
+
+ public PreferredAccountRecorder(
+ @Nullable String number, @Nullable Suggestion suggestion, @Nullable String dataId) {
+ this.number = number;
+ this.suggestion = suggestion;
+ this.dataId = dataId;
+ }
+
+ /**
+ * Record the result from {@link
+ * com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener#onPhoneAccountSelected}
+ */
+ public void record(
+ Context context, PhoneAccountHandle selectedAccountHandle, boolean setDefault) {
+
+ if (suggestion != null) {
+ if (suggestion.phoneAccountHandle.equals(selectedAccountHandle)) {
+ Logger.get(context)
+ .logImpression(DialerImpression.Type.DUAL_SIM_SELECTION_SUGGESTED_SIM_SELECTED);
+ } else {
+ Logger.get(context)
+ .logImpression(DialerImpression.Type.DUAL_SIM_SELECTION_NON_SUGGESTED_SIM_SELECTED);
+ }
+ }
+
+ if (dataId != null && setDefault) {
+ Logger.get(context).logImpression(DialerImpression.Type.DUAL_SIM_SELECTION_PREFERRED_SET);
+ DialerExecutorComponent.get(context)
+ .dialerExecutorFactory()
+ .createNonUiTaskBuilder(new WritePreferredAccountWorker())
+ .build()
+ .executeParallel(
+ new WritePreferredAccountWorkerInput(context, dataId, selectedAccountHandle));
+ }
+ if (number != null) {
+ DialerExecutorComponent.get(context)
+ .dialerExecutorFactory()
+ .createNonUiTaskBuilder(
+ new UserSelectionReporter(selectedAccountHandle, number, setDefault))
+ .build()
+ .executeParallel(context);
+ }
+ }
+
+ private static class UserSelectionReporter implements Worker<Context, Void> {
+
+ private final String number;
+ private final PhoneAccountHandle phoneAccountHandle;
+ private final boolean remember;
+
+ public UserSelectionReporter(
+ @NonNull PhoneAccountHandle phoneAccountHandle, @Nullable String number, boolean remember) {
+ this.phoneAccountHandle = Assert.isNotNull(phoneAccountHandle);
+ this.number = Assert.isNotNull(number);
+ this.remember = remember;
+ }
+
+ @Nullable
+ @Override
+ public Void doInBackground(@NonNull Context context) throws Throwable {
+ SimSuggestionComponent.get(context)
+ .getSuggestionProvider()
+ .reportUserSelection(context, number, phoneAccountHandle, remember);
+ return null;
+ }
+ }
+
+ private static class WritePreferredAccountWorkerInput {
+ private final Context context;
+ private final String dataId;
+ private final PhoneAccountHandle phoneAccountHandle;
+
+ WritePreferredAccountWorkerInput(
+ @NonNull Context context,
+ @NonNull String dataId,
+ @NonNull PhoneAccountHandle phoneAccountHandle) {
+ this.context = Assert.isNotNull(context);
+ this.dataId = Assert.isNotNull(dataId);
+ this.phoneAccountHandle = Assert.isNotNull(phoneAccountHandle);
+ }
+ }
+
+ private static class WritePreferredAccountWorker
+ implements Worker<WritePreferredAccountWorkerInput, Void> {
+
+ @Nullable
+ @Override
+ @WorkerThread
+ public Void doInBackground(WritePreferredAccountWorkerInput input) throws Throwable {
+ ContentValues values = new ContentValues();
+ values.put(
+ PreferredSim.PREFERRED_PHONE_ACCOUNT_COMPONENT_NAME,
+ input.phoneAccountHandle.getComponentName().flattenToString());
+ values.put(PreferredSim.PREFERRED_PHONE_ACCOUNT_ID, input.phoneAccountHandle.getId());
+ input
+ .context
+ .getContentResolver()
+ .update(
+ PreferredSimFallbackContract.CONTENT_URI,
+ values,
+ PreferredSim.DATA_ID + " = ?",
+ new String[] {String.valueOf(input.dataId)});
+ return null;
+ }
+ }
+}
diff --git a/java/com/android/dialer/preferredsim/PreferredAccountWorker.java b/java/com/android/dialer/preferredsim/PreferredAccountWorker.java
new file mode 100644
index 000000000..bfaaa7cde
--- /dev/null
+++ b/java/com/android/dialer/preferredsim/PreferredAccountWorker.java
@@ -0,0 +1,273 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.dialer.preferredsim;
+
+import android.content.ContentResolver;
+import android.content.ContentUris;
+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.Data;
+import android.provider.ContactsContract.PhoneLookup;
+import android.provider.ContactsContract.QuickContact;
+import android.provider.ContactsContract.RawContacts;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.VisibleForTesting;
+import android.support.annotation.WorkerThread;
+import android.telecom.PhoneAccountHandle;
+import com.android.dialer.common.Assert;
+import com.android.dialer.common.LogUtil;
+import com.android.dialer.common.concurrent.DialerExecutor.Worker;
+import com.android.dialer.configprovider.ConfigProviderBindings;
+import com.android.dialer.preferredsim.PreferredAccountWorker.Result;
+import com.android.dialer.preferredsim.PreferredSimFallbackContract.PreferredSim;
+import com.android.dialer.preferredsim.suggestion.SimSuggestionComponent;
+import com.android.dialer.preferredsim.suggestion.SuggestionProvider.Suggestion;
+import com.android.dialer.util.PermissionsUtil;
+import com.google.auto.value.AutoValue;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableSet;
+
+/** Query a preferred SIM to make a call with. */
+public class PreferredAccountWorker implements Worker<Context, Result> {
+
+ /** The result of the worker. */
+ @AutoValue
+ public abstract static class Result {
+
+ /** The preferred phone account for the number. Absent if not set or invalid. */
+ public abstract Optional<PhoneAccountHandle> getPhoneAccountHandle();
+
+ /**
+ * {@link android.provider.ContactsContract.Data#_ID} of the row matching the number. If the
+ * preferred account is to be set it should be stored in this row
+ */
+ public abstract Optional<String> getDataId();
+
+ public abstract Optional<Suggestion> getSuggestion();
+
+ static Builder builder() {
+ return new AutoValue_PreferredAccountWorker_Result.Builder();
+ }
+
+ @AutoValue.Builder
+ abstract static class Builder {
+
+ public abstract Builder setPhoneAccountHandle(
+ Optional<PhoneAccountHandle> phoneAccountHandle);
+
+ public abstract Builder setDataId(Optional<String> dataId);
+
+ public abstract Builder setSuggestion(Optional<Suggestion> suggestion);
+
+ public abstract Result build();
+ }
+ }
+
+ @VisibleForTesting
+ public static final String METADATA_SUPPORTS_PREFERRED_SIM =
+ "supports_per_number_preferred_account";
+
+ private final String phoneNumber;
+
+ public PreferredAccountWorker(String phoneNumber) {
+ this.phoneNumber = phoneNumber;
+ }
+
+ @NonNull
+ @Override
+ @WorkerThread
+ public Result doInBackground(Context context) throws Throwable {
+ Result.Builder resultBuilder = Result.builder();
+ if (!isPreferredSimEnabled(context)) {
+ return resultBuilder.build();
+ }
+ if (!PermissionsUtil.hasContactsReadPermissions(context)) {
+ LogUtil.i("PreferredAccountWorker.doInBackground", "missing READ_CONTACTS permission");
+ return resultBuilder.build();
+ }
+ Optional<String> dataId = getDataId(context, phoneNumber);
+ Optional<PhoneAccountHandle> phoneAccountHandle = Optional.absent();
+ if (dataId.isPresent()) {
+ resultBuilder.setDataId(dataId);
+ phoneAccountHandle = getPreferredAccount(context, dataId.get());
+ }
+ resultBuilder.setPhoneAccountHandle(phoneAccountHandle);
+ Optional<Suggestion> suggestion = Optional.absent();
+ if (!phoneAccountHandle.isPresent()) {
+ suggestion =
+ SimSuggestionComponent.get(context)
+ .getSuggestionProvider()
+ .getSuggestion(context, phoneNumber);
+ resultBuilder.setSuggestion(suggestion);
+ }
+ return resultBuilder.build();
+ }
+
+ @WorkerThread
+ @NonNull
+ private static Optional<String> getDataId(
+ @NonNull Context context, @Nullable String phoneNumber) {
+ Assert.isWorkerThread();
+ if (VERSION.SDK_INT < VERSION_CODES.N) {
+ return Optional.absent();
+ }
+ try (Cursor cursor =
+ context
+ .getContentResolver()
+ .query(
+ Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)),
+ new String[] {PhoneLookup.DATA_ID},
+ null,
+ null,
+ null)) {
+ if (cursor == null) {
+ return Optional.absent();
+ }
+ ImmutableSet<String> validAccountTypes = PreferredAccountUtil.getValidAccountTypes(context);
+ String result = null;
+ while (cursor.moveToNext()) {
+ Optional<String> accountType =
+ getAccountType(context.getContentResolver(), cursor.getLong(0));
+ if (accountType.isPresent() && !validAccountTypes.contains(accountType.get())) {
+ // Empty accountType is treated as writable
+ LogUtil.i("CallingAccountSelector.getDataId", "ignoring non-writable " + accountType);
+ continue;
+ }
+ if (result != null && !result.equals(cursor.getString(0))) {
+ // TODO(twyen): if there are multiple entries attempt to grab from the contact that
+ // initiated the call.
+ LogUtil.i("CallingAccountSelector.getDataId", "lookup result not unique, ignoring");
+ return Optional.absent();
+ }
+ result = cursor.getString(0);
+ }
+ return Optional.fromNullable(result);
+ }
+ }
+
+ @WorkerThread
+ private static Optional<String> getAccountType(ContentResolver contentResolver, long dataId) {
+ Assert.isWorkerThread();
+ Optional<Long> rawContactId = getRawContactId(contentResolver, dataId);
+ if (!rawContactId.isPresent()) {
+ return Optional.absent();
+ }
+ try (Cursor cursor =
+ contentResolver.query(
+ ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId.get()),
+ new String[] {RawContacts.ACCOUNT_TYPE},
+ null,
+ null,
+ null)) {
+ if (cursor == null || !cursor.moveToFirst()) {
+ return Optional.absent();
+ }
+ return Optional.fromNullable(cursor.getString(0));
+ }
+ }
+
+ @WorkerThread
+ private static Optional<Long> getRawContactId(ContentResolver contentResolver, long dataId) {
+ Assert.isWorkerThread();
+ try (Cursor cursor =
+ contentResolver.query(
+ ContentUris.withAppendedId(Data.CONTENT_URI, dataId),
+ new String[] {Data.RAW_CONTACT_ID},
+ null,
+ null,
+ null)) {
+ if (cursor == null || !cursor.moveToFirst()) {
+ return Optional.absent();
+ }
+ return Optional.of(cursor.getLong(0));
+ }
+ }
+
+ @WorkerThread
+ @NonNull
+ private static Optional<PhoneAccountHandle> getPreferredAccount(
+ @NonNull Context context, @NonNull String dataId) {
+ Assert.isWorkerThread();
+ Assert.isNotNull(dataId);
+ try (Cursor cursor =
+ context
+ .getContentResolver()
+ .query(
+ PreferredSimFallbackContract.CONTENT_URI,
+ new String[] {
+ PreferredSim.PREFERRED_PHONE_ACCOUNT_COMPONENT_NAME,
+ PreferredSim.PREFERRED_PHONE_ACCOUNT_ID
+ },
+ PreferredSim.DATA_ID + " = ?",
+ new String[] {dataId},
+ null)) {
+ if (cursor == null) {
+ return Optional.absent();
+ }
+ if (!cursor.moveToFirst()) {
+ return Optional.absent();
+ }
+ return PreferredAccountUtil.getValidPhoneAccount(
+ context, cursor.getString(0), cursor.getString(1));
+ }
+ }
+
+ @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
+ public 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;
+ }
+}
diff --git a/java/com/android/dialer/preferredsim/suggestion/AndroidManifest.xml b/java/com/android/dialer/preferredsim/suggestion/AndroidManifest.xml
new file mode 100644
index 000000000..78f7c26d9
--- /dev/null
+++ b/java/com/android/dialer/preferredsim/suggestion/AndroidManifest.xml
@@ -0,0 +1,19 @@
+<!--
+ ~ Copyright (C) 2018 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License
+ -->
+<manifest
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.dialer.preferredsim.suggestion">
+</manifest>
diff --git a/java/com/android/dialer/preferredsim/suggestion/SuggestionProvider.java b/java/com/android/dialer/preferredsim/suggestion/SuggestionProvider.java
index 0a91d1511..cfa37c883 100644
--- a/java/com/android/dialer/preferredsim/suggestion/SuggestionProvider.java
+++ b/java/com/android/dialer/preferredsim/suggestion/SuggestionProvider.java
@@ -18,10 +18,14 @@ package com.android.dialer.preferredsim.suggestion;
import android.content.Context;
import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import android.telecom.PhoneAccountHandle;
import com.android.dialer.common.Assert;
+import com.android.dialer.common.LogUtil;
import com.google.common.base.Optional;
+import java.util.ArrayList;
+import java.util.List;
/** Provides hints to the user when selecting a SIM to make a call. */
public interface SuggestionProvider {
@@ -72,4 +76,37 @@ public interface SuggestionProvider {
@WorkerThread
void reportIncorrectSuggestion(
@NonNull Context context, @NonNull String number, @NonNull PhoneAccountHandle newAccount);
+
+ /**
+ * Return a list of suggestion strings matching the list position of the {@code
+ * phoneAccountHandles}. The list will contain {@code null} if the PhoneAccountHandle does not
+ * have suggestions.
+ */
+ @Nullable
+ static List<String> buildHint(
+ Context context,
+ List<PhoneAccountHandle> phoneAccountHandles,
+ @Nullable Suggestion suggestion) {
+ if (suggestion == null) {
+ return null;
+ }
+ List<String> hints = new ArrayList<>();
+ for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
+ if (!phoneAccountHandle.equals(suggestion.phoneAccountHandle)) {
+ hints.add(null);
+ continue;
+ }
+ switch (suggestion.reason) {
+ case INTRA_CARRIER:
+ hints.add(context.getString(R.string.pre_call_select_phone_account_hint_intra_carrier));
+ break;
+ case FREQUENT:
+ hints.add(context.getString(R.string.pre_call_select_phone_account_hint_frequent));
+ break;
+ default:
+ LogUtil.w("CallingAccountSelector.buildHint", "unhandled reason " + suggestion.reason);
+ }
+ }
+ return hints;
+ }
}
diff --git a/java/com/android/dialer/preferredsim/suggestion/res/values/strings.xml b/java/com/android/dialer/preferredsim/suggestion/res/values/strings.xml
new file mode 100644
index 000000000..c4175313d
--- /dev/null
+++ b/java/com/android/dialer/preferredsim/suggestion/res/values/strings.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2018 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License
+ -->
+<resources>
+<!-- Hint text under a SIM when selecting SIM to call, indicating the SIM is on the same carrier
+ as the outgoing call.[CHAR LIMIT=40]-->
+<string name="pre_call_select_phone_account_hint_intra_carrier">Uses same carrier</string>
+
+ <!-- Hint text under a SIM when selecting SIM to call, indicating user often use the SIM to call
+ the contact.[CHAR LIMIT=40]-->
+<string name="pre_call_select_phone_account_hint_frequent">Recently used</string>
+</resources> \ No newline at end of file