长按home键,代码调用栈如下:
主要源码逻辑:
AssistUtils::getAssistComponentForUser
public ComponentName getAssistComponentForUser(int userId) {
final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Settings.Secure.ASSISTANT, userId);
if (setting != null) {
return ComponentName.unflattenFromString(setting);
}
//start, add by wangsenhao for cts, 2018.10.29
String defaultSetting = mContext.getResources().getString(R.string.config_defaultAssistantComponentName);
Log.d("assist", "defaultSetting:"+defaultSetting);
if(android.os.SystemProperties.get("ro.hct_cts_go_support", "0").equals("1")){
defaultSetting = "com.google.android.apps.assistant/com.google.android.apps.assistant.go.MainActivity";
}
if (defaultSetting != null) {
return ComponentName.unflattenFromString(defaultSetting);
}
//end, add by wangsenhao for cts, 2018.10.29
// Fallback to keep backward compatible behavior when there is no user setting.
if (activeServiceSupportsAssistGesture()) {
return getActiveServiceComponentName();
}
final SearchManager searchManager =
(SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
if (searchManager == null) {
return null;
}
final Intent intent = searchManager.getAssistIntent(false);
PackageManager pm = mContext.getPackageManager();
ResolveInfo info = pm.resolveActivityAsUser(intent, PackageManager.MATCH_DEFAULT_ONLY,
userId);
if (info != null) {
return new ComponentName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name);
}
return null;
}
AssistUtils::getAssistComponentForUser中返回的要拉起的component的优先级顺序如下:
第一优先级:从setting provider的数据获取
adb shell settings get secure assistant
第二优先级:从config文件配置获取:目前MTK平台的方式
String defaultSetting = mContext.getResources().getString(R.string.config_defaultAssistantComponentName);
Log.d("assist", "defaultSetting:"+defaultSetting);
Framework/…/Configg.xml
<string name="config_defaultAssistantComponentName" translatable="false">com.google.android.googlequicksearchbox/com.google.android.voiceinteraction.GsaVoiceInteractionService</string>
第三优先级:从mVoiceInteractionManagerService服务获取
if (activeServiceSupportsAssistGesture()) {
return getActiveServiceComponentName();
}
第四优先级,从apk自定义的Action获取#目前语音助手的方式
final Intent intent = searchManager.getAssistIntent(false);
PackageManager pm = mContext.getPackageManager();
ResolveInfo info = pm.resolveActivityAsUser(intent, PackageManager.MATCH_DEFAULT_ONLY,
userId);
if (info != null) {
return new ComponentName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name);
}
return null;
综上,长按home键应用或者项目建议实现方式:
方式一:语音助手,在手机启动时,去设置secure# assistant的值
方式二:提供配置的patch,提供给各个项目使用
<string name="config_defaultAssistantComponentName" translatable="false">………</string>
网友评论