Search in sources :

Example 11 with WakeLock

use of android.os.PowerManager.WakeLock in project Osmand by osmandapp.

the class NavigationService method onLocationChanged.

@Override
public void onLocationChanged(Location l) {
    if (l != null && !settings.MAP_ACTIVITY_ENABLED.get()) {
        net.osmand.Location location = OsmAndLocationProvider.convertLocation(l, (OsmandApplication) getApplication());
        if (!isContinuous()) {
            // unregister listener and wait next time
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            try {
                locationManager.removeUpdates(this);
            } catch (SecurityException e) {
                // $NON-NLS-1$
                Log.d(PlatformUtil.TAG, "Location service permission not granted");
            }
            WakeLock lock = getLock(this);
            if (lock.isHeld()) {
                lock.release();
            }
        }
        locationProvider.setLocationFromService(location, isContinuous());
    }
}
Also used : LocationManager(android.location.LocationManager) WakeLock(android.os.PowerManager.WakeLock)

Example 12 with WakeLock

use of android.os.PowerManager.WakeLock in project Osmand by osmandapp.

the class OnNavigationServiceAlarmReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    final WakeLock lock = NavigationService.getLock(context);
    final NavigationService service = ((OsmandApplication) context.getApplicationContext()).getNavigationService();
    // do not do nothing
    if (lock.isHeld() || service == null) {
        return;
    }
    // 
    lock.acquire();
    // request location updates
    final LocationManager locationManager = (LocationManager) service.getSystemService(Context.LOCATION_SERVICE);
    try {
        locationManager.requestLocationUpdates(service.getServiceOffProvider(), 0, 0, service);
        if (service.getServiceOffInterval() > service.getServiceError()) {
            service.getHandler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    // if lock is not anymore held
                    if (lock.isHeld()) {
                        lock.release();
                        locationManager.removeUpdates(service);
                    }
                }
            }, service.getServiceError());
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
}
Also used : LocationManager(android.location.LocationManager) WakeLock(android.os.PowerManager.WakeLock)

Example 13 with WakeLock

use of android.os.PowerManager.WakeLock in project ExoPlayer by google.

the class WakeLockManagerTest method stayAwakeTrue_wakeLockIsOnlyHeldWhenEnabled.

@Test
public void stayAwakeTrue_wakeLockIsOnlyHeldWhenEnabled() {
    WakeLockManager wakeLockManager = new WakeLockManager(context);
    wakeLockManager.setEnabled(true);
    wakeLockManager.setStayAwake(true);
    WakeLock wakeLock = ShadowPowerManager.getLatestWakeLock();
    assertThat(wakeLock.isHeld()).isTrue();
    wakeLockManager.setEnabled(false);
    assertThat(wakeLock.isHeld()).isFalse();
}
Also used : WakeLock(android.os.PowerManager.WakeLock) Test(org.junit.Test)

Example 14 with WakeLock

use of android.os.PowerManager.WakeLock in project Signal-Android by WhisperSystems.

the class WakeLockUtil method runWithLock.

/**
 * Run a runnable with a wake lock. Ensures that the lock is safely acquired and released.
 *
 * @param tag will be prefixed with "signal:" if it does not already start with it.
 */
public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) {
    WakeLock wakeLock = null;
    try {
        wakeLock = acquire(context, lockType, timeout, tag);
        task.run();
    } finally {
        if (wakeLock != null) {
            release(wakeLock, tag);
        }
    }
}
Also used : WakeLock(android.os.PowerManager.WakeLock)

Example 15 with WakeLock

use of android.os.PowerManager.WakeLock in project androidannotations by androidannotations.

the class WakeLockActivityTest method testNoWakeLockUsedBefore.

@Test
public void testNoWakeLockUsedBefore() {
    WakeLock wakeLock = ShadowPowerManager.getLatestWakeLock();
    assertThat(wakeLock).isNull();
}
Also used : WakeLock(android.os.PowerManager.WakeLock) Test(org.junit.Test)

Aggregations

WakeLock (android.os.PowerManager.WakeLock)17 PowerManager (android.os.PowerManager)7 Test (org.junit.Test)4 LocationManager (android.location.LocationManager)3 NonNull (android.annotation.NonNull)1 Nullable (android.annotation.Nullable)1 AlarmManager (android.app.AlarmManager)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 TimestampedValue (android.os.TimestampedValue)1 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)1 NitzData (com.android.internal.telephony.NitzData)1 DeviceState (com.android.internal.telephony.NitzStateMachine.DeviceState)1 NitzSignalInputFilterPredicate (com.android.internal.telephony.nitz.NitzStateMachineImpl.NitzSignalInputFilterPredicate)1 Rlog (com.android.telephony.Rlog)1 Arrays (java.util.Arrays)1 Objects (java.util.Objects)1