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 17 package com.android.settings.applications.assist; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.service.voice.VoiceInteractionService; 25 import android.service.voice.VoiceInteractionServiceInfo; 26 import android.support.annotation.VisibleForTesting; 27 28 import com.android.internal.app.AssistUtils; 29 import com.android.settings.R; 30 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController; 31 import com.android.settingslib.applications.DefaultAppInfo; 32 33 import java.util.List; 34 35 public 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