DefaultAssistPreferenceController.java revision 2ea67003364d8f4a79d6afd624d56709839151ee
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.applications.defaultapps.DefaultAppInfo;
30import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
31
32import java.util.List;
33
34public class DefaultAssistPreferenceController extends DefaultAppPreferenceController {
35
36    private final AssistUtils mAssistUtils;
37    private final boolean mShowSetting;
38    private final String mPrefKey;
39
40    public DefaultAssistPreferenceController(Context context, String prefKey,
41            boolean showSetting) {
42        super(context);
43        mPrefKey = prefKey;
44        mShowSetting = showSetting;
45        mAssistUtils = new AssistUtils(context);
46    }
47
48    @Override
49    protected Intent getSettingIntent(DefaultAppInfo info) {
50        if (!mShowSetting) {
51            return null;
52        }
53        final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
54        if (cn == null) {
55            return null;
56        }
57        final Intent probe = new Intent(VoiceInteractionService.SERVICE_INTERFACE)
58                .setPackage(cn.getPackageName());
59
60        final PackageManager pm = mPackageManager.getPackageManager();
61        final List<ResolveInfo> services = pm.queryIntentServices(probe, PackageManager
62                .GET_META_DATA);
63        if (services == null || services.isEmpty()) {
64            return null;
65        }
66        final String activity = getAssistSettingsActivity(cn, services.get(0), pm);
67        if (activity == null) {
68            return null;
69        }
70        return new Intent(Intent.ACTION_MAIN)
71                .setComponent(new ComponentName(cn.getPackageName(), activity));
72    }
73
74    @Override
75    public boolean isAvailable() {
76        return true;
77    }
78
79    @Override
80    public String getPreferenceKey() {
81        return mPrefKey;
82    }
83
84    @Override
85    protected DefaultAppInfo getDefaultAppInfo() {
86        final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
87        if (cn == null) {
88            return null;
89        }
90        return new DefaultAppInfo(mPackageManager, mUserId, cn);
91    }
92
93    @VisibleForTesting
94    String getAssistSettingsActivity(ComponentName cn, ResolveInfo resolveInfo, PackageManager pm) {
95        final VoiceInteractionServiceInfo voiceInfo =
96            new VoiceInteractionServiceInfo(pm, resolveInfo.serviceInfo);
97        if (!voiceInfo.getSupportsAssist()) {
98            return null;
99        }
100        return voiceInfo.getSettingsActivity();
101    }
102}
103