Skip to content

Commit

Permalink
Chore: migrate to MagicLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr328 committed Nov 28, 2021
1 parent e8446b4 commit a224ead
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 361 deletions.
12 changes: 6 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ subprojects {
extensions.configure<BaseExtension> {
val minSdkVersion = 29
val targetSdkVersion = 31
val buildVersionName = "v11"
val buildVersionCode = 11
val buildVersionName = "v12"
val buildVersionCode = 12

compileSdkVersion(targetSdkVersion)

Expand All @@ -44,11 +44,11 @@ subprojects {
applicationId = "com.github.kr328.clipboard"
}

minSdk = 29
targetSdk = 32
minSdk = minSdkVersion
targetSdk = targetSdkVersion

versionName = "v11"
versionCode = 11
versionName = buildVersionName
versionCode = buildVersionCode

if (!isApp) {
consumerProguardFiles("consumer-rules.pro")
Expand Down
8 changes: 7 additions & 1 deletion hideapi/src/main/java/android/content/IClipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.RemoteException;

public interface IClipboard {
public interface IClipboard extends IInterface {
ClipData getPrimaryClip(String pkg, int userId) throws RemoteException;

ClipDescription getPrimaryClipDescription(String callingPackage, int userId) throws RemoteException;
Expand All @@ -23,5 +24,10 @@ abstract class Stub extends Binder implements IClipboard {
public static IClipboard asInterface(IBinder binder) {
throw new IllegalArgumentException("Stub!");
}

@Override
public IBinder asBinder() {
throw new IllegalArgumentException("Stub");
}
}
}
4 changes: 0 additions & 4 deletions hideapi/src/main/java/android/os/IServiceManager.java

This file was deleted.

29 changes: 0 additions & 29 deletions hideapi/src/main/java/android/os/ShellCallback.java

This file was deleted.

1 change: 1 addition & 0 deletions module/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ dependencies {
implementation(project(":shared"))

implementation(deps.refine.runtime)
implementation(deps.magic.library)
}
22 changes: 11 additions & 11 deletions module/src/main/java/com/github/kr328/clipboard/ClipboardProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import com.github.kr328.clipboard.shared.Constants;
import com.github.kr328.clipboard.shared.Log;
import com.github.kr328.clipboard.util.BinderUtil;
import com.github.kr328.clipboard.util.ProxyFactory.TransactHook;
import com.github.kr328.magic.proxy.AIDLProxy.TransactProxy;
import com.github.kr328.magic.util.BinderUtils;

import dev.rikka.tools.refine.Refine;

Expand All @@ -32,55 +32,55 @@ public ClipboardProxy(IClipboard original) {
}

@Override
@TransactHook
@TransactProxy
public ClipData getPrimaryClip(String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

return original.getPrimaryClip(packageName, userId);
}

@Override
@TransactHook
@TransactProxy
public ClipDescription getPrimaryClipDescription(String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

return original.getPrimaryClipDescription(packageName, userId);
}

@Override
@TransactHook
@TransactProxy
public boolean hasPrimaryClip(String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

return original.hasPrimaryClip(packageName, userId);
}

@Override
@TransactHook
@TransactProxy
public boolean hasClipboardText(String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

return original.hasClipboardText(packageName, userId);
}

@Override
@TransactHook
@TransactProxy
public void addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener, String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

original.addPrimaryClipChangedListener(listener, packageName, userId);
}

@Override
@TransactHook
@TransactProxy
public void removePrimaryClipChangedListener(IOnPrimaryClipChangedListener listener, String callingPackage, int userId) throws RemoteException {
final String packageName = asDefaultIME(callingPackage, userId);

original.removePrimaryClipChangedListener(listener, packageName, userId);
}

@Override
@TransactHook(Constants.TRANSACT_CODE_GET_SERVICE)
@TransactProxy(Constants.TRANSACT_CODE_GET_SERVICE)
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
if (Constants.TRANSACT_CODE_GET_SERVICE == code) {
final Context context = ActivityThread.currentActivityThread().getSystemContext();
Expand Down Expand Up @@ -122,7 +122,7 @@ private String asDefaultIME(String callingPkg, int userId) {
if (pm.getPackageUidAsUser(callingPkg, userId) != Binder.getCallingUid())
return callingPkg;

final String componentName = BinderUtil.withEvaluated(() ->
final String componentName = BinderUtils.withEvaluated(() ->
SettingsSecure.getStringForUser(
context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,
Expand All @@ -135,7 +135,7 @@ private String asDefaultIME(String callingPkg, int userId) {

final String packageName = ComponentName.unflattenFromString(componentName).getPackageName();

final int targetUid = BinderUtil.withEvaluated(() -> pm.getPackageUidAsUser(packageName, userId));
final int targetUid = BinderUtils.withEvaluated(() -> pm.getPackageUidAsUser(packageName, userId));

final long token = Binder.clearCallingIdentity();
final long newToken = token & 0xFFFFFFFFL | ((long) targetUid << 32);
Expand Down
27 changes: 14 additions & 13 deletions module/src/main/java/com/github/kr328/clipboard/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import android.content.Context;
import android.content.IClipboard;
import android.os.Binder;
import android.os.IBinder;
import android.os.Process;

import com.github.kr328.clipboard.shared.Log;
import com.github.kr328.clipboard.util.ProxyFactory;
import com.github.kr328.clipboard.util.ServiceProxy;
import com.github.kr328.magic.proxy.AIDLProxy;
import com.github.kr328.magic.proxy.ServiceManagerProxy;
import com.github.kr328.zloader.ZygoteLoader;

import java.util.Properties;

public class Injector extends ServiceProxy {
public class Injector {
@SuppressWarnings("unused")
public static void main(String processName, Properties properties) {
Log.i("Injected into " + processName);
Expand All @@ -24,27 +23,29 @@ public static void main(String processName, Properties properties) {
return;
}

Injector injector = new Injector();

try {
injector.install();
new ServiceManagerProxy.Builder()
.setAddServiceFilter(Injector::replaceService)
.build()
.install();

Log.i("Inject successfully");
} catch (Exception e) {
} catch (Throwable e) {
Log.e("Inject: " + e, e);
}
}

@Override
protected IBinder onAddService(String name, IBinder service) {
private static Binder replaceService(String name, Binder service) {
if (Context.CLIPBOARD_SERVICE.equals(name)) {
try {
return ProxyFactory.instance((Binder) service, new ClipboardProxy(IClipboard.Stub.asInterface(service)));
} catch (Exception e) {
final IClipboard original = IClipboard.Stub.asInterface(service);

return AIDLProxy.newServer(IClipboard.class, original, new ClipboardProxy(original));
} catch (Throwable e) {
Log.e("Proxy clipboard: " + e, e);
}
}

return super.onAddService(name, service);
return service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.github.kr328.clipboard.shared.Constants;
import com.github.kr328.clipboard.shared.IClipboardWhitelist;
import com.github.kr328.clipboard.shared.Log;
import com.github.kr328.clipboard.util.BinderUtil;
import com.github.kr328.magic.util.BinderUtils;

public class WhitelistService extends IClipboardWhitelist.Stub {
@Override
Expand Down Expand Up @@ -37,7 +37,7 @@ public void removePackage(String packageName) {

private void forceStopPackage(String packageName) {
try {
BinderUtil.withEvaluated(() -> {
BinderUtils.withEvaluated(() -> {
final ActivityThread thread = ActivityThread.currentActivityThread();
if (thread == null)
throw new IllegalStateException("System unavailable");
Expand Down

This file was deleted.

Loading

0 comments on commit a224ead

Please sign in to comment.