From 4a9fdd9a98363cbdf8ddc51e52d9149c25fc5d0a Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 20 Oct 2024 13:01:14 +0800 Subject: [PATCH] fix: Android 14 register receiver --- .../telegram/messenger/ApplicationLoader.java | 15 ++ .../telegram/ui/DocumentSelectActivity.java | 2 +- .../nekogram/helpers/ApkInstaller.java | 5 +- .../nekogram/helpers/MonetHelper.java | 44 ------ .../nekomimi/nekogram/utils/MonetHelper.java | 137 ------------------ 5 files changed, 17 insertions(+), 186 deletions(-) delete mode 100644 TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/MonetHelper.java diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java index 4f25225acb..5756832d47 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java @@ -36,6 +36,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.core.content.FileProvider; import androidx.multidex.MultiDex; @@ -690,6 +691,20 @@ public boolean openApkInstall(Activity activity, TLRPC.Document document) { return exists; } + @Nullable + public static Intent registerReceiverNotExported(@Nullable BroadcastReceiver receiver, IntentFilter filter) { + return ApplicationLoader.registerReceiverNotExported(ApplicationLoader.applicationContext, receiver, filter); + } + + @Nullable + public static Intent registerReceiverNotExported(Context context, @Nullable BroadcastReceiver receiver, IntentFilter filter) { + if (SDK_INT < 33) { + return context.registerReceiver(receiver, filter); + } else { + return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); + } + } + public boolean showUpdateAppPopup(Context context, TLRPC.TL_help_appUpdate update, int account) { try { (new UpdateAppAlertDialog(context, update, account)).show(); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/DocumentSelectActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/DocumentSelectActivity.java index 1879973e6f..9094c8d421 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/DocumentSelectActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/DocumentSelectActivity.java @@ -263,7 +263,7 @@ public View createView(Context context) { filter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE); filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); filter.addDataScheme("file"); - ApplicationLoader.applicationContext.registerReceiver(receiver, filter); + ApplicationLoader.registerReceiverNotExported(receiver, filter); } actionBar.setBackgroundColor(Theme.getColor(Theme.key_dialogBackground)); diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/ApkInstaller.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/ApkInstaller.java index 81b2efd71a..a38167f71e 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/ApkInstaller.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/ApkInstaller.java @@ -159,10 +159,7 @@ public static void installUpdate(Activity context, TLRPC.Document document) { private static InstallReceiver register(Context context, Runnable onSuccess) { var receiver = new InstallReceiver(context, ApplicationLoader.getApplicationId(), onSuccess); - var filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); - filter.addDataScheme("package"); - context.registerReceiver(receiver, filter); - context.registerReceiver(receiver, new IntentFilter(ApkInstaller.class.getName())); + ApplicationLoader.registerReceiverNotExported(context, receiver, new IntentFilter(ApkInstaller.class.getName())); return receiver; } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/MonetHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/MonetHelper.java index f4f15d744a..11b3425a2d 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/MonetHelper.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/MonetHelper.java @@ -1,28 +1,19 @@ package tw.nekomimi.nekogram.helpers; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; import android.os.Build; -import android.os.PatternMatcher; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.util.Log; import org.telegram.messenger.ApplicationLoader; -import org.telegram.messenger.FileLog; import org.telegram.messenger.R; -import org.telegram.ui.ActionBar.Theme; import java.util.HashMap; @RequiresApi(api = Build.VERSION_CODES.S) public class MonetHelper { private static final HashMap ids = new HashMap<>(); - private static final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED"; - private static final OverlayChangeReceiver overlayChangeReceiver = new OverlayChangeReceiver(); static { ids.put("a1_0", android.R.color.system_accent1_0); @@ -111,39 +102,4 @@ public static int getColor(String color, boolean amoled) { return 0; } } - - private static class OverlayChangeReceiver extends BroadcastReceiver { - - public void register(Context context) { - IntentFilter packageFilter = new IntentFilter(ACTION_OVERLAY_CHANGED); - packageFilter.addDataScheme("package"); - packageFilter.addDataSchemeSpecificPart("android", PatternMatcher.PATTERN_LITERAL); - context.registerReceiver(this, packageFilter); - } - - public void unregister(Context context) { - context.unregisterReceiver(this); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (ACTION_OVERLAY_CHANGED.equals(intent.getAction())) { - if (Theme.getActiveTheme().isMonet()) { - Theme.applyTheme(Theme.getActiveTheme()); - } - } - } - } - - public static void registerReceiver(Context context) { - overlayChangeReceiver.register(context); - } - - public static void unregisterReceiver(Context context) { - try { - overlayChangeReceiver.unregister(context); - } catch (IllegalArgumentException e) { - FileLog.e(e); - } - } } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/MonetHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/MonetHelper.java deleted file mode 100644 index 5194f6fd2c..0000000000 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/MonetHelper.java +++ /dev/null @@ -1,137 +0,0 @@ -package tw.nekomimi.nekogram.utils; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Build; -import android.os.PatternMatcher; - -import androidx.annotation.RequiresApi; - -import com.google.android.exoplayer2.util.Log; - -import org.telegram.messenger.ApplicationLoader; -import org.telegram.ui.ActionBar.Theme; - -import java.util.LinkedHashMap; - -@RequiresApi(api = Build.VERSION_CODES.S) -public class MonetHelper { - private static final LinkedHashMap ids = new LinkedHashMap<>(); - private static final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED"; - private static final OverlayChangeReceiver overlayChangeReceiver = new OverlayChangeReceiver(); - - static { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - ids.put("a1_0", android.R.color.system_accent1_0); - ids.put("a1_10", android.R.color.system_accent1_10); - ids.put("a1_50", android.R.color.system_accent1_50); - ids.put("a1_100", android.R.color.system_accent1_100); - ids.put("a1_200", android.R.color.system_accent1_200); - ids.put("a1_300", android.R.color.system_accent1_300); - ids.put("a1_400", android.R.color.system_accent1_400); - ids.put("a1_500", android.R.color.system_accent1_500); - ids.put("a1_600", android.R.color.system_accent1_600); - ids.put("a1_700", android.R.color.system_accent1_700); - ids.put("a1_800", android.R.color.system_accent1_800); - ids.put("a1_900", android.R.color.system_accent1_900); - ids.put("a1_1000", android.R.color.system_accent1_1000); - ids.put("a2_0", android.R.color.system_accent2_0); - ids.put("a2_10", android.R.color.system_accent2_10); - ids.put("a2_50", android.R.color.system_accent2_50); - ids.put("a2_100", android.R.color.system_accent2_100); - ids.put("a2_200", android.R.color.system_accent2_200); - ids.put("a2_300", android.R.color.system_accent2_300); - ids.put("a2_400", android.R.color.system_accent2_400); - ids.put("a2_500", android.R.color.system_accent2_500); - ids.put("a2_600", android.R.color.system_accent2_600); - ids.put("a2_700", android.R.color.system_accent2_700); - ids.put("a2_800", android.R.color.system_accent2_800); - ids.put("a2_900", android.R.color.system_accent2_900); - ids.put("a2_1000", android.R.color.system_accent2_1000); - ids.put("a3_0", android.R.color.system_accent3_0); - ids.put("a3_10", android.R.color.system_accent3_10); - ids.put("a3_50", android.R.color.system_accent3_50); - ids.put("a3_100", android.R.color.system_accent3_100); - ids.put("a3_200", android.R.color.system_accent3_200); - ids.put("a3_300", android.R.color.system_accent3_300); - ids.put("a3_400", android.R.color.system_accent3_400); - ids.put("a3_500", android.R.color.system_accent3_500); - ids.put("a3_600", android.R.color.system_accent3_600); - ids.put("a3_700", android.R.color.system_accent3_700); - ids.put("a3_800", android.R.color.system_accent3_800); - ids.put("a3_900", android.R.color.system_accent3_900); - ids.put("a3_1000", android.R.color.system_accent3_1000); - ids.put("n1_0", android.R.color.system_neutral1_0); - ids.put("n1_10", android.R.color.system_neutral1_10); - ids.put("n1_50", android.R.color.system_neutral1_50); - ids.put("n1_100", android.R.color.system_neutral1_100); - ids.put("n1_200", android.R.color.system_neutral1_200); - ids.put("n1_300", android.R.color.system_neutral1_300); - ids.put("n1_400", android.R.color.system_neutral1_400); - ids.put("n1_500", android.R.color.system_neutral1_500); - ids.put("n1_600", android.R.color.system_neutral1_600); - ids.put("n1_700", android.R.color.system_neutral1_700); - ids.put("n1_800", android.R.color.system_neutral1_800); - ids.put("n1_900", android.R.color.system_neutral1_900); - ids.put("n1_1000", android.R.color.system_neutral1_1000); - ids.put("n2_0", android.R.color.system_neutral2_0); - ids.put("n2_10", android.R.color.system_neutral2_10); - ids.put("n2_50", android.R.color.system_neutral2_50); - ids.put("n2_100", android.R.color.system_neutral2_100); - ids.put("n2_200", android.R.color.system_neutral2_200); - ids.put("n2_300", android.R.color.system_neutral2_300); - ids.put("n2_400", android.R.color.system_neutral2_400); - ids.put("n2_500", android.R.color.system_neutral2_500); - ids.put("n2_600", android.R.color.system_neutral2_600); - ids.put("n2_700", android.R.color.system_neutral2_700); - ids.put("n2_800", android.R.color.system_neutral2_800); - ids.put("n2_900", android.R.color.system_neutral2_900); - ids.put("n2_1000", android.R.color.system_neutral2_1000); - } - } - - public static int getColor(String color) { - try { - //noinspection ConstantConditions - int id = ids.getOrDefault(color, 0); - return ApplicationLoader.applicationContext.getColor(id); - } catch (Exception e) { - Log.e("Theme", "Error loading color " + color); - e.printStackTrace(); - return 0; - } - } - - private static class OverlayChangeReceiver extends BroadcastReceiver { - - public void register(Context context) { - IntentFilter packageFilter = new IntentFilter(ACTION_OVERLAY_CHANGED); - packageFilter.addDataScheme("package"); - packageFilter.addDataSchemeSpecificPart("android", PatternMatcher.PATTERN_LITERAL); - context.registerReceiver(this, packageFilter); - } - - public void unregister(Context context) { - context.unregisterReceiver(this); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (ACTION_OVERLAY_CHANGED.equals(intent.getAction())) { - if (Theme.getActiveTheme().isMonet()) { - Theme.applyTheme(Theme.getActiveTheme()); - } - } - } - } - - public static void registerReceiver(Context context) { - overlayChangeReceiver.register(context); - } - - public static void unregisterReceiver(Context context) { - overlayChangeReceiver.unregister(context); - } -}