Compare commits
2 Commits
master
...
dailyremin
Author | SHA1 | Date |
---|---|---|
|
bd2fc73d7b | |
|
807ee43e97 |
|
@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
|
|||
|
||||
android {
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion '30.0.2'
|
||||
buildToolsVersion '30.0.3'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
|
@ -19,9 +19,9 @@ android {
|
|||
multiDexEnabled true
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
checkReleaseBuilds false
|
||||
lint {
|
||||
abortOnError false
|
||||
checkReleaseBuilds false
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
|
|
|
@ -127,7 +127,8 @@
|
|||
</provider>
|
||||
<receiver
|
||||
android:name="sh.ftp.rocketninelabs.meditationassistant.DailyReminderReceiver"
|
||||
android:exported="true">
|
||||
android:exported="true"
|
||||
android:directBootAware="true">
|
||||
<intent-filter>
|
||||
<action android:name="sh.ftp.rocketninelabs.meditationassistant.DAILY_NOTIFICATION"></action>
|
||||
<action android:name="sh.ftp.rocketninelabs.meditationassistant.DAILY_NOTIFICATION_UPDATED"></action>
|
||||
|
|
|
@ -22,9 +22,6 @@ public class DailyReminderReceiver extends BroadcastReceiver {
|
|||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ma = (MeditationAssistant) context.getApplicationContext();
|
||||
} catch (Exception e) {
|
||||
|
@ -33,24 +30,26 @@ public class DailyReminderReceiver extends BroadcastReceiver {
|
|||
}
|
||||
|
||||
if (!getMeditationAssistant().getPrefs().getBoolean("pref_daily_reminder", false)) {
|
||||
cancelReminder(context);
|
||||
getMeditationAssistant().cancelDailyReminder(context);
|
||||
return; // The user has not enabled the daily reminder
|
||||
}
|
||||
Log.d("MeditationAssistant", "onReceive in DailyReminderReceiver");
|
||||
|
||||
if (intent != null && intent.getAction() != null && intent.getAction().equals(MeditationAssistant.ACTION_REMINDER)) { // otherwise, it was just an update
|
||||
Log.d("MeditationAssistant", "Daily notification intent!");
|
||||
Log.d("MeditationAssistant", "Received daily reminder notification intent");
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("d-M-yyyy", Locale.US);
|
||||
if (getMeditationAssistant().getTimeToStopMeditate() != 0) {
|
||||
Log.d("MeditationAssistant", "Skipping daily notification today, session in progress...");
|
||||
Log.d("MeditationAssistant", "Skipping daily reminder notification today, session in progress...");
|
||||
} else if (getMeditationAssistant().db.numSessionsByDate(Calendar.getInstance()) > 0) {
|
||||
Log.d("MeditationAssistant", "Skipping daily notification today, there has already been a session recorded...");
|
||||
Log.d("MeditationAssistant", "Skipping daily reminder notification today, there has already been a session recorded...");
|
||||
} else {
|
||||
long last_reminder = getMeditationAssistant().getPrefs().getLong("last_reminder", 0);
|
||||
if (last_reminder == 0 || getMeditationAssistant().getTimestamp() - last_reminder > 120) {
|
||||
getMeditationAssistant().getPrefs().edit().putLong("last_reminder", getMeditationAssistant().getTimestamp()).apply();
|
||||
|
||||
Log.d("MeditationAssistant", "Showing daily reminder notification");
|
||||
|
||||
String reminderText = getMeditationAssistant().getPrefs().getString("pref_daily_reminder_text", "").trim();
|
||||
if (reminderText.equals("")) {
|
||||
reminderText = context.getString(R.string.reminderText);
|
||||
|
@ -64,6 +63,10 @@ public class DailyReminderReceiver extends BroadcastReceiver {
|
|||
.setTicker(reminderText)
|
||||
.setAutoCancel(true);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
notificationBuilder.setChannelId("reminder");
|
||||
}
|
||||
|
||||
if (getMeditationAssistant().getPrefs().getBoolean("pref_vibrate_reminder", true)) {
|
||||
long[] vibrationPattern = {0, 200, 500, 200, 500};
|
||||
notificationBuilder.setVibrate(vibrationPattern);
|
||||
|
@ -80,76 +83,20 @@ public class DailyReminderReceiver extends BroadcastReceiver {
|
|||
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
||||
stackBuilder.addParentStack(MainActivity.class);
|
||||
stackBuilder.addNextIntent(notificationIntent);
|
||||
PendingIntent resultPendingIntent =
|
||||
stackBuilder.getPendingIntent(
|
||||
0,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
);
|
||||
|
||||
//Intent launchMain = new Intent(context, MainActivity.class);
|
||||
//PendingIntent launchNotification = PendingIntent.getActivity(context, 1008, launchMain, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
notificationBuilder.setContentIntent(resultPendingIntent);
|
||||
|
||||
Notification notification = notificationBuilder.build();
|
||||
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(1946, notification);
|
||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(MeditationAssistant.dailyReminderNotificationID, notification);
|
||||
} else {
|
||||
Log.d("MeditationAssistant", "Skipping daily reminder notification today, a daily notification was recently shown...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String reminderTime = ma.getPrefs().getString("pref_daily_reminder_time", "19:00");
|
||||
String[] reminderTimeSplit = ((reminderTime != null && reminderTime != "") ? reminderTime : "19:00").split(":");
|
||||
Integer reminderHour = Integer.valueOf(reminderTimeSplit[0]);
|
||||
Integer reminderMinute = Integer.valueOf(reminderTimeSplit[1]);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, reminderHour);
|
||||
calendar.set(Calendar.MINUTE, reminderMinute);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
|
||||
if (Calendar.getInstance().getTimeInMillis() > calendar.getTimeInMillis()) {
|
||||
calendar.add(Calendar.DATE, 1); // Tomorrow
|
||||
}
|
||||
|
||||
cancelReminder(context);
|
||||
|
||||
getMeditationAssistant().reminderPendingIntent = PendingIntent
|
||||
.getBroadcast(
|
||||
context,
|
||||
1946,
|
||||
new Intent(
|
||||
MeditationAssistant.ACTION_REMINDER),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT
|
||||
);
|
||||
|
||||
/* Don't use setAlarmClock here as it will always place an alarm icon in the status bar */
|
||||
getMeditationAssistant().setAlarm(false, calendar.getTimeInMillis(), getMeditationAssistant().reminderPendingIntent);
|
||||
Log.d("MeditationAssistant", "Set daily reminder alarm for " + calendar.toString());
|
||||
}
|
||||
|
||||
private void cancelReminder(Context context) {
|
||||
if (getMeditationAssistant().reminderPendingIntent != null) {
|
||||
try {
|
||||
getMeditationAssistant().getAlarmManager().cancel(getMeditationAssistant().reminderPendingIntent);
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "AlarmManager update was not canceled. " + e.toString());
|
||||
}
|
||||
try {
|
||||
PendingIntent.getBroadcast(context, 0, new Intent(
|
||||
MeditationAssistant.ACTION_REMINDER),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT
|
||||
).cancel();
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "PendingIntent broadcast was not canceled. " + e.toString());
|
||||
}
|
||||
try {
|
||||
getMeditationAssistant().reminderPendingIntent.cancel();
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "PendingIntent was not canceled. " + e.toString());
|
||||
}
|
||||
}
|
||||
getMeditationAssistant().setDailyReminder(context);
|
||||
}
|
||||
|
||||
public MeditationAssistant getMeditationAssistant() {
|
||||
|
|
|
@ -14,6 +14,7 @@ import android.content.ComponentName;
|
|||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
|
@ -100,6 +101,7 @@ public class MeditationAssistant extends Application {
|
|||
|
||||
public static int CSV_COLUMN_COUNT = 5;
|
||||
|
||||
public static int dailyReminderNotificationID = 1946; // Terence McKenna's year of birth
|
||||
public static int sessionNotificationID = 1990;
|
||||
public static int bellNotificationID = 1991;
|
||||
|
||||
|
@ -169,6 +171,7 @@ public class MeditationAssistant extends Application {
|
|||
private Button sessionDialogCompletedTimeButton = null;
|
||||
private Button sessionDialogLengthButton = null;
|
||||
private EditText sessionDialogMessage = null;
|
||||
private DailyReminderReceiver dailyReminderReceiver = null;
|
||||
private DatePickerDialog.OnDateSetListener sessionDialogDateSetListener =
|
||||
new DatePickerDialog.OnDateSetListener() {
|
||||
@Override
|
||||
|
@ -1201,6 +1204,14 @@ public class MeditationAssistant extends Application {
|
|||
+ Build.VERSION.SDK_INT
|
||||
);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
dailyReminderReceiver = new DailyReminderReceiver();
|
||||
IntentFilter reminderFilter = new IntentFilter();
|
||||
reminderFilter.addAction(ACTION_REMINDER);
|
||||
reminderFilter.addAction(ACTION_UPDATED);
|
||||
registerReceiver(dailyReminderReceiver, reminderFilter);
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
PackageManager pm = getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(this, FilePickerActivity.class),
|
||||
|
@ -1251,15 +1262,52 @@ public class MeditationAssistant extends Application {
|
|||
|
||||
db = DatabaseHandler.getInstance(getApplicationContext());
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
/* Send the daily notification updated intent just in case the receiver hasn't been called yet */
|
||||
Log.d("MeditationAssistant", "Sending initial daily notification updated intent");
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(MeditationAssistant.ACTION_UPDATED);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
setDailyReminder(getApplicationContext());
|
||||
}
|
||||
|
||||
public void setDailyReminder(Context context) {
|
||||
String reminderTime = getPrefs().getString("pref_daily_reminder_time", "19:00");
|
||||
String[] reminderTimeSplit = ((reminderTime != null && reminderTime != "") ? reminderTime : "19:00").split(":");
|
||||
Integer reminderHour = Integer.valueOf(reminderTimeSplit[0]);
|
||||
Integer reminderMinute = Integer.valueOf(reminderTimeSplit[1]);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, reminderHour);
|
||||
calendar.set(Calendar.MINUTE, reminderMinute);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
|
||||
if (Calendar.getInstance().getTimeInMillis() > calendar.getTimeInMillis()) {
|
||||
calendar.add(Calendar.DATE, 1); // Tomorrow
|
||||
}
|
||||
|
||||
cancelDailyReminder(context);
|
||||
|
||||
reminderPendingIntent = PendingIntent.getBroadcast(context, dailyReminderNotificationID, new Intent(MeditationAssistant.ACTION_REMINDER), PendingIntent.FLAG_CANCEL_CURRENT);
|
||||
|
||||
/* Don't use setAlarmClock here as it will always place an alarm icon in the status bar */
|
||||
setAlarm(true, calendar.getTimeInMillis(), reminderPendingIntent);
|
||||
Log.d("MeditationAssistant", "Set daily reminder alarm for " + calendar.toString());
|
||||
}
|
||||
|
||||
public void cancelDailyReminder(Context context) {
|
||||
if (reminderPendingIntent != null) {
|
||||
try {
|
||||
getAlarmManager().cancel(reminderPendingIntent);
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "AlarmManager update was not canceled. " + e.toString());
|
||||
}
|
||||
try {
|
||||
PendingIntent.getBroadcast(context, 0, new Intent(MeditationAssistant.ACTION_REMINDER), PendingIntent.FLAG_CANCEL_CURRENT).cancel();
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "PendingIntent broadcast was not canceled. " + e.toString());
|
||||
}
|
||||
try {
|
||||
reminderPendingIntent.cancel();
|
||||
} catch (Exception e) {
|
||||
Log.e("MeditationAssistant", "PendingIntent was not canceled. " + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
public String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
|
@ -1451,9 +1499,14 @@ public class MeditationAssistant extends Application {
|
|||
bellChannel.enableLights(false);
|
||||
bellChannel.enableVibration(false);
|
||||
|
||||
NotificationChannel reminderChannel = new NotificationChannel("reminder", getString(R.string.pref_daily_reminder), NotificationManager.IMPORTANCE_DEFAULT);
|
||||
reminderChannel.enableLights(true);
|
||||
reminderChannel.enableVibration(true);
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
notificationManager.createNotificationChannel(sessionChannel);
|
||||
notificationManager.createNotificationChannel(bellChannel);
|
||||
notificationManager.createNotificationChannel(reminderChannel);
|
||||
}
|
||||
|
||||
public void showMindfulnessBellNotification() {
|
||||
|
|
|
@ -687,11 +687,9 @@ public class SettingsActivity extends PreferenceActivity {
|
|||
reminderPreferenceFragment = (ReminderPreferenceFragment) preferenceFragment;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder_text") : preferenceFragment.findPreference("pref_daily_reminder_text"));
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder_time") : preferenceFragment.findPreference("pref_daily_reminder_time"));
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder") : preferenceFragment.findPreference("pref_daily_reminder"));
|
||||
}
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder_text") : preferenceFragment.findPreference("pref_daily_reminder_text"));
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder_time") : preferenceFragment.findPreference("pref_daily_reminder_time"));
|
||||
bindPreferenceSummaryToValue(preferenceFragment == null ? findPreference("pref_daily_reminder") : preferenceFragment.findPreference("pref_daily_reminder"));
|
||||
}
|
||||
if (pref_type.equals("all") || pref_type.equals("meditation")) {
|
||||
if (preferenceFragment != null) {
|
||||
|
@ -827,12 +825,10 @@ public class SettingsActivity extends PreferenceActivity {
|
|||
addPreferencesFromResource(R.xml.pref_session);
|
||||
|
||||
// Add 'Daily Reminder' preferences
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
fakeHeader = new PreferenceCategory(this);
|
||||
fakeHeader.setTitle(R.string.pref_daily_reminder);
|
||||
getPreferenceScreen().addPreference(fakeHeader);
|
||||
addPreferencesFromResource(R.xml.pref_reminder);
|
||||
}
|
||||
fakeHeader = new PreferenceCategory(this);
|
||||
fakeHeader.setTitle(R.string.pref_daily_reminder);
|
||||
getPreferenceScreen().addPreference(fakeHeader);
|
||||
addPreferencesFromResource(R.xml.pref_reminder);
|
||||
|
||||
// Add 'Meditation' preferences
|
||||
fakeHeader = new PreferenceCategory(this);
|
||||
|
@ -874,9 +870,7 @@ public class SettingsActivity extends PreferenceActivity {
|
|||
public void onBuildHeaders(List<Header> target) {
|
||||
if (!isSimplePreferences(this)) {
|
||||
loadHeadersFromResource(R.xml.pref_headers, target);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
loadHeadersFromResource(R.xml.pref_headers_pre26, target);
|
||||
}
|
||||
loadHeadersFromResource(R.xml.pref_headers_reminder, target);
|
||||
loadHeadersFromResource(R.xml.pref_headers_footer, target);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:7.0.2'
|
||||
classpath 'com.android.tools.build:gradle:7.2.0'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||
|
|
Loading…
Reference in New Issue