diff --git a/app/src/main/java/org/bepass/oblivion/service/OblivionVpnService.java b/app/src/main/java/org/bepass/oblivion/service/OblivionVpnService.java index c3461715..8598b77b 100644 --- a/app/src/main/java/org/bepass/oblivion/service/OblivionVpnService.java +++ b/app/src/main/java/org/bepass/oblivion/service/OblivionVpnService.java @@ -86,7 +86,7 @@ public void run() { } }; // For JNI Calling in a new threa - private static final Executor executorService = Executors.newSingleThreadExecutor(); + private static final ExecutorService executorService = Executors.newFixedThreadPool(1); // For PingHTTPTestConnection to don't busy-waiting private static ScheduledExecutorService scheduler; private Notification notification; @@ -258,8 +258,6 @@ private void performConnectionTest(String bindAddress, ConnectionStateChangeList return; } - scheduler = Executors.newScheduledThreadPool(1); - final long startTime = System.currentTimeMillis(); final long timeout = 60 * 1000; // 1 minute @@ -267,20 +265,19 @@ private void performConnectionTest(String bindAddress, ConnectionStateChangeList if (System.currentTimeMillis() - startTime >= timeout) { changeListener.onChange(ConnectionState.DISCONNECTED); stopForegroundService(); - scheduler.shutdown(); + scheduler.shutdown(); // Shutdown scheduler after test completion return; } boolean result = pingOverHTTP(bindAddress); if (result) { changeListener.onChange(ConnectionState.CONNECTED); - scheduler.shutdown(); + scheduler.shutdown(); // Shutdown scheduler after successful connection } }; - - // Schedule the ping task to run with a fixed delay of 1 second - scheduler.scheduleWithFixedDelay(pingTask, 0, 1, TimeUnit.SECONDS); + // Schedule the ping task to run with a fixed delay of 5 seconds using shared scheduler + scheduler.scheduleWithFixedDelay(pingTask, 0, 5, TimeUnit.SECONDS); } private void stopForegroundService() { @@ -347,6 +344,7 @@ private void start() { } executorService.execute(() -> { + FileManager.initialize(this); bindAddress = getBindAddress(); Log.i(TAG, "Configuring VPN service"); try { @@ -396,12 +394,18 @@ public int onStartCommand(Intent intent, int flags, int startId) { public void onCreate() { super.onCreate(); handler.post(logRunnable); + if (scheduler == null) { + scheduler = Executors.newScheduledThreadPool(1); + } } @Override public void onDestroy() { super.onDestroy(); handler.removeCallbacks(logRunnable); + if (scheduler != null && !scheduler.isShutdown()) { + scheduler.shutdown(); + } if (wLock != null && wLock.isHeld()) { wLock.release(); wLock = null; @@ -424,7 +428,7 @@ public void onRevoke() { } else { Log.w(TAG, "No wake lock to release"); } - + FileManager.initialize(this); // Close the VPN interface try { if (!FileManager.getBoolean("USERSETTING_proxymode")) { diff --git a/app/src/main/java/org/bepass/oblivion/utils/LocaleHandler.java b/app/src/main/java/org/bepass/oblivion/utils/LocaleHandler.java index 2a38946c..3ad667fd 100644 --- a/app/src/main/java/org/bepass/oblivion/utils/LocaleHandler.java +++ b/app/src/main/java/org/bepass/oblivion/utils/LocaleHandler.java @@ -2,6 +2,7 @@ import android.app.AlertDialog; import android.content.Context; +import android.content.res.Resources; import android.util.Log; import androidx.appcompat.app.AppCompatDelegate; @@ -12,7 +13,6 @@ import org.bepass.oblivion.R; import java.util.Locale; -import java.util.Objects; public class LocaleHandler { private final Context context; @@ -24,14 +24,15 @@ public class LocaleHandler { public LocaleHandler(Context context) { this.context = context; - // Safely load configured locales LocaleListCompat locales; try { + // Attempt to get configured locales locales = LocaleConfigXKt.getConfiguredLocales(context); if (locales.isEmpty()) { - throw new Exception("Locale configuration is empty or null."); + throw new Resources.NotFoundException("No locales found"); } } catch (Exception e) { + // Log error and fall back to default locale Log.e("LocaleHandler", "Failed to load locale configuration. Falling back to default locale.", e); locales = LocaleListCompat.create(Locale.forLanguageTag(DEFAULT_LOCALE)); } @@ -41,7 +42,8 @@ public LocaleHandler(Context context) { public void setPersianAsDefaultLocaleIfNeeds() { if (!FileManager.getBoolean(IS_SET_DEFAULT_LOCALE)) { - AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(configuredLocales.getFirstMatch(new String[] { DEFAULT_LOCALE }))); + Locale persianLocale = Locale.forLanguageTag(DEFAULT_LOCALE); + AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(persianLocale)); FileManager.set(IS_SET_DEFAULT_LOCALE, true); } } @@ -60,8 +62,9 @@ public void showLanguageSelectionDialog() { private String[] getAvailableLanguagesNames() { String[] languageNames = new String[configuredLocales.size()]; for (int index = 0; index < configuredLocales.size(); index++) { - languageNames[index] = Objects.requireNonNull(configuredLocales.get(index)).getDisplayName(); + Locale locale = configuredLocales.get(index); + languageNames[index] = locale != null ? locale.getDisplayName() : "Unknown"; } return languageNames; } -} +} \ No newline at end of file