Skip to content

Commit 603b0de

Browse files
authored
Merge pull request #1557 from OneSignal/upgrade/badges_for_oppo
Remove Runtime.getRuntime().exec from OPPO Badge implementation
2 parents ad3e83a + 6173b33 commit 603b0de

File tree

2 files changed

+60
-124
lines changed

2 files changed

+60
-124
lines changed
Lines changed: 33 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
// Subpackaged to prevent conflicts with other plugins
21
package com.onesignal.shortcutbadger.impl;
32

43
import android.annotation.TargetApi;
54
import android.content.ComponentName;
65
import android.content.Context;
76
import android.content.Intent;
7+
import android.content.pm.ProviderInfo;
88
import android.net.Uri;
99
import android.os.Build;
1010
import android.os.Bundle;
11+
import java.util.Collections;
12+
import java.util.List;
1113

1214
import com.onesignal.shortcutbadger.Badger;
1315
import com.onesignal.shortcutbadger.ShortcutBadgeException;
1416
import com.onesignal.shortcutbadger.util.BroadcastHelper;
15-
import com.onesignal.shortcutbadger.util.CloseHelper;
16-
17-
import java.io.BufferedReader;
18-
import java.io.IOException;
19-
import java.io.InputStreamReader;
20-
import java.lang.reflect.InvocationTargetException;
21-
import java.lang.reflect.Method;
22-
import java.util.Collections;
23-
import java.util.List;
2417

2518
/**
2619
* Created by NingSo on 2016/10/14.上午10:09
2720
*
2821
* @author: NingSo
2922
* Email: ningso.ping@gmail.com
30-
* <p>
31-
* OPPO R9 not supported
32-
* Version number 6 applies only to chat-type apps
3323
*/
3424

