Android 5.1设置内加入导航栏显示与隐藏开关

平台:RK3288
版本:Android5.1
需要修改的文件有以下几个
packages/apps/Settings/res/xml/dashboard_categories.xml
packages/apps/Settings/res/xml/nobar_switch.xml
packages/apps/Settings/src/com/android/settings/NaBarSetting.java
frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
frameworks/base/packages/SystemUI/res/values/strings.xml
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

1、dashboard_categories.xml

packages/apps/Settings/res/xml/dashboard_categories.xml
Setting中显示内容的条目数量是由资源数量决定的,即要有多少资源才能显示多少个视图。而这个资源就定义在dashboard_categories.xml中

         
                ....
                />

         
        
       

        
....

2、nobar_switch.xml

packages/apps/Settings/res/xml/nobar_switch.xml
NaBarSetting界面的布局





        



3、创建NaBarSetting.java

packages/apps/Settings/src/com/android/settings/NaBarSetting.java
NaBarSetting.java是一个fragment,开关界面和逻辑操作都在这里,设置了两个广播用于让PhoneStatusBar接收,然后开关NavigationBar。
具体代码如下:

package com.android.settings;

import java.util.Timer;
import java.util.TimerTask;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.provider.MediaStore;
import android.provider.Settings;
import android.provider.MediaStore.Images.Media;
import android.provider.Settings.SettingNotFoundException;
import java.io.File; 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import android.util.Log;
import android.hardware.usb.UsbManager;
import java.util.List;
import android.os.SystemProperties;

public class NaBarSetting extends SettingsPreferenceFragment implements
    Preference.OnPreferenceChangeListener {
    private static final String TAG = "NaBarSettings";
    private CheckBoxPreference mEnableNabar;
    private File mFile = null;
    private String mMode = null;
    private static final String HOST_MODE = new String("1");
    private static final String SLAVE_MODE = new String("2");
    private static String ENABLE_NABAR_SWITCH = "enable.nabar.switch";
    private Context mContext;

    private Handler mHandler;
    private static final String KEY_ENABLE_NABAR = "enable_nabar";、

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.nabar_switch);

        mEnableNabar = (CheckBoxPreference) findPreference(KEY_ENABLE_NABAR);
        mEnableNabar.setOnPreferenceChangeListener(this);

        mContext=getActivity();

    }

    @Override
        public void onResume() {
        super.onResume();
                mEnableNabar.setEnabled(true);
            String enableUms= SystemProperties.get(ENABLE_NABAR_SWITCH,"true");
            if("true".equals(enableUms))//if has UMS function,flash is primary storage
            {
                mEnableNabar.setChecked(true);
            }else{
                mEnableNabar.setChecked(false);
            }

}

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        try {
            getActivity().unregisterReceiver(mReceiver);
        }catch (Exception e){
            Log.e(TAG,"unregisterReceiver Ex:"+e);
        }
    }


    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if (KEY_ENABLE_NABAR.equals(key)) {
            if((Boolean) objValue == true) {
                mEnableNabar.setChecked(true);
                SystemProperties.set(ENABLE_NABAR_SWITCH, "true");
        Intent intent=new Intent();
            //开启导航栏广播
            intent.setAction("rk.android.navbarview.SHOW");
            mContext.sendBroadcast(intent);
            } else {
                mEnableNabar.setChecked(false);
                SystemProperties.set(ENABLE_NABAR_SWITCH, "false");
        Intent intent=new Intent();
            //关闭导航栏广播
            intent.setAction("rk.android.navbarview.HIDE");
            mContext.sendBroadcast(intent);
            }
        }
        return true;
    }
}

4.strings.xml

frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
frameworks/base/packages/SystemUI/res/values/strings.xml

/values-zh-rCN/strings.xml
 
    NavigationBar Settings
    Show NavigationBar
    

values/strings.xml
 
    导航栏设置
    显示导航栏
 

5.PhoneStatusBar.java
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
广播监听receiver加入隐藏导航栏广播监听

