Implement RemotePreference on hookee side

This commit is contained in:
Nullptr 2022-12-28 13:48:41 +08:00 committed by LoveSy
parent 39178b1dad
commit 4137282af9
5 changed files with 30 additions and 22 deletions

View File

@ -94,7 +94,11 @@ public class ApplicationServiceClient implements ILSPApplicationService, IBinder
}
@Override
public Bundle requestRemotePreference(String packageName, int userId, IBinder callback) {
public Bundle requestRemotePreference(String packageName, int userId, String group, IBinder callback) {
try {
return service.requestRemotePreference(packageName, userId, group, callback);
} catch (RemoteException | NullPointerException ignored) {
}
return null;
}

View File

@ -44,6 +44,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -56,6 +57,7 @@ import io.github.libxposed.XposedModuleInterface;
public class LSPosedContext extends XposedContext {
public static final String TAG = "LSPosedContext";
public static final int PER_USER_RANGE = 100000;
static final Set<XposedModule> modules = ConcurrentHashMap.newKeySet();
@ -64,6 +66,7 @@ public class LSPosedContext extends XposedContext {
private final Context mBase;
private final String mPackageName;
private final String mApkPath;
private final Map<String, SharedPreferences> mRemotePrefs = new ConcurrentHashMap<>();
LSPosedContext(Context base, String packageName, String apkPath) {
this.mBase = base;
@ -235,7 +238,8 @@ public class LSPosedContext extends XposedContext {
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
throw new AbstractMethodError();
if (name == null) throw new IllegalArgumentException("name must not be null");
return mRemotePrefs.computeIfAbsent(name, __ -> new LSPosedRemotePreference(mPackageName, Process.myPid() / PER_USER_RANGE, name));
}
@Override

View File

@ -1,6 +1,7 @@
package de.robv.android.xposed;
package org.lsposed.lspd.impl;
import static org.lsposed.lspd.core.ApplicationServiceClient.serviceClient;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.RemoteException;
@ -13,12 +14,13 @@ import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import de.robv.android.xposed.XposedBridge;
import io.github.xposed.xposedservice.IXRemotePreferenceCallback;
@SuppressWarnings("unchecked")
public class XRemotePreference implements SharedPreferences {
public class LSPosedRemotePreference implements SharedPreferences {
private Map<String, Object> mMap = new ConcurrentHashMap<>();
private final Map<String, Object> mMap = new ConcurrentHashMap<>();
private static final Object CONTENT = new Object();
final WeakHashMap<OnSharedPreferenceChangeListener, Object> mListeners = new WeakHashMap<>();
@ -27,12 +29,12 @@ public class XRemotePreference implements SharedPreferences {
@Override
synchronized public void onUpdate(Bundle bundle) {
if (bundle.containsKey("map"))
mMap = (ConcurrentHashMap<String, Object>) bundle.getSerializable("map");
mMap.putAll((Map<String, ?>) bundle.getSerializable("map"));
if (bundle.containsKey("diff")) {
for (var key : bundle.getStringArrayList("diff")) {
synchronized (mListeners) {
mListeners.forEach((listener, __) -> {
listener.onSharedPreferenceChanged(XRemotePreference.this, key);
listener.onSharedPreferenceChanged(LSPosedRemotePreference.this, key);
});
}
}
@ -40,13 +42,9 @@ public class XRemotePreference implements SharedPreferences {
}
};
public XRemotePreference(String packageName) {
this(packageName, 0);
}
public XRemotePreference(String packageName, int userId) {
public LSPosedRemotePreference(String packageName, int userId, String group) {
try {
Bundle output = serviceClient.requestRemotePreference(packageName, userId, callback.asBinder());
Bundle output = serviceClient.requestRemotePreference(packageName, userId, group, callback.asBinder());
callback.onUpdate(output);
} catch (RemoteException e) {
XposedBridge.log(e);
@ -124,6 +122,5 @@ public class XRemotePreference implements SharedPreferences {
synchronized (mListeners) {
mListeners.remove(listener);
}
}
}

View File

@ -46,10 +46,10 @@ public class LSPApplicationService extends ILSPApplicationService.Stub {
private final static Map<Pair<Integer, Integer>, ProcessInfo> processes = new ConcurrentHashMap<>();
static class ProcessInfo implements DeathRecipient {
int uid;
int pid;
String processName;
IBinder heartBeat;
final int uid;
final int pid;
final String processName;
final IBinder heartBeat;
ProcessInfo(int uid, int pid, String processName, IBinder heartBeat) throws RemoteException {
this.uid = uid;
@ -96,7 +96,7 @@ public class LSPApplicationService extends ILSPApplicationService.Stub {
var obfuscation = ConfigManager.getInstance().dexObfuscate();
var signatures = ObfuscationManager.getSignatures();
reply.writeInt(signatures.size() * 2);
for(Map.Entry<String,String> entry : signatures.entrySet()){
for (Map.Entry<String, String> entry : signatures.entrySet()) {
reply.writeString(entry.getKey());
// return val = key if obfuscation disabled
reply.writeString(obfuscation ? entry.getValue() : entry.getKey());
@ -143,9 +143,12 @@ public class LSPApplicationService extends ILSPApplicationService.Stub {
}
@Override
public Bundle requestRemotePreference(String packageName, int userId, IBinder callback) throws RemoteException {
public Bundle requestRemotePreference(String packageName, int userId, String group, IBinder callback) throws RemoteException {
ensureRegistered();
return null;
// TODO: Handle callback
var bundle = new Bundle();
bundle.putSerializable("map", ConfigManager.getInstance().getModulePrefs(packageName, userId, group));
return bundle;
}
@Override

View File

@ -11,7 +11,7 @@ interface ILSPApplicationService {
String getPrefsPath(String packageName);
Bundle requestRemotePreference(String packageName, int userId, IBinder callback);
Bundle requestRemotePreference(String packageName, int userId, String group, IBinder callback);
ParcelFileDescriptor requestInjectedManagerBinder(out List<IBinder> binder);
}