Skip to content

Commit

Permalink
Adapt to Android 12 (API level 31).
Browse files Browse the repository at this point in the history
  • Loading branch information
mosentest authored and heiher committed Jun 30, 2023
1 parent b38c8b0 commit 6701cbe
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
19 changes: 13 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
compileSdkVersion 31

defaultConfig {
applicationId "hev.sockstun"
minSdkVersion 24
targetSdkVersion 24
targetSdkVersion 31
versionCode 1
versionName "1.5"
setProperty("archivesBaseName", "$applicationId-$versionName")
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
abiFilters "armeabi-v7a", "arm64-v8a",'x86','x86_64'
}
}

signingConfigs {
release {
}
release {
}
}

buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
}
debug {
minifyEnabled false
signingConfig signingConfigs.release
}
}

def propsFile = rootProject.file('gradle.properties')
def propsFile = rootProject.file('store.properties')
def configName = 'release'

if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
Expand Down Expand Up @@ -55,3 +59,6 @@ android {
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
}
14 changes: 10 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hev.sockstun">
package="hev.sockstun"
xmlns:tools="http://schemas.android.com/tools">
<application android:label="@string/app_name" android:theme="@android:style/Theme.Holo">
<service android:name=".TProxyService" android:process=":native"
android:permission="android.permission.BIND_VPN_SERVICE">
android:permission="android.permission.BIND_VPN_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
</service>
<receiver android:enabled="true" android:name=".ServiceReceiver">
<receiver android:enabled="true" android:name=".ServiceReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity" android:label="@string/app_name"
android:excludeFromRecents="true">
android:excludeFromRecents="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand All @@ -25,4 +29,6 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
</manifest>
8 changes: 6 additions & 2 deletions app/src/main/java/hev/sockstun/ServiceReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.VpnService;
import android.os.Build;

public class ServiceReceiver extends BroadcastReceiver {
@Override
Expand All @@ -27,8 +28,11 @@ public void onReceive(Context context, Intent intent) {
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
i = new Intent(context, TProxyService.class);
context.startService(i.setAction(TProxyService.ACTION_CONNECT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i.setAction(TProxyService.ACTION_CONNECT));
} else {
context.startService(i.setAction(TProxyService.ACTION_CONNECT));
}
}
}
}
Expand Down
40 changes: 32 additions & 8 deletions app/src/main/java/hev/sockstun/TProxyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.app.Notification;
import android.app.Notification.Builder;
Expand All @@ -20,6 +25,8 @@
import android.net.VpnService;
import android.content.pm.PackageManager.NameNotFoundException;

import androidx.core.app.NotificationCompat;

public class TProxyService extends VpnService {
private static native void TProxyStartService(String config_path, int fd);
private static native void TProxyStopService();
Expand Down Expand Up @@ -144,14 +151,9 @@ public void startService() {
TProxyStartService(tproxy_file.getAbsolutePath(), tunFd.getFd());
prefs.setEnable(true);

Intent i = new Intent(this, TProxyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
Notification notify = new Notification.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentIntent(pi)
.build();
startForeground(1, notify);
String channelName = "socks5";
initNotificationChannel(channelName);
createNotification(channelName);
}

public void stopService() {
Expand All @@ -172,4 +174,26 @@ public void stopService() {

System.exit(0);
}

private void createNotification(String channelName) {
Intent i = new Intent(this, TProxyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelName);
Notification notify = notification
.setContentTitle(getString(R.string.app_name))
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentIntent(pi)
.build();
startForeground(1, notify);
}

// create NotificationChannel
private void initNotificationChannel(String channelName) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
NotificationChannel channel = new NotificationChannel(channelName, name, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
android.enableJetifier=true
android.useAndroidX=true

0 comments on commit 6701cbe

Please sign in to comment.