improve reminder scheduling on different android versions

This commit is contained in:
Phillip Thelen 2016-10-28 19:53:08 +02:00
parent b6706b3b3e
commit 6d0efd5717
4 changed files with 23 additions and 4 deletions

View file

@ -18,4 +18,5 @@
<string name="qr_album_name" translatable="false">habitica</string>
<string name="qr_file_name" translatable="false"> habitrpg-qr-code.jpg</string>
<string name="habitica_user_count">1,750,000</string>
</resources>

View file

@ -203,7 +203,7 @@
<string name="complete_tutorial">Got it!</string>
<string name="dismiss_tutorial">Remind me again</string>
<string name="intro_1_title">Welcome to Habitica</string>
<string name="intro_1_description">Join over 1,100,000 people having fun while getting things done. Create an avatar and track your real-life tasks.</string>
<string name="intro_1_description" formatted="false">Join over %s people having fun while getting things done. Create an avatar and track your real-life tasks.</string>
<string name="intro_2_title">Game Progress = Life Progress</string>
<string name="intro_2_description">Unlock features in the game by checking off your real-life tasks. Earn armor, pets, and more to reward you for meeting your goals!</string>
<string name="intro_3_title">Get Social and Fight Monsters</string>

View file

@ -19,6 +19,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
@ -26,6 +27,8 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static android.os.Build.VERSION.SDK_INT;
/**
* Created by keithholliday on 5/29/16.
*/
@ -155,7 +158,7 @@ public class TaskAlarmManager {
PendingIntent sender = PendingIntent.getBroadcast(context, intentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
setAlarm(context, cal.getTimeInMillis(), sender);
remindersItem.save();
}
@ -183,6 +186,10 @@ public class TaskAlarmManager {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
if (cal.getTimeInMillis() < new Date().getTime()) {
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)+1);
}
long trigger_time = cal.getTimeInMillis();
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
@ -198,7 +205,7 @@ public class TaskAlarmManager {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger_time, pendingIntent);
setAlarm(context, trigger_time, pendingIntent);
}
}
@ -208,4 +215,15 @@ public class TaskAlarmManager {
PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
alarmManager.cancel(displayIntent);
}
private static void setAlarm(Context context, long time, PendingIntent pendingIntent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (SDK_INT < Build.VERSION_CODES.KITKAT)
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M)
alarmManager.setWindow(AlarmManager.RTC_WAKEUP, time, time+60000, pendingIntent);
else if (SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}

View file

@ -73,7 +73,7 @@ public class IntroActivity extends BaseActivity implements View.OnClickListener,
case 0: {
fragment.setImage(ResourcesCompat.getDrawable(getResources(), R.drawable.intro_1, null));
fragment.setTitle(getString(R.string.intro_1_title));
fragment.setDescription(getString(R.string.intro_1_description));
fragment.setDescription(getString(R.string.intro_1_description, getString(R.string.habitica_user_count)));
break;
}
case 1: {