任務:當電流低於16%的時候,關閉手電,而且在提醒用戶不能打開。java
監聽電量,實現關閉手電
手電圖標實如今FlashlightTile.javaide
監聽電量,通常會使用廣播,可是SystemUI BatteryController註冊了電量廣播,能夠利用BatteryController裏面有相關的操做監聽,this
當電量低於16%,不能打開手電筒,並彈出提示框
當電量低於16%,若是手電筒處於開啓狀態,關閉手電筒。
public class FlashlightTile extends QSTileImpl<BooleanState> implements
FlashlightController.FlashlightListener ,
+ BatteryController.BatteryStateChangeCallback
{.net
private final Icon mIcon = ResourceIcon.get(R.drawable.ic_signal_flashlight);
private final FlashlightController mFlashlightController;rem
+ // add for PCAAO-1862
+ private int mLevel = 100;
+ private final int lowLevel = 16;
+ private final BatteryController mBatteryController;
+ // END.get
public FlashlightTile(QSHost host) {
super(host);
mFlashlightController = Dependency.get(FlashlightController.class);
+ mBatteryController = Dependency.get(BatteryController.class);
}string
@Override
public void handleSetListening(boolean listening) {
Log.i("FlashlightTile","handleSetListening "+listening);
if (listening) {
+ mFlashlightController.addCallback(this);
mBatteryController.addCallback(this);
} else {
mFlashlightController.removeCallback(this);
+ mBatteryController.removeCallback(this);
}
}flash
@Override
protected void handleClick() {
if (ActivityManager.isUserAMonkey()) {
return;
}
+ // add for PCAAO-1862
+ boolean flashLightEnable = mFlashlightController.isEnabled();
+ if (!flashLightEnable && mLevel <= lowLevel) {
+ SystemUIDialog dialog = new SystemUIDialog(mContext);
+ dialog.setTitle(R.string.airplane_dlg_title);
+ dialog.setMessage(R.string.flash_light_dlg_msg);
+ dialog.setPositiveButton(R.string.yes, null);
+ //dialog.setNegativeButton(R.string.cancel, null);
+ dialog.setShowForAllUsers(true);
+ dialog.show();
+ return;
+ }
+ // END.it
boolean newState = !mState.value;
refreshState(newState);
mFlashlightController.setFlashlight(newState);class
}
+ @Override
+ public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
+ mLevel = level;
+
+ //if
+ boolean flashLightEnable = mFlashlightController.isEnabled();
+ Log.i("FlashlightTile","flashLightEnable ,mState.value"+flashLightEnable+","+mState.value);
+ if (mLevel <= lowLevel && flashLightEnable && mState.value){
+ refreshState(false);
+ if(mFlashlightController!=null){
+ mFlashlightController.setFlashlight(false);
+ }
+ }
+ }
}