Alarm: small fixes, added 24-hour format preference

This commit is contained in:
Snek 2017-03-22 12:05:14 +03:00
parent 43eae7b7d4
commit 816fbb850a
8 changed files with 26 additions and 3 deletions

View File

@ -8,7 +8,7 @@ android {
minSdkVersion 9
targetSdkVersion 20
versionCode 1
versionName "0.9.5-alpha.3"
versionName "0.9.5-test.2"
}
buildTypes {
release {

View File

@ -10,6 +10,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
@ -29,6 +30,7 @@ public class AlarmActivity extends Activity {
private TimePicker alarmTimePicker;
private ToggleButton alarmToggle;
private SharedPreferences settings;
private Vibrator v;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -41,7 +43,7 @@ public class AlarmActivity extends Activity {
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, ALARM_ID, alarmIntent, PendingIntent.FLAG_NO_CREATE);
alarmTimePicker.setIs24HourView(true);
alarmTimePicker.setIs24HourView(settings.getBoolean("24-hour_format", true));
if (settings.getBoolean("alarm_toggle", false)) {
alarmToggle.setChecked(true);
@ -52,10 +54,11 @@ public class AlarmActivity extends Activity {
public void onToggleClicked(View view) {
SharedPreferences.Editor editor = settings.edit();
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);;
if (alarmToggle.isChecked()) {
editor.putBoolean("alarm_toggle", true);
Calendar calendar = Calendar.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d(TAG, "Current API functions have been executed");
setTime(calendar);
@ -63,10 +66,14 @@ public class AlarmActivity extends Activity {
Log.d(TAG, "Legacy API functions have been executed");
setTimeLegacy(calendar);
}
Log.d(TAG, "Alarm On");
} else {
AlarmReceiver.stopRingtone(this);
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
editor.putBoolean("alarm_toggle", false);
alarmManager.cancel(pendingIntent);
Log.d(TAG, "Alarm Off");
@ -78,9 +85,11 @@ public class AlarmActivity extends Activity {
private void setTimeLegacy(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
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();
}
@ -89,9 +98,11 @@ public class AlarmActivity extends Activity {
private void setTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getHour() + " hour(s) " + alarmTimePicker.getMinute() + " minute(s)", Toast.LENGTH_SHORT).show();
}

View File

@ -44,6 +44,7 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {
public static void stopRingtone(Context context) {
settings = PreferenceManager.getDefaultSharedPreferences(context);
if (isPlaying) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("alarm_toggle", false);
@ -51,6 +52,7 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {
m.release();
isPlaying = false;
}
}
public static boolean isPlaying() {

View File

@ -33,6 +33,7 @@ public class LangContext extends ContextWrapper {
} else {
setSystemLocaleLegacy(config, locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
@ -53,6 +54,7 @@ public class LangContext extends ContextWrapper {
} else {
setSystemLocaleLegacy(config, locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {

View File

@ -185,6 +185,7 @@ public class LaunchActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
if (isPressed) {
status.setText(R.string.disconnected);
} else if (!isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
@ -194,6 +195,7 @@ public class LaunchActivity extends AppCompatActivity {
} else {
status.setText(R.string.call);
}
isPressed = false;
connect.setImageResource(R.drawable.connect_unselect);
cancel.setImageResource(R.drawable.cancel_unselect);

View File

@ -24,6 +24,7 @@
<string name="pref_alarm_ringtone">Выберите рингтон для будильника</string>
<string name="pref_alarm_ringtone_desc">Рингтон будильника</string>
<string name="pref_alarm_vibrate">Вибрация будильника</string>
<string name="pref_alarm_24hour_format">24-часовой формат</string>
<!-- Многие фразы были вырваны из контекста, поэтому прошу не пинать -->
<string name="line_hello">Привет.</string>

View File

@ -25,6 +25,7 @@
<string name="pref_alarm_ringtone">Alarm ringtone</string>
<string name="pref_alarm_ringtone_desc">Select ringtone for alarm</string>
<string name="pref_alarm_vibrate">Vibrate on alarm</string>
<string name="pref_alarm_24hour_format">24-hour format</string>
<!-- Subtitles -->
<string name="line_hello">Hello.</string>

View File

@ -39,6 +39,10 @@
android:key="ringtone"
android:summary="@string/pref_alarm_ringtone_desc"
android:title="@string/pref_alarm_ringtone" />
<SwitchPreference
android:defaultValue="true"
android:key="24-hour_format"
android:title="@string/pref_alarm_24hour_format" />
<SwitchPreference
android:defaultValue="false"
android:key="vibrate"