ساعت سخنگو برای فارسی زبانان
| import android.os.Build; |
| 9 |
| 10 | import java.util.Calendar; |
| 11 |
| 12 | /** Schedules up to five independent daily reminders. */ |
| 13 | public final class ReminderManager { |
| 14 | private static final int REMINDER_COUNT = 5; |
| 15 | private static final int REQUEST_BASE = 8301; |
| 16 | private static final String ACTION_PREFIX = "ir.hamed.klox.REMINDER_"; |
| 17 |
| 18 | private ReminderManager() {} |
| 19 |
| 20 | public static void schedule(Context ctx) { |
| 21 | Context c = ctx.getApplicationContext(); |
| 22 | SharedPreferences p = c.getSharedPreferences("settings", Context.MODE_PRIVATE); |
| 23 | AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE); |
| 24 | if (am == null) return; |
| 25 |
| 26 | for (int slot = 1; slot <= REMINDER_COUNT; slot++) { |
| 27 | scheduleOne(c, p, am, slot); |
| 28 | } |
| 29 | } |
| 30 |
| 31 | private static void scheduleOne(Context c, SharedPreferences p, AlarmManager am, int slot) { |
| 32 | cancelOne(c, am, slot); |
| 33 |
| 34 | String prefix = key(slot); |
| 35 | if (!p.getBoolean(prefix + "Enabled", false)) return; |
| 36 | String text = p.getString(prefix + "Text", "").trim(); |
| 37 | if (text.isEmpty()) return; |
| 38 |
| 39 | int min = Math.max(0, Math.min(1439, p.getInt(prefix + "Minute", 9 * 60))); |
| 40 | Calendar now = Calendar.getInstance(); |
| 41 | Calendar target = (Calendar) now.clone(); |
| 42 | target.set(Calendar.HOUR_OF_DAY, min / 60); |
| 43 | target.set(Calendar.MINUTE, min % 60); |
| 44 | target.set(Calendar.SECOND, 0); |
| 45 | target.set(Calendar.MILLISECOND, 0); |
| 46 | int daysMask = p.getInt(prefix + "DaysMask", 127); |
| 47 | if (daysMask == 0) daysMask = 127; |
| 48 | // Find the next selected weekday at the configured time. Calendar uses Sunday=1..Saturday=7. |
| 49 | boolean found = false; |
| 50 | for (int d = 0; d < 8; d++) { |
| 51 | int dow = target.get(Calendar.DAY_OF_WEEK); |
| 52 | if (target.after(now) && (daysMask & (1 << (dow - 1))) != 0) { |
| 53 | found = true; |
| 54 | break; |
| 55 | } |
| 56 | target.add(Calendar.DAY_OF_YEAR, 1); |
| 57 | } |
| 58 | if (!found) return; |
| 59 |
| 60 | Intent intent = new Intent(c, ReminderReceiver.class) |
| 61 | .setAction(ACTION_PREFIX + slot) |
| 62 | .putExtra(ReminderReceiver.EXTRA_SLOT, slot); |
| 63 | PendingIntent pi = PendingIntent.getBroadcast( |
| 64 | c, REQUEST_BASE + slot, intent, |
| 65 | PendingIntent.FLAG_UPDATE_CURRENT | pendingIntentFlags()); |
| 66 |
| 67 | if (Build.VERSION.SDK_INT >= 31 && am.canScheduleExactAlarms()) { |
| 68 | am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pi); |
| 69 | } else if (Build.VERSION.SDK_INT >= 23) { |
| 70 | am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pi); |
| 71 | } else { |
| 72 | am.set(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pi); |
| 73 | } |
| 74 | } |
| 75 |
| 76 | public static void cancel(Context ctx) { |
| 77 | Context c = ctx.getApplicationContext(); |
| 78 | AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE); |
| 79 | if (am == null) return; |
| 80 | for (int slot = 1; slot <= REMINDER_COUNT; slot++) cancelOne(c, am, slot); |
| 81 | } |
| 82 |
| 83 | private static void cancelOne(Context c, AlarmManager am, int slot) { |
| 84 | Intent intent = new Intent(c, ReminderReceiver.class) |
| 85 | .setAction(ACTION_PREFIX + slot) |
| 86 | .putExtra(ReminderReceiver.EXTRA_SLOT, slot); |
| 87 | PendingIntent pi = PendingIntent.getBroadcast( |
| 88 | c, REQUEST_BASE + slot, intent, |
| 89 | PendingIntent.FLAG_UPDATE_CURRENT | pendingIntentFlags()); |
| 90 | am.cancel(pi); |
| 91 | } |
| 92 |
| 93 | public static void fireNow(Context ctx, int slot) { |
| 94 | if (slot < 1 || slot > REMINDER_COUNT) slot = 1; |
| 95 | ReminderService.start(ctx, slot); |
| 96 | } |
| 97 |
| 98 | /** Backward-compatible test call. */ |
| 99 | public static void fireNow(Context ctx) { |
| 100 | fireNow(ctx, 1); |
| 101 | } |
| 102 |
| 103 | private static String key(int slot) { |
| 104 | return "reminder" + slot; |
| 105 | } |
| 106 |
| 107 | private static int pendingIntentFlags() { |
| 108 | return Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0; |
| 109 | } |
| 110 |
| 111 | public static void migrateLegacyReminder(Context ctx) { |
| 112 | SharedPreferences p = ctx.getSharedPreferences("settings", Context.MODE_PRIVATE); |
| 113 | if (p.getBoolean("reminderMigrationDone", false)) return; |
| 114 | boolean legacyEnabled = p.getBoolean("reminderEnabled", false); |
| 115 | String legacyText = p.getString("reminderText", "").trim(); |
| 116 | if (legacyEnabled || !legacyText.isEmpty()) { |
| 117 | SharedPreferences.Editor e = p.edit() |
| 118 | .putBoolean("reminder1Enabled", legacyEnabled) |
| 119 | .putString("reminder1Text", legacyText) |
| 120 | .putInt("reminder1Minute", p.getInt("reminderMinute", 9 * 60)) |
| 121 | .putInt("reminder1DaysMask", p.getInt("reminderDaysMask", 127)); |
| 122 | String audio = p.getString("reminderAudioUri", ""); |
| 123 | if (!audio.isEmpty()) e.putString("reminder1AudioUri", audio); |
| 124 | e.apply(); |
| 125 | } |
| 126 | p.edit().putBoolean("reminderMigrationDone", true).apply(); |
| 127 | } |
| 128 | } |