Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9edb779e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove interactors from voice input settings" into sc-dev

parents d0fa5b20 b3729f66
Loading
Loading
Loading
Loading
+3 −53
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.content.pm.PackageManager;
import android.provider.Settings;
import android.text.TextUtils;

import com.android.internal.app.AssistUtils;
import com.android.settings.R;
import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
import com.android.settingslib.applications.DefaultAppInfo;
@@ -35,8 +34,6 @@ import java.util.List;
public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {

    private VoiceInputHelper mHelper;
    private AssistUtils mAssistUtils;
    private String mAssistRestrict;

    @Override
    public int getMetricsCategory() {
@@ -46,13 +43,8 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAssistUtils = new AssistUtils(context);
        mHelper = new VoiceInputHelper(context);
        mHelper.buildUi();
        final ComponentName assist = getCurrentAssist();
        if (isCurrentAssistVoiceService(assist, getCurrentService(mHelper))) {
            mAssistRestrict = assist.flattenToShortString();
        }
    }

    @Override
@@ -64,16 +56,9 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
    protected List<VoiceInputDefaultAppInfo> getCandidates() {
        final List<VoiceInputDefaultAppInfo> candidates = new ArrayList<>();
        final Context context = getContext();
        boolean hasEnabled = true;
        for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
            final boolean enabled = TextUtils.equals(info.key, mAssistRestrict);
            hasEnabled |= enabled;
            candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
        }

        final boolean assistIsService = !hasEnabled;
        for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
            final boolean enabled = !assistIsService;
            final boolean enabled = true;
            candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
        }
        return candidates;
