ساعت سخنگو برای فارسی زبانان
| import android.os.Build; |
| 9 |
| 10 | import java.util.ArrayList; |
| 11 | import java.util.Calendar; |
| 12 | import java.util.List; |
| 13 | import java.util.Locale; |
| 14 |
| 15 | /** |
| 16 | * Audio clock built around the three original feminine clip families: |
| 17 | * |
| 18 | * bN.mp3 = exact hour (top of the hour) |
| 19 | * bN_.mp3 = hour phrase used when minutes follow |
| 20 | * bNm.mp3 = minute, rounded to five-minute steps |
| 21 | * |
| 22 | * The announcement time is rounded to the nearest five minutes. If rounding |
| 23 | * crosses :55, the hour rolls forward and the exact-hour clip b(N+1) is used. |
| 24 | * Playback is strictly serialized: Ding -> hour -> minute. |
| 25 | */ |
| 26 | public final class AudioTimeSpeaker { |
| 27 | private final Context context; |
| 28 | private final AudioManager audioManager; |
| 29 | private MediaPlayer player; |
| 30 | private AudioManager.OnAudioFocusChangeListener focusListener; |
| 31 | private AudioFocusRequest focusRequest; |
| 32 | private int originalVolume = -1; |
| 33 |
| 34 | public AudioTimeSpeaker(Context c) { |
| 35 | context = c.getApplicationContext(); |
| 36 | audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 37 | } |
| 38 |
| 39 | public void speakCurrentTime() { |
| 40 | Calendar now = Calendar.getInstance(Locale.US); |
| 41 | speak(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE)); |
| 42 | } |
| 43 |
| 44 | /** Scheduled clock announcement: when the user enables ding-only mode, |
| 45 | * play only the selected ding and do not announce the time. */ |
| 46 | public void speakScheduledTime() { |
| 47 | android.content.SharedPreferences p = context.getSharedPreferences("settings", Context.MODE_PRIVATE); |
| 48 | if (p.getBoolean("dingOnlyMode", false)) { |
| 49 | // Scheduled announcements have a hard gate here: no hour/minute |
| 50 | // clips are ever added when Ding-only mode is enabled. |
| 51 | playSelectedDing(); |
| 52 | return; |
| 53 | } |
| 54 | Calendar now = Calendar.getInstance(Locale.US); |
| 55 | speak(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE)); |
| 56 | } |
| 57 |
| 58 | /** Explicit entry point used by the scheduled foreground service. */ |
| 59 | public void playDingOnly() { |
| 60 | playSelectedDing(); |
| 61 | } |
| 62 |
| 63 | public void speak(int hour, int minute) { |
| 64 | stop(); |
| 65 | List<Integer> sequence = buildSequence(hour, minute); |
| 66 | if (sequence.isEmpty()) return; |
| 67 | requestFocus(); |
| 68 | boostVolume(); |
| 69 | playAt(sequence, 0); |
| 70 | } |
| 71 |
| 72 | private List<Integer> buildSequence(int hour, int minute) { |
| 73 | List<Integer> ids = new ArrayList<>(); |
| 74 | android.content.SharedPreferences p = context.getSharedPreferences("settings", Context.MODE_PRIVATE); |
| 75 |
| 76 | addSelectedDing(ids, p); |
| 77 |
| 78 | // Round the COMPLETE time to the nearest five minutes. |
| 79 | // 01/02 -> :00, 03/04 -> :05, ... 58/59 -> next hour :00. |
| 80 | int h = Math.floorMod(hour, 24); |
| 81 | int m = Math.max(0, Math.min(59, minute)); |
| 82 | int rounded = ((m + 2) / 5) * 5; |
| 83 | if (rounded >= 60) { |
| 84 | rounded = 0; |
| 85 | h = (h + 1) % 24; |
| 86 | } |
| 87 |
| 88 | boolean numericOnly = p.getBoolean("numericOnly", false); |
| 89 |
| 90 | if (rounded == 0) { |
| 91 | // At a rounded exact hour, the original bN clip is the correct |
| 92 | // single hour announcement. |
| 93 | int exactHour = raw("b" + h); |
| 94 | if (exactHour == 0) exactHour = raw("b" + h + "_"); |
| 95 | addIfExists(ids, exactHour); |
| 96 | return ids; |
| 97 | } |
| 98 |
| 99 | // When minutes follow, bN_ is the dedicated hour-with-minute clip. |
| 100 | // Never use bN here: it is the exact-hour clip and causes duplicated |
| 101 | // constructions such as "ساعت ۹ ساعت ۲۳". |
| 102 | int hourWithMinute = raw("b" + h + "_"); |
| 103 | if (hourWithMinute == 0) hourWithMinute = raw("b" + h); |
| 104 | addIfExists(ids, hourWithMinute); |
| 105 |
| 106 | // The source audio set provides feminine minute clips only in five- |
| 107 | // minute steps. Use the rounded five-minute clip directly. |
| 108 | int minuteClip = raw("b" + rounded + "m"); |
| 109 | if (minuteClip == 0) { |
| 110 | // Defensive fallback for a damaged/incomplete resource set. |
| 111 | minuteClip = raw("b" + rounded + "_"); |
| 112 | } |
| 113 | addIfExists(ids, minuteClip); |
| 114 |
| 115 | // numericOnly is retained for backwards compatibility with the UI. |
| 116 | // With the original supplied audio set there is no separate bare-hour |
| 117 | // recording; therefore we keep the same audio-safe clip mapping rather |
| 118 | // than silently dropping the hour. |
| 119 | return ids; |
| 120 | } |
| 121 |
| 122 | public void testSelectedDing() { |
| 123 | playSelectedDing(); |
| 124 | } |
| 125 |
| 126 | private void playSelectedDing() { |
| 127 | stop(); |
| 128 | android.content.SharedPreferences p = context.getSharedPreferences("settings", Context.MODE_PRIVATE); |
| 129 | List<Integer> one = new ArrayList<>(); |
| 130 | addSelectedDing(one, p); |
| 131 | if (one.isEmpty()) return; |
| 132 | requestFocus(); |
| 133 | boostVolume(); |
| 134 | playAt(one, 0); |
| 135 | } |
| 136 |
| 137 | private void addSelectedDing(List<Integer> ids, android.content.SharedPreferences p) { |
| 138 | String custom = p.getString("customDingUri", "").trim(); |
| 139 | if (!custom.isEmpty()) { |
| 140 | // -1 is a private marker for a persisted content:// audio URI. |
| 141 | ids.add(-1); |
| 142 | return; |
| 143 | } |
| 144 | int dingNumber = p.getInt("dingNumber", 1); |
| 145 | if (dingNumber < 1 || dingNumber > 5) dingNumber = 1; |
| 146 | addIfExists(ids, raw("ding" + dingNumber)); |
| 147 | } |
| 148 |
| 149 | private void addIfExists(List<Integer> ids, int id) { |
| 150 | if (id != 0) ids.add(id); |
| 151 | } |
| 152 |
| 153 | private int raw(String name) { |
| 154 | return context.getResources().getIdentifier(name, "raw", context.getPackageName()); |
| 155 | } |
| 156 |
| 157 | private void playAt(final List<Integer> sequence, final int index) { |
| 158 | if (index >= sequence.size()) { |
| 159 | restoreVolume(); |
| 160 | releaseFocus(); |
| 161 | return; |
| 162 | } |
| 163 | releasePlayerOnly(); |
| 164 | try { |
| 165 | final MediaPlayer next; |
| 166 | if (sequence.get(index) == -1) { |
| 167 | String custom = context.getSharedPreferences("settings", Context.MODE_PRIVATE).getString("customDingUri", "").trim(); |
| 168 | next = custom.isEmpty() ? null : MediaPlayer.create(context, android.net.Uri.parse(custom)); |
| 169 | if (next == null) { |
| 170 | // The selected file may have been deleted or its permission revoked. |
| 171 | // Fall back to the currently selected built-in Ding instead of failing silently. |
| 172 | List<Integer> fallback = new ArrayList<>(); |
| 173 | int dingNumber = context.getSharedPreferences("settings", Context.MODE_PRIVATE).getInt("dingNumber", 1); |
| 174 | if (dingNumber < 1 || dingNumber > 5) dingNumber = 1; |
| 175 | addIfExists(fallback, raw("ding" + dingNumber)); |
| 176 | if (!fallback.isEmpty()) { playAt(fallback, 0); return; } |
| 177 | } |
| 178 | } else { |
| 179 | next = MediaPlayer.create(context, sequence.get(index)); |
| 180 | } |
| 181 | player = next; |
| 182 | if (next == null) { |
| 183 | playAt(sequence, index + 1); |
| 184 | return; |
| 185 | } |
| 186 | if (Build.VERSION.SDK_INT >= 21) { |
| 187 | next.setAudioAttributes(new AudioAttributes.Builder() |
| 188 | .setUsage(AudioAttributes.USAGE_MEDIA) |
| 189 | .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) |
| 190 | .build()); |
| 191 | } |
| 192 | next.setOnCompletionListener(mp -> { |
| 193 | try { mp.release(); } catch (Exception ignored) { } |
| 194 | if (player == mp) player = null; |
| 195 | playAt(sequence, index + 1); |
| 196 | }); |
| 197 | next.setOnErrorListener((mp, what, extra) -> { |
| 198 | try { mp.release(); } catch (Exception ignored) { } |
| 199 | if (player == mp) player = null; |
| 200 | playAt(sequence, index + 1); |
| 201 | return true; |
| 202 | }); |
| 203 | if (Build.VERSION.SDK_INT >= 23) { |
| 204 | float speed = context.getSharedPreferences("settings", Context.MODE_PRIVATE) |
| 205 | .getInt("announcementSpeed", 100) / 100f; |
| 206 | try { |
| 207 | next.setPlaybackParams(new android.media.PlaybackParams() |
| 208 | .setSpeed(speed) |
| 209 | .setPitch(1.0f)); |
| 210 | } catch (Exception ignored) { } |
| 211 | } |
| 212 | next.start(); |
| 213 | } catch (Exception ignored) { |
| 214 | if (index < sequence.size() && sequence.get(index) == -1) { |
| 215 | List<Integer> fallback = new ArrayList<>(); |
| 216 | int dingNumber = context.getSharedPreferences("settings", Context.MODE_PRIVATE).getInt("dingNumber", 1); |
| 217 | if (dingNumber < 1 || dingNumber > 5) dingNumber = 1; |
| 218 | addIfExists(fallback, raw("ding" + dingNumber)); |
| 219 | if (!fallback.isEmpty()) { playAt(fallback, 0); return; } |
| 220 | } |
| 221 | playAt(sequence, index + 1); |
| 222 | } |
| 223 | } |
| 224 |
| 225 | private void requestFocus() { |
| 226 | if (audioManager == null) return; |
| 227 | focusListener = focus -> { }; |
| 228 | if (Build.VERSION.SDK_INT >= 26) { |
| 229 | focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) |
| 230 | .setAudioAttributes(new AudioAttributes.Builder() |
| 231 | .setUsage(AudioAttributes.USAGE_MEDIA) |
| 232 | .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) |
| 233 | .build()) |
| 234 | .setOnAudioFocusChangeListener(focusListener) |
| 235 | .build(); |
| 236 | audioManager.requestAudioFocus(focusRequest); |
| 237 | } else { |
| 238 | audioManager.requestAudioFocus(focusListener, AudioManager.STREAM_MUSIC, |
| 239 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); |
| 240 | } |
| 241 | } |
| 242 |
| 243 | private void releaseFocus() { |
| 244 | if (audioManager == null || focusListener == null) return; |
| 245 | if (Build.VERSION.SDK_INT >= 26 && focusRequest != null) { |
| 246 | audioManager.abandonAudioFocusRequest(focusRequest); |
| 247 | focusRequest = null; |
| 248 | } else if (Build.VERSION.SDK_INT < 26) { |
| 249 | audioManager.abandonAudioFocus(focusListener); |
| 250 | } |
| 251 | focusListener = null; |
| 252 | } |
| 253 |
| 254 | private void boostVolume() { |
| 255 | if (audioManager == null) return; |
| 256 | try { |
| 257 | originalVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); |
| 258 | int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); |
| 259 | int pct = context.getSharedPreferences("settings", Context.MODE_PRIVATE) |
| 260 | .getInt("announcementVolume", 80); |
| 261 | int target = Math.round(max * (pct / 100f)); |
| 262 | if (target < 1) target = 1; |
| 263 | audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Math.min(max, target), 0); |
| 264 | } catch (Exception ignored) { } |
| 265 | } |
| 266 |
| 267 | private void restoreVolume() { |
| 268 | if (audioManager != null && originalVolume >= 0) { |
| 269 | try { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0); } |
| 270 | catch (Exception ignored) { } |
| 271 | originalVolume = -1; |
| 272 | } |
| 273 | } |
| 274 |
| 275 | private void releasePlayerOnly() { |
| 276 | if (player != null) { |
| 277 | try { player.stop(); } catch (Exception ignored) { } |
| 278 | try { player.release(); } catch (Exception ignored) { } |
| 279 | player = null; |
| 280 | } |
| 281 | } |
| 282 |
| 283 | public void stop() { |
| 284 | releasePlayerOnly(); |
| 285 | restoreVolume(); |
| 286 | releaseFocus(); |
| 287 | } |
| 288 | } |