forked from Yink/Amadeus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Yink#44 from RIP95/master
When things get complicated... REFACTOR!
- Loading branch information
Showing
14 changed files
with
275 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.example.yink.amadeus; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
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; | ||
|
||
class Alarm { | ||
|
||
private static MediaPlayer m; | ||
private static SharedPreferences settings; | ||
private static Vibrator v; | ||
|
||
static final int ALARM_ID = 104859; | ||
static final int ALARM_NOTIFICATION_ID = 102434; | ||
|
||
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)) { | ||
v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); | ||
long[] pattern = {500, 2000}; | ||
v.vibrate(pattern, 0); | ||
} | ||
|
||
m = MediaPlayer.create(context, ringtone); | ||
|
||
m.setLooping(true); | ||
m.start(); | ||
|
||
if (m.isPlaying()) { | ||
isPlaying = true; | ||
} | ||
|
||
Log.d(TAG, "Start"); | ||
|
||
} | ||
|
||
static void cancel(Context context) { | ||
|
||
settings = PreferenceManager.getDefaultSharedPreferences(context); | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
Intent alarmIntent = new Intent(context, AlarmReceiver.class); | ||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
|
||
if (isPlaying) { | ||
SharedPreferences.Editor editor = settings.edit(); | ||
editor.putBoolean("alarm_toggle", false); | ||
editor.apply(); | ||
m.release(); | ||
notificationManager.cancel(ALARM_NOTIFICATION_ID); | ||
alarmManager.cancel(pendingIntent); | ||
releaseCpuLock(); | ||
isPlaying = false; | ||
if (v != null) { | ||
v.cancel(); | ||
} | ||
} | ||
|
||
Log.d(TAG, "Cancel"); | ||
|
||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/yink/amadeus/AlarmBootReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.example.yink.amadeus; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.os.Build; | ||
import android.preference.PreferenceManager; | ||
import android.support.v4.content.WakefulBroadcastReceiver; | ||
import android.util.Log; | ||
|
||
public class AlarmBootReceiver extends WakefulBroadcastReceiver { | ||
|
||
final String TAG = "AlarmBootReceiver"; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
|
||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); | ||
|
||
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()) | ||
&& settings.getBoolean("alarm_toggle", false)) { | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService( | ||
Context.ALARM_SERVICE); | ||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, | ||
Alarm.ALARM_ID, new Intent(context, AlarmReceiver.class), 0); | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent); | ||
} else { | ||
alarmManager.set(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent); | ||
} | ||
Log.d(TAG, "Alarm has been recovered"); | ||
} | ||
|
||
} | ||
} |
43 changes: 12 additions & 31 deletions
43
app/src/main/java/com/example/yink/amadeus/AlarmReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,41 @@ | ||
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; | ||
import android.content.SharedPreferences; | ||
import android.media.MediaPlayer; | ||
import android.preference.PreferenceManager; | ||
import android.support.v4.content.WakefulBroadcastReceiver; | ||
import android.util.Log; | ||
|
||
public class AlarmReceiver extends WakefulBroadcastReceiver { | ||
public class AlarmReceiver extends BroadcastReceiver { | ||
|
||
static MediaPlayer m; | ||
static boolean isPlaying = false; | ||
static SharedPreferences settings; | ||
static SharedPreferences.Editor editor; | ||
private final String TAG = "AlarmReceiver"; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
|
||
Log.d(TAG, "Broadcast received!"); | ||
|
||
int[] ringtones = { | ||
R.raw.ringtone_gate_of_steiner, R.raw.ringtone_village, | ||
R.raw.ringtone_beginning_of_fight, R.raw.ringtone_easygoingness, | ||
R.raw.ringtone_reunion, R.raw.ringtone_precaution, | ||
R.raw.ringtone_over_the_sky | ||
}; | ||
|
||
settings = PreferenceManager.getDefaultSharedPreferences(context); | ||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); | ||
int index = Integer.parseInt(settings.getString("ringtone", "0")); | ||
|
||
m = MediaPlayer.create(context, ringtones[index]); | ||
|
||
m.setLooping(true); | ||
m.start(); | ||
Log.d(TAG, "Starting alarm..."); | ||
|
||
if (m.isPlaying()) { | ||
isPlaying = true; | ||
} | ||
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); | ||
} | ||
|
||
public static void stopRingtone(Context context) { | ||
settings = PreferenceManager.getDefaultSharedPreferences(context); | ||
if (isPlaying) { | ||
editor = settings.edit(); | ||
editor.putBoolean("alarm_toggle", false); | ||
editor.apply(); | ||
m.release(); | ||
isPlaying = false; | ||
} | ||
} | ||
|
||
public static boolean isPlaying() { | ||
return isPlaying; | ||
} | ||
} |
Oops, something went wrong.