From 85b8fa17fb45518bba10a1438105b163348edb92 Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Sun, 24 Nov 2024 17:22:12 +0100 Subject: [PATCH] Set provider of FakeContext as a workaround The ContentProvider of FakeContext is tricky, being null or not, it can always break notification construction in some devices. Close #79 as fixed. --- .../lsposed/lspd/service/ServiceManager.java | 33 +++++++++++++------ .../org/lsposed/lspd/util/FakeContext.java | 10 ++++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/daemon/src/main/java/org/lsposed/lspd/service/ServiceManager.java b/daemon/src/main/java/org/lsposed/lspd/service/ServiceManager.java index c0d564f0..1e890081 100644 --- a/daemon/src/main/java/org/lsposed/lspd/service/ServiceManager.java +++ b/daemon/src/main/java/org/lsposed/lspd/service/ServiceManager.java @@ -20,6 +20,7 @@ package org.lsposed.lspd.service; import android.app.ActivityThread; +import android.app.Notification; import android.content.Context; import android.ddm.DdmHandleAppName; import android.os.Binder; @@ -39,8 +40,10 @@ import androidx.annotation.RequiresApi; import com.android.internal.os.BinderInternal; import org.lsposed.daemon.BuildConfig; +import org.lsposed.lspd.util.FakeContext; import java.io.File; +import java.lang.AbstractMethodError; import java.lang.Class; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -152,8 +155,7 @@ public class ServiceManager { ConfigFileManager.reloadConfiguration(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) - notificationWorkaround(); + notificationWorkaround(); BridgeService.send(mainService, new BridgeService.Listener() { @Override @@ -242,15 +244,26 @@ public class ServiceManager { } private static void notificationWorkaround() { - try { - Class feature = Class.forName("android.app.FeatureFlagsImpl"); - Field systemui_is_cached = feature.getDeclaredField("systemui_is_cached"); - systemui_is_cached.setAccessible(true); - systemui_is_cached.set(null, true); - Log.d(TAG, "set flag systemui_is_cached to true"); - } catch (Throwable e) { - Log.e(TAG, "failed to change feature flags", e); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + try { + Class feature = Class.forName("android.app.FeatureFlagsImpl"); + Field systemui_is_cached = feature.getDeclaredField("systemui_is_cached"); + systemui_is_cached.setAccessible(true); + systemui_is_cached.set(null, true); + Log.d(TAG, "set flag systemui_is_cached to true"); + } catch (Throwable e) { + Log.e(TAG, "failed to change feature flags", e); + } } + + try { + new Notification.Builder(new FakeContext(), "notification_workaround").build(); + } catch (AbstractMethodError e) { + FakeContext.nullProvider = ! FakeContext.nullProvider; + } catch (Throwable e) { + Log.e(TAG, "failed to build notifications", e); + } + } private static class BinderProxy extends Binder { diff --git a/daemon/src/main/java/org/lsposed/lspd/util/FakeContext.java b/daemon/src/main/java/org/lsposed/lspd/util/FakeContext.java index 69b07e5c..ce5d5a3a 100644 --- a/daemon/src/main/java/org/lsposed/lspd/util/FakeContext.java +++ b/daemon/src/main/java/org/lsposed/lspd/util/FakeContext.java @@ -21,6 +21,9 @@ import hidden.HiddenApiBridge; public class FakeContext extends ContextWrapper { static ApplicationInfo systemApplicationInfo = null; static Resources.Theme theme = null; + + public static Boolean nullProvider = false; + private String packageName = "android"; public FakeContext() { super(null); @@ -59,8 +62,11 @@ public class FakeContext extends ContextWrapper { @Override public ContentResolver getContentResolver() { - return new ContentResolver(this) { - }; + if (nullProvider) { + return null; + } else { + return new ContentResolver(this) {}; + } } public int getUserId() {