Android4.1 Rotation 小结

1, 操作Android 辅助功能:
  •          获取启动时的旋转方向:
               -------> RotationPolicy.isRotationLocked()  : packages/apps/Settings/src/com/android/settings/AccessibilitySettings.java
                         ---->isRotationLocked()  : frameworks/base/core/java/com/android/internal/view/RotationPolicy.java
                                   ---->  Settings.System.ACCELEROMETER_ROTATION ----- 获取系统中的设置属性。
  1. 61     /** 
  2. 62      * Returns true if rotation lock is enabled. 
  3. 63      */  
  4. 64     public static boolean isRotationLocked(Context context) {  
  5. 65         return Settings.System.getInt(context.getContentResolver(),  
  6. 66                 Settings.System.ACCELEROMETER_ROTATION, 0) == 0;  
  7. 67     }  

  •         当  enable 或者 disable "Auto-rotate screen" 的时候
    •  RotationPolicy.setRotationLockForAccessibility()     :   packages/apps/Settings/src/com/android/settings/AccessibilitySettings.java
      • setRotationLockForAccessibility()   :   frameworks/base/core/java/com/android/internal/view/RotationPolicy.java
        1. 102     public static void setRotationLockForAccessibility(Context context, final boolean enabled) {  
        2. 103         Settings.System.putInt(context.getContentResolver(),  
        3. 104                 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0);  
        4. 105  
        5. 106         AsyncTask.execute(new Runnable() {  
        6. 107             @Override  
        7. 108             public void run() {  
        8. 109                 try {  
        9. 110                     IWindowManager wm = IWindowManager.Stub.asInterface(  
        10. 111                             ServiceManager.getService(Context.WINDOW_SERVICE));  
        11. 112                     if (enabled) {  
        12. 113                         wm.freezeRotation(Surface.ROTATION_0);  
        13. 114                     } else {  
        14. 115                         wm.thawRotation();  
        15. 116                     }  
        16. 117                 } catch (RemoteException exc) {  
        17. 118                     Log.w(TAG, "Unable to save auto-rotate setting");  
        18. 119                 }  
        19. 120             }  
        20. 121         });  
        21. 122     }  

        • 如果enable: 调用WindowManagerService的freezeRotation()
        • 否则调用wm.thawRotation() ----- 即WindowManagerService的thawRotation()
      • freezeRotation()   ---------- frameworks/base/services/java/com/android/server/wm/WindowManagerService.java
          1. 5663     /** 
          2. 5664      * Freeze rotation changes.  (Enable "rotation lock".) 
          3. 5665      * Persists across reboots. 
          4. 5666      * @param rotation The desired rotation to freeze to, or -1 to use the 
          5. 5667      * current rotation. 
          6. 5668      */  
          7. 5669     public void freezeRotation(int rotation) {  
          8. 5670         if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,  
          9. 5671                 "freezeRotation()")) {  
          10. 5672             throw new SecurityException("Requires SET_ORIENTATION permission");  
          11. 5673         }  
          12. 5674         if (rotation < -1 || rotation > Surface.ROTATION_270) {  
          13. 5675             throw new IllegalArgumentException("Rotation argument must be -1 or a valid "  
          14. 5676                     + "rotation constant.");  
          15. 5677         }  
          16. 5678  
          17. 5679         if (DEBUG_ORIENTATION) Slog.v(TAG, "freezeRotation: mRotation=" + mRotation);  
          18. 5680  
          19. 5681         mPolicy.setUserRotationMode(WindowManagerPolicy.USER_ROTATION_LOCKED,  
          20. 5682                 rotation == -1 ? mRotation : rotation);  
          21. 5683         updateRotationUnchecked(falsefalse);  
          22. 5684       

        • setUserRotationMode()  ------frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
        •    通过PhoneWindowManager.setUserRotationMode()去设置Settings.System相关的数据库值,在PhoneWindowManager中会有一个Observe去监听Settings.System的数值变化,如果有变动就去调用SettingsObserver.onChange()  .
        •  updateRotationUnchecked   -------   WindowManagerService.updateRotationUnchecked()

               


               分析: thawRotation()    ----------- 释放rotation ----- 无论什么时候都使能旋转:
                                   调用 setUserRotationMode()  -----mPolicy.setUserRotationMode( WindowManagerPolicy.USER_ROTATION_FREE, 777); // rot not used
                    分析setUserRotationMode : 
  1.  public void setUserRotationMode(int mode, int rot) {  
  2.         ContentResolver res = mContext.getContentResolver();  
  3.   
  4.         // mUserRotationMode and mUserRotation will be assigned by the content observer  
  5.         if (mode == WindowManagerPolicy.USER_ROTATION_LOCKED) {  
  6.             Settings.System.putIntForUser(res,  
  7.                     Settings.System.USER_ROTATION,  
  8.                     rot,  
  9.                     UserHandle.USER_CURRENT);  
  10.             Settings.System.putIntForUser(res,  
  11.                     Settings.System.ACCELEROMETER_ROTATION,  
  12.                     0,  
  13.                     UserHandle.USER_CURRENT);  
  14.         } else {  
  15.             Settings.System.putIntForUser(res,  
  16.                     Settings.System.ACCELEROMETER_ROTATION,  
  17.                     1,  
  18.                     UserHandle.USER_CURRENT);  
  19.         }  
  20. }  



           ---------------- 也就是说,如果在启动系统之前,调用thawrotation(),  就是强制地执行 Settings.System.ACCELEROMETER_ROTATION; 这样系统就会一直默认旋转。
     以前遇到有一个问题:在系统启动时,强制打开系统旋转,这样在辅助功能的设置就会无效,正是这个原因。
 
  1. private final class BootCompletedReceiver extends BroadcastReceiver {  
  2.   @Override  
  3.   public void onReceive(Context context, Intent intent) {  
  4.     // At any given time accessories could be inserted  
  5.     // one on the board, one on the dock and one on HDMI:  
  6.     // observe three UEVENTs  
  7.     init();  // set initial status  
  8. *     try{  
  9.       IWindowManager wm = IWindowManager.Stub.asInterface(  
  10.                     ServiceManager.getService(Context.WINDOW_SERVICE));  
  11.       wm.thawRotation();  
  12.     }catch(Exception e){  
  13.   
  14.     }*/  
  15.     for (int i = 0; i < uEventInfo.size(); ++i) {  
  16.         UEventInfo uei = uEventInfo.get(i);  
  17.         startObserving("DEVPATH="+uei.getDevPath());  
  18.     }  
  19.   }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值