3525
public class OPPOHomeBader implements Badger {
@@ -40,31 +30,18 @@ public class OPPOHomeBader implements Badger {
4030
private static final String INTENT_EXTRA_BADGE_COUNT = "number";
4131
private static final String INTENT_EXTRA_BADGE_UPGRADENUMBER = "upgradeNumber";
4232
private static final String INTENT_EXTRA_BADGEUPGRADE_COUNT = "app_badge_count";
43-
private static int ROMVERSION = -1;
33+
private int mCurrentTotalCount = -1;
4434

45-
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
4635
@Override
4736
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
48-
if (badgeCount == 0) {
49-
badgeCount = -1;
37+
if (mCurrentTotalCount == badgeCount) {
38+
return;
5039
}
51-
Intent intent = new Intent(INTENT_ACTION);
52-
intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
53-
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
54-
intent.putExtra(INTENT_EXTRA_BADGE_UPGRADENUMBER, badgeCount);
55-
if (BroadcastHelper.canResolveBroadcast(context, intent)) {
56-
context.sendBroadcast(intent);
40+
mCurrentTotalCount = badgeCount;
41+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
42+
executeBadgeByContentProvider(context, badgeCount);
5743
} else {
58-
int version = getSupportVersion();
59-
if (version == 6) {
60-
try {
61-
Bundle extras = new Bundle();
62-
extras.putInt(INTENT_EXTRA_BADGEUPGRADE_COUNT, badgeCount);
63-
context.getContentResolver().call(Uri.parse(PROVIDER_CONTENT_URI), "setAppBadgeCount", null, extras);
64-
} catch (Throwable th) {
65-
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
66-
}
67-
}
44+
executeBadgeByBroadcast(context, componentName, badgeCount);
6845
}
6946
}
7047

@@ -73,101 +50,33 @@ public List<String> getSupportLaunchers() {
7350
return Collections.singletonList("com.oppo.launcher");
7451
}
7552

76-
private int getSupportVersion() {
77-
int i = ROMVERSION;
78-
if (i >= 0) {
79-
return ROMVERSION;
80-
}
81-
try {
82-
i = ((Integer) executeClassLoad(getClass("com.color.os.ColorBuild"), "getColorOSVERSION", null, null)).intValue();
83-
} catch (Exception e) {
84-
i = 0;
85-
}
86-
if (i == 0) {
87-
try {
88-
String str = getSystemProperty("ro.build.version.opporom");
89-
if (str.startsWith("V1.4")) {
90-
return 3;
91-
}
92-
if (str.startsWith("V2.0")) {
93-
return 4;
94-
}
95-
if (str.startsWith("V2.1")) {
96-
return 5;
97-
}
98-
} catch (Exception ignored) {
99-
100-
}
101-
}
102-
ROMVERSION = i;
103-
return ROMVERSION;
104-
}
105-
106-
107-
private Object executeClassLoad(Class cls, String str, Class[] clsArr, Object[] objArr) {
108-
Object obj = null;
109-
if (!(cls == null || checkObjExists(str))) {
110-
Method method = getMethod(cls, str, clsArr);
111-
if (method != null) {
112-
method.setAccessible(true);
113-
try {
114-
obj = method.invoke(null, objArr);
115-
} catch (IllegalAccessException e) {
116-
e.printStackTrace();
117-
} catch (InvocationTargetException e) {
118-
e.printStackTrace();
119-
}
120-
}
121-
}
122-
return obj;
123-
}
124-
125-
private Method getMethod(Class cls, String str, Class[] clsArr) {
126-
Method method = null;
127-
if (cls == null || checkObjExists(str)) {
128-
return method;
129-
}
130-
try {
131-
cls.getMethods();
132-
cls.getDeclaredMethods();
133-
return cls.getDeclaredMethod(str, clsArr);
134-
} catch (Exception e) {
135-
try {
136-
return cls.getMethod(str, clsArr);
137-
} catch (Exception e2) {
138-
return cls.getSuperclass() != null ? getMethod(cls.getSuperclass(), str, clsArr) : method;
139-
}
140-
}
141-
}
142-
143-
private Class getClass(String str) {
144-
Class cls = null;
145-
try {
146-
cls = Class.forName(str);
147-
} catch (ClassNotFoundException ignored) {
53+
private void executeBadgeByBroadcast(Context context, ComponentName componentName,
54+
int badgeCount) throws ShortcutBadgeException {
55+
if (badgeCount == 0) {
56+
badgeCount = -1;
14857
}
149-
return cls;
150-
}
151-
58+
Intent intent = new Intent(INTENT_ACTION);
59+
intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
60+
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
61+
intent.putExtra(INTENT_EXTRA_BADGE_UPGRADENUMBER, badgeCount);
15262

153-
private boolean checkObjExists(Object obj) {
154-
return obj == null || obj.toString().equals("") || obj.toString().trim().equals("null");
63+
BroadcastHelper.sendIntentExplicitly(context, intent);
15564
}
15665

157-
158-
private String getSystemProperty(String propName) {
159-
String line;
160-
BufferedReader input = null;
66+
/**
67+
* Send request to OPPO badge content provider to set badge in OPPO home launcher.
68+
*
69+
* @param context the context to use
70+
* @param badgeCount the badge count
71+
*/
72+
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
73+
private void executeBadgeByContentProvider(Context context, int badgeCount) throws ShortcutBadgeException {
16174
try {
162-
Process p = Runtime.getRuntime().exec("getprop " + propName);
163-
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
164-
line = input.readLine();
165-
input.close();
166-
} catch (IOException ex) {
167-
return null;
168-
} finally {
169-
CloseHelper.closeQuietly(input);
75+
Bundle extras = new Bundle();
76+
extras.putInt(INTENT_EXTRA_BADGEUPGRADE_COUNT, badgeCount);
77+
context.getContentResolver().call(Uri.parse(PROVIDER_CONTENT_URI), "setAppBadgeCount", null, extras);
78+
} catch (Throwable ignored) {
79+
throw new ShortcutBadgeException("Unable to execute Badge By Content Provider");
17080
}
171-
return line;
17281
}
173-
}
82+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/util/BroadcastHelper.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import android.content.pm.PackageManager;
66
import android.content.pm.ResolveInfo;
77

8+
import com.onesignal.shortcutbadger.ShortcutBadgeException;
9+
10+
import java.util.Collections;
811
import java.util.List;
912

1013
/**
@@ -16,4 +19,28 @@ public static boolean canResolveBroadcast(Context context, Intent intent) {
1619
List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
1720
return receivers != null && receivers.size() > 0;
1821
}
22+
23+
public static List<ResolveInfo> resolveBroadcast(Context context, Intent intent) {
24+
PackageManager packageManager = context.getPackageManager();
25+
List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
26+
27+
return receivers != null ? receivers : Collections.<ResolveInfo>emptyList();
28+
}
29+
30+
public static void sendIntentExplicitly(Context context, Intent intent) throws ShortcutBadgeException {
31+
List<ResolveInfo> resolveInfos = resolveBroadcast(context, intent);
32+
33+
if (resolveInfos.size() == 0) {
34+
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
35+
}
36+
37+
for (ResolveInfo info : resolveInfos) {
38+
Intent actualIntent = new Intent(intent);
39+
40+
if (info != null) {
41+
actualIntent.setPackage(info.resolvePackageName);
42+
context.sendBroadcast(actualIntent);
43+
}
44+
}
45+
}
1946
}

0 commit comments

Comments
 (0)