@@ -90,23 +75,8 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {

    @Override
    protected boolean setDefaultKey(String value) {
        for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
            if (TextUtils.equals(value, info.key)) {
                Settings.Secure.putString(getContext().getContentResolver(),
                        Settings.Secure.VOICE_INTERACTION_SERVICE, value);
                Settings.Secure.putString(getContext().getContentResolver(),
                        Settings.Secure.VOICE_RECOGNITION_SERVICE,
                        new ComponentName(info.service.packageName,
                                info.serviceInfo.getRecognitionService())
                                .flattenToShortString());
                return true;
            }
        }

        for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
            if (TextUtils.equals(value, info.key)) {
                Settings.Secure.putString(getContext().getContentResolver(),
                        Settings.Secure.VOICE_INTERACTION_SERVICE, "");
                Settings.Secure.putString(getContext().getContentResolver(),
                        Settings.Secure.VOICE_RECOGNITION_SERVICE, value);
                return true;
@@ -116,23 +86,7 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
    }

    public static ComponentName getCurrentService(VoiceInputHelper helper) {
        if (helper.mCurrentVoiceInteraction != null) {
            return helper.mCurrentVoiceInteraction;
        } else if (helper.mCurrentRecognizer != null) {
        return helper.mCurrentRecognizer;
        } else {
            return null;
        }
    }

    private ComponentName getCurrentAssist() {
        return mAssistUtils.getAssistComponentForUser(mUserId);
    }

    public static boolean isCurrentAssistVoiceService(ComponentName currentAssist,
            ComponentName currentVoiceService) {
        return currentAssist == null && currentVoiceService == null ||
                currentAssist != null && currentAssist.equals(currentVoiceService);
    }

    public static class VoiceInputDefaultAppInfo extends DefaultAppInfo {
@@ -152,12 +106,8 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {

        @Override
        public CharSequence loadLabel() {
            if (mInfo instanceof VoiceInputHelper.InteractionInfo) {
                return mInfo.appLabel;
            } else {
            return mInfo.label;
        }
        }

        public Intent getSettingIntent() {
            if (mInfo.settings == null) {
+1 −27
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.internal.app.AssistUtils;
import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
import com.android.settingslib.applications.DefaultAppInfo;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -42,17 +41,13 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC
    private static final String KEY_VOICE_INPUT = "voice_input_settings";

    private VoiceInputHelper mHelper;
    private AssistUtils mAssistUtils;
    private PreferenceScreen mScreen;
    private Preference mPreference;
    private SettingObserver mSettingObserver;
    private Context mContext;

    public DefaultVoiceInputPreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        mContext = context;
        mSettingObserver = new SettingObserver();
        mAssistUtils = new AssistUtils(context);
        mHelper = new VoiceInputHelper(context);
        mHelper.buildUi();
        if (lifecycle != null) {
@@ -80,7 +75,6 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC

    @Override
    public void onResume() {
        mSettingObserver.register(mContext.getContentResolver(), true);
        updatePreference();
    }

@@ -91,9 +85,7 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC
    }

    @Override
    public void onPause() {
        mSettingObserver.register(mContext.getContentResolver(), false);
    }
    public void onPause() {}

    @Override
    protected DefaultAppInfo getDefaultAppInfo() {
@@ -101,12 +93,6 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC
        if (defaultKey == null) {
            return null;
        }
        for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
            if (TextUtils.equals(defaultKey, info.key)) {
                return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mContext,
                        mPackageManager, mUserId, info, true /* enabled */);
            }
        }

        for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
            if (TextUtils.equals(defaultKey, info.key)) {
@@ -149,16 +135,4 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC
        }
        return currentService.flattenToShortString();
    }

    class SettingObserver extends AssistSettingObserver {
        @Override
        protected List<Uri> getSettingUris() {
            return null;
        }

        @Override
        public void onSettingChange() {
            updatePreference();
        }
    }
}
+3 −53
Original line number Diff line number Diff line
@@ -26,10 +26,7 @@ import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.provider.Settings;
import android.service.voice.VoiceInteractionService;
import android.service.voice.VoiceInteractionServiceInfo;
import android.speech.RecognitionService;
import android.util.ArraySet;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
@@ -46,9 +43,9 @@ public final class VoiceInputHelper {
    static final String TAG = "VoiceInputHelper";
    final Context mContext;

    final List<ResolveInfo> mAvailableVoiceInteractions;
    final List<ResolveInfo> mAvailableRecognition;

    // TODO: Remove this superclass as we only have 1 class now (RecognizerInfo).
    static public class BaseInfo implements Comparable {
        public final ServiceInfo service;
        public final ComponentName componentName;
@@ -75,15 +72,6 @@ public final class VoiceInputHelper {
        }
    }

    static public class InteractionInfo extends BaseInfo {
        public final VoiceInteractionServiceInfo serviceInfo;

        public InteractionInfo(PackageManager pm, VoiceInteractionServiceInfo _service) {
            super(pm, _service.getServiceInfo(), _service.getSettingsActivity());
            serviceInfo = _service;
        }
    }

    static public class RecognizerInfo extends BaseInfo {
        public final boolean mSelectableAsDefault;

@@ -96,56 +84,21 @@ public final class VoiceInputHelper {
        }
    }

    final ArrayList<InteractionInfo> mAvailableInteractionInfos = new ArrayList<>();
    final ArrayList<RecognizerInfo> mAvailableRecognizerInfos = new ArrayList<>();

    ComponentName mCurrentVoiceInteraction;
    ComponentName mCurrentRecognizer;

    public VoiceInputHelper(Context context) {
        mContext = context;

        mAvailableVoiceInteractions = mContext.getPackageManager().queryIntentServices(
                new Intent(VoiceInteractionService.SERVICE_INTERFACE),
                PackageManager.GET_META_DATA);
        mAvailableRecognition = mContext.getPackageManager().queryIntentServices(
                new Intent(RecognitionService.SERVICE_INTERFACE),
                PackageManager.GET_META_DATA);
    }

    public void buildUi() {
        // Get the currently selected interactor from the secure setting.
        String currentSetting = Settings.Secure.getString(
                mContext.getContentResolver(), Settings.Secure.VOICE_INTERACTION_SERVICE);
        if (currentSetting != null && !currentSetting.isEmpty()) {
            mCurrentVoiceInteraction = ComponentName.unflattenFromString(currentSetting);
        } else {
            mCurrentVoiceInteraction = null;
        }

        ArraySet<ComponentName> interactorRecognizers = new ArraySet<>();

        // Iterate through all the available interactors and load up their info to show
        // in the preference.
        int size = mAvailableVoiceInteractions.size();
        for (int i = 0; i < size; i++) {
            ResolveInfo resolveInfo = mAvailableVoiceInteractions.get(i);
            VoiceInteractionServiceInfo info = new VoiceInteractionServiceInfo(
                    mContext.getPackageManager(), resolveInfo.serviceInfo);
            if (info.getParseError() != null) {
                Log.w("VoiceInteractionService", "Error in VoiceInteractionService "
                        + resolveInfo.serviceInfo.packageName + "/"
                        + resolveInfo.serviceInfo.name + ": " + info.getParseError());
                continue;
            }
            mAvailableInteractionInfos.add(new InteractionInfo(mContext.getPackageManager(), info));
            interactorRecognizers.add(new ComponentName(resolveInfo.serviceInfo.packageName,
                    info.getRecognitionService()));
        }
        Collections.sort(mAvailableInteractionInfos);

        // Get the currently selected recognizer from the secure setting.
        currentSetting = Settings.Secure.getString(
        String currentSetting = Settings.Secure.getString(
                mContext.getContentResolver(), Settings.Secure.VOICE_RECOGNITION_SERVICE);
        if (currentSetting != null && !currentSetting.isEmpty()) {
            mCurrentRecognizer = ComponentName.unflattenFromString(currentSetting);
@@ -155,14 +108,11 @@ public final class VoiceInputHelper {

        // Iterate through all the available recognizers and load up their info to show
        // in the preference.
        size = mAvailableRecognition.size();
        int size = mAvailableRecognition.size();
        for (int i = 0; i < size; i++) {
            ResolveInfo resolveInfo = mAvailableRecognition.get(i);
            ComponentName comp = new ComponentName(resolveInfo.serviceInfo.packageName,
                    resolveInfo.serviceInfo.name);
            if (interactorRecognizers.contains(comp)) {
                //continue;
            }
            ServiceInfo si = resolveInfo.serviceInfo;
            String settingsActivity = null;
            // Always show in voice input settings unless specifically set to False.