Skip to content

Commit

Permalink
WakeLock attempt flutter#2
Browse files Browse the repository at this point in the history
  • Loading branch information
krista-koivisto committed Sep 23, 2019
1 parent 9543dba commit 2bdd8a3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import androidx.legacy.content.WakefulBroadcastReceiver;

public class AlarmBroadcastReceiver extends WakefulBroadcastReceiver {
private PowerManager.WakeLock screenWakeLock;
/**
* Invoked by the OS when a timer goes off.
*
Expand All @@ -30,16 +28,6 @@ public class AlarmBroadcastReceiver extends WakefulBroadcastReceiver {
*/
@Override
public void onReceive(Context context, Intent intent) {
if (screenWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "mocha:event_trigger");
screenWakeLock.acquire(61*60*1000L /*61 minutes*/);
}

AlarmService.enqueueAlarmProcessing(context, intent);

if (screenWakeLock != null) {
screenWakeLock.release();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class AlarmService extends JobIntentService {
private static final String SHARED_PREFERENCES_KEY = "io.flutter.android_alarm_manager_plugin";
private static final int JOB_ID = 1984; // Random job ID.
private static final Object sPersistentAlarmsLock = new Object();
private static PowerManager.WakeLock screenWakeLock;

private static AtomicBoolean sIsIsolateRunning = new AtomicBoolean(false);

Expand Down Expand Up @@ -114,14 +116,14 @@ public static void startBackgroundIsolate(Context context, long callbackHandle)
* AlarmService.initialized} message. Processes all alarm events that came in while the isolate
* was starting.
*/
public static void onInitialized() {
public static void onInitialized(Context context) {
Log.i(TAG, "AlarmService started!");
sIsIsolateRunning.set(true);
synchronized (sAlarmQueue) {
// Handle all the alarm events received before the Dart isolate was
// initialized, then clear the queue.
for (Intent intent : sAlarmQueue) {
executeDartCallbackInBackgroundIsolate(intent, null);
executeDartCallbackInBackgroundIsolate(context, intent, null);
}
sAlarmQueue.clear();
}
Expand Down Expand Up @@ -164,7 +166,12 @@ public static void setPluginRegistrant(PluginRegistrantCallback callback) {
* corresponds to a callback registered with the Dart VM.
*/
private static void executeDartCallbackInBackgroundIsolate(
Intent intent, final CountDownLatch latch) {
Context context, Intent intent, final CountDownLatch latch) {
if (screenWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "mocha:event_trigger");
screenWakeLock.acquire(61*60*1000L /*61 minutes*/);
}
// Grab the handle for the callback associated with this alarm. Pay close
// attention to the type of the callback handle as storing this value in a
// variable of the wrong size will cause the callback lookup to fail.
Expand Down Expand Up @@ -205,6 +212,10 @@ public void notImplemented() {
// when reading the source code. Especially on the Dart side.
sBackgroundChannel.invokeMethod(
"", new Object[] {callbackHandle, intent.getIntExtra("id", -1)}, result);

if (screenWakeLock != null) {
screenWakeLock.release();
}
}

private static void scheduleAlarm(
Expand Down Expand Up @@ -492,7 +503,7 @@ protected void onHandleWork(@NonNull final Intent intent) {
new Runnable() {
@Override
public void run() {
executeDartCallbackInBackgroundIsolate(intent, latch);
executeDartCallbackInBackgroundIsolate(getApplicationContext(), intent, latch);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void onMethodCall(MethodCall call, Result result) {
// is running. From this point forward, the Android side of this plugin can send
// callback handles through the background method channel, and the Dart side will execute
// the Dart methods corresponding to those callback handles.
AlarmService.onInitialized();
AlarmService.onInitialized(mContext);
result.success(true);
} else if (method.equals("Alarm.periodic")) {
// This message indicates that the Flutter app would like to schedule a periodic
Expand Down
2 changes: 1 addition & 1 deletion packages/android_alarm_manager/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: android_alarm_manager
description: Flutter plugin for accessing the Android AlarmManager service, and
running Dart code in the background when alarms fire.
version: 0.4.5
version: 0.4.5+lockfix1
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager

Expand Down

0 comments on commit 2bdd8a3

Please sign in to comment.