安卓添加虚拟按键(home键,返回键,列表键)

Platform: msm8916
OS:安卓5.1
Kernel: 3.10.49

相关文章:安卓去除虚拟按键

实现功能:1.导航栏添加声音图标(声音有两种状态,打开和关闭)

                  2.在打开状态下———点击———》图标会改变成关闭状态。

                  3.点击时同时会发送广播,通知应用。

 

          com.android.open.sound

          com.android.close.sound

 

文件修改:

xml布局文件:

 

		--- a/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
+++ b/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
@@ -87,7 +87,22 @@
+++ b/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
@@ -87,7 +87,22 @@
                 android:scaleType="center"
                android:visibility="gone"
                android:contentDescription="@string/accessibility_recent"
+               />
+
+            
+            
             
+               
+           
+            
             

 

--- a/frameworks/base/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
+++ b/frameworks/base/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
@@ -80,7 +80,15 @@
                android:layout_weight="0"
                android:visibility="gone"
                 android:contentDescription="@string/accessibility_recent"
-                />
+               />
+            
             
+               />
+            
             
--- a/frameworks/base/packages/SystemUI/res/values/strings.xml
+++ b/frameworks/base/packages/SystemUI/res/values/strings.xml
@@ -204,6 +204,8 @@
     Menu
     
     Overview
+    Sound
+    Nosound
--- a/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
+++ b/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
@@ -85,6 +85,8 @@
     "主屏幕"
     "菜单"
     "概览"
+    "打开声音"
+    "关闭声音"

java代码的修改:

--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -45,6 +45,13 @@ import android.view.inputmethod.InputMethodManager;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.content.BroadcastReceiver;
+import android.widget.Toast; 
+import android.os.IBinder;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.content.Intent;
+import android.content.IntentFilter;
 
 import com.android.systemui.R;
 import com.android.systemui.statusbar.BaseStatusBar;
@@ -90,7 +97,7 @@ public class NavigationBarView extends LinearLayout {
 
     // performs manual animation in sync with layout transitions
     private final NavTransitionListener mTransitionListener = new NavTransitionListener();
-
+    AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
     private OnVerticalChangedListener mOnVerticalChangedListener;
     private boolean mIsLayoutRtl;
     private boolean mDelegateIntercepted;
@@ -255,6 +262,40 @@ public class NavigationBarView extends LinearLayout {
         return mCurrentView.findViewById(R.id.recent_apps);
     }
 
+       //add by zk
+       public View getAddVolume(){  
+       
+                IntentFilter mfilter = new IntentFilter( );
+         mfilter.addAction("com.android.open.audio");
+         mfilter.addAction("com.android.close.audio");
+                mContext.registerReceiver( mReceiver, mfilter );
+                
+
+               ImageView mView = (ImageView)mCurrentView.findViewById(R.id.sound);             
+               
+       
+       
+       return mView;  
+    }  
+       BroadcastReceiver mReceiver = new BroadcastReceiver( ) {//接收应用发送过来的广播,保存了关机前的状态。
+                @Override
+                public void onReceive( Context context, Intent intent ) {
+                        String action = intent.getAction( );
+                        Log.e(TAG, "action ======= " + action);
+                                               ImageView mView = (ImageView)mCurrentView.findViewById(R.id.sound);     
+                        if(action.equals("com.android.open.audio"))
+                        {
+                            mView.setImageResource(R.drawable.ic_sysbar_sound);  //显示打开音量图标                
+                        }else if(action.equals("com.android.close.audio"))
+                        {
+                             mView.setImageResource(R.drawable.ic_sysbar_nosound);//显示关闭的图标
+                        }
+                }
+     };
+       
+       
+       //end
+       
     public View getMenuButton() {
         return mCurrentView.findViewById(R.id.menu);
     }
@@ -369,6 +410,7 @@ public class NavigationBarView extends LinearLayout {
         getBackButton()   .setVisibility(disableBack       ? View.INVISIBLE : View.VISIBLE);
         getHomeButton()   .setVisibility(disableHome       ? View.INVISIBLE : View.VISIBLE);
         getRecentsButton().setVisibility(disableRecent     ? View.INVISIBLE : View.VISIBLE);
+               getAddVolume().setVisibility(disableRecent     ? View.INVISIBLE : View.VISIBLE);
        getRecentsButton().setVisibility(View.GONE);
 
         mBarTransitions.applyBackButtonQuiescentAlpha(mBarTransitions.getMode(), true /*animate*/);
@@ -477,7 +519,7 @@ public class NavigationBarView extends LinearLayout {
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         super.onLayout(changed, l, t, r, b);
-        mDelegateHelper.setInitialTouchRegion(getHomeButton(), getBackButton(), getRecentsButton());
+        mDelegateHelper.setInitialTouchRegion(getHomeButton(), getBackButton(), getRecentsButton(),getAddVolume());
     }
 
     @Override
@@ -650,7 +692,7 @@ public class NavigationBarView extends LinearLayout {
         dumpButton(pw, "home", getHomeButton());
         dumpButton(pw, "rcnt", getRecentsButton());
         dumpButton(pw, "menu", getMenuButton());
-
+               dumpButton(pw, "menu", getAddVolume());
         pw.println("    }");
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -184,6 +184,15 @@ import com.android.systemui.statusbar.stack.StackScrollAlgorithm;
 import com.android.systemui.statusbar.stack.StackScrollState.ViewState;
 import com.android.systemui.volume.VolumeComponent;
 
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.content.Intent; 
+import android.app.Application;
+import android.view.KeyCharacterMap;
+import android.view.KeyCharacterMap.FallbackAction;
+import android.view.KeyEvent;
+
+
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -211,7 +220,8 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                       = "com.android.internal.policy.statusbar.START";
 
               public static final boolean SHOW_LOCKSCREEN_MEDIA_ARTWORK = true;
-
+                   
+                  private static int sound_flag = 0;
               private static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
               private static final int MSG_CLOSE_PANELS = 1001;
               private static final int MSG_OPEN_SETTINGS_PANEL = 1002;
@@ -643,7 +653,29 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
 
                       notifyUserAboutHiddenNotifications();
                       mScreenPinningRequest = new ScreenPinningRequest(mContext);
+                          //add by zk
+                               IntentFilter mfilter = new IntentFilter( );
+                               mfilter.addAction("com.android.open.audio");
+                               mfilter.addAction("com.android.close.audio");
+                               mContext.registerReceiver( mReceiver, mfilter );
+                          
               }//接收应用发送过来的广播,保存的是关机之前的状态。
+                  BroadcastReceiver mReceiver = new BroadcastReceiver( ) {
+                @Override
+                public void onReceive( Context context, Intent intent ) {
+                        String action = intent.getAction( );
+                        Log.e(TAG, "action ======= " + action);
+                        if(action.equals("com.android.open.audio"))
+                        {
+                            sound_flag = 1;                 
+                        }else if(action.equals("com.android.close.audio"))
+                        {
+                            sound_flag = 0;
+                        }
+                }
+        };
+        
+                  
 
               boolean isMSim() {
                       return (TelephonyManager.getDefault().getPhoneCount() > 1);
@@ -1299,6 +1331,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                     mNavigationBarView.getBackButton().setLongClickable(true);
                     mNavigationBarView.getBackButton().setOnLongClickListener(mLongPressBackRecentsListener);
                     mNavigationBarView.getHomeButton().setOnTouchListener(mHomeActionListener);
                    //监听那个按键被按下。
+                    mNavigationBarView.getAddVolume().setOnClickListener(mAddVolumeClickListener);
                     mNavigationBarView.getRecentsButton().setVisibility(View.GONE);
                     updateSearchPanel();
                       }
@@ -2844,6 +2877,37 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                               }
                               checkBarModes();
                       }
