Skip to content

Commit

Permalink
Alarm: handle wakelock properly
Browse files Browse the repository at this point in the history
  • Loading branch information
deiteris committed Mar 24, 2017
1 parent 3bba6d0 commit 151a2bd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
33 changes: 29 additions & 4 deletions app/src/main/java/com/example/yink/amadeus/Alarm.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.PowerManager;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
Expand All @@ -22,9 +23,12 @@ class Alarm {

private static final String TAG = "Alarm";
private static boolean isPlaying = false;
private static PowerManager.WakeLock sCpuWakeLock;

static void start(Context context, int ringtone) {

acquireCpuWakeLock(context);

settings = PreferenceManager.getDefaultSharedPreferences(context);

if (settings.getBoolean("vibrate", false)) {
Expand Down Expand Up @@ -61,11 +65,11 @@ static void cancel(Context context) {
m.release();
notificationManager.cancel(ALARM_NOTIFICATION_ID);
alarmManager.cancel(pendingIntent);
releaseCpuLock();
isPlaying = false;
}

if (v != null) {
v.cancel();
if (v != null) {
v.cancel();
}
}

Log.d(TAG, "Cancel");
Expand All @@ -76,4 +80,25 @@ static boolean isPlaying() {
return isPlaying;
}

private static void acquireCpuWakeLock(Context context) {
if (sCpuWakeLock != null) {
return;
}

PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, TAG);
sCpuWakeLock.acquire();
}

private static void releaseCpuLock() {
if (sCpuWakeLock != null) {
sCpuWakeLock.release();
sCpuWakeLock = null;
}
}

}
8 changes: 4 additions & 4 deletions app/src/main/java/com/example/yink/amadeus/AlarmReceiver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.yink.amadeus;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand All @@ -9,7 +10,7 @@
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class AlarmReceiver extends WakefulBroadcastReceiver {
public class AlarmReceiver extends BroadcastReceiver {

private final String TAG = "AlarmReceiver";

Expand All @@ -32,9 +33,8 @@ public void onReceive(Context context, Intent intent) {

Alarm.start(context, ringtones[index]);

ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
Intent service = new Intent(context, AlarmService.class);
context.startService(service);
setResultCode(Activity.RESULT_OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ protected void onHandleIntent(Intent intent) {
Intent launch = new Intent(this, LaunchActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);
AlarmReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
Expand Down

0 comments on commit 151a2bd

Please sign in to comment.