Java Code Examples for android.os.PowerManager.WakeLock#isHeld()

The following examples show how to use android.os.PowerManager.WakeLock#isHeld() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WakeLockUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param tag will be prefixed with "signal:" if it does not already start with it.
 */
public static void release(@Nullable WakeLock wakeLock, @NonNull String tag) {
  tag = prefixTag(tag);
  try {
    if (wakeLock == null) {
      Log.d(TAG, "Wakelock was null. Skipping. Tag: " + tag);
    } else if (wakeLock.isHeld()) {
      wakeLock.release();
      Log.d(TAG, "Released wakelock with tag: " + tag);
    } else {
      Log.d(TAG, "Wakelock wasn't held at time of release: " + tag);
    }
  } catch (Exception e) {
    Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
  }
}
 
Example 2
Source File: WakeLockManager.java    From OmniList with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Releases a partial {@link WakeLock} with a tag contained in the given{@link Intent}
 *
 * @param intent intent*/
private void releasePartialWakeLock(Intent intent) {
    if (intent.hasExtra(WakeLockManager.EXTRA_WAKELOCK_TAG)) {
        final int hash = intent.getIntExtra(WakeLockManager.EXTRA_WAKELOCK_HASH, -1);
        final String tag = intent.getStringExtra(WakeLockManager.EXTRA_WAKELOCK_TAG);
        // We use copy on write list. Iterator won't cause ConcurrentModificationException
        for (WakeLock wakeLock : wakeLocks) {
            if (hash == wakeLock.hashCode()) {
                if (wakeLock.isHeld()) {
                    wakeLock.release();
                    LogUtils.d("releasePartialWakeLock: " + wakeLock.toString() + " " + tag + " was released");
                } else {
                    LogUtils.d("releasePartialWakeLock: " + wakeLock.toString() + " " + tag + " was already released!");
                }
                wakeLocks.remove(wakeLock);
                return;
            }
        }
        LogUtils.e("releasePartialWakeLock: " + "Hash " + hash + " was not found");
    }
}
 
Example 3
Source File: ScreenLockUtil.java    From AndroidModulePattern with Apache License 2.0 6 votes vote down vote up
/**
 * 保持屏幕常亮
 *
 * @param activity you know
 */
public static void keepScreenOn(Activity activity) {
    WakeLock wakeLock = mWakeLockArray.get(activity);
    if (wakeLock == null) {
        PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK,
                activity.getClass().getName());
    }

    if (!wakeLock.isHeld()) {
        wakeLock.acquire();
    }

    mWakeLockArray.put(activity, wakeLock);

    cancelLockScreen(activity);

    Log.i(TAG, "开启屏幕常亮");
}
 
Example 4
Source File: InCallWakeLockUtils.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
private boolean _acquireWakeLock(WakeLock lock, long timeout) {
    synchronized (lock) {
        if (!lock.isHeld()) {
            if (timeout > 0) {
                lock.acquire(timeout);
            } else {
                lock.acquire();
            }
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: InCallWakeLockUtils.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
private boolean _releaseWakeLock(WakeLock lock) {
    synchronized (lock) {
        if (lock.isHeld()) {
            lock.release();
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: ScreenLockUtil.java    From AndroidModulePattern with Apache License 2.0 5 votes vote down vote up
/**
 * 取消屏幕常亮
 *
 * @param activity you know
 */
public static void cancelKeepScreen(Activity activity) {
    WakeLock wakeLock = mWakeLockArray.get(activity);
    if (wakeLock != null) {
        if (wakeLock.isHeld()) {
            wakeLock.release();
        }
    }

    Log.i(TAG, "取消屏幕常亮");
}
 
Example 7
Source File: InCallWakeLockUtils.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
private boolean _acquireWakeLock(WakeLock lock, long timeout) {
    synchronized (lock) {
        if (!lock.isHeld()) {
            if (timeout > 0) {
                lock.acquire(timeout);
            } else {
                lock.acquire();
            }
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: InCallWakeLockUtils.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
private boolean _releaseWakeLock(WakeLock lock) {
    synchronized (lock) {
        if (lock.isHeld()) {
            lock.release();
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: Lock.java    From Kernel-Tuner with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized void acquire(Context context) {
	WakeLock wakeLock = getLock(context);
	if (!wakeLock.isHeld()) {
		wakeLock.acquire();
		//Log.d("alogcat", "wake lock acquired");
	}
}
 
Example 10
Source File: SettingsDownloader.java    From SettingsDeployer with MIT License 4 votes vote down vote up
@Override
protected Boolean doInBackground(String... strParams)
{
    Boolean result = Boolean.FALSE;

    // get a wakelock to hold for the duration of the background work. downloading
    // may be slow. extraction usually isn't too slow but also takes a bit of time. limit the wakelock's time!
    PowerManager powerManager = (PowerManager)m_ctx.getSystemService(Context.POWER_SERVICE);
    WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SettingsDownloaderWakeLock");
    wakeLock.acquire(FOUR_MINUTES);

    // download the file
    String filename = downloadFile(strParams[0]);
    if (filename != null)
    {
        File fSettingsZip = new File(filename);
        if (fSettingsZip.exists() && !isCancelled())
        {
            try
            {
                m_logWriter.write("Successfully downloaded to: "+filename+"\n");

                // extract to wanted directory
                String destDir = strParams[1];
                boolean bSuccess = extractSettingsZip(fSettingsZip, destDir);
                result = (bSuccess ? Boolean.TRUE : Boolean.FALSE);

                // delete settings zip
                if (fSettingsZip.exists()) {
                    fSettingsZip.delete();
                }
            }
            catch(Exception e)
            {
                Log.e("SettingsDownloader", "Error: "+e.toString());
            }
        }
    }

    // if our max time hasn't passed but work is done or an error occurred we bail out and release
    if (wakeLock.isHeld())
    {
        wakeLock.release();
    }

    return result;
}