Skip to content

Commit 6ffa24c

Browse files
committed
Updated to latest ShortcutBadger 1.1.8
* Adds support for the Huawei launcher. * Support for newer Sony launchers.
1 parent 7efb067 commit 6ffa24c

17 files changed

+276
-90
lines changed

OneSignalSDK/onesignal/consumer-proguard-rules.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
-keep class com.onesignal.shortcutbadger.impl.ApexHomeBadger { <init>(...); }
1313
-keep class com.onesignal.shortcutbadger.impl.AsusHomeLauncher { <init>(...); }
1414
-keep class com.onesignal.shortcutbadger.impl.DefaultBadger { <init>(...); }
15+
-keep class com.onesignal.shortcutbadger.impl.HuaweiHomeBadger { <init>(...); }
1516
-keep class com.onesignal.shortcutbadger.impl.NewHtcHomeBadger { <init>(...); }
1617
-keep class com.onesignal.shortcutbadger.impl.NovaHomeBadger { <init>(...); }
1718
-keep class com.onesignal.shortcutbadger.impl.SolidHomeBadger { <init>(...); }

OneSignalSDK/onesignal/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@
2626

2727
<!-- Sony -->
2828
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
29+
<uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE"/>
2930

3031
<!-- Apex -->
3132
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>
3233

3334
<!-- Solid -->
3435
<uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE"/>
36+
37+
<!-- Huawei -->
38+
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
39+
<uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
40+
<uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
41+
3542
<!-- End: ShortcutBadger -->
3643

3744
<application>

OneSignalSDK/onesignal/src/main/java/com/onesignal/BadgeCountUpdater.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ static void update(SQLiteDatabase readableDb, Context context) {
6969
}
7070

7171
static void updateCount(int count, Context context) {
72-
ShortcutBadger.applyCount(context, count);
72+
// Can throw if badges are not support on the device.
73+
// Or app does not have a default launch Activity.
74+
try {
75+
ShortcutBadger.applyCountOrThrow(context, count);
76+
} catch(Throwable t) {}
7377
}
7478
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/Badger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface Badger {
2020

2121
/**
2222
* Called to let {@link ShortcutBadger} knows which launchers are supported by this badger. It should return a
23-
* {@link List<String>} containing supported launchers package names
23+
* @return List containing supported launchers package names
2424
*/
2525
List<String> getSupportLaunchers();
26-
}
26+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// Subpackaged to prevent conflicts with other plugins
22
package com.onesignal.shortcutbadger;
33

