1.修改配置文件 frameworks\base\packages\SystemUI\res\values\config.xml
<!-- Nav bar button default ordering/layout --> <string name="config_navBarLayout" translatable="false">left[.5W];leftrotate,volume_sub,back,home,recent,volume_add,screenshot,rightrotate,setting,hidenav;right[.5W]</string> <string name="config_navBarLayoutQuickstep" translatable="false">back[1.7WC];home;contextual[1.7WC]</string> <string name="config_navBarLayoutHandle" translatable="false">back[70AC];home_handle;ime_switcher[70AC]</string>
添加自己新增加的button名称
同步修改 frameworks\base\packages\SystemUI\res\values-sw600dp\config.xml frameworks\base\packages\SystemUI\res\values-sw900dp\config.xml文件夹
sw是smallwidth的意思,当屏幕的最小边像素大于900 就会使用values-sw900dp\config.xml
2.为新添加的button 创建layout布局 放在layout文件夹下
<com.android.systemui.navigationbar.buttons.KeyButtonView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:systemui="http://schemas.android.com/apk/res-auto" android:id="@+id/hidenav" button id android:layout_width="@dimen/navigation_key_width" android:layout_height="match_parent" android:layout_weight="0" systemui:keyCode="29" 很重要 指定keyCode 用于响应点击事件 android:scaleType="center" android:paddingStart="@dimen/navigation_key_padding" android:paddingEnd="@dimen/navigation_key_padding" />
3.修改SystemUI Java源码
frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\NavigationBarInflaterView.java 在 createView里 加载button对应的layout文件 else if (SETTING.equals(button)) { v = inflater.inflate(R.layout.setting, parent, false); }else if (HIDE.equals(button)) { v = inflater.inflate(R.layout.hidenav, parent, false); } frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\NavigationBarView.java
在构造方法中 添加button
mButtonDispatchers.put(R.id.left, new ButtonDispatcher(R.id.left)); mButtonDispatchers.put(R.id.right, new ButtonDispatcher(R.id.right)); mButtonDispatchers.put(R.id.setting, new ButtonDispatcher(R.id.setting)); mButtonDispatchers.put(R.id.hidenav, new ButtonDispatcher(R.id.hidenav));
定义新增加的Button
private KeyButtonDrawable mLeft; private KeyButtonDrawable mRight; private KeyButtonDrawable mSetting; private KeyButtonDrawable mHide;
并在updateIcons方法中 为KeyButtonDrawable 指定 资源文件
private void updateIcons(Configuration oldConfig) { final boolean orientationChange = oldConfig.orientation != mConfiguration.orientation; final boolean densityChange = oldConfig.densityDpi != mConfiguration.densityDpi; final boolean dirChange = oldConfig.getLayoutDirection() != mConfiguration.getLayoutDirection(); if (orientationChange || densityChange) { mDockedIcon = getDrawable(R.drawable.ic_sysbar_docked); mHomeDefaultIcon = getHomeDrawable(); } if (densityChange || dirChange) { mRecentIcon = getDrawable(R.drawable.ic_sysbar_recent); mContextualButtonGroup.updateIcons(mLightIconColor, mDarkIconColor); } if (orientationChange || densityChange || dirChange) { mBackIcon = getBackDrawable(); } mVolumeAddIcon = getDrawable(R.drawable.ic_sysbar_volume_add_button); mVolumeSubIcon = getDrawable(R.drawable.ic_sysbar_volume_sub_button); mScreenshotIcon = getDrawable(R.drawable.ic_sysbar_capture_button); mLeft = getDrawable(R.drawable.leftrotate); mRight = getDrawable(R.drawable.rightrotate); mSetting = getDrawable(R.drawable.settings); mHide = getDrawable(R.drawable.hide); }
创建 获取ButtonDispatcher 的方法
public ButtonDispatcher getHideButton() { return mButtonDispatchers.get(R.id.hidenav); }
在updateNavButtonIcons方法中 为button指定resource 和可见性
getLeftButton().setImageDrawable(mLeft); getRightButton().setImageDrawable(mRight); getHideButton().setImageDrawable(mHide); getSettingsButton().setImageDrawable(mSetting); getLeftButton().setVisibility(View.VISIBLE); getRightButton().setVisibility(View.VISIBLE); getHideButton().setVisibility(View.VISIBLE); getSettingsButton().setVisibility(View.VISIBLE);
4.在frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\buttons\KeyButtonView.java为button 的onTouchEvent方法处理button的点击事件
case MotionEvent.ACTION_UP: final boolean doIt = isPressed() && !mLongClicked; setPressed(false); final boolean doHapticFeedback = (SystemClock.uptimeMillis() - mDownTime) > 150; if (showSwipeUI) { if (doIt) { // Apply haptic feedback on touch up since there is none on touch down performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); playSoundEffect(SoundEffectConstants.CLICK); } } else if (doHapticFeedback && !mLongClicked) { // Always send a release ourselves because it doesn't seem to be sent elsewhere // and it feels weird to sometimes get a release haptic and other times not. performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE); } if (mCode != KEYCODE_UNKNOWN) { Log.e("longjiang","mCode"+mCode); int rightCount = 0; int leftCount = 0; if(mCode == 26){ rightCount++; Log.e("longjiang","rightCount"+rightCount); Settings.System.putInt(mContext.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0); Settings.System.putInt(mContext.getContentResolver(),Settings.System.USER_ROTATION, (4-rightCount%4)%4); }else if(mCode == 25){ leftCount++; Log.e("longjiang","leftCount"+leftCount); Settings.System.putInt(mContext.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0); Settings.System.putInt(mContext.getContentResolver(),Settings.System.USER_ROTATION, leftCount%4); }else if(mCode == 27){ //通过目标APP的包名创建intent对象 Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.android.settings"); // 这里如果intent为空,就说名没有安装要跳转的应用嘛 if (intent != null) { //传参方式与Activity间跳转相同 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(intent); } else { //没有安装目标app应用 } }else if(mCode == 29){ SystemProperties.set("vendor.hide.nav","true"); // View gameView = mContext.getWindow().getDecorView(); // gameView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); // hide nav bar //gameView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE // show nav bar); } if (doIt) { sendEvent(KeyEvent.ACTION_UP, 0); sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); } else { sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); } } else { // no key code, just a regular ImageView if (doIt && mOnClickListener != null) { mOnClickListener.onClick(this); sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); } } removeCallbacks(mCheckLongPress); break; }