WIP: Alarm feature improvements

This commit is contained in:
RIP95 2017-03-19 16:00:15 +03:00
parent 74263c58d5
commit c1c05cd2d6
5 changed files with 50 additions and 10 deletions

View File

@ -42,7 +42,9 @@
</activity>
<activity
android:name=".AlarmActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:theme="@style/Theme.AlarmDialog"
android:excludeFromRecents="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
@ -50,7 +52,11 @@
<service
android:name=".AlarmService"
android:enabled="true" />
<receiver android:name=".AlarmReceiver" />
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -1,5 +1,6 @@
package com.example.yink.amadeus;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
@ -30,7 +31,6 @@ public class AlarmActivity extends Activity {
public static final int alarmCode = 104856;
SharedPreferences settings;
SharedPreferences.Editor editor;
Intent alarmIntent;
@Override
public void onStart() {
@ -45,7 +45,7 @@ public class AlarmActivity extends Activity {
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(this, AlarmReceiver.class);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, alarmCode, alarmIntent, PendingIntent.FLAG_NO_CREATE);
if (settings.getBoolean("alarm_toggle", false)) {
@ -60,18 +60,45 @@ public class AlarmActivity extends Activity {
if (alarmToggle.isChecked()) {
editor.putBoolean("alarm_toggle", true);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d(TAG, "Current API functions have been executed");
calendar.set(Calendar.HOUR_OF_DAY, getCurrentHour(alarmTimePicker));
calendar.set(Calendar.MINUTE, getCurrentMinute(alarmTimePicker));
Toast.makeText(this, "Alarm has been set for " + getCurrentHour(alarmTimePicker) + " hour(s) " + getCurrentMinute(alarmTimePicker) + " minute(s)", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "Legacy API functions have been executed");
calendar.set(Calendar.HOUR_OF_DAY, getCurrentHourLegacy(alarmTimePicker));
calendar.set(Calendar.MINUTE, getCurrentMinuteLegacy(alarmTimePicker));
Toast.makeText(this, "Alarm has been set for " + getCurrentHourLegacy(alarmTimePicker) + " hour(s) " + getCurrentMinuteLegacy(alarmTimePicker) + " minute(s)", Toast.LENGTH_SHORT).show();
}
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getCurrentHour() + " hour(s) " + alarmTimePicker.getCurrentMinute() + " minute(s)", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Alarm On");
} else {
AlarmReceiver.stopRingtone();
editor.putBoolean("alarm_toggle", false);
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm has been removed", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Alarm Off");
}
editor.apply();
}
@SuppressWarnings("deprecation")
public int getCurrentHourLegacy(TimePicker tp) {
return tp.getCurrentHour();
}
@SuppressWarnings("deprecation")
public int getCurrentMinuteLegacy(TimePicker tp) {
return tp.getCurrentMinute();
}
@TargetApi(Build.VERSION_CODES.M)
public int getCurrentHour(TimePicker tp) {
return tp.getHour();
}
@TargetApi(Build.VERSION_CODES.M)
public int getCurrentMinute(TimePicker tp) {
return tp.getMinute();
}
}

View File

@ -7,6 +7,7 @@ import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v4.content.WakefulBroadcastReceiver;
public class AlarmReceiver extends WakefulBroadcastReceiver {

View File

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient_background" >
android:layout_height="fill_parent">
<TimePicker
android:layout_width="wrap_content"

View File

@ -15,4 +15,11 @@
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/colorPrimaryDark</item>
</style>
<style name="Theme.AlarmDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>