Skip to content

Commit

Permalink
Fixed bugs and made improvements for GitHub issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX authored and markpash committed May 22, 2024
1 parent 8965410 commit b87aff6
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 35 deletions.
9 changes: 5 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED"
android:minSdkVersion="34" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -36,7 +37,8 @@
android:screenOrientation="portrait"
android:enableOnBackInvokedCallback="true"
android:theme="@style/Theme.OblivionUI"
tools:targetApi="31">
tools:targetApi="tiramisu"
tools:ignore="DiscouragedApi">

<service
android:name=".OblivionVpnService"
Expand Down Expand Up @@ -78,7 +80,6 @@
<activity
android:name="org.bepass.oblivion.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.OblivionUI">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -91,7 +92,7 @@
android:exported="false" />

<activity
android:name="org.bepass.oblivion.BugActivity"
android:name="org.bepass.oblivion.LogActivity"
android:exported="false" />

<activity
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/bepass/oblivion/ConnectionState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public enum ConnectionState {
public boolean isDisconnected() {
return this == DISCONNECTED;
}
public boolean isConnecting(){
return this == CONNECTING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class BugActivity extends AppCompatActivity {
public class LogActivity extends AppCompatActivity {

private final Handler handler = new Handler(Looper.getMainLooper());
private TextView logs;
Expand All @@ -25,7 +24,7 @@ public class BugActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bug);
setContentView(R.layout.activity_log);

ImageView back = findViewById(R.id.back);
logs = findViewById(R.id.logs);
Expand Down Expand Up @@ -63,26 +62,23 @@ protected void onPause() {
}

private void readLogsFromFile() {
new Thread(() -> {
try (FileInputStream fis = openFileInput("logs.txt")) {
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder();
String line;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput("logs.txt")))) {
StringBuilder sb = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}

String finalLog = sb.toString();
runOnUiThread(() -> {
logs.setText(finalLog);
if (!isUserScrollingUp) {
logScrollView.post(() -> logScrollView.fullScroll(ScrollView.FOCUS_DOWN));
}
});
} catch (IOException e) {
e.printStackTrace();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
}).start();

String finalLog = sb.toString();
runOnUiThread(() -> {
logs.setText(finalLog);
if (!isUserScrollingUp) {
logScrollView.post(() -> logScrollView.fullScroll(ScrollView.FOCUS_DOWN));
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
106 changes: 100 additions & 6 deletions app/src/main/java/org/bepass/oblivion/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package org.bepass.oblivion;

import static org.bepass.oblivion.OblivionVpnService.startVpnService;
import static org.bepass.oblivion.OblivionVpnService.stopVpnService;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
Expand All @@ -16,6 +30,7 @@
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -28,6 +43,15 @@ public class MainActivity extends StateAwareBaseActivity {
private long backPressedTime;
private Toast backToast;

private static final int REQUEST_CODE_BATTERY_OPTIMIZATIONS = 1;
private final ActivityResultLauncher<Intent> batteryOptimizationLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
// Do nothing, as no return value is expected
});

private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -39,10 +63,14 @@ protected void onCreate(Bundle savedInstanceState) {

// Set the layout of the main activity
setContentView(R.layout.activity_main);

if (!isIgnoringBatteryOptimizations()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestIgnoreBatteryOptimizations();
}
}
// Views
ImageView infoIcon = findViewById(R.id.info_icon);
ImageView bugIcon = findViewById(R.id.bug_icon);
ImageView logIcon = findViewById(R.id.bug_icon);
ImageView settingsIcon = findViewById(R.id.setting_icon);

FrameLayout switchButtonFrame = findViewById(R.id.switch_button_frame);
Expand All @@ -53,7 +81,7 @@ protected void onCreate(Bundle savedInstanceState) {

// Set listeners
infoIcon.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, InfoActivity.class)));
bugIcon.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, BugActivity.class)));
logIcon.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, LogActivity.class)));
settingsIcon.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, SettingsActivity.class)));
switchButtonFrame.setOnClickListener(v -> switchButton.toggle());

Expand All @@ -64,12 +92,11 @@ protected void onCreate(Bundle savedInstanceState) {
}
switchButton.setChecked(false);
});

