Fix notification

This commit is contained in:
Nullptr 2023-01-06 23:02:56 +08:00 committed by LoveSy
parent b39c866d97
commit ad463ac1e0
2 changed files with 44 additions and 27 deletions

View File

@ -35,8 +35,8 @@ import java.util.concurrent.ConcurrentHashMap;
import io.github.libxposed.service.IXposedScopeCallback; import io.github.libxposed.service.IXposedScopeCallback;
public class LSPNotificationManager { public class LSPNotificationManager {
private static final String UPDATED_CHANNEL_ID = "lsposed_module_updated"; static final String UPDATED_CHANNEL_ID = "lsposed_module_updated";
private static final String SCOPE_CHANNEL_ID = "lsposed_module_scope"; static final String SCOPE_CHANNEL_ID = "lsposed_module_scope";
private static final String STATUS_CHANNEL_ID = "lsposed_status"; private static final String STATUS_CHANNEL_ID = "lsposed_status";
private static final int STATUS_NOTIFICATION_ID = 2000; private static final int STATUS_NOTIFICATION_ID = 2000;
private static final String opPkg = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? private static final String opPkg = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ?
@ -251,23 +251,6 @@ public class LSPNotificationManager {
} }
} }
static void cancelUpdatedNotification(String modulePackageName, int moduleUserId) {
try {
var idKey = getNotificationIdKey(UPDATED_CHANNEL_ID, modulePackageName, moduleUserId);
var idValue = notificationIds.get(idKey);
if (idValue == null) return;
var nm = getNotificationManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
nm.cancelNotificationWithTag("android", "android", modulePackageName, idValue, 0);
} else {
nm.cancelNotificationWithTag("android", modulePackageName, idValue, 0);
}
notificationIds.remove(idKey);
} catch (RemoteException e) {
Log.e(TAG, "cancel notification", e);
}
}
static void requestModuleScope(String modulePackageName, int moduleUserId, String scopePackageName, IXposedScopeCallback callback) { static void requestModuleScope(String modulePackageName, int moduleUserId, String scopePackageName, IXposedScopeCallback callback) {
try { try {
var context = new FakeContext(); var context = new FakeContext();
@ -315,4 +298,21 @@ public class LSPNotificationManager {
Log.e(TAG, "request module scope", e); Log.e(TAG, "request module scope", e);
} }
} }
static void cancelNotification(String channel, String modulePackageName, int moduleUserId) {
try {
var idKey = getNotificationIdKey(channel, modulePackageName, moduleUserId);
var idValue = notificationIds.get(idKey);
if (idValue == null) return;
var nm = getNotificationManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
nm.cancelNotificationWithTag("android", "android", modulePackageName, idValue, 0);
} else {
nm.cancelNotificationWithTag("android", modulePackageName, idValue, 0);
}
notificationIds.remove(idKey);
} catch (RemoteException e) {
Log.e(TAG, "cancel notification", e);
}
}
} }

View File

@ -20,6 +20,8 @@
package org.lsposed.lspd.service; package org.lsposed.lspd.service;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static org.lsposed.lspd.service.LSPNotificationManager.SCOPE_CHANNEL_ID;
import static org.lsposed.lspd.service.LSPNotificationManager.UPDATED_CHANNEL_ID;
import static org.lsposed.lspd.service.PackageService.PER_USER_RANGE; import static org.lsposed.lspd.service.PackageService.PER_USER_RANGE;
import static org.lsposed.lspd.service.ServiceManager.TAG; import static org.lsposed.lspd.service.ServiceManager.TAG;
import static org.lsposed.lspd.service.ServiceManager.getExecutorService; import static org.lsposed.lspd.service.ServiceManager.getExecutorService;
@ -118,13 +120,13 @@ public class LSPosedService extends ILSPosedService.Stub {
broadcastAndShowNotification(moduleName, userId, intent, true); broadcastAndShowNotification(moduleName, userId, intent, true);
} }
if (moduleName != null) { if (moduleName != null) {
LSPNotificationManager.cancelUpdatedNotification(moduleName, userId); LSPNotificationManager.cancelNotification(UPDATED_CHANNEL_ID, moduleName, userId);
} }
break; break;
} }
case Intent.ACTION_PACKAGE_REMOVED: case Intent.ACTION_PACKAGE_REMOVED:
if (moduleName != null) { if (moduleName != null) {
LSPNotificationManager.cancelUpdatedNotification(moduleName, userId); LSPNotificationManager.cancelNotification(UPDATED_CHANNEL_ID, moduleName, userId);
} }
break; break;
case Intent.ACTION_PACKAGE_ADDED: case Intent.ACTION_PACKAGE_ADDED:
@ -242,32 +244,47 @@ public class LSPosedService extends ILSPosedService.Stub {
return; return;
} }
var scopePackageName = data.getPath(); var scopePackageName = data.getPath();
if (scopePackageName == null) return;
scopePackageName = scopePackageName.substring(1);
var action = data.getQueryParameter("action"); var action = data.getQueryParameter("action");
if (scopePackageName == null || action == null) return; if (action == null) return;
var iCallback = IXposedScopeCallback.Stub.asInterface(callback);
try { try {
ApplicationInfo applicationInfo = null;
try {
applicationInfo = PackageService.getApplicationInfo(scopePackageName, 0, userId);
} catch (Throwable ignored) {
}
if (applicationInfo == null) {
iCallback.onScopeRequestFailed(scopePackageName, "Package not found");
return;
}
switch (action) { switch (action) {
case "allow": case "approve":
ConfigManager.getInstance().setModuleScope(packageName, scopePackageName, userId); ConfigManager.getInstance().setModuleScope(packageName, scopePackageName, userId);
IXposedScopeCallback.Stub.asInterface(callback).onScopeRequestApproved(scopePackageName); iCallback.onScopeRequestApproved(scopePackageName);
break; break;
case "deny": case "deny":
IXposedScopeCallback.Stub.asInterface(callback).onScopeRequestDenied(scopePackageName); iCallback.onScopeRequestDenied(scopePackageName);
break; break;
case "delete": case "delete":
IXposedScopeCallback.Stub.asInterface(callback).onScopeRequestTimeout(scopePackageName); iCallback.onScopeRequestTimeout(scopePackageName);
break; break;
case "block": case "block":
// TODO // TODO
break; break;
} }
Log.i(TAG, action + " scope " + scopePackageName + " for " + packageName + " in user " + userId);
} catch (Throwable e) { } catch (Throwable e) {
try { try {
IXposedScopeCallback.Stub.asInterface(callback).onScopeRequestFailed(scopePackageName, e.getMessage()); iCallback.onScopeRequestFailed(scopePackageName, e.getMessage());
} catch (Throwable ignored) { } catch (Throwable ignored) {
// callback died // callback died
} }
} }
LSPNotificationManager.cancelNotification(SCOPE_CHANNEL_ID, packageName, userId);
} }
private void registerReceiver(List<IntentFilter> filters, String requiredPermission, int userId, Consumer<Intent> task) { private void registerReceiver(List<IntentFilter> filters, String requiredPermission, int userId, Consumer<Intent> task) {