SystemUI 之 BrightnessDialog、BrightnessController

来源:互联网 发布:给男朋友织围巾知乎 编辑:程序博客网 时间:2024/05/18 22:54

本博客转自:http://www.cnblogs.com/yinhaojun/p/3876132.html

步骤1:当用户点击BrightnessPreference的时候,由BrightnessPreference做对应的处理(注:其实是发送了一个广播)  

复制代码
 1 public class BrightnessPreference extends Preference { 2  3     public BrightnessPreference(Context context, AttributeSet attrs) { 4         super(context, attrs); 5     } 6  7     @Override 8     protected void onClick() { 9         //发送了一个显示亮度调节对话框的广播10         Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);11         getContext().sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF);12     }13 }
复制代码

 

   步骤2:对广播的注册并处理,有一个SettingsUI的类继承自SystemUI,该类负责监听上面的广播并进行处理,详见:

复制代码
 1 public class SettingsUI extends SystemUI { 2     private static final String TAG = "SettingsUI"; 3     private static final boolean DEBUG = false; 4     private final Handler mHandler = new Handler(); 5     private BrightnessDialog mBrightnessDialog; 6     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 7         @Override 8         public void onReceive(Context context, Intent intent) { 9             String action = intent.getAction();10             if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) {11                 if (DEBUG) Log.d(TAG, "showing brightness dialog");12                 if (mBrightnessDialog == null) {13                     mBrightnessDialog = new BrightnessDialog(mContext);14                     mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {15                         @Override16                         public void onDismiss(DialogInterface dialog) {17                             mBrightnessDialog = null;}});18                 }19                 if (!mBrightnessDialog.isShowing()) {20                     //显示亮度调节的dialog21                     mBrightnessDialog.show(); }22             } else {23                 Log.w(TAG, "unknown intent: " + intent);}}};24 25     public void start() {26         //监听ACTION_SHOW_BRIGHTNESS_DIALOG广播27         IntentFilter filter = new IntentFilter();28         filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);29         mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, mHandler);30     }
复制代码

 

   步骤3:上面是负责显示亮度调节的对话框,具体的设置需要查看BrightnessDialog,详见:

 

复制代码
 1     /** 2      * Create the brightness dialog and any resources that are used for the 3      * entire lifetime of the dialog. 4      */ 5     @Override 6     public void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8          9         Window window = getWindow();10         window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);11         window.getAttributes().privateFlags |=12                 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;13         window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);14         window.requestFeature(Window.FEATURE_NO_TITLE);15         //dialog显示的内容quick_settings_brightness_dialog.xml16         setContentView(R.layout.quick_settings_brightness_dialog);17         setCanceledOnTouchOutside(true);18     }19 20 21     @Override22     protected void onStart() {23         super.onStart();24         //对亮度的调节主要是使用了BrightnessController类25         mBrightnessController = new BrightnessController(getContext(),26                 (ImageView) findViewById(R.id.brightness_icon),27                 (ToggleSlider) findViewById(R.id.brightness_slider));28         dismissBrightnessDialog(mBrightnessDialogLongTimeout);29         mBrightnessController.addStateChangedCallback(this);30     }
复制代码

 

   步骤4: BrightnessController详解:

    我们这里主要还是学习下一些基本的内容,具体包括获取设备最大的亮度和最低的亮度值、如果设置屏幕的亮度、当前的亮度值和亮度调节模式保存和获取的位置。

      • 获取设备的最大和最低亮度值        
1         //获取PowerManager对象2         PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);3         //鑾峰彇璁惧鐨勬渶澶т寒搴?4         mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();5         //鑾峰彇璁惧鐨勬渶浣庝寒搴?6         mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();

 

      • 设置屏幕亮度
        //首先获取IPowerManager对象        mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
    private void setBrightness(int brightness) {        try {            mPower.setTemporaryScreenBrightnessSettingOverride(brightness);        } catch (RemoteException ex) {        }    }

 

      • 亮度值和亮度模式的读取和存储
复制代码
1     //设置亮度的设置模式,mode=0表示为手动,mode=1表示自动调节2     private void setMode(int mode) {3         //存储位置为Settings.system中4         Settings.System.putIntForUser(mContext.getContentResolver(),5                 Settings.System.SCREEN_BRIGHTNESS_MODE, mode,6                 mUserTracker.getCurrentUserId());7     }
//以下的代码是负责取出设置模式的

int automatic;
try {
    automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    UserHandle.USER_CURRENT);
} catch (SettingNotFoundException snfe) {
    automatic = 0;
}

复制代码

 

复制代码
          //以下的代码是将亮度值保存到Settings.system中 
       if (!tracking) { AsyncTask.execute(new Runnable() { public void run() { Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, val, UserHandle.USER_CURRENT); } }); }
复制代码

 

复制代码
    /** Fetch the brightness from the system settings and update the slider */    private void updateSlider() {        int value;        try {            //从Settings.System中获取亮度值            value = Settings.System.getIntForUser(mContext.getContentResolver(),                    Settings.System.SCREEN_BRIGHTNESS,                    UserHandle.USER_CURRENT);        } catch (SettingNotFoundException ex) {            value = mMaximumBacklight;        }        mControl.setMax(mMaximumBacklight - mMinimumBacklight);        mControl.setValue(value - mMinimumBacklight);    }
复制代码

 

 

  杂谈:

    我们在BrightnessController中找到了一个内部类叫做BrightnessController,它继承自ContentObserver。该过程使用了观察者设计模式,主要是用来负责对ContentProvider中指定的URI进行监听,如果该URI对应的数据发生了变化就可以及时得到监听并处理。它的使用请参考:http://www.cnblogs.com/slider/archive/2012/02/14/2351702.html 

    BrightnessController主要是监听了

1
2
3
4
private final Uri BRIGHTNESS_MODE_URI =
        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);//亮度调节模式
private final Uri BRIGHTNESS_URI =
        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);//亮度值
复制代码
 1         //当监听的uri发生变化时,回调onChange函数 2         @Override 3         public void onChange(boolean selfChange, Uri uri) { 4             if (selfChange) return; 5             if (BRIGHTNESS_MODE_URI.equals(uri)) { 6                 updateMode();//更新模式 7             } else if (BRIGHTNESS_URI.equals(uri)) { 8                 updateSlider();//更新拖动条 9             } else {10                 updateMode();11                 updateSlider();12             }13             for (BrightnessStateChangeCallback cb : mChangeCallbacks) {14                 cb.onBrightnessLevelChanged();15             }16         }

0 0