35
35
import android .Manifest ;
36
36
import android .annotation .SuppressLint ;
37
37
import android .app .Activity ;
38
+ import android .app .ActivityManager ;
38
39
import android .app .AlarmManager ;
39
40
import android .app .AppOpsManager ;
40
41
import android .app .PendingIntent ;
55
56
import android .util .Log ;
56
57
import android .view .View ;
57
58
import android .view .WindowManager ;
59
+ import android .widget .Toast ;
58
60
59
61
import androidx .annotation .RequiresApi ;
60
62
import androidx .appcompat .app .AlertDialog ;
63
+ import androidx .core .app .ActivityCompat ;
61
64
import androidx .core .app .NotificationCompat ;
62
65
import androidx .core .app .NotificationManagerCompat ;
63
66
import androidx .core .content .ContextCompat ;
@@ -120,7 +123,7 @@ public static Boolean isAppInstalled(Context context, String packageName) {
120
123
}
121
124
122
125
public static void setLanguage (Activity activity , String languageCode , String countryCode ) {
123
- List <LanguageModel > availableLanguages = refreshAvailableLanguages ();
126
+ List <LanguageModel > availableLanguages = refreshAvailableLanguages ();
124
127
125
128
if (languageCode .equalsIgnoreCase (LANGUAGE_SYSTEM_DEFAULT )) {
126
129
// setting default system language if available
@@ -142,8 +145,7 @@ public static void setLanguage(Activity activity, String languageCode, String co
142
145
// System country code available
143
146
countryCode = systemCountryCode ;
144
147
break ;
145
- }
146
- else {
148
+ } else {
147
149
// System country code not available
148
150
countryCode = "" ;
149
151
}
@@ -158,11 +160,9 @@ public static void setLanguage(Activity activity, String languageCode, String co
158
160
Locale locale ;
159
161
if (countryCode .equals ("rTW" )) {
160
162
locale = Locale .TAIWAN ;
161
- }
162
- else if (countryCode .equals ("rCN" )) {
163
+ } else if (countryCode .equals ("rCN" )) {
163
164
locale = Locale .CHINESE ;
164
- }
165
- else {
165
+ } else {
166
166
locale = new Locale (languageCode , countryCode );
167
167
}
168
168
conf .locale = locale ;
@@ -198,6 +198,7 @@ public static List<LanguageModel> refreshAvailableLanguages() {
198
198
list .add (new LanguageModel ("Polish" , "pl" , "" ));
199
199
list .add (new LanguageModel ("Czech" , "cs" , "" ));
200
200
list .add (new LanguageModel ("Vietnamese" , "vi" , "" ));
201
+ list .add (new LanguageModel ("Japanese" , "ja" , "" ));
201
202
202
203
Collections .sort (list , new Comparator <LanguageModel >() {
203
204
@ Override
@@ -213,18 +214,26 @@ public int compare(LanguageModel languageModel, LanguageModel t1) {
213
214
214
215
public static void refreshService (Context context ) {
215
216
if (PreferenceManager .getDefaultSharedPreferences (context ).getBoolean ("combine_notifications" , false )) {
216
- context .startService (new Intent (context , CompoundNotification .class ));
217
+ if (!isCombinedNotificationServiceRunning (context )) {
218
+ context .startService (new Intent (context , CompoundNotification .class ));
219
+ }
217
220
}
218
221
else {
219
222
if (PreferenceManager .getDefaultSharedPreferences (context ).getBoolean ("network_signal_notification" , false )) {
220
- context .startService (new Intent (context , LiveNetworkMonitor .class ));
223
+ if (!isLiveNetworkServiceRunning (context )) {
224
+ context .startService (new Intent (context , LiveNetworkMonitor .class ));
225
+ }
221
226
}
222
227
if (PreferenceManager .getDefaultSharedPreferences (context ).getBoolean ("setup_notification" , false )) {
223
- context .startService (new Intent (context , NotificationService .class ));
228
+ if (!isNotificationServiceRunning (context )) {
229
+ context .startService (new Intent (context , NotificationService .class ));
230
+ }
224
231
}
225
232
}
226
233
if (PreferenceManager .getDefaultSharedPreferences (context ).getBoolean ("data_usage_alert" , false )) {
227
- context .startService (new Intent (context , DataUsageMonitor .class ));
234
+ if (!isDataUsageAlertServiceRunning (context )) {
235
+ context .startService (new Intent (context , DataUsageMonitor .class ));
236
+ }
228
237
}
229
238
230
239
}
@@ -234,8 +243,7 @@ public static void setRefreshAlarm(Context context) {
234
243
long wakeupMillis = 0l ;
235
244
try {
236
245
wakeupMillis = getTimePeriod (context , SESSION_CUSTOM , -1 )[1 ];
237
- }
238
- catch (ParseException e ) {
246
+ } catch (ParseException e ) {
239
247
e .printStackTrace ();
240
248
}
241
249
@@ -246,18 +254,15 @@ public static void setRefreshAlarm(Context context) {
246
254
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
247
255
if (alarmManager .canScheduleExactAlarms ()) {
248
256
alarmManager .setExact (AlarmManager .RTC_WAKEUP , wakeupMillis , pendingIntent );
249
- }
250
- else {
251
- Log .e (TAG , "setRefreshAlarm: permission SCHEDULE_EXACT_ALARM not granted" );
257
+ } else {
258
+ Log .e (TAG , "setRefreshAlarm: permission SCHEDULE_EXACT_ALARM not granted" );
252
259
postAlarmPermissionDeniedNotification (context );
253
260
}
254
- }
255
- else {
261
+ } else {
256
262
alarmManager .setExact (AlarmManager .RTC_WAKEUP , wakeupMillis , pendingIntent );
257
263
}
258
- Log .d (TAG , "setRefreshAlarm: set" );
259
- }
260
- else {
264
+ Log .d (TAG , "setRefreshAlarm: set" );
265
+ } else {
261
266
Log .e (TAG , "setRefreshAlarm: something is wrong here " + wakeupMillis );
262
267
}
263
268
}
@@ -279,18 +284,15 @@ public static void setDataPlanNotification(Context context) {
279
284
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
280
285
if (alarmManager .canScheduleExactAlarms ()) {
281
286
alarmManager .setExact (AlarmManager .RTC_WAKEUP , wakeupMillis , pendingIntent );
282
- }
283
- else {
284
- Log .e (TAG , "setRefreshAlarm: permission SCHEDULE_EXACT_ALARM not granted" );
287
+ } else {
288
+ Log .e (TAG , "setRefreshAlarm: permission SCHEDULE_EXACT_ALARM not granted" );
285
289
postAlarmPermissionDeniedNotification (context );
286
290
}
287
- }
288
- else {
291
+ } else {
289
292
alarmManager .setExact (AlarmManager .RTC_WAKEUP , wakeupMillis , pendingIntent );
290
293
}
291
- Log .d (TAG , "setDataPlanNotification: set" );
292
- }
293
- else {
294
+ Log .d (TAG , "setDataPlanNotification: set" );
295
+ } else {
294
296
Log .e (TAG , "setDataPlanNotification: something is wrong here " + wakeupMillis );
295
297
}
296
298
}
@@ -390,7 +392,7 @@ public static void postAlarmPermissionDeniedNotification(Context context) {
390
392
.setOnlyAlertOnce (true )
391
393
.setPriority (NotificationCompat .PRIORITY_MAX );
392
394
NotificationManagerCompat managerCompat = NotificationManagerCompat .from (context );
393
- managerCompat . notify ( OTHER_NOTIFICATION_ID , builder . build () );
395
+ postNotification ( context , managerCompat , builder , OTHER_NOTIFICATION_ID );
394
396
}
395
397
396
398
@ SuppressLint ("SimpleDateFormat" )
@@ -449,4 +451,60 @@ else if (date.endsWith("3")) {
449
451
}
450
452
return suffix ;
451
453
}
454
+
455
+ private static boolean isLiveNetworkServiceRunning (Context context ) {
456
+ // Check if the service is already running
457
+ ActivityManager manager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
458
+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )) {
459
+ if (LiveNetworkMonitor .class .getName ().equals (service .service .getClassName ()) ||
460
+ LiveNetworkMonitor .isServiceRunning ) {
461
+ return true ;
462
+ }
463
+ }
464
+ return false ;
465
+ }
466
+
467
+ private static boolean isCombinedNotificationServiceRunning (Context context ) {
468
+ // Check if the service is already running
469
+ ActivityManager manager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
470
+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )) {
471
+ if (CompoundNotification .class .getName ().equals (service .service .getClassName ()) ||
472
+ CompoundNotification .isServiceRunning ) {
473
+ return true ;
474
+ }
475
+ }
476
+ return false ;
477
+ }
478
+
479
+ private static boolean isNotificationServiceRunning (Context context ) {
480
+ // Check if the service is already running
481
+ ActivityManager manager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
482
+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )) {
483
+ if (NotificationService .class .getName ().equals (service .service .getClassName ())) {
484
+ return true ;
485
+ }
486
+ }
487
+ return false ;
488
+ }
489
+
490
+ private static boolean isDataUsageAlertServiceRunning (Context context ) {
491
+ // Check if the service is already running
492
+ ActivityManager manager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
493
+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )) {
494
+ if (DataUsageMonitor .class .getName ().equals (service .service .getClassName ())) {
495
+ return true ;
496
+ }
497
+ }
498
+ return false ;
499
+ }
500
+
501
+ public static void postNotification (Context context , NotificationManagerCompat notificationManager ,
502
+ NotificationCompat .Builder builder , int notificationId ) {
503
+ if (notificationManager != null && builder != null ) {
504
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .POST_NOTIFICATIONS ) ==
505
+ PackageManager .PERMISSION_GRANTED ) {
506
+ notificationManager .notify (notificationId , builder .build ());
507
+ }
508
+ }
509
+ }
452
510
}
0 commit comments