api: device.keepScreenOn(), device.keepScreenDim(), device.wakeUp(), device.vibrate();

fix: crash when rotating editor
This commit is contained in:
hyb1996
2017-12-29 14:09:27 +08:00
parent c12814769f
commit de4f086415
6 changed files with 65 additions and 7 deletions

View File

@@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application

View File

@@ -10,6 +10,8 @@ import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import android.os.Vibrator;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -84,6 +86,8 @@ public class Device {
public static final String serial = Build.SERIAL;
private Context mContext;
private PowerManager.WakeLock mWakeLock;
private int mWakeLockFlag;
public Device(Context context) {
mContext = context;
@@ -195,7 +199,6 @@ public class Device {
return info.availMem;
}
public boolean isCharging() {
Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (intent == null) {
@@ -205,6 +208,58 @@ public class Device {
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
public void keepAwake(int flags, long timeout) {
checkWakeLock(flags);
mWakeLock.acquire(timeout);
}
@SuppressLint("WakelockTimeout")
public void keepAwake(int flags) {
checkWakeLock(flags);
mWakeLock.acquire();
}
public void wakeUp() {
keepScreenOn(200);
}
public void keepScreenOn() {
keepAwake(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP);
}
public void keepScreenOn(long timeout) {
keepAwake(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, timeout);
}
public void keepScreenDim() {
keepAwake(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP);
}
public void keepScreenDim(long timeout) {
keepAwake(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, timeout);
}
private void checkWakeLock(int flags) {
if (mWakeLock == null || flags != mWakeLockFlag) {
cancelKeepingAwake();
mWakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(flags, Device.class.getName());
}
}
public void cancelKeepingAwake() {
if (mWakeLock != null && mWakeLock.isHeld())
mWakeLock.release();
}
public void vibrate(long millis) {
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(millis);
}
public void cancelVibration() {
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).cancel();
}
private void checkWriteSettingsPermission() {
if (SettingsCompat.canWriteSettings(mContext)) {
return;

View File

@@ -35,7 +35,7 @@ public class Timers {
return mMainTimer;
}
public VolatileBox<Long> getMaxCallbackUptimeMillisForAllThreads() {
VolatileBox<Long> getMaxCallbackUptimeMillisForAllThreads() {
return mMaxCallbackUptimeMillisForAllThreads;
}
@@ -81,8 +81,6 @@ public class Timers {
public boolean hasPendingCallbacks() {
//如果是脚本主线程则检查所有子线程中的定时回调。mFutureCallbackUptimeMillis用来记录所有子线程中定时最久的一个。
if (mThreads.getMainThread() == Thread.currentThread()) {
Log.d(LOG_TAG, "[main thread]hasPendingCallbacks:" + (mMaxCallbackUptimeMillisForAllThreads.get() > SystemClock.uptimeMillis()));
Log.d(LOG_TAG, "mMaxCallbackUptimeMillisForAllThreads:" + mMaxCallbackUptimeMillisForAllThreads.get());
return mMaxCallbackUptimeMillisForAllThreads.get() > SystemClock.uptimeMillis();
}
//否则检查当前线程的定时回调