DefaultAssistPreferenceController.java revision 69084b3a8d4647040ee674a01ce990ea2440a1c1
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.applications.assist;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.service.voice.VoiceInteractionService;
25import android.service.voice.VoiceInteractionServiceInfo;
26import android.support.annotation.VisibleForTesting;
27
28import com.android.internal.app.AssistUtils;
29import com.android.settings.R;
30import com.android.settings.applications.defaultapps.DefaultAppInfo;
31import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
32
33import java.util.List;
34
35public class DefaultAssistPreferenceController extends DefaultAppPreferenceController {
36
37    private final AssistUtils mAssistUtils;
38    private final boolean mShowSetting;
39    private final String mPrefKey;
40
41    public DefaultAssistPreferenceController(Context context, String prefKey,
42            boolean showSetting) {
43        super(context);
44        mPrefKey = prefKey;
45        mShowSetting = showSetting;
46        mAssistUtils = new AssistUtils(context);
47    }
48
49    @Override
50    protected Intent getSettingIntent(DefaultAppInfo info) {
51        if (!mShowSetting) {
52            return null;
53        }
54        final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
55        if (cn == null) {
56            return null;
57        }
58        final Intent probe = new Intent(VoiceInteractionService.SERVICE_INTERFACE)
59                .setPackage(cn.getPackageName());
60
61        final PackageManager pm = mPackageManager.getPackageManager();
62        final List<ResolveInfo> services = pm.queryIntentServices(probe, PackageManager
63                .GET_META_DATA);
64        if (services == null || services.isEmpty()) {
65            return null;
66        }
67        final String activity = getAssistSettingsActivity(cn, services.get(0), pm);
68        if (activity == null) {
69            return null;
70        }
71        return new Intent(Intent.ACTION_MAIN)
72                .setComponent(new ComponentName(cn.getPackageName(), activity));
73    }
74
75    @Override
76    public boolean isAvailable() {
77        return mContext.getResources().getBoolean(R.bool.config_show_assist_and_voice_input);
78    }
79
80    @Override
81    public String getPreferenceKey() {
82        return mPrefKey;
83    }
84
85    @Override
86    protected DefaultAppInfo getDefaultAppInfo() {
87        final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
88        if (cn == null) {
89            return null;
90        }
91        return new DefaultAppInfo(mContext, mPackageManager, mUserId, cn);
92    }
93
94    @VisibleForTesting
95    String getAssistSettingsActivity(ComponentName cn, ResolveInfo resolveInfo, PackageManager pm) {
96        final VoiceInteractionServiceInfo voiceInfo =
97            new VoiceInteractionServiceInfo(pm, resolveInfo.serviceInfo);
98        if (!voiceInfo.getSupportsAssist()) {
99            return null;
100        }
101        return voiceInfo.getSettingsActivity();
102    }
103}
104