+                          
+       private boolean flag=false; 
+       private View.OnClickListener mAddVolumeClickListener = new View.OnClickListener() {
+               @Override  
+        public void onClick(View view) {  
+                
+          
+                       ImageView mView = (ImageView)view;
+                       
+                       if(sound_flag == 0){//根据标志位判断显示的状态。
+                                       //unmute
+                                       Log.d(TAG, "sendBroadcast:com.android.open.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_sound);
+                                       Intent intent = new Intent();
+                    			intent.setAction("com.android.open.sound");//向应用发送广播。
+                    			mContext.sendBroadcast(intent);
+                                       sound_flag = 1;
+                                       
+                                                 
+                               }else {
+                                       //mute
+                                       Log.d(TAG, "sendBroadcast:com.android.close.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_nosound);   
+                                       Intent intent = new Intent();
+                    intent.setAction("com.android.close.sound");//向应用发送广播。
+                    mContext.sendBroadcast(intent);
+                                       sound_flag = 0;
+                               }
+        }  
+    };  
 
			intent.setAction("com.android.open.sound");//向应用发送广播。
+                    			mContext.sendBroadcast(intent);
+                                       sound_flag = 1;
+                                       
+                                                 
+                               }else {
+                                       //mute
+                                       Log.d(TAG, "sendBroadcast:com.android.close.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_nosound);   
+                                       Intent intent = new Intent();
+                    intent.setAction("com.android.close.sound");//向应用发送广播。
+                    mContext.sendBroadcast(intent);
+                                       sound_flag = 0;
+                               }
+        }  
+    };  
 
 

图片资源目录:

#       frameworks/base/packages/SystemUI/res/drawable-hdpi/

#       frameworks/base/packages/SystemUI/res/drawable-mdpi/

#       frameworks/base/packages/SystemUI/res/drawable-xhdpi/

#       frameworks/base/packages/SystemUI/res/drawable-xxhdpi

 

居中问题patch文件:https://download.csdn.net/download/kai_zone/10576998

 

 

 

你可能感兴趣的:(安卓系统相关)