《SystemUI》限制低电量打开手电筒

2019-04-14 08:43发布

任务:当电流低于16%的时候,关闭手电,并且在提醒用户不能打开。
监听电量,实现关闭手电
手电图标实现在FlashlightTile.java 监听电量,一般会使用广播,但是SystemUI BatteryController注册了电量广播,可以利用BatteryController里面有相关的操作监听,
  1. 当电量低于16%,不能打开手电筒,并弹出提示框
  2. 当电量低于16%,如果手电筒处于开启状态,关闭手电筒。
public class FlashlightTile extends QSTileImpl implements FlashlightController.FlashlightListener , + BatteryController.BatteryStateChangeCallback { private final Icon mIcon = ResourceIcon.get(R.drawable.ic_signal_flashlight); private final FlashlightController mFlashlightController; + // add for PCAAO-1862 + private int mLevel = 100; + private final int lowLevel = 16; + private final BatteryController mBatteryController; + // END. public FlashlightTile(QSHost host) { super(host); mFlashlightController = Dependency.get(FlashlightController.class); + mBatteryController = Dependency.get(BatteryController.class); } @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); } } @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. boolean newState = !mState.value; refreshState(newState); mFlashlightController.setFlashlight(newState); } + @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); + } + } + } }