Android 8.1 中Systemui中的常见修改(五)长按Home键唤起Google Now

Android默认处理长按Home键会唤起最近使用应用列表,而部分厂商在安装了GMS后,Google会要求修改长按Home键唤起Google Now

修改frameworks/base/core/res/res/values/Config.xml

     
    0

注:这里的值可以设置三个,1代表唤起recent app界面;2代表唤起搜索界面(Google Now);3代表Nothing

我们在看处理方法:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java

    @VisibleForTesting
    boolean onHomeLongClick(View v) {
        if (shouldDisableNavbarGestures()) {
            return false;
        }
        MetricsLogger.action(getContext(), MetricsEvent.ACTION_ASSIST_LONG_PRESS);
        mAssistManager.startAssist(new Bundle() /* args */);
        mStatusBar.awakenDreams();
        if (mNavigationBarView != null) {
            mNavigationBarView.abortCurrentGesture();
        }
        return true;
    }

实际上是在下面的方法中进行处理的

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

 // These need to match the documentation/constant in
 // core/res/res/values/config.xml
 static final int LONG_PRESS_HOME_NOTHING = 0;
 static final int LONG_PRESS_HOME_ALL_APPS = 1;
 static final int LONG_PRESS_HOME_ASSIST = 2;
 static final int LAST_LONG_PRESS_HOME_BEHAVIOR = LONG_PRESS_HOME_ASSIST;



    //通过下面的方法读取config.xml文件中的相应值去对比,确定长按home按键的行为。
    /**
     * Read values from config.xml that may be overridden depending on
     * the configuration of the device.
     * eg. Disable long press on home goes to recents on sw600dp.
     */
    private void readConfigurationDependentBehaviors() {
        final Resources res = mContext.getResources();

        mLongPressOnHomeBehavior = res.getInteger(
                com.android.internal.R.integer.config_longPressOnHomeBehavior);
        if (mLongPressOnHomeBehavior < LONG_PRESS_HOME_NOTHING ||
                mLongPressOnHomeBehavior > LAST_LONG_PRESS_HOME_BEHAVIOR) {
            mLongPressOnHomeBehavior = LONG_PRESS_HOME_NOTHING;
        }

        mDoubleTapOnHomeBehavior = res.getInteger(
                com.android.internal.R.integer.config_doubleTapOnHomeBehavior);
        if (mDoubleTapOnHomeBehavior < DOUBLE_TAP_HOME_NOTHING ||
                mDoubleTapOnHomeBehavior > DOUBLE_TAP_HOME_RECENT_SYSTEM_UI) {
            mDoubleTapOnHomeBehavior = LONG_PRESS_HOME_NOTHING;
        }

        mShortPressWindowBehavior = SHORT_PRESS_WINDOW_NOTHING;
        if (mContext.getPackageManager().hasSystemFeature(FEATURE_PICTURE_IN_PICTURE)) {
            mShortPressWindowBehavior = SHORT_PRESS_WINDOW_PICTURE_IN_PICTURE;
        }

        mNavBarOpacityMode = res.getInteger(
                com.android.internal.R.integer.config_navBarOpacityMode);
    }


   //长按的处理办法
    private void handleLongPressOnHome(int deviceId) {
        if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_NOTHING) {
            return;
        }
        mHomeConsumed = true;
        performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
        switch (mLongPressOnHomeBehavior) {
            case LONG_PRESS_HOME_ALL_APPS:
                launchAllAppsAction();
                break;
            case LONG_PRESS_HOME_ASSIST:
                launchAssistAction(null, deviceId);
                break;
            default:
                Log.w(TAG, "Undefined home long press behavior: " + mLongPressOnHomeBehavior);
                break;
        }
    }

    private void launchAllAppsAction() {
        Intent intent = new Intent(Intent.ACTION_ALL_APPS);
        if (mHasFeatureLeanback) {
            final PackageManager pm = mContext.getPackageManager();
            Intent intentLauncher = new Intent(Intent.ACTION_MAIN);
            intentLauncher.addCategory(Intent.CATEGORY_HOME);
            ResolveInfo resolveInfo = pm.resolveActivityAsUser(intentLauncher,
                    PackageManager.MATCH_SYSTEM_ONLY,
                    mCurrentUserId);
            if (resolveInfo != null) {
                intent.setPackage(resolveInfo.activityInfo.packageName);
            }
        }
        startActivityAsUser(intent, UserHandle.CURRENT);
    }

最后在插播一句,处理home按键的最初方法是

 @Override
    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {

}

 

你可能感兴趣的:(android)