Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Updating ShortCutBadger for Android #646

Merged
merged 4 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ dependencies {
implementation "androidx.appcompat:appcompat:1.0.0"
implementation 'com.facebook.react:react-native:+'
implementation "com.google.android.gms:play-services-gcm:${safeExtGet('googlePlayServicesVersion', '+')}"
implementation 'me.leolin:ShortcutBadger:1.1.8@aar'
implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
implementation "com.google.firebase:firebase-messaging:${safeExtGet('firebaseMessagingVersion', '+')}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@

import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadger;
import me.leolin.shortcutbadger.impl.SamsungHomeBadger;

/**
* Helper for setting application launcher icon badge counts.
* <p>
* This is a wrapper around {@link ShortcutBadger}, with a couple enhancements:
* <p>
* - If the first attempt fails, don't retry. This keeps logs clean, as failed attempts are noisy.
* - Test and apply a separate method for older Samsung devices, which ShortcutBadger has
* (perhaps over-aggressively) deprecated. ref: https://github.com/leolin310148/ShortcutBadger/issues/40
* This is a wrapper around {@link ShortcutBadger}:
*/
public class ApplicationBadgeHelper {

public static final ApplicationBadgeHelper INSTANCE = new ApplicationBadgeHelper();

private static final String LOG_TAG = "ApplicationBadgeHelper";
private static final Badger LEGACY_SAMSUNG_BADGER = new SamsungHomeBadger();

private Boolean applyAutomaticBadger;
private Boolean applySamsungBadger;
private ComponentName componentName;

private ApplicationBadgeHelper() {
Expand All @@ -40,7 +32,6 @@ public void setApplicationIconBadgeNumber(Context context, int number) {
componentName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
}
tryAutomaticBadge(context, number);
tryLegacySamsungBadge(context, number);
}

private void tryAutomaticBadge(Context context, int number) {
Expand All @@ -57,43 +48,4 @@ private void tryAutomaticBadge(Context context, int number) {
}
ShortcutBadger.applyCount(context, number);
}

private void tryLegacySamsungBadge(Context context, int number) {
// First attempt to apply legacy samsung badge. Check if eligible, then attempt it.
if (null == applySamsungBadger) {
applySamsungBadger = isLegacySamsungLauncher(context) && applyLegacySamsungBadge(context, number);
if (applySamsungBadger) {
FLog.i(LOG_TAG, "First attempt to use legacy Samsung badger succeeded; permanently enabling method.");
} else {
FLog.w(LOG_TAG, "First attempt to use legacy Samsung badger failed; permanently disabling method.");
}
return;
} else if (!applySamsungBadger) {
return;
}
applyLegacySamsungBadge(context, number);
}

private boolean isLegacySamsungLauncher(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

if (resolveInfo == null || resolveInfo.activityInfo.name.toLowerCase().contains("resolver")) {
return false;
}

String currentHomePackage = resolveInfo.activityInfo.packageName;
return LEGACY_SAMSUNG_BADGER.getSupportLaunchers().contains(currentHomePackage);
}

private boolean applyLegacySamsungBadge(Context context, int number) {
try {
LEGACY_SAMSUNG_BADGER.executeBadge(context, componentName, number);
return true;
} catch (Exception e) {
FLog.w(LOG_TAG, "Legacy Samsung badger failed", e);
return false;
}
}
}