ساعت سخنگو برای فارسی زبانان
| import android.os.Bundle; |
| 9 | import android.provider.Settings; |
| 10 | import android.view.*; |
| 11 | import android.widget.*; |
| 12 |
| 13 | import java.util.Locale; |
| 14 | import java.util.Calendar; |
| 15 |
| 16 | public class SettingsActivity extends Activity { |
| 17 | private SharedPreferences prefs; |
| 18 | private LinearLayout root, slotContainer, reminderContainer; |
| 19 | private SeekBar fontSize; |
| 20 | private TextView preview; |
| 21 | private static final int SLOTS = 7; |
| 22 | private static final int REMINDERS = 5; |
| 23 | private static final int PICK_REMINDER_AUDIO_BASE = 7100; |
| 24 | private static final int PICK_CUSTOM_DING = 7201; |
| 25 | private static final int PICK_HOME_BACKGROUND = 7301; |
| 26 | private AudioTimeSpeaker speakerForTest; |
| 27 |
| 28 | @Override public void onCreate(Bundle b) { |
| 29 | super.onCreate(b); |
| 30 | setContentView(R.layout.activity_settings); |
| 31 | prefs = getSharedPreferences("settings", MODE_PRIVATE); |
| 32 | ReminderManager.migrateLegacyReminder(this); |
| 33 | root = findViewById(R.id.root); |
| 34 | slotContainer = findViewById(R.id.slotContainer); |
| 35 | reminderContainer = findViewById(R.id.reminderContainer); |
| 36 |
| 37 | Switch master = findViewById(R.id.master); |
| 38 | master.setChecked(prefs.getBoolean("enabled", true)); |
| 39 | master.setOnCheckedChangeListener((v, on) -> { |
| 40 | prefs.edit().putBoolean("enabled", on).apply(); |
| 41 | if (on) ScheduleManager.scheduleNext(this); else ScheduleManager.cancel(this); |
| 42 | }); |
| 43 |
| 44 | Switch shake = findViewById(R.id.shakeToSpeak); |
| 45 | shake.setChecked(prefs.getBoolean("shakeToSpeak", false)); |
| 46 | shake.setOnCheckedChangeListener((v, on) -> { |
| 47 | prefs.edit().putBoolean("shakeToSpeak", on).apply(); |
| 48 | try { |
| 49 | if (on) ShakeService.start(this); |
| 50 | else stopService(new Intent(this, ShakeService.class)); |
| 51 | } catch (Exception e) { |
| 52 | Toast.makeText(this, "اجرای سرویس تکان دادن ممکن نشد", Toast.LENGTH_SHORT).show(); |
| 53 | } |
| 54 | }); |
| 55 |
| 56 | SeekBar shakeSensitivity = findViewById(R.id.shakeSensitivity); |
| 57 | TextView shakeSensitivityLabel = findViewById(R.id.shakeSensitivityLabel); |
| 58 | int savedShake = Math.max(0, Math.min(100, prefs.getInt("shakeSensitivity", 55))); |
| 59 | shakeSensitivity.setProgress(savedShake); |
| 60 | updateShakeSensitivityLabel(shakeSensitivityLabel, savedShake); |
| 61 | shakeSensitivity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| 62 | public void onProgressChanged(SeekBar bar, int value, boolean fromUser) { |
| 63 | updateShakeSensitivityLabel(shakeSensitivityLabel, value); |
| 64 | if (fromUser) prefs.edit().putInt("shakeSensitivity", value).apply(); |
| 65 | } |
| 66 | public void onStartTrackingTouch(SeekBar bar) {} |
| 67 | public void onStopTrackingTouch(SeekBar bar) { prefs.edit().putInt("shakeSensitivity", bar.getProgress()).apply(); } |
| 68 | }); |
| 69 |
| 70 | buildSlots(); |
| 71 | buildDingSelection(); |
| 72 | buildVolume(); |
| 73 | buildSpeed(); |
| 74 | buildDateMode(); |
| 75 | buildPersistentNotification(); |
| 76 | buildBatteryOptimization(); |
| 77 | buildReminders(); |
| 78 | buildAppearance(); |
| 79 | buildCreatorLinks(); |
| 80 |
| 81 | findViewById(R.id.back).setOnClickListener(v -> finish()); |
| 82 | if (Build.VERSION.SDK_INT >= 31) { |
| 83 | try { |
| 84 | AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); |
| 85 | if (am != null && !am.canScheduleExactAlarms()) { |
| 86 | startActivity(new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)); |
| 87 | } |
| 88 | } catch (Exception ignored) {} |
| 89 | } |
| 90 | } |
| 91 |
| 92 | private void buildSlots() { |
| 93 | LayoutInflater inf = LayoutInflater.from(this); |
| 94 | for (int s = 0; s < SLOTS; s++) { |
| 95 | View v = inf.inflate(R.layout.slot_item, slotContainer, false); |
| 96 | slotContainer.addView(v); |
| 97 | final int idx = s; |
| 98 | TextView title = v.findViewById(R.id.slotTitle); |
| 99 | Switch en = v.findViewById(R.id.enabled); |
| 100 | Button start = v.findViewById(R.id.start), end = v.findViewById(R.id.end); |
| 101 | SeekBar bar = v.findViewById(R.id.interval); |
| 102 | TextView label = v.findViewById(R.id.intervalLabel); |
| 103 | LinearLayout days = v.findViewById(R.id.slotDays); |
| 104 | title.setText("بازه " + (s + 1)); |
| 105 | boolean def = s == 0; |
| 106 | en.setChecked(prefs.getBoolean("slot" + s + "Enabled", def)); |
| 107 | int defStart = s == 0 ? 420 : s == 1 ? 840 : s == 2 ? 1200 : 0; |
| 108 | int defEnd = s == 0 ? 600 : s == 1 ? 1200 : s == 2 ? 1440 : s == 3 ? 360 : 60; |
| 109 | int st = prefs.getInt("slot" + s + "Start", defStart); |
| 110 | int ed = prefs.getInt("slot" + s + "End", defEnd); |
| 111 | int inter = prefs.getInt("slot" + s + "Interval", 30); |
| 112 | start.setText("شروع: " + fmt(st)); |
| 113 | end.setText("پایان: " + fmt(ed)); |
| 114 | bar.setProgress(Math.max(0, Math.min(119, inter - 1))); |
| 115 | label.setText("فاصله اعلام: " + fmtNumber(inter) + " دقیقه"); |
| 116 | buildDayChecks(days, "slot" + s + "DaysMask", "روزهای اعلام", true); |
| 117 | en.setOnCheckedChangeListener((x, on) -> { |
| 118 | prefs.edit().putBoolean("slot" + idx + "Enabled", on).apply(); |
| 119 | ScheduleManager.scheduleNext(this); |
| 120 | }); |
| 121 | start.setOnClickListener(x -> pickTime(idx, true, start)); |
| 122 | end.setOnClickListener(x -> pickTime(idx, false, end)); |
| 123 | bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| 124 | public void onProgressChanged(SeekBar b, int p, boolean fromUser) { |
| 125 | int m = p + 1; |
| 126 | label.setText("فاصله اعلام: " + fmtNumber(m) + " دقیقه"); |
| 127 | if (fromUser) prefs.edit().putInt("slot" + idx + "Interval", m).apply(); |
| 128 | } |
| 129 | public void onStartTrackingTouch(SeekBar b) {} |
| 130 | public void onStopTrackingTouch(SeekBar b) { |
| 131 | prefs.edit().putInt("slot" + idx + "Interval", b.getProgress() + 1).apply(); |
| 132 | ScheduleManager.scheduleNext(SettingsActivity.this); |
| 133 | } |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 |
| 138 | private void pickTime(int idx, boolean startFlag, Button button) { |
| 139 | int old = prefs.getInt("slot" + idx + (startFlag ? "Start" : "End"), startFlag ? 420 : 600); |
| 140 | int h = (old / 60) % 24, m = old % 60; |
| 141 | new TimePickerDialog(this, (view, hh, mm) -> { |
| 142 | int val = hh * 60 + mm; |
| 143 | prefs.edit().putInt("slot" + idx + (startFlag ? "Start" : "End"), val).apply(); |
| 144 | button.setText((startFlag ? "شروع: " : "پایان: ") + fmt(val)); |
| 145 | ScheduleManager.scheduleNext(this); |
| 146 | }, h, m, true).show(); |
| 147 | } |
| 148 |
| 149 | private void buildDingSelection() { |
| 150 | RadioGroup group = findViewById(R.id.dingChoices); |
| 151 | int saved = prefs.getInt("dingNumber", 1); |
| 152 | int[] ids = {R.id.ding1, R.id.ding2, R.id.ding3, R.id.ding4, R.id.ding5}; |
| 153 | String customUri = prefs.getString("customDingUri", ""); |
| 154 | if (!customUri.isEmpty()) { |
| 155 | group.check(R.id.dingCustom); |
| 156 | } else { |
| 157 | if (saved < 1 || saved > 5) saved = 1; |
| 158 | group.check(ids[saved - 1]); |
| 159 | } |
| 160 | group.setOnCheckedChangeListener((g, id) -> { |
| 161 | for (int i = 0; i < ids.length; i++) { |
| 162 | if (id == ids[i]) { |
| 163 | prefs.edit().putInt("dingNumber", i + 1).remove("customDingUri").apply(); |
| 164 | TextView st = findViewById(R.id.customDingStatus); |
| 165 | if (st != null) st.setText("Ding سفارشی: استفاده نمیشود"); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | if (id == R.id.dingCustom && prefs.getString("customDingUri", "").isEmpty()) { |
| 170 | pickCustomDingFile(); |
| 171 | } |
| 172 | }); |
| 173 | Switch dingOnly = findViewById(R.id.dingOnlyMode); |
| 174 | dingOnly.setChecked(prefs.getBoolean("dingOnlyMode", false)); |
| 175 | dingOnly.setOnCheckedChangeListener((v, on) -> prefs.edit().putBoolean("dingOnlyMode", on).apply()); |
| 176 |
| 177 | Button customDing = findViewById(R.id.pickCustomDing); |
| 178 | TextView customDingStatus = findViewById(R.id.customDingStatus); |
| 179 | String currentCustomUri = prefs.getString("customDingUri", ""); |
| 180 | customDingStatus.setText(currentCustomUri.isEmpty() ? "Ding سفارشی: استفاده نمیشود" : "Ding سفارشی: انتخاب شده ✓"); |
| 181 | customDing.setOnClickListener(v -> { |
| 182 | pickCustomDingFile(); |
| 183 | }); |
| 184 | Button resetDing = findViewById(R.id.resetDing); |
| 185 | if (resetDing != null) resetDing.setOnClickListener(v -> { |
| 186 | prefs.edit().remove("customDingUri").putInt("dingNumber", 1).apply(); |
| 187 | group.check(R.id.ding1); |
| 188 | customDingStatus.setText("Ding سفارشی: استفاده نمیشود"); |
| 189 | Toast.makeText(this, "تنظیمات Ding به پیشفرض برگشت", Toast.LENGTH_SHORT).show(); |
| 190 | }); |
| 191 |
| 192 | findViewById(R.id.testDing).setOnClickListener(v -> { |
| 193 | if (speakerForTest == null) speakerForTest = new AudioTimeSpeaker(this); |
| 194 | speakerForTest.testSelectedDing(); |
| 195 | }); |
| 196 | } |
| 197 |
| 198 | private void pickCustomDingFile() { |
| 199 | Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 200 | i.addCategory(Intent.CATEGORY_OPENABLE); |
| 201 | i.setType("audio/*"); |
| 202 | i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 203 | startActivityForResult(i, PICK_CUSTOM_DING); |
| 204 | } |
| 205 |
| 206 | private void buildDateMode() { |
| 207 | RadioGroup g = findViewById(R.id.dateMode); |
| 208 | String saved = prefs.getString("dateMode", "jalali"); |
| 209 | g.check("gregorian".equals(saved) ? R.id.dateGregorian : R.id.dateJalali); |
| 210 | g.setOnCheckedChangeListener((x, id) -> prefs.edit().putString("dateMode", id == R.id.dateGregorian ? "gregorian" : "jalali").apply()); |
| 211 | } |
| 212 |
| 213 | private void buildPersistentNotification() { |
| 214 | Switch sw = findViewById(R.id.persistentNotification); |
| 215 | sw.setChecked(prefs.getBoolean("persistentNotification", false)); |
| 216 | sw.setOnCheckedChangeListener((b, on) -> { |
| 217 | prefs.edit().putBoolean("persistentNotification", on).apply(); |
| 218 | MainActivity.updatePersistentNotification(this); |
| 219 | }); |
| 220 | } |
| 221 |
| 222 | private void buildBatteryOptimization() { |
| 223 | TextView status = findViewById(R.id.batteryStatus); |
| 224 | Button button = findViewById(R.id.batterySettings); |
| 225 | updateBatteryStatus(status); |
| 226 | button.setOnClickListener(v -> { |
| 227 | try { |
| 228 | if (Build.VERSION.SDK_INT >= 23) { |
| 229 | android.os.PowerManager pm = (android.os.PowerManager) getSystemService(POWER_SERVICE); |
| 230 | if (pm != null && pm.isIgnoringBatteryOptimizations(getPackageName())) { |
| 231 | startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)); |
| 232 | } else { |
| 233 | Intent i = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); |
| 234 | i.setData(Uri.parse("package:" + getPackageName())); |
| 235 | startActivity(i); |
| 236 | } |
| 237 | } else startActivity(new Intent(Settings.ACTION_SETTINGS)); |
| 238 | } catch (Exception e) { |
| 239 | try { startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)); } |
| 240 | catch (Exception ignored) { startActivity(new Intent(Settings.ACTION_SETTINGS)); } |
| 241 | } |
| 242 | }); |
| 243 | } |
| 244 |
| 245 | private void updateBatteryStatus(TextView status) { |
| 246 | if (Build.VERSION.SDK_INT >= 23) { |
| 247 | android.os.PowerManager pm = (android.os.PowerManager) getSystemService(POWER_SERVICE); |
| 248 | boolean ok = pm != null && pm.isIgnoringBatteryOptimizations(getPackageName()); |
| 249 | status.setText(ok ? "وضعیت باتری: محدودیت بهینهسازی برای این برنامه حذف شده است ✓" : "وضعیت باتری: ممکن است سیستم اجرای پسزمینه را محدود کند"); |
| 250 | } else status.setText("وضعیت باتری: این نسخه اندروید محدودیت بهینهسازی باتری ندارد"); |
| 251 | } |
| 252 |
| 253 | private void buildReminders() { |
| 254 | LayoutInflater inf = LayoutInflater.from(this); |
| 255 | for (int slot = 1; slot <= REMINDERS; slot++) { |
| 256 | View v = inf.inflate(R.layout.reminder_item, reminderContainer, false); |
| 257 | reminderContainer.addView(v); |
| 258 | final int index = slot; |
| 259 | String key = "reminder" + slot; |
| 260 | TextView title = v.findViewById(R.id.reminderTitle); |
| 261 | Switch enabled = v.findViewById(R.id.reminderEnabled); |
| 262 | EditText text = v.findViewById(R.id.reminderText); |
| 263 | Button time = v.findViewById(R.id.reminderStart); |
| 264 | Button pickAudio = v.findViewById(R.id.pickReminderAudio); |
| 265 | Button test = v.findViewById(R.id.testReminder); |
| 266 | TextView end = v.findViewById(R.id.reminderEnd); |
| 267 | TextView intervalLabel = v.findViewById(R.id.reminderIntervalLabel); |
| 268 | LinearLayout days = v.findViewById(R.id.reminderDays); |
| 269 | SeekBar interval = v.findViewById(R.id.reminderInterval); |
| 270 | buildDayChecks(days, key + "DaysMask", "روزهای یادآوری", false); |
| 271 | end.setVisibility(View.GONE); |
| 272 | intervalLabel.setVisibility(View.GONE); |
| 273 | interval.setVisibility(View.GONE); |
| 274 |
| 275 | title.setText("یادآوری " + fmtNumber(slot)); |
| 276 | enabled.setChecked(prefs.getBoolean(key + "Enabled", false)); |
| 277 | text.setText(prefs.getString(key + "Text", "")); |
| 278 | int minute = prefs.getInt(key + "Minute", 9 * 60); |
| 279 | time.setText("زمان: " + fmt(minute)); |
| 280 | String audio = prefs.getString(key + "AudioUri", ""); |
| 281 | pickAudio.setText(audio.isEmpty() ? "انتخاب صدای این یادآوری" : "صدای انتخابشده ✓ (تغییر صدا)"); |
| 282 |
| 283 | enabled.setOnCheckedChangeListener((button, on) -> { |
| 284 | saveReminder(index, text, on); |
| 285 | }); |
| 286 | text.setOnFocusChangeListener((view, hasFocus) -> { |
| 287 | if (!hasFocus) saveReminder(index, text, enabled.isChecked()); |
| 288 | }); |
| 289 | time.setOnClickListener(view -> { |
| 290 | int old = prefs.getInt(key + "Minute", 9 * 60); |
| 291 | new TimePickerDialog(this, (tp, h, m) -> { |
| 292 | int val = h * 60 + m; |
| 293 | prefs.edit().putInt(key + "Minute", val).apply(); |
| 294 | time.setText("زمان: " + fmt(val)); |
| 295 | saveReminder(index, text, enabled.isChecked()); |
| 296 | }, old / 60, old % 60, true).show(); |
| 297 | }); |
| 298 | pickAudio.setOnClickListener(view -> { |
| 299 | Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 300 | i.addCategory(Intent.CATEGORY_OPENABLE); |
| 301 | i.setType("audio/*"); |
| 302 | startActivityForResult(i, PICK_REMINDER_AUDIO_BASE + index); |
| 303 | }); |
| 304 | test.setOnClickListener(view -> ReminderManager.fireNow(this, index)); |
| 305 | } |
| 306 | } |
| 307 |
| 308 | private void saveReminder(int slot, EditText text, boolean enabled) { |
| 309 | String key = "reminder" + slot; |
| 310 | prefs.edit().putBoolean(key + "Enabled", enabled).putString(key + "Text", text.getText().toString().trim()).apply(); |
| 311 | ReminderManager.schedule(this); |
| 312 | } |
| 313 |
| 314 | @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 315 | super.onActivityResult(requestCode, resultCode, data); |
| 316 | if (requestCode == PICK_CUSTOM_DING && resultCode == RESULT_OK && data != null && data.getData() != null) { |
| 317 | Uri u = data.getData(); |
| 318 | try { getContentResolver().takePersistableUriPermission(u, Intent.FLAG_GRANT_READ_URI_PERMISSION); } catch (Exception ignored) {} |
| 319 | prefs.edit().putString("customDingUri", u.toString()).apply(); |
| 320 | RadioGroup g = findViewById(R.id.dingChoices); |
| 321 | if (g != null) g.check(R.id.dingCustom); |
| 322 | TextView status = findViewById(R.id.customDingStatus); |
| 323 | if (status != null) status.setText("Ding سفارشی: انتخاب شده ✓"); |
| 324 | Toast.makeText(this, "Ding سفارشی اضافه و فعال شد", Toast.LENGTH_SHORT).show(); |
| 325 | return; |
| 326 | } |
| 327 | if (requestCode == PICK_HOME_BACKGROUND && resultCode == RESULT_OK && data != null && data.getData() != null) { |
| 328 | Uri u = data.getData(); |
| 329 | try { getContentResolver().takePersistableUriPermission(u, Intent.FLAG_GRANT_READ_URI_PERMISSION); } catch (Exception ignored) {} |
| 330 | prefs.edit().putString("homeCustomBackgroundUri", u.toString()).putString("homeBackground", "custom").apply(); |
| 331 | updateCustomBackgroundStatus(); |
| 332 | Toast.makeText(this, "پسزمینه سفارشی انتخاب و فعال شد ✓", Toast.LENGTH_SHORT).show(); |
| 333 | return; |
| 334 | } |
| 335 | if (requestCode > PICK_REMINDER_AUDIO_BASE && requestCode <= PICK_REMINDER_AUDIO_BASE + REMINDERS |
| 336 | && resultCode == RESULT_OK && data != null && data.getData() != null) { |
| 337 | int slot = requestCode - PICK_REMINDER_AUDIO_BASE; |
| 338 | Uri u = data.getData(); |
| 339 | try { getContentResolver().takePersistableUriPermission(u, Intent.FLAG_GRANT_READ_URI_PERMISSION); } catch (Exception ignored) {} |
| 340 | prefs.edit().putString("reminder" + slot + "AudioUri", u.toString()).apply(); |
| 341 | Toast.makeText(this, "صدای یادآوری " + fmtNumber(slot) + " انتخاب شد", Toast.LENGTH_SHORT).show(); |
| 342 | ReminderManager.schedule(this); |
| 343 | } |
| 344 | } |
| 345 |
| 346 | private void buildDayChecks(LinearLayout container, String prefKey, String labelText, boolean schedule) { |
| 347 | if (container == null) return; |
| 348 | container.removeAllViews(); |
| 349 | TextView label = new TextView(this); |
| 350 | label.setText(labelText + ":"); |
| 351 | label.setTextColor(getResources().getColor(R.color.muted)); |
| 352 | label.setTextSize(13); |
| 353 | container.addView(label); |
| 354 |
| 355 | LinearLayout row = new LinearLayout(this); |
| 356 | row.setOrientation(LinearLayout.HORIZONTAL); |
| 357 | row.setWeightSum(7f); |
| 358 | container.addView(row); |
| 359 |
| 360 | // Persian order: شنبه، یکشنبه، دوشنبه، سهشنبه، چهارشنبه، پنجشنبه، جمعه. |
| 361 | final String[] names = {"ش", "ی", "د", "س", "چ", "پ", "ج"}; |
| 362 | final int[] calendarDays = {Calendar.SATURDAY, Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY}; |
| 363 | int mask = prefs.getInt(prefKey, 127); |
| 364 | for (int i = 0; i < 7; i++) { |
| 365 | CheckBox cb = new CheckBox(this); |
| 366 | cb.setText(names[i]); |
| 367 | cb.setTextColor(getResources().getColor(R.color.text)); |
| 368 | cb.setGravity(Gravity.CENTER); |
| 369 | cb.setTextSize(13); |
| 370 | cb.setChecked((mask & (1 << (calendarDays[i] - 1))) != 0); |
| 371 | final int bit = 1 << (calendarDays[i] - 1); |
| 372 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 46, 1f); |
| 373 | row.addView(cb, lp); |
| 374 | cb.setOnCheckedChangeListener((button, checked) -> { |
| 375 | int m = prefs.getInt(prefKey, 127); |
| 376 | if (checked) m |= bit; else m &= ~bit; |
| 377 | if (m == 0) { |
| 378 | button.setChecked(true); |
| 379 | Toast.makeText(this, "حداقل یک روز باید انتخاب شود", Toast.LENGTH_SHORT).show(); |
| 380 | return; |
| 381 | } |
| 382 | prefs.edit().putInt(prefKey, m).apply(); |
| 383 | if (schedule) ScheduleManager.scheduleNext(this); else ReminderManager.schedule(this); |
| 384 | }); |
| 385 | } |
| 386 | } |
| 387 |
| 388 | private void buildVolume() { |
| 389 | SeekBar v = findViewById(R.id.volume); TextView l = findViewById(R.id.volumeLabel); |
| 390 | int saved = prefs.getInt("announcementVolume", 80); |
| 391 | v.setProgress(Math.max(0, Math.min(80, saved - 20))); l.setText(fmtNumber(saved) + "٪"); |
| 392 | v.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| 393 | public void onProgressChanged(SeekBar s, int p, boolean f) { int val = p + 20; l.setText(fmtNumber(val) + "٪"); if (f) prefs.edit().putInt("announcementVolume", val).apply(); } |
| 394 | public void onStartTrackingTouch(SeekBar s) {} |
| 395 | public void onStopTrackingTouch(SeekBar s) { prefs.edit().putInt("announcementVolume", s.getProgress() + 20).apply(); } |
| 396 | }); |
| 397 | } |
| 398 |
| 399 | private void buildSpeed() { |
| 400 | SeekBar v = findViewById(R.id.speed); TextView l = findViewById(R.id.speedLabel); |
| 401 | int saved = Math.max(70, Math.min(130, prefs.getInt("announcementSpeed", 100))); |
| 402 | v.setProgress(saved - 70); l.setText(fmtNumber(saved) + "٪"); |
| 403 | v.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| 404 | public void onProgressChanged(SeekBar s, int p, boolean f) { int val = p + 70; l.setText(fmtNumber(val) + "٪"); if (f) prefs.edit().putInt("announcementSpeed", val).apply(); } |
| 405 | public void onStartTrackingTouch(SeekBar s) {} |
| 406 | public void onStopTrackingTouch(SeekBar s) { prefs.edit().putInt("announcementSpeed", s.getProgress() + 70).apply(); } |
| 407 | }); |
| 408 | } |
| 409 |
| 410 | private void updateShakeSensitivityLabel(TextView label, int value) { |
| 411 | String text; |
| 412 | if (value < 30) text = "زیاد — با تکانهای سبکتر"; |
| 413 | else if (value < 70) text = "متوسط"; |
| 414 | else text = "کم — مناسب خودرو و مسیرهای پرلرزش"; |
| 415 | label.setText(text); |
| 416 | } |
| 417 |
| 418 | private void buildAppearance() { |
| 419 | fontSize = findViewById(R.id.fontSize); preview = findViewById(R.id.fontPreview); |
| 420 | float saved = prefs.getFloat("fontSize", 64); |
| 421 | fontSize.setProgress(Math.max(0, Math.min(46, Math.round(saved - 36)))); |
| 422 | preview.setTextSize(saved / 2); |
| 423 | fontSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| 424 | public void onProgressChanged(SeekBar s, int p, boolean f) { |
| 425 | float z = 36 + p; prefs.edit().putFloat("fontSize", z).apply(); preview.setTextSize(z / 2); |
| 426 | } |
| 427 | public void onStartTrackingTouch(SeekBar s) {} |
| 428 | public void onStopTrackingTouch(SeekBar s) {} |
| 429 | }); |
| 430 | RadioGroup colors = findViewById(R.id.colors); |
| 431 | int c = prefs.getInt("color", 0xffd89b2b); |
| 432 | if (c == 0xff66bb6a) colors.check(R.id.green); else if (c == 0xffce93d8) colors.check(R.id.purple); else if (c == 0xff8ab4f8) colors.check(R.id.blue); else colors.check(R.id.orange); |
| 433 | colors.setOnCheckedChangeListener((g, id) -> { |
| 434 | int col = id == R.id.green ? 0xff66bb6a : id == R.id.purple ? 0xffce93d8 : id == R.id.blue ? 0xff8ab4f8 : 0xffd89b2b; |
| 435 | prefs.edit().putInt("color", col).apply(); preview.setTextColor(col); |
| 436 | }); |
| 437 | RadioGroup fonts = findViewById(R.id.fontChoices); |
| 438 | String savedFont = prefs.getString("fontChoice", "yekan"); |
| 439 | fonts.check("vazir".equals(savedFont) ? R.id.fontVazir : "nazanin".equals(savedFont) ? R.id.fontNazanin : R.id.fontYekan); |
| 440 | fonts.setOnCheckedChangeListener((g,id) -> { |
| 441 | String f = id == R.id.fontVazir ? "vazir" : id == R.id.fontNazanin ? "nazanin" : "yekan"; |
| 442 | prefs.edit().putString("fontChoice", f).apply(); applySelectedFont(root, f); applySelectedFont(preview, f); |
| 443 | }); |
| 444 | RadioGroup backs = findViewById(R.id.backgroundChoices); |
| 445 | String bg = prefs.getString("homeBackground", "default"); |
| 446 | if ("custom".equals(bg) && !prefs.getString("homeCustomBackgroundUri", "").isEmpty()) { |
| 447 | backs.clearCheck(); |
| 448 | } else { |
| 449 | backs.check("warm".equals(bg) ? R.id.bgWarm : "plain".equals(bg) ? R.id.bgPlain : "landscape".equals(bg) ? R.id.bgLandscape : R.id.bgDefault); |
| 450 | } |
| 451 | backs.setOnCheckedChangeListener((g,id) -> { |
| 452 | String v = id == R.id.bgWarm ? "warm" : id == R.id.bgPlain ? "plain" : id == R.id.bgLandscape ? "landscape" : "default"; |
| 453 | prefs.edit().putString("homeBackground", v).remove("homeCustomBackgroundUri").apply(); |
| 454 | updateCustomBackgroundStatus(); |
| 455 | }); |
| 456 |
| 457 | Button pickBackground = findViewById(R.id.pickHomeBackground); |
| 458 | if (pickBackground != null) pickBackground.setOnClickListener(v -> pickHomeBackgroundFile()); |
| 459 | updateCustomBackgroundStatus(); |
| 460 | applySelectedFont(root, savedFont); |
| 461 | } |
| 462 |
| 463 |
| 464 | private void pickHomeBackgroundFile() { |
| 465 | Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 466 | i.addCategory(Intent.CATEGORY_OPENABLE); |
| 467 | i.setType("image/*"); |
| 468 | i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 469 | startActivityForResult(i, PICK_HOME_BACKGROUND); |
| 470 | } |
| 471 |
| 472 | private void updateCustomBackgroundStatus() { |
| 473 | TextView status = findViewById(R.id.customBackgroundStatus); |
| 474 | if (status == null) return; |
| 475 | String uri = prefs.getString("homeCustomBackgroundUri", ""); |
| 476 | status.setText(uri.isEmpty() ? "پسزمینه سفارشی: انتخاب نشده" : "پسزمینه سفارشی: فعال ✓"); |
| 477 | } |
| 478 |
| 479 | private Typeface selectedTypeface(String f) { |
| 480 | try { |
| 481 | if ("vazir".equals(f)) return Typeface.createFromAsset(getAssets(), "fonts/Vazir.ttf"); |
| 482 | if ("yekan".equals(f)) return Typeface.createFromAsset(getAssets(), "fonts/YEKAN.TTF"); |
| 483 | } catch (Exception ignored) {} |
| 484 | return "nazanin".equals(f) ? Typeface.create("serif", Typeface.NORMAL) : Typeface.create("sans-serif", Typeface.NORMAL); |
| 485 | } |
| 486 |
| 487 | private void applySelectedFont(View v, String f) { |
| 488 | Typeface tf = selectedTypeface(f); |
| 489 | if (v instanceof TextView) ((TextView)v).setTypeface(tf); |
| 490 | if (v instanceof ViewGroup) for (int i=0;i<((ViewGroup)v).getChildCount();i++) applySelectedFont(((ViewGroup)v).getChildAt(i), f); |
| 491 | } |
| 492 |
| 493 | private void buildCreatorLinks() { |
| 494 | findViewById(R.id.creatorEmail).setOnClickListener(v -> open("mailto:hamedmohammadinikche@gmail.com")); |
| 495 | findViewById(R.id.creatorGithub).setOnClickListener(v -> open("https://github.com/shahabsystem")); |
| 496 | findViewById(R.id.coffee).setOnClickListener(v -> open("https://coffeebede.com/shahabsystem")); |
| 497 | findViewById(R.id.remit).setOnClickListener(v -> open("https://reymit.ir/shahabsystem")); |
| 498 | } |
| 499 |
| 500 | private void open(String u) { |
| 501 | try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(u))); } catch (Exception ignored) {} |
| 502 | } |
| 503 |
| 504 | private String fmt(int m) { |
| 505 | m = ((m % 1440) + 1440) % 1440; |
| 506 | return String.format(Locale.US, "%02d:%02d", m / 60, m % 60) |
| 507 | .replace('0','۰').replace('1','۱').replace('2','۲').replace('3','۳').replace('4','۴') |
| 508 | .replace('5','۵').replace('6','۶').replace('7','۷').replace('8','۸').replace('9','۹'); |
| 509 | } |
| 510 |
| 511 | private String fmtNumber(int n) { return String.valueOf(n).replace('0','۰').replace('1','۱').replace('2','۲').replace('3','۳').replace('4','۴').replace('5','۵').replace('6','۶').replace('7','۷').replace('8','۸').replace('9','۹'); } |
| 512 |
| 513 | @Override protected void onResume() { |
| 514 | super.onResume(); |
| 515 | TextView status = findViewById(R.id.batteryStatus); |
| 516 | if (status != null) updateBatteryStatus(status); |
| 517 | } |
| 518 |
| 519 | @Override protected void onDestroy() { if (speakerForTest != null) speakerForTest.stop(); super.onDestroy(); } |
| 520 |
| 521 | private void applyTypeface(View v, Typeface tf) { |
| 522 | if (v instanceof TextView) ((TextView) v).setTypeface(tf); |
| 523 | if (v instanceof ViewGroup) for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) applyTypeface(((ViewGroup) v).getChildAt(i), tf); |
| 524 | } |
| 525 | } |