ساعت سخنگو برای فارسی زبانان
| import android.os.*; |
| 9 | import android.view.*; |
| 10 | import android.widget.*; |
| 11 | import java.text.SimpleDateFormat; |
| 12 | import java.util.*; |
| 13 |
| 14 | public class MainActivity extends Activity { |
| 15 | private TextView clock, date, statusText; |
| 16 | private AudioTimeSpeaker speaker; |
| 17 | private SharedPreferences prefs; |
| 18 | private final Handler handler = new Handler(Looper.getMainLooper()); |
| 19 | private final Runnable clockTicker = new Runnable() { |
| 20 | @Override public void run() { |
| 21 | updateClock(); |
| 22 | handler.postDelayed(this, 1000); |
| 23 | } |
| 24 | }; |
| 25 |
| 26 | @Override public void onCreate(Bundle b) { |
| 27 | super.onCreate(b); |
| 28 | setContentView(R.layout.activity_main); |
| 29 | prefs = getSharedPreferences("settings", MODE_PRIVATE); |
| 30 | clock = findViewById(R.id.clock); |
| 31 | date = findViewById(R.id.date); |
| 32 | statusText = findViewById(R.id.statusText); |
| 33 | applyPrefs(); |
| 34 | speaker = new AudioTimeSpeaker(this); |
| 35 | findViewById(R.id.speak).setOnClickListener(v -> speakTime()); |
| 36 | findViewById(R.id.settings).setOnClickListener(v -> startActivity(new Intent(this, SettingsActivity.class))); |
| 37 | findViewById(R.id.exit).setOnClickListener(v -> exitNow()); |
| 38 | ScheduleManager.scheduleNext(this); |
| 39 | ReminderManager.schedule(this); |
| 40 | updatePersistentNotification(this); |
| 41 | if (prefs.getBoolean("shakeToSpeak", false)) { |
| 42 | try { ShakeService.start(this); } catch (Exception ignored) {} |
| 43 | } |
| 44 | if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) |
| 45 | requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 900); |
| 46 | } |
| 47 |
| 48 | @Override protected void onResume() { |
| 49 | super.onResume(); |
| 50 | if (prefs != null) { applyPrefs(); updateClock(); } |
| 51 | handler.removeCallbacks(clockTicker); |
| 52 | handler.post(clockTicker); |
| 53 | } |
| 54 |
| 55 | private void updateClock() { |
| 56 | Date now = new Date(); |
| 57 | String time = new SimpleDateFormat("HH:mm", Locale.US).format(now); |
| 58 | String d = formattedDate(now, prefs.getString("dateMode", "jalali")); |
| 59 | clock.setText(toPersian(time)); |
| 60 | date.setText(toPersian(d)); |
| 61 | boolean enabled = prefs.getBoolean("enabled", true); |
| 62 | statusText.setText(enabled ? "فعال • زمانبندی در پسزمینه روشن است" : "غیرفعال"); |
| 63 | statusText.setTextColor(enabled ? 0xff32d889 : 0xffff6b6b); |
| 64 | } |
| 65 |
| 66 | private String toPersian(String s) { |
| 67 | return s.replace('0','۰').replace('1','۱').replace('2','۲').replace('3','۳').replace('4','۴').replace('5','۵').replace('6','۶').replace('7','۷').replace('8','۸').replace('9','۹'); |
| 68 | } |
| 69 |
| 70 | private void speakTime() { if (speaker != null) speaker.speakCurrentTime(); } |
| 71 |
| 72 | private void applyPrefs() { |
| 73 | float size = Math.max(72f, prefs.getFloat("fontSize", 82)); |
| 74 | clock.setTextSize(size); |
| 75 | date.setTextSize(Math.max(30f, size * .38f)); |
| 76 | String font = prefs.getString("fontChoice", "yekan"); |
| 77 | try { |
| 78 | Typeface tf; |
| 79 | if ("vazir".equals(font)) tf = Typeface.createFromAsset(getAssets(), "fonts/Vazir.ttf"); |
| 80 | else if ("yekan".equals(font)) tf = Typeface.createFromAsset(getAssets(), "fonts/YEKAN.TTF"); |
| 81 | else tf = Typeface.create("serif", Typeface.NORMAL); |
| 82 | clock.setTypeface(tf); date.setTypeface(tf); |
| 83 | } catch (Exception ignored) {} |
| 84 | int color = prefs.getInt("color", 0xffd89b2b); |
| 85 | clock.setTextColor(color); |
| 86 | getWindow().setStatusBarColor(0xff050b14); |
| 87 | getWindow().setNavigationBarColor(0xff050b14); |
| 88 |
| 89 | String bg = prefs.getString("homeBackground", "landscape"); |
| 90 | ImageView image = findViewById(R.id.homeBackground); |
| 91 | String customUri = prefs.getString("homeCustomBackgroundUri", ""); |
| 92 | if ("custom".equals(bg) && !customUri.isEmpty()) { |
| 93 | try { |
| 94 | image.setImageURI(android.net.Uri.parse(customUri)); |
| 95 | if (image.getDrawable() != null) return; |
| 96 | } catch (Exception ignored) {} |
| 97 | } |
| 98 | if ("plain".equals(bg)) image.setImageResource(R.drawable.bg_home_plain); |
| 99 | else if ("warm".equals(bg)) image.setImageResource(R.drawable.bg_home_warm); |
| 100 | else image.setImageResource(R.drawable.bg_home_landscape); |
| 101 | } |
| 102 |
| 103 | private void exitNow() { if (speaker != null) speaker.stop(); finishAndRemoveTask(); } |
| 104 |
| 105 | private String formattedDate(Date date, String mode) { |
| 106 | Calendar c = Calendar.getInstance(); c.setTime(date); |
| 107 | if ("gregorian".equals(mode)) return new SimpleDateFormat("yyyy/MM/dd", Locale.US).format(date); |
| 108 | int gy=c.get(Calendar.YEAR), gm=c.get(Calendar.MONTH)+1, gd=c.get(Calendar.DAY_OF_MONTH); |
| 109 | int[] r=jalali(gy,gm,gd); return String.format(Locale.US,"%04d/%02d/%02d",r[0],r[1],r[2]); |
| 110 | } |
| 111 |
| 112 | private static int[] jalali(int gy,int gm,int gd){ |
| 113 | int[] gdm={31,(gy%4==0&&(gy%100!=0||gy%400==0))?29:28,31,30,31,30,31,31,30,31,30,31}; |
| 114 | int gy2=gy-1600, gm2=gm-1, gd2=gd-1; int gdn=365*gy2+(gy2+3)/4-(gy2+99)/100+(gy2+399)/400; |
| 115 | for(int i=0;i<gm2;i++)gdn+=gdm[i]; gdn+=gd2; int jdn=gdn-79; int jy=979+33*(jdn/12053); jdn%=12053; jy+=4*(jdn/1461); jdn%=1461; |
| 116 | if(jdn>=366){jy+=(jdn-1)/365;jdn=(jdn-1)%365;} int jm,jd; |
| 117 | if(jdn<186){jm=1+jdn/31;jd=1+jdn%31;}else{jm=7+(jdn-186)/30;jd=1+(jdn-186)%30;} return new int[]{jy,jm,jd}; |
| 118 | } |
| 119 |
| 120 | public static void updatePersistentNotification(Context ctx){ |
| 121 | SharedPreferences p=ctx.getSharedPreferences("settings",Context.MODE_PRIVATE); |
| 122 | NotificationManager nm=(NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE); |
| 123 | final String ch="clock_persistent"; |
| 124 | if(Build.VERSION.SDK_INT>=26)nm.createNotificationChannel(new NotificationChannel(ch,"ساعت دائمی",NotificationManager.IMPORTANCE_LOW)); |
| 125 | if(!p.getBoolean("persistentNotification",false)){nm.cancel(1701);return;} |
| 126 | String mode=p.getString("dateMode","jalali"); Calendar now=Calendar.getInstance(); String date; |
| 127 | if("gregorian".equals(mode)) date=new SimpleDateFormat("yyyy/MM/dd",Locale.US).format(now.getTime()); |
| 128 | else {int[] j=jalali(now.get(Calendar.YEAR),now.get(Calendar.MONTH)+1,now.get(Calendar.DAY_OF_MONTH));date=String.format(Locale.US,"%04d/%02d/%02d",j[0],j[1],j[2]);} |
| 129 | String notificationText=toPersianStatic(date); |
| 130 | android.widget.RemoteViews rv=new android.widget.RemoteViews(ctx.getPackageName(),R.layout.notification_large); |
| 131 | rv.setTextViewText(R.id.notificationTitle,"تاریخ امروز"); rv.setTextViewText(R.id.notificationText,notificationText); |
| 132 | Notification n; |
| 133 | if(Build.VERSION.SDK_INT>=26)n=new Notification.Builder(ctx,ch).setSmallIcon(R.drawable.ic_status_clock).setCustomContentView(rv).setContentTitle("تاریخ امروز").setContentText(notificationText).setOngoing(true).setOnlyAlertOnce(true).build(); |
| 134 | else n=new Notification.Builder(ctx).setSmallIcon(R.drawable.ic_status_clock).setContent(rv).setContentTitle("تاریخ امروز").setContentText(notificationText).setOngoing(true).setOnlyAlertOnce(true).build(); |
| 135 | nm.notify(1701,n); |
| 136 | } |
| 137 | private static String toPersianStatic(String s){return s.replace('0','۰').replace('1','۱').replace('2','۲').replace('3','۳').replace('4','۴').replace('5','۵').replace('6','۶').replace('7','۷').replace('8','۸').replace('9','۹');} |
| 138 | @Override protected void onDestroy(){handler.removeCallbacks(clockTicker);if(speaker!=null)speaker.stop();super.onDestroy();} |
| 139 | } |