首页 > 代码库 > Android4.0-4.4 添加实体按键振动支持的方法(java + smali版本)

Android4.0-4.4 添加实体按键振动支持的方法(java + smali版本)

有些手机比如泛泰A820L, 泛泰A890 A900 以及Nubia Z5S 和Z5S mini具有实体按键(这里所说的实体按键是相对于虚拟按键而言, 包含物理按键和触摸屏上多出来的触摸实体按键), 当编译第三方ROM比如CM或者Mokee或者Omni时就会发现如果不做修改的话默认触摸实体按键时是没有振动支持的, 甚至有些patchrom的MIUI或者ColorOS等系统也可能不支持. 不知道是CM代码的问题还是编译的Config没写完全造成的, 后来经过本人修改后是可以支持按键振动, 不过只能作为临时解决方案参考.


转帖请注明出处和网址来源:

http://blog.csdn.net/syhost/article/details/32067695


java版本代码

在本人提交的cm的review上, 没有被通过,因为如果加进来不修改的话,会影响虚拟键的振动,会导致home键振动两次, 不过只是作为支持实体按键振动的话还是可以的:

http://review.cyanogenmod.org/#/c/55274/1/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java


修改的位置为android.policy.jar的源码里  frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java


diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index f0719e3..ca38166 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -2333,6 +2333,22 @@ public class PhoneWindowManager implements WindowManagerPolicy {
             WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
             WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
         };
+       
+    private void setKeyVib(WindowState win) {
+        WindowManager.LayoutParams attrs;
+        Intent service;
+        ContentResolver res;
+        
+		// If a system window has focus, then it doesn't make sense
+		// right now to interact with applications.
+		attrs = win != null ? win.getAttrs() : null;
+		service = new Intent();
+		res = mContext.getContentResolver();
+		if(1 == Settings.System.getInt(res, Settings.System.HAPTIC_FEEDBACK_ENABLED,0))//when setting feedback enable
+		{
+			performHapticFeedbackLw(null, HapticFeedbackConstants.KEYBOARD_TAP, true); //启动震动 by syhost
+		}
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -2441,7 +2457,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                             ViewConfiguration.getDoubleTapTimeout());
                     return -1;
                 }
-
+                
+				setKeyVib(win);
                 // Go home!
                 launchHomeFromHotKey();
                 return -1;
@@ -2514,6 +2531,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                 }
                 if (repeatCount == 0) {
                     mMenuPressed = true;
+                    setKeyVib(win);
                     if (mEnableShiftMenuBugReports && (metaState & chordBug) == chordBug) {
                         Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
                         mContext.sendOrderedBroadcast(intent, null);
@@ -2645,9 +2663,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
             }
             return -1;
         } else if (keyCode == KeyEvent.KEYCODE_BACK) {
+        	if (down && repeatCount == 0) {
+        		setKeyVib(win);
+        	}
             if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
                     Settings.Secure.KILL_APP_LONGPRESS_BACK, 0, UserHandle.USER_CURRENT) == 1) {
                 if (down && repeatCount == 0) {
+                	setKeyVib(win);
                     mHandler.postDelayed(mBackLongPress, mBackKillTimeout);
                 }
             }




对应的smali版本, 

在本人之前适配的泛泰A820L的cm10版本的乐蛙ROM里可以找到, 是分几次完成的:


patch1:

https://github.com/syhost/lewa_patchrom_ef65l/commit/18afafa2c7b770bdb593fee086c7cc07f8789e58

    goto :goto_0
  .end method
  
 +.method private setKeyVib(Landroid/view/WindowManagerPolicy$WindowState;)V
 +    .locals 7
 +    .parameter "win"
 +
 +    .prologue
 +    const/4 v3, 0x0
 +
 +    const/4 v6, 0x1
 +
 +    .line 2110
 +    if-eqz p1, :cond_1
 +
 +    invoke-interface {p1}, Landroid/view/WindowManagerPolicy$WindowState;->getAttrs()Landroid/view/WindowManager$LayoutParams;
 +
 +    move-result-object v0
 +
 +    .line 2111
 +    .local v0, attrs:Landroid/view/WindowManager$LayoutParams;
 +    :goto_0
 +    new-instance v2, Landroid/content/Intent;
 +
 +    invoke-direct {v2}, Landroid/content/Intent;-><init>()V
 +
 +    .line 2112
 +    .local v2, service:Landroid/content/Intent;
 +    iget-object v4, p0, Lcom/android/internal/policy/impl/PhoneWindowManager;->mContext:Landroid/content/Context;
 +
 +    invoke-virtual {v4}, Landroid/content/Context;->getContentResolver()Landroid/content/ContentResolver;
 +
 +    move-result-object v1
 +
 +    .line 2113
 +    .local v1, res:Landroid/content/ContentResolver;
 +    const-string v4, "haptic_feedback_enabled"
 +
 +    const/4 v5, 0x0
 +
 +    invoke-static {v1, v4, v5}, Landroid/provider/Settings$System;->getInt(Landroid/content/ContentResolver;Ljava/lang/String;I)I
 +
 +    move-result v4
 +
 +    if-ne v6, v4, :cond_0
 +
 +    .line 2115
 +    const/4 v4, 0x3
 +
 +    invoke-virtual {p0, v3, v4, v6}, Lcom/android/internal/policy/impl/PhoneWindowManager;->performHapticFeedbackLw(Landroid/view/WindowManagerPolicy$WindowState;IZ)Z
 +
 +    .line 2117
 +    :cond_0
 +    return-void
 +
 +    .end local v0           #attrs:Landroid/view/WindowManager$LayoutParams;
 +    .end local v1           #res:Landroid/content/ContentResolver;
 +    .end local v2           #service:Landroid/content/Intent;
 +    :cond_1
 +    move-object v0, v3
 +
 +    .line 2110
 +    goto :goto_0
 +.end method
 +
  .method private takeScreenshot()V
      .locals 8
  
 @@ -8384,6 +8446,7 @@
      .line 1799
      .end local v11           #ex:Landroid/os/RemoteException;
      :cond_6
 +    invoke-direct/range {p0 .. p1}, Lcom/android/internal/policy/impl/PhoneWindowManager;->setKeyVib(Landroid/view/WindowManagerPolicy$WindowState;)V
      invoke-virtual/range {p0 .. p0}, Lcom/android/internal/policy/impl/PhoneWindowManager;->launchHomeFromHotKey()V
  
      goto :goto_4
 @@ -8631,6 +8694,8 @@
  
      if-nez v23, :cond_1c
  
 +    invoke-direct/range {p0 .. p1}, Lcom/android/internal/policy/impl/PhoneWindowManager;->setKeyVib(Landroid/view/WindowManagerPolicy$WindowState;)V
 +
      .line 1853
      move-object/from16 v0, p0



patch2:

https://github.com/syhost/lewa_patchrom_ef65l/commit/0e5ff497456e9428546be24bae36dfcf4544863f

 
      move/from16 v1, v32
  
 -    if-ne v0, v1, :cond_1c
 +    if-ne v0, v1, :cond_34
  
      .line 1894
      if-eqz v9, :cond_1a
 @@ -8887,7 +8887,25 @@
      const-wide/16 v32, -0x1
  
      goto/16 :goto_1
 +    
 +    :cond_34
 +    const/16 v32, 0x4
 +
 +    move/from16 v0, v18
 +
 +    move/from16 v1, v32
 +
 +    if-ne v0, v1, :cond_1c
 +
 +    if-eqz v9, :cond_1c
  
 +    if-nez v23, :cond_1c
 +
 +    invoke-direct/range {p0 .. p1}, Lcom/android/internal/policy/impl/PhoneWindowManager;->setKeyVib(Landroid/view/WindowManagerPolicy$WindowState;)V
 +    
 +    goto/16 :goto_34
 +    
 +    
      .line 1897
      :cond_19
      const/16 v32, 0x1
 @@ -8946,6 +8964,7 @@
      goto :goto_9
  
      .line 1920
 +    :goto_34
      :cond_1c
      move-object/from16 v0, p0

上面的patch2主要是加了防止长按home键长振, 对应的java部分是:

+        	if (down && repeatCount == 0) {
+        		setKeyVib(win);
+        	}