Skip to content

Commit

Permalink
enhance vpnservice resource
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX authored and markpash committed Aug 31, 2024
1 parent 6eb68b6 commit 6b277ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -258,29 +258,26 @@ private void performConnectionTest(String bindAddress, ConnectionStateChangeList
return;
}

scheduler = Executors.newScheduledThreadPool(1);

final long startTime = System.currentTimeMillis();
final long timeout = 60 * 1000; // 1 minute

Runnable pingTask = () -> {
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() {
Expand Down Expand Up @@ -347,6 +344,7 @@ private void start() {
}

executorService.execute(() -> {
FileManager.initialize(this);
bindAddress = getBindAddress();
Log.i(TAG, "Configuring VPN service");
try {
Expand Down Expand Up @@ -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;
Expand All @@ -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")) {
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/org/bepass/oblivion/utils/LocaleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,7 +13,6 @@
import org.bepass.oblivion.R;

import java.util.Locale;
import java.util.Objects;

public class LocaleHandler {
private final Context context;
Expand All @@ -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));
}
Expand All @@ -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);
}
}
Expand All @@ -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;
}
}
}

0 comments on commit 6b277ce

Please sign in to comment.