// Listener for toggle switch
switchButton.setOnCheckedChangeListener((view, isChecked) -> {
if (!isChecked) {
if (!lastKnownConnectionState.isDisconnected()) {
OblivionVpnService.stopVpnService(this);
stopVpnService(this);
}
return;
}
Expand All @@ -78,11 +105,28 @@ protected void onCreate(Bundle savedInstanceState) {
vpnPermissionLauncher.launch(vpnIntent);
return;
}
// Handle the case when the VPN is in the connecting state and the user clicks to abort
if (lastKnownConnectionState.isConnecting()) {
stopVpnService(this);
return;
}
// Start the VPN service if it's disconnected
if (lastKnownConnectionState.isDisconnected()) {
OblivionVpnService.startVpnService(this);
}
// To check is Internet Connection is available
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(!lastKnownConnectionState.isDisconnected()) {
checkInternetConnectionAndDisconnectVPN();
handler.postDelayed(this, 3000); // Check every 3 seconds
}
}
}, 5000); // Start checking after 5 seconds
});


// Request permission to create push notifications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityResultLauncher<String> pushNotificationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
Expand Down Expand Up @@ -112,6 +156,56 @@ public void handleOnBackPressed() {
});
}

// Check internet connectivity
private boolean isConnectedToInternet() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network activeNetwork = connectivityManager.getActiveNetwork();
if (activeNetwork != null) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
return networkCapabilities != null &&
(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
}
} else {
// For API levels below 23, use the deprecated method
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
}
return false;
}

// Periodically check internet connection and disconnect VPN if not connected
private void checkInternetConnectionAndDisconnectVPN() {
if (!isConnectedToInternet()) {
stopVpnService(this);
}
}
private boolean isIgnoringBatteryOptimizations() {
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (pm != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return pm.isIgnoringBatteryOptimizations(packageName);
}
}
return false;
}
@SuppressLint("BatteryLife")
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestIgnoreBatteryOptimizations() {
try {
Intent intent = new Intent();
String packageName = getPackageName();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
batteryOptimizationLauncher.launch(intent);
} catch (Exception e) {
Toast.makeText(this, "Unable to request ignore battery optimizations", Toast.LENGTH_SHORT).show();
}
}
protected void cleanOrMigrateSettings() {
// Get the global FileManager instance
FileManager fileManager = FileManager.getInstance(getApplicationContext());
Expand Down Expand Up @@ -161,7 +255,7 @@ void onConnectionStateChange(ConnectionState state) {
publicIP.setVisibility(View.GONE);
ipProgressBar.setVisibility(View.VISIBLE);
switchButton.setChecked(true, false);
switchButton.setEnabled(false);
switchButton.setEnabled(true);
break;
case CONNECTED:
switchButton.setEnabled(true);
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/org/bepass/oblivion/OblivionVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static synchronized void stopVpnService(Context context) {
intent.setAction(OblivionVpnService.FLAG_VPN_STOP);
ContextCompat.startForegroundService(context, intent);
}

public static void registerConnectionStateObserver(String key, Messenger serviceMessenger, ConnectionStateChangeListener observer) {
// Create a message for the service
Message subscriptionMessage = Message.obtain(null, OblivionVpnService.MSG_CONNECTION_STATE_SUBSCRIBE);
Expand Down Expand Up @@ -302,6 +301,7 @@ private void start() {
if (wLock == null) {
wLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "oblivion:vpn");
wLock.setReferenceCounted(false);
wLock.acquire();
}

executorService.execute(() -> {
Expand Down Expand Up @@ -357,6 +357,10 @@ public void onCreate() {
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(logRunnable);
if (wLock != null && wLock.isHeld()) {
wLock.release();
wLock = null;
}
}

@Override
Expand All @@ -375,7 +379,7 @@ public void onRevoke() {
e.printStackTrace();
}

if (wLock != null) {
if (wLock != null && wLock.isHeld()) {
wLock.release();
wLock = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gradient"
tools:context="org.bepass.oblivion.BugActivity">
tools:context="org.bepass.oblivion.LogActivity">

<RelativeLayout
android:id="@+id/top_bar"
Expand Down

0 comments on commit b87aff6

Please sign in to comment.