美文网首页Android OS
定制home键长按

定制home键长按

作者: 古风子 | 来源:发表于2019-01-04 17:18 被阅读0次

    长按home键,代码调用栈如下:

    123.png

    主要源码逻辑:

    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>

    相关文章

      网友评论

        本文标题:定制home键长按

        本文链接:https://www.haomeiwen.com/subject/jfvqrqtx.html