static void releaseWakeLock(@Nullable PowerManager.WakeLock wakeLock) { try { if (wakeLock != null && wakeLock.isHeld()) { wakeLock.release(); } } catch (Exception e) { // just to make sure if the PowerManager crashes while acquiring a wake lock CAT.e(e); } }
@Override public void onCreate() { super.onCreate(); PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wakeLock.setReferenceCounted(false); if (!wakeLock.isHeld()) wakeLock.acquire(); if (UPLOAD_POOL_SIZE <= 0) { UPLOAD_POOL_SIZE = Runtime.getRuntime().availableProcessors(); } // Creates a thread pool manager uploadThreadPool = new ThreadPoolExecutor( UPLOAD_POOL_SIZE, // Initial pool size UPLOAD_POOL_SIZE, // Max pool size KEEP_ALIVE_TIME_IN_SECONDS, TimeUnit.SECONDS, uploadTasksQueue); }
static boolean acquireWakeLock(@NonNull Context context, @Nullable PowerManager.WakeLock wakeLock, long timeoutMillis) { if (wakeLock != null && !wakeLock.isHeld() && JobUtil.hasWakeLockPermission(context)) { // Even if we have the permission, some devices throw an exception in the try block nonetheless, // I'm looking at you, Samsung SM-T805 try { wakeLock.acquire(timeoutMillis); return true; } catch (Exception e) { // saw an NPE on rooted Galaxy Nexus Android 4.1.1 // android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:288) CAT.e(e); } } return false; }
/** * Releases the currently held wake lock for this client */ private void releaseWakeLock() { if(wakelock != null && wakelock.isHeld()){ wakelock.release(); } }
protected synchronized void releaseLocks() { if (wakeLock != null && wakeLock.isHeld()) { wakeLock.release(); wakeLock = null; } if (wifiLock != null && wifiLock.isHeld()) { wifiLock.release(); wifiLock = null; } }
mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "wakeLock"); if (!mWakeLock.isHeld()) mWakeLock.acquire();
public static void clean(boolean releaseLocks) { setForwarding(false); try { if (releaseLocks) { Logger.debug("Releasing locks."); if (mWifiLock != null && mWifiLock.isHeld()) mWifiLock.release(); if (mWakeLock != null && mWakeLock.isHeld()) mWakeLock.release(); } RemoteReader.terminateAll(); GitHubParser.resetAll(); synchronized (mTargets) { for (Target t : mTargets) for (Session s : t.getSessions()) s.stopSession(); mTargets.clear(); } Client.Disconnect(); mCoreInitialized = false; mInitialized = false; Services.getNetworkRadar().onAutoScanChanged(); } catch (Exception e) { errorLogging(e); } }
@Override public void onDestroy() { super.onDestroy(); stopAllUploads(); uploadThreadPool.shutdown(); if (isExecuteInForeground()) { Logger.debug(TAG, "Stopping foreground execution"); stopForeground(true); } if (wakeLock.isHeld()) wakeLock.release(); uploadTasksMap.clear(); uploadDelegates.clear(); Logger.debug(TAG, "UploadService destroyed"); }
@Override protected void onPostExecute(Payload data) { super.onPostExecute(data); // Sync has ended so release the wake lock if (mWakeLock.isHeld()) { mWakeLock.release(); } if (mListener != null) { mListener.onPostExecute(data); } }
private void screenOn() { if(mWakeLock != null) { if (!mWakeLock.isHeld()) { mWakeLock.acquire(); } } }
private void screenOff() { if(mWakeLock != null) { if (mWakeLock.isHeld()) { mWakeLock.release(); } } }
public boolean isWakelockEnabled() { if (mWakeLock == null) { return false; } else { return mWakeLock.isHeld(); } }
@Override public Job.Result call() throws Exception { try { // just in case something was blocking and the wake lock is no longer acquired WakeLockUtil.acquireWakeLock(mJob.getContext(), mWakeLock, WAKE_LOCK_TIMEOUT); return runJob(); } finally { markJobAsFinished(mJob); if (mWakeLock == null || !mWakeLock.isHeld()) { CAT.w("Wake lock was not held after job %s was done. The job took too long to complete. This could have unintended side effects on your app.", mJob); } WakeLockUtil.releaseWakeLock(mWakeLock); } }
@Test public void acquire_shouldLogLatestWakeLock() throws Exception { ShadowPowerManager.reset(); assertThat(ShadowPowerManager.getLatestWakeLock()).isNull(); PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); lock.acquire(); assertThat(ShadowPowerManager.getLatestWakeLock()).isNotNull(); assertThat(ShadowPowerManager.getLatestWakeLock()).isSameAs(lock); assertThat(lock.isHeld()).isTrue(); lock.release(); assertThat(ShadowPowerManager.getLatestWakeLock()).isNotNull(); assertThat(ShadowPowerManager.getLatestWakeLock()).isSameAs(lock); assertThat(lock.isHeld()).isFalse(); ShadowPowerManager.reset(); assertThat(ShadowPowerManager.getLatestWakeLock()).isNull(); }
public void releaseWifiAndCpu() { Log.d(TAG, "releaseWifiAndCpu() called"); if (wakeLock != null && wakeLock.isHeld()) wakeLock.release(); if (wifiLock != null && wifiLock.isHeld()) wifiLock.release(); wakeLock = null; wifiLock = null; } }
public void acquireWifiAndCpu() { Log.d(TAG, "acquireWifiAndCpu() called"); if (wakeLock != null && wakeLock.isHeld() && wifiLock != null && wifiLock.isHeld()) return; wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG); if (wakeLock != null) wakeLock.acquire(); if (wifiLock != null) wifiLock.acquire(); }
@Test public void acquire_shouldAcquireAndReleaseReferenceCountedLock() throws Exception { PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); assertThat(lock.isHeld()).isFalse(); lock.acquire(); assertThat(lock.isHeld()).isTrue(); lock.acquire(); assertThat(lock.isHeld()).isTrue(); lock.release(); assertThat(lock.isHeld()).isTrue(); lock.release(); assertThat(lock.isHeld()).isFalse(); }
@Test public void newWakeLock_shouldAcquireAndReleaseNonReferenceCountedLock() throws Exception { PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); lock.setReferenceCounted(false); assertThat(lock.isHeld()).isFalse(); lock.acquire(); assertThat(lock.isHeld()).isTrue(); lock.acquire(); assertThat(lock.isHeld()).isTrue(); lock.release(); assertThat(lock.isHeld()).isFalse(); }