1
- // Subpackaged to prevent conflicts with other plugins
2
1
package com .onesignal .shortcutbadger .impl ;
3
2
4
3
import android .annotation .TargetApi ;
5
4
import android .content .ComponentName ;
6
5
import android .content .Context ;
7
6
import android .content .Intent ;
7
+ import android .content .pm .ProviderInfo ;
8
8
import android .net .Uri ;
9
9
import android .os .Build ;
10
10
import android .os .Bundle ;
11
+ import java .util .Collections ;
12
+ import java .util .List ;
11
13
12
14
import com .onesignal .shortcutbadger .Badger ;
13
15
import com .onesignal .shortcutbadger .ShortcutBadgeException ;
14
16
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 ;
24
17
25
18
/**
26
19
* Created by NingSo on 2016/10/14.上午10:09
27
20
*
28
21
* @author: NingSo
29
22
* Email: ningso.ping@gmail.com
30
- * <p>
31
- * OPPO R9 not supported
32
- * Version number 6 applies only to chat-type apps
33
23
*/
34
24
35
25
public class OPPOHomeBader implements Badger {
@@ -40,31 +30,18 @@ public class OPPOHomeBader implements Badger {
40
30
private static final String INTENT_EXTRA_BADGE_COUNT = "number" ;
41
31
private static final String INTENT_EXTRA_BADGE_UPGRADENUMBER = "upgradeNumber" ;
42
32
private static final String INTENT_EXTRA_BADGEUPGRADE_COUNT = "app_badge_count" ;
43
- private static int ROMVERSION = -1 ;
33
+ private int mCurrentTotalCount = -1 ;
44
34
45
- @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
46
35
@ Override
47
36
public void executeBadge (Context context , ComponentName componentName , int badgeCount ) throws ShortcutBadgeException {
48
- if (badgeCount == 0 ) {
49
- badgeCount = - 1 ;
37
+ if (mCurrentTotalCount == badgeCount ) {
38
+ return ;
50
39
}
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 );
57
43
} 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 );
68
45
}
69
46
}
70
47
@@ -73,101 +50,33 @@ public List<String> getSupportLaunchers() {
73
50
return Collections .singletonList ("com.oppo.launcher" );
74
51
}
75
52
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 ;
148
57
}
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 );
152
62
153
- private boolean checkObjExists (Object obj ) {
154
- return obj == null || obj .toString ().equals ("" ) || obj .toString ().trim ().equals ("null" );
63
+ BroadcastHelper .sendIntentExplicitly (context , intent );
155
64
}
156
65
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 {
161
74
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" );
170
80
}
171
- return line ;
172
81
}
173
- }
82
+ }
0 commit comments