Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit aa8e577

Browse files
committed
Modify the method name prefix from obtain to get, optimize code
1 parent d1aa9f3 commit aa8e577

File tree

10 files changed

+177
-210
lines changed

10 files changed

+177
-210
lines changed

lservice/build.gradle.kts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,16 @@ android {
1212
minSdk = 27
1313
}
1414

15-
buildTypes {
16-
release {
17-
isMinifyEnabled = false
18-
}
19-
}
2015
compileOptions {
21-
sourceCompatibility = JavaVersion.VERSION_1_8
22-
targetCompatibility = JavaVersion.VERSION_1_8
16+
sourceCompatibility = JavaVersion.VERSION_17
17+
targetCompatibility = JavaVersion.VERSION_17
2318
}
2419
buildFeatures {
2520
aidl = true
2621
}
2722
}
2823

2924
dependencies {
30-
implementation(libs.androidx.appcompat)
31-
api(libs.libsu.core)
3225
api(libs.libsu.service)
3326
api(libs.rikkax.parcelablelist)
3427
compileOnly(fileTree("libs") { include("*.jar") })

lservice/src/main/AndroidManifest.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

lservice/src/main/aidl/io/github/houvven/lservice/ILSPosedBridgeService.aidl

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ILServiceBridge.aidl
2+
package io.github.houvven.lservice;
3+
4+
import android.os.IBinder;
5+
import org.lsposed.lspd.service.ILSPApplicationService;
6+
7+
// Declare any non-default types here with import statements
8+
9+
interface ILServiceBridge {
10+
11+
IBinder getManagerServiceBinder(ILSPApplicationService applicationService);
12+
13+
IBinder getApplicationServiceBinder();
14+
}

lservice/src/main/java/io/github/houvven/lservice/Constants.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

lservice/src/main/java/io/github/houvven/lservice/LSPosedBridgeRootService.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

lservice/src/main/java/io/github/houvven/lservice/LSPosedBridgeService.java

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.github.houvven.lservice;
2+
3+
import android.content.Context;
4+
import android.content.pm.PackageManager;
5+
import android.os.Binder;
6+
import android.os.IBinder;
7+
import android.os.Parcel;
8+
import android.os.ParcelFileDescriptor;
9+
import android.os.RemoteException;
10+
import android.os.ServiceManager;
11+
import android.system.ErrnoException;
12+
import android.system.Os;
13+
import android.util.Log;
14+
15+
import com.topjohnwu.superuser.ShellUtils;
16+
17+
import org.lsposed.lspd.service.ILSPApplicationService;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.ArrayList;
22+
import java.util.Random;
23+
24+
class LServiceBridge extends ILServiceBridge.Stub {
25+
26+
private final PackageManager pm;
27+
28+
private boolean isInstalledAutomatically = false;
29+
30+
public LServiceBridge(Context ctx, String apkPath) {
31+
this.pm = ctx.getPackageManager();
32+
try {
33+
int uid = getUid(apkPath);
34+
Os.setuid(uid);
35+
if (isInstalledAutomatically) {
36+
ShellUtils.fastCmd(String.format("pm uninstall %s", apkPath));
37+
}
38+
Log.i(TAG, String.format("set uid success, %d", uid));
39+
} catch (ErrnoException | InterruptedException e) {
40+
Log.e(TAG, "set uid failed.", e);
41+
}
42+
}
43+
44+
@Override
45+
public IBinder getManagerServiceBinder(ILSPApplicationService applicationService) throws RemoteException {
46+
ArrayList<IBinder> binders = new ArrayList<>(1);
47+
try (ParcelFileDescriptor descriptor = applicationService.requestInjectedManagerBinder(binders)) {
48+
if (binders.get(0) != null) {
49+
descriptor.detachFd();
50+
}
51+
return binders.get(0);
52+
} catch (IOException e) {
53+
return null;
54+
}
55+
}
56+
57+
@Override
58+
public IBinder getApplicationServiceBinder() throws RemoteException {
59+
Parcel data = Parcel.obtain();
60+
Parcel reply = Parcel.obtain();
61+
try {
62+
IBinder binder = ServiceManager.getService(Context.ACTIVITY_SERVICE);
63+
data.writeInterfaceToken(LSPosed);
64+
data.writeInt(2); // org.lsposed.lspd.service.BridgeService.ACTION#ACTION_GET_BINDER
65+
Random random = new Random();
66+
data.writeString(String.valueOf(random.nextLong()));
67+
data.writeStrongBinder(new Binder());
68+
binder.transact(TRANSACTION_CODE, data, reply, 0);
69+
reply.readException();
70+
return reply.readStrongBinder();
71+
} finally {
72+
data.recycle();
73+
reply.recycle();
74+
}
75+
}
76+
77+
private int getUid(String apkPath) throws InterruptedException {
78+
try {
79+
return pm.getPackageUid(MANAGE_PACKAGE_NAME, 0);
80+
} catch (PackageManager.NameNotFoundException e) {
81+
boolean installed = ShellUtils.fastCmdResult(String.format("pm install -r %s", apkPath));
82+
if (installed) {
83+
isInstalledAutomatically = true;
84+
Thread.sleep(250);
85+
return getUid(apkPath);
86+
}
87+
return -1;
88+
}
89+
}
90+
91+
92+
private static final String TAG = "LServiceBridge";
93+
94+
private static final String LSPosed = "LSPosed";
95+
private static final int TRANSACTION_CODE = 1598837584;
96+
private static final String MANAGE_PACKAGE_NAME = "org.lsposed.manager";
97+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.github.houvven.lservice;
2+
3+
import android.content.ComponentName;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.ServiceConnection;
7+
import android.os.IBinder;
8+
import android.os.RemoteException;
9+
import android.util.Log;
10+
11+
import com.topjohnwu.superuser.ipc.RootService;
12+
13+
import org.lsposed.lspd.ILSPManagerService;
14+
import org.lsposed.lspd.service.ILSPApplicationService;
15+
16+
public class LServiceBridgeRootService extends RootService {
17+
18+
@Override
19+
public IBinder onBind(Intent intent) {
20+
String path = intent.getStringExtra("apkPath");
21+
return new LServiceBridge(this.getApplicationContext(), path).asBinder();
22+
}
23+
24+
public static void bind(Context context, ServiceConnection connection, String apkPath) {
25+
Intent intent = new Intent(context, LServiceBridgeRootService.class);
26+
intent.putExtra("apkPath", apkPath);
27+
RootService.bind(intent, connection);
28+
}
29+
30+
31+
public static class DefaultServiceConnection implements ServiceConnection {
32+
33+
private ILServiceBridge lServiceBridge = null;
34+
35+
public ILServiceBridge getLServiceBridge() {
36+
return this.lServiceBridge;
37+
}
38+
39+
@Override
40+
public void onServiceConnected(ComponentName name, IBinder service) {
41+
Log.i(TAG, String.format("connect: %s", name));
42+
lServiceBridge = LServiceBridge.Stub.asInterface(service);
43+
}
44+
45+
@Override
46+
public void onServiceDisconnected(ComponentName name) {
47+
Log.i(TAG, String.format("disconnect: %s", name));
48+
lServiceBridge = null;
49+
RootService.unbind(this);
50+
}
51+
52+
public ILSPApplicationService getApplicationService() throws RemoteException, NullPointerException {
53+
IBinder binder = lServiceBridge.getApplicationServiceBinder();
54+
return ILSPApplicationService.Stub.asInterface(binder);
55+
}
56+
57+
public ILSPManagerService getManagerService(ILSPApplicationService applicationService) throws RemoteException, NullPointerException {
58+
IBinder binder = lServiceBridge.getManagerServiceBinder(applicationService);
59+
return ILSPManagerService.Stub.asInterface(binder);
60+
}
61+
62+
private static final String TAG = "DefaultLServiceConnection";
63+
}
64+
}

0 commit comments

Comments
 (0)