4-
@Deprecated
54
public class ShortcutBadgeException extends Exception {
65
public ShortcutBadgeException(String message) {
76
super(message);
87
}
8+
9+
public ShortcutBadgeException(String message, Exception e) {
10+
super(message, e);
11+
}
12+
913
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/ShortcutBadger.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import android.content.pm.ResolveInfo;
1010
import android.os.Build;
1111
import android.util.Log;
12+
1213
import com.onesignal.shortcutbadger.impl.*;
1314

14-
import java.lang.reflect.Constructor;
1515
import java.util.LinkedList;
1616
import java.util.List;
1717

@@ -21,7 +21,7 @@
2121
*/
2222
public final class ShortcutBadger {
2323

24-
private static final String LOG_TAG = ShortcutBadger.class.getSimpleName();
24+
private static final String LOG_TAG = "ShortcutBadger";
2525

2626
private static final List<Class<? extends Badger>> BADGERS = new LinkedList<Class<? extends Badger>>();
2727

@@ -30,18 +30,19 @@ public final class ShortcutBadger {
3030
BADGERS.add(ApexHomeBadger.class);
3131
BADGERS.add(NewHtcHomeBadger.class);
3232
BADGERS.add(NovaHomeBadger.class);
33-
BADGERS.add(SolidHomeBadger.class);
3433
BADGERS.add(SonyHomeBadger.class);
3534
BADGERS.add(XiaomiHomeBadger.class);
3635
BADGERS.add(AsusHomeLauncher.class);
36+
BADGERS.add(HuaweiHomeBadger.class);
3737
}
3838

3939
private static Badger sShortcutBadger;
4040
private static ComponentName sComponentName;
4141

4242
/**
4343
* Tries to update the notification count
44-
* @param context Caller context
44+
*
45+
* @param context Caller context
4546
* @param badgeCount Desired badge count
4647
* @return true in case of success, false otherwise
4748
*/
@@ -50,29 +51,35 @@ public static boolean applyCount(Context context, int badgeCount) {
5051
applyCountOrThrow(context, badgeCount);
5152
return true;
5253
} catch (ShortcutBadgeException e) {
53-
Log.e(LOG_TAG, "Unable to execute badge:" + e.getMessage());
54+
Log.e(LOG_TAG, "Unable to execute badge", e);
5455
return false;
5556
}
5657
}
5758

5859
/**
5960
* Tries to update the notification count, throw a {@link ShortcutBadgeException} if it fails
60-
* @param context Caller context
61+
*
62+
* @param context Caller context
6163
* @param badgeCount Desired badge count
6264
*/
6365
public static void applyCountOrThrow(Context context, int badgeCount) throws ShortcutBadgeException {
64-
if (sShortcutBadger == null)
65-
initBadger(context);
66+
if (sShortcutBadger == null) {
67+
boolean launcherReady = initBadger(context);
68+
69+
if (!launcherReady)
70+
throw new ShortcutBadgeException("No default launcher available");
71+
}
6672

6773
try {
6874
sShortcutBadger.executeBadge(context, sComponentName, badgeCount);
69-
} catch (Throwable e) {
70-
throw new ShortcutBadgeException("Unable to execute badge:" + e.getMessage());
75+
} catch (Exception e) {
76+
throw new ShortcutBadgeException("Unable to execute badge", e);
7177
}
7278
}
7379

7480
/**
7581
* Tries to remove the notification count
82+
*
7683
* @param context Caller context
7784
* @return true in case of success, false otherwise
7885
*/
@@ -82,55 +89,52 @@ public static boolean removeCount(Context context) {
8289

8390
/**
8491
* Tries to remove the notification count, throw a {@link ShortcutBadgeException} if it fails
92+
*
8593
* @param context Caller context
8694
*/
8795
public static void removeCountOrThrow(Context context) throws ShortcutBadgeException {
8896
applyCountOrThrow(context, 0);
8997
}
9098

91-
private static void initBadger(Context context) {
92-
Intent launcherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
99+
// Initialize Badger if a launcher is availalble (eg. set as default on the device)
100+
// Returns true if a launcher is available, in this case, the Badger will be set and sShortcutBadger will be non null.
101+
private static boolean initBadger(Context context) {
93102

94-
// App does not have a launcher intent. (AKA app drawer icon.)
95-
if (launcherIntent == null)
96-
return;
103+
sComponentName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
97104

98-
sComponentName = launcherIntent.getComponent();
105+
Intent intent = new Intent(Intent.ACTION_MAIN);
106+
intent.addCategory(Intent.CATEGORY_HOME);
107+
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
99108

100-
Log.d(LOG_TAG, "Finding badger");
109+
if (resolveInfo == null || resolveInfo.activityInfo.name.toLowerCase().contains("resolver"))
110+
return false;
101111

102-
//find the home launcher Package
103-
try {
104-
Intent intent = new Intent(Intent.ACTION_MAIN);
105-
intent.addCategory(Intent.CATEGORY_HOME);
106-
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
107-
String currentHomePackage = resolveInfo.activityInfo.packageName;
108-
109-
for (Class<? extends Badger> badger : BADGERS) {
110-
Badger shortcutBadger = badger.newInstance();
111-
if (shortcutBadger.getSupportLaunchers().contains(currentHomePackage)) {
112-
sShortcutBadger = shortcutBadger;
113-
break;
114-
}
115-
}
112+
String currentHomePackage = resolveInfo.activityInfo.packageName;
116113

117-
if (sShortcutBadger == null && Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
118-
sShortcutBadger = new XiaomiHomeBadger();
119-
return;
114+
for (Class<? extends Badger> badger : BADGERS) {
115+
Badger shortcutBadger = null;
116+
try {
117+
shortcutBadger = badger.newInstance();
118+
} catch (Exception e) {
119+
}
120+
if (shortcutBadger != null && shortcutBadger.getSupportLaunchers().contains(currentHomePackage)) {
121+
sShortcutBadger = shortcutBadger;
122+
break;
120123
}
121-
} catch (Exception e) {
122-
Log.e(LOG_TAG, e.getMessage(), e);
123124
}
124125

125-
if (sShortcutBadger == null)
126-
sShortcutBadger = new DefaultBadger();
127-
126+
if (sShortcutBadger == null) {
127+
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi"))
128+
sShortcutBadger = new XiaomiHomeBadger();
129+
else
130+
sShortcutBadger = new DefaultBadger();
131+
}
128132

129-
Log.d(LOG_TAG, "Current badger:" + sShortcutBadger.getClass().getCanonicalName());
133+
return true;
130134
}
131135

132136
// Avoid anybody to instantiate this class
133137
private ShortcutBadger() {
134138

135139
}
136-
}
140+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/impl/AdwHomeBadger.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.onesignal.shortcutbadger.Badger;
99
import com.onesignal.shortcutbadger.ShortcutBadgeException;
10-
import com.onesignal.shortcutbadger.ShortcutBadger;
10+
import com.onesignal.shortcutbadger.util.BroadcastHelper;
1111

1212
import java.util.Arrays;
1313
import java.util.List;
@@ -19,22 +19,28 @@ public class AdwHomeBadger implements Badger {
1919

2020
public static final String INTENT_UPDATE_COUNTER = "org.adw.launcher.counter.SEND";
2121
public static final String PACKAGENAME = "PNAME";
22+
public static final String CLASSNAME = "CNAME";
2223
public static final String COUNT = "COUNT";
2324

2425
@Override
2526
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
2627

2728
Intent intent = new Intent(INTENT_UPDATE_COUNTER);
2829
intent.putExtra(PACKAGENAME, componentName.getPackageName());
30+
intent.putExtra(CLASSNAME, componentName.getClassName());
2931
intent.putExtra(COUNT, badgeCount);
30-
context.sendBroadcast(intent);
32+
if(BroadcastHelper.canResolveBroadcast(context, intent)) {
33+
context.sendBroadcast(intent);
34+
} else {
35+
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
36+
}
3137
}
3238

3339
@Override
3440
public List<String> getSupportLaunchers() {
3541
return Arrays.asList(
36-
"org.adw.launcher",
37-
"org.adwfreak.launcher"
42+
"org.adw.launcher",
43+
"org.adwfreak.launcher"
3844
);
3945
}
40-
}
46+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/impl/ApexHomeBadger.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.onesignal.shortcutbadger.Badger;
99
import com.onesignal.shortcutbadger.ShortcutBadgeException;
10-
import com.onesignal.shortcutbadger.ShortcutBadger;
10+
import com.onesignal.shortcutbadger.util.BroadcastHelper;
1111

1212
import java.util.Arrays;
1313
import java.util.List;
@@ -29,7 +29,11 @@ public void executeBadge(Context context, ComponentName componentName, int badge
2929
intent.putExtra(PACKAGENAME, componentName.getPackageName());
3030
intent.putExtra(COUNT, badgeCount);
3131
intent.putExtra(CLASS, componentName.getClassName());
32-
context.sendBroadcast(intent);
32+
if(BroadcastHelper.canResolveBroadcast(context, intent)) {
33+
context.sendBroadcast(intent);
34+
} else {
35+
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
36+
}
3337
}
3438

3539
@Override

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/impl/AsusHomeLauncher.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.onesignal.shortcutbadger.Badger;
99
import com.onesignal.shortcutbadger.ShortcutBadgeException;
10-
import com.onesignal.shortcutbadger.ShortcutBadger;
10+
import com.onesignal.shortcutbadger.util.BroadcastHelper;
1111

1212
import java.util.Arrays;
1313
import java.util.List;
@@ -29,7 +29,11 @@ public void executeBadge(Context context, ComponentName componentName, int badge
2929
intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
3030
intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
3131
intent.putExtra("badge_vip_count", 0);
32-
context.sendBroadcast(intent);
32+
if(BroadcastHelper.canResolveBroadcast(context, intent)) {
33+
context.sendBroadcast(intent);
34+
} else {
35+
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
36+
}
3337
}
3438

3539
@Override

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/impl/DefaultBadger.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.onesignal.shortcutbadger.Badger;
99
import com.onesignal.shortcutbadger.ShortcutBadgeException;
10-
import com.onesignal.shortcutbadger.ShortcutBadger;
10+
import com.onesignal.shortcutbadger.util.BroadcastHelper;
1111

1212
import java.util.ArrayList;
1313
import java.util.List;
@@ -27,7 +27,11 @@ public void executeBadge(Context context, ComponentName componentName, int badge
2727
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
2828
intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
2929
intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
30-
context.sendBroadcast(intent);
30+
if(BroadcastHelper.canResolveBroadcast(context, intent)) {
31+
context.sendBroadcast(intent);
32+
} else {
33+
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
34+
}
3135
}
3236

3337
@Override

0 commit comments

Comments
 (0)