All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parse.ParseWakeLock Maven / Gradle / Ivy

Go to download

A library that gives you access to the powerful Parse cloud platform from your Android app.

There is a newer version: 1.17.3
Show newest version
/*
 * Copyright (c) 2015-present, Parse, LLC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.parse;

import android.content.Context;
import android.os.PowerManager;

/**
 * Utility class that wraps a PowerManager.WakeLock and logs an error if the app doesn't have 
 * permissions to acquire wake locks.
 */
/** package */ class ParseWakeLock {
  private static final String TAG = "com.parse.ParseWakeLock";
  
  private static volatile boolean hasWakeLockPermission = true;
  private final PowerManager.WakeLock wakeLock;
  
  public static ParseWakeLock acquireNewWakeLock(Context context, int type, String reason, long timeout) {
    PowerManager.WakeLock wl = null;
    
    if (hasWakeLockPermission) {
      try {
        PowerManager pm = (PowerManager)context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
        
        if (pm != null) {
          wl = pm.newWakeLock(type, reason);
        
          if (wl != null) {
            wl.setReferenceCounted(false);
            
            if (timeout == 0) {
              wl.acquire();
            } else {
              wl.acquire(timeout);
            }
          }
        }
      } catch (SecurityException e) {
        PLog.e(TAG, "Failed to acquire a PowerManager.WakeLock. This is" +
            "necessary for reliable handling of pushes. Please add this to your Manifest.xml: " +
            " ");
        
        hasWakeLockPermission = false;
        wl = null;
      }
    }
    
    return new ParseWakeLock(wl);
  }
  
  private ParseWakeLock(PowerManager.WakeLock wakeLock) {
    this.wakeLock = wakeLock;
  }
  
  public void release() {
    if (wakeLock != null) {
      wakeLock.release();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy