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