private BroadcastReceiver receiver=new BroadcastReceiver() {

            @Override
             public void onReceive(Context context, Intent intent) {
             ......
              }else if(action.equals("rk.android.navbarview.HIDE")){
                                removeBar();
              }else{
                  takeScreenshot();
              }
             .......
};

makeStatusBarView方法内加入广播注册

.....
IntentFilter intentfilter=new IntentFilter();
        intentfilter.addAction("rk.android.screenshot.SHOW");
        intentfilter.addAction("rk.android.screenshot.ACTION");
//cyh add
         intentfilter.addAction("rk.android.navbarview.HIDE");
//cyh end
        context.registerReceiver(receiver, intentfilter);
.....

添加移除导航栏的方法removeBar和增加导航栏的方法addBar()
boolean mBarIsAdd = true;

 public void addBar(){
                if (!mBarIsAdd){
                        Log.d(TAG,"add Bar");
                        SystemProperties.set(ENABLE_NABAR_SWITCH, "true");


          try {
            boolean showNav = mWindowManagerService.hasNavigationBar();
            if (DEBUG) Log.v(TAG, "hasNavigationBar=" + showNav);
            if (showNav) {
                        mNavigationBarView =
                    (NavigationBarView) View.inflate(mContext, R.layout.navigation_bar_win, null);
              }else{
                mNavigationBarView =
                    (NavigationBarView) View.inflate(mContext, R.layout.navigation_bar, null);
              } 
                mNavigationBarView.setDisabledFlags(mDisabled);
                mNavigationBarView.setBar(this);
                mNavigationBarView.setOnVerticalChangedListener(
                        new NavigationBarView.OnVerticalChangedListener() {
                    @Override
                    public void onVerticalChanged(boolean isVertical) {
                        if (mSearchPanelView != null) {
                            mSearchPanelView.setHorizontal(isVertical);
                        }
                        mNotificationPanel.setQsScrimEnabled(!isVertical);
                    }
                });
                mNavigationBarView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        checkUserAutohide(v, event);
                        return false;
                    }});
                               
            }
        } catch (RemoteException ex) {
        }
              addNavigationBar();
              updateNotifications();
              checkBarModes();
              mBarIsAdd = true;
                }
     }


 private void removeBar(){
                if (mBarIsAdd){
                        Log.d(TAG,"remove Bar");
                                 
                       if (mNavigationBarView != null){                     
                       mWindowManager.removeViewImmediate(mNavigationBarView);
                       };
                      if(signalCluster_win!=null)
                                              signalCluster_win=null;
                                          if (mNavigationBarView != null)
                                              mNavigationBarView = null;

                      if(mNotificationLite != null){
                                    mNotificationLite.closeCenter();
                                  }
                        mBarIsAdd = false;
                        SystemProperties.set(ENABLE_NABAR_SWITCH, "false");
                }
        }

6.PhoneWindowManager.java

frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
注册广播

public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {
......
 context.registerReceiver(mScreenshotReceiver, intentfilter);

 //cyh add register for navigationbar
        IntentFilter statubarintentfilter=new IntentFilter();
        statubarintentfilter.addAction("rk.android.navbarview.SHOW");
        context.registerReceiver(mStatuBarReceiver, statubarintentfilter);
 //cyh end

加入Receiver 和 addBar方法

private BroadcastReceiver mStatuBarReceiver=new BroadcastReceiver() {
        @Override
                public void onReceive(Context context, Intent intent) {
                        if (mNavigationBar != null && mNavigationBarOnBottom) {
                                requestTransientBars(mNavigationBar);
                        }
                        addBar();
                }
        };

 public void addBar() {
         try {
            IStatusBarService statusbar = getStatusBarService();
            if (statusbar != null) {
                statusbar.addBar();
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "RemoteException when add Bar", e);
            // re-acquire status bar service next time it is needed.
            mStatusBarService = null;
        }
    }

你可能感兴趣的:(Android 5.1设置内加入导航栏显示与隐藏开关)