From efae104f4d4153aaa968fab316203ce9b074761e Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Fri, 18 Jan 2019 08:18:46 -0800 Subject: [PATCH] Assistant is now set as role. Hence remove old assitant picker Bug: 110557011 Test: - Set assitant via Default app settings and via roles UI - m -j RunSettingsRoboTests Change-Id: Id41c5087506ece392ee644a33d1a79b7f9b17ee8 --- res/xml/manage_assist.xml | 3 +- .../assist/DefaultAssistPicker.java | 246 ------------------ .../DefaultAssistPreferenceController.java | 25 ++ .../assist/DefaultAssistPickerTest.java | 110 -------- 4 files changed, 26 insertions(+), 358 deletions(-) delete mode 100644 src/com/android/settings/applications/assist/DefaultAssistPicker.java delete mode 100644 tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java diff --git a/res/xml/manage_assist.xml b/res/xml/manage_assist.xml index a2b2ad5d87a..bb387b5bdeb 100644 --- a/res/xml/manage_assist.xml +++ b/res/xml/manage_assist.xml @@ -25,8 +25,7 @@ + android:summary="@string/summary_placeholder" /> mAvailableAssistants = new ArrayList<>(); - - private AssistUtils mAssistUtils; - private ActivityManager mActivityManager; - - @Override - public int getMetricsCategory() { - return SettingsEnums.DEFAULT_ASSIST_PICKER; - } - - @Override - protected boolean shouldShowItemNone() { - return true; - } - - @Override - public void onAttach(Context context) { - super.onAttach(context); - mAssistUtils = new AssistUtils(context); - mActivityManager = context.getSystemService(ActivityManager.class); - } - - @Override - protected int getPreferenceScreenResId() { - return R.xml.default_assist_settings; - } - - @Override - protected List getCandidates() { - mAvailableAssistants.clear(); - addAssistServices(); - addAssistActivities(); - - final List packages = new ArrayList<>(); - final List candidates = new ArrayList<>(); - for (Info info : mAvailableAssistants) { - final String packageName = info.component.getPackageName(); - if (packages.contains(packageName)) { - // A service appears before an activity thus overrides it if from the same package. - continue; - } - packages.add(packageName); - candidates.add(new DefaultAppInfo(getContext(), mPm, mUserId, info.component)); - } - return candidates; - } - - @Override - protected String getDefaultKey() { - final ComponentName cn = getCurrentAssist(); - if (cn != null) { - return new DefaultAppInfo(getContext(), mPm, mUserId, cn).getKey(); - } - return null; - } - - @Override - protected String getConfirmationMessage(CandidateInfo appInfo) { - if (appInfo == null) { - return null; - } - return getContext().getString(R.string.assistant_security_warning, appInfo.loadLabel()); - } - - @Override - protected boolean setDefaultKey(String key) { - if (TextUtils.isEmpty(key)) { - setAssistNone(); - return true; - } - ComponentName cn = ComponentName.unflattenFromString(key); - final Info info = findAssistantByPackageName(cn.getPackageName()); - if (info == null) { - setAssistNone(); - return true; - } - - if (info.isVoiceInteractionService()) { - setAssistService(info); - } else { - setAssistActivity(info); - } - return true; - } - - public ComponentName getCurrentAssist() { - return mAssistUtils.getAssistComponentForUser(mUserId); - } - - @VisibleForTesting - void addAssistServices() { - if (mActivityManager.isLowRamDevice()) { - return; - } - final List services = mPm.queryIntentServices( - ASSIST_SERVICE_PROBE, PackageManager.GET_META_DATA); - for (ResolveInfo resolveInfo : services) { - VoiceInteractionServiceInfo voiceInteractionServiceInfo = - new VoiceInteractionServiceInfo(mPm, resolveInfo.serviceInfo); - if (!voiceInteractionServiceInfo.getSupportsAssist()) { - continue; - } - - mAvailableAssistants.add(new Info( - new ComponentName(resolveInfo.serviceInfo.packageName, - resolveInfo.serviceInfo.name), - voiceInteractionServiceInfo)); - } - } - - private void addAssistActivities() { - final List activities = mPm.queryIntentActivities( - ASSIST_ACTIVITY_PROBE, PackageManager.MATCH_DEFAULT_ONLY); - for (ResolveInfo resolveInfo : activities) { - mAvailableAssistants.add(new Info( - new ComponentName(resolveInfo.activityInfo.packageName, - resolveInfo.activityInfo.name))); - } - } - - private Info findAssistantByPackageName(String packageName) { - for (Info info : mAvailableAssistants) { - if (TextUtils.equals(info.component.getPackageName(), packageName)) { - return info; - } - } - return null; - } - - private void setAssistNone() { - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.ASSISTANT, ""); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_INTERACTION_SERVICE, ""); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_RECOGNITION_SERVICE, getDefaultRecognizer()); - } - - private void setAssistService(Info serviceInfo) { - final String serviceComponentName = serviceInfo.component. - flattenToShortString(); - final String serviceRecognizerName = new ComponentName( - serviceInfo.component.getPackageName(), - serviceInfo.voiceInteractionServiceInfo.getRecognitionService()) - .flattenToShortString(); - - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.ASSISTANT, serviceComponentName); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_INTERACTION_SERVICE, serviceComponentName); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_RECOGNITION_SERVICE, serviceRecognizerName); - } - - private void setAssistActivity(Info activityInfo) { - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.ASSISTANT, activityInfo.component.flattenToShortString()); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_INTERACTION_SERVICE, ""); - Settings.Secure.putString(getContext().getContentResolver(), - Settings.Secure.VOICE_RECOGNITION_SERVICE, getDefaultRecognizer()); - } - - private String getDefaultRecognizer() { - final ResolveInfo resolveInfo = mPm.resolveService( - new Intent(RecognitionService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); - if (resolveInfo == null || resolveInfo.serviceInfo == null) { - Log.w(TAG, "Unable to resolve default voice recognition service."); - return ""; - } - - return new ComponentName(resolveInfo.serviceInfo.packageName, - resolveInfo.serviceInfo.name).flattenToShortString(); - } - - static class Info { - public final ComponentName component; - public final VoiceInteractionServiceInfo voiceInteractionServiceInfo; - - Info(ComponentName component) { - this.component = component; - this.voiceInteractionServiceInfo = null; - } - - Info(ComponentName component, VoiceInteractionServiceInfo voiceInteractionServiceInfo) { - this.component = component; - this.voiceInteractionServiceInfo = voiceInteractionServiceInfo; - } - - public boolean isVoiceInteractionService() { - return voiceInteractionServiceInfo != null; - } - } -} diff --git a/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java b/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java index f95f96adee5..b2e36445a96 100644 --- a/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java +++ b/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java @@ -16,15 +16,19 @@ package com.android.settings.applications.assist; +import android.app.role.RoleManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.provider.Settings; import android.service.voice.VoiceInteractionService; import android.service.voice.VoiceInteractionServiceInfo; +import android.text.TextUtils; import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; import com.android.internal.app.AssistUtils; import com.android.settings.R; @@ -38,6 +42,7 @@ public class DefaultAssistPreferenceController extends DefaultAppPreferenceContr private final AssistUtils mAssistUtils; private final boolean mShowSetting; private final String mPrefKey; + private final Intent mIntent; public DefaultAssistPreferenceController(Context context, String prefKey, boolean showSetting) { @@ -45,6 +50,15 @@ public class DefaultAssistPreferenceController extends DefaultAppPreferenceContr mPrefKey = prefKey; mShowSetting = showSetting; mAssistUtils = new AssistUtils(context); + + final String packageName = mPackageManager.getPermissionControllerPackageName(); + if (packageName != null) { + mIntent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP) + .setPackage(packageName) + .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_ASSISTANT); + } else { + mIntent = null; + } } @Override @@ -72,6 +86,17 @@ public class DefaultAssistPreferenceController extends DefaultAppPreferenceContr .setComponent(new ComponentName(cn.getPackageName(), activity)); } + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (TextUtils.equals(preference.getKey(), "default_assist")) { + if (mIntent != null) { + mContext.startActivity(mIntent); + } + return true; + } + return false; + } + @Override public boolean isAvailable() { return mContext.getResources().getBoolean(R.bool.config_show_assist_and_voice_input); diff --git a/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java b/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java deleted file mode 100644 index 3804d6d32f9..00000000000 --- a/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2017 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.settings.applications.assist; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; - -import android.app.ActivityManager; -import android.content.ComponentName; -import android.content.Context; -import android.provider.Settings; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.shadow.api.Shadow; -import org.robolectric.shadows.ShadowActivityManager; -import org.robolectric.util.ReflectionHelpers; - -import java.util.ArrayList; -import java.util.List; - -@RunWith(RobolectricTestRunner.class) -public class DefaultAssistPickerTest { - - private static ComponentName sTestAssist; - - @BeforeClass - public static void beforeClass() { - sTestAssist = new ComponentName("com.android.settings", "assist"); - } - - private Context mContext; - private DefaultAssistPicker mPicker; - private ShadowActivityManager mShadowActivityManager; - - @Before - public void setUp() { - mContext = RuntimeEnvironment.application; - mShadowActivityManager = Shadow.extract(mContext.getSystemService(ActivityManager.class)); - mPicker = spy(new DefaultAssistPicker()); - mPicker.onAttach(mContext); - doReturn(mContext).when(mPicker).getContext(); - } - - @Test - public void setDefaultAppKey_shouldUpdateDefaultAssist() { - final List assistants = new ArrayList<>(); - assistants.add(new DefaultAssistPicker.Info(sTestAssist)); - ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultKey(sTestAssist.flattenToString()); - - assertThat(Settings.Secure.getString(mContext.getContentResolver(), - Settings.Secure.ASSISTANT)) - .isEqualTo(sTestAssist.flattenToString()); - assertThat(mPicker.getDefaultKey()).isEqualTo(sTestAssist.flattenToString()); - } - - @Test - public void setDefaultAppKey_noAvailableAssist_shouldClearDefaultAssist() { - final List assistants = new ArrayList<>(); - ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultKey(sTestAssist.flattenToString()); - - assertThat(Settings.Secure.getString(mContext.getContentResolver(), - Settings.Secure.ASSISTANT)) - .isEmpty(); - assertThat(mPicker.getDefaultKey()).isNull(); - } - - @Test - public void setDefaultAppKeyToNull_shouldClearDefaultAssist() { - final List assistants = new ArrayList<>(); - assistants.add(new DefaultAssistPicker.Info(sTestAssist)); - ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultKey(null); - - assertThat(Settings.Secure.getString(mContext.getContentResolver(), - Settings.Secure.ASSISTANT)) - .isEmpty(); - assertThat(mPicker.getDefaultKey()).isNull(); - } - - @Test - public void addAssistService_lowRamDevice_shouldDoNothing() { - mShadowActivityManager.setIsLowRamDevice(true); - mPicker.addAssistServices(); - - assertThat(mPicker.mAvailableAssistants).hasSize(0); - } -}