46
46
import org .json .JSONException ;
47
47
import org .json .JSONObject ;
48
48
49
+ import java .io .InputStream ;
49
50
import java .security .MessageDigest ;
50
51
import java .util .Locale ;
52
+ import java .util .Scanner ;
51
53
import java .util .UUID ;
52
54
import java .util .regex .Pattern ;
53
55
@@ -83,13 +85,23 @@ int initializationChecker(Context context, int deviceType, String oneSignalAppId
83
85
return subscribableStatus ;
84
86
}
85
87
86
-
87
88
static boolean hasFCMLibrary () {
88
- return classExists ("com.google.firebase.messaging.FirebaseMessaging" );
89
+ try {
90
+ // Using class instead of Strings for proguard compatibility
91
+ // noinspection ConstantConditions
92
+ return com .google .firebase .messaging .FirebaseMessaging .class != null ;
93
+ } catch (Throwable e ) {
94
+ return false ;
95
+ }
89
96
}
90
97
91
- static boolean hasGCMLibrary () {
92
- return classExists ("com.google.android.gms.gcm.GoogleCloudMessaging" );
98
+ private static boolean hasGCMLibrary () {
99
+ try {
100
+ // noinspection ConstantConditions
101
+ return com .google .android .gms .gcm .GoogleCloudMessaging .class != null ;
102
+ } catch (Throwable e ) {
103
+ return false ;
104
+ }
93
105
}
94
106
95
107
Integer checkForGooglePushLibrary () {
@@ -110,14 +122,44 @@ Integer checkForGooglePushLibrary() {
110
122
return null ;
111
123
}
112
124
113
- Integer checkAndroidSupportLibrary (Context context ) {
114
- if (!classExists ("android.support.v4.view.MenuCompat" )) {
125
+ private static boolean hasWakefulBroadcastReceiver () {
126
+ try {
127
+ // noinspection ConstantConditions
128
+ return android .support .v4 .content .WakefulBroadcastReceiver .class != null ;
129
+ } catch (Throwable e ) {
130
+ return false ;
131
+ }
132
+ }
133
+
134
+ private static boolean hasNotificationManagerCompat () {
135
+ try {
136
+ // noinspection ConstantConditions
137
+ return android .support .v4 .app .NotificationManagerCompat .class != null ;
138
+ } catch (Throwable e ) {
139
+ return false ;
140
+ }
141
+ }
142
+
143
+ private static boolean hasJobIntentService () {
144
+ try {
145
+ // noinspection ConstantConditions
146
+ return android .support .v4 .app .JobIntentService .class != null ;
147
+ } catch (Throwable e ) {
148
+ return false ;
149
+ }
150
+ }
151
+
152
+ private Integer checkAndroidSupportLibrary (Context context ) {
153
+ String version = getAndroidSupporVersionFromMetaFile ();
154
+ boolean hasWakefulBroadcastReceiver = hasWakefulBroadcastReceiver ();
155
+ boolean hasNotificationManagerCompat = hasNotificationManagerCompat ();
156
+
157
+ if (version == null && !hasWakefulBroadcastReceiver && !hasNotificationManagerCompat ) {
115
158
OneSignal .Log (OneSignal .LOG_LEVEL .FATAL , "Could not find the Android Support Library. Please make sure it has been correctly added to your project." );
116
159
return UserState .PUSH_STATUS_MISSING_ANDROID_SUPPORT_LIBRARY ;
117
160
}
118
161
119
- if (!classExists ("android.support.v4.content.WakefulBroadcastReceiver" ) ||
120
- !classExists ("android.support.v4.app.NotificationManagerCompat" )) {
162
+ if (!hasWakefulBroadcastReceiver || !hasNotificationManagerCompat ) {
121
163
OneSignal .Log (OneSignal .LOG_LEVEL .FATAL , "The included Android Support Library is to old or incomplete. Please update to the 26.0.0 revision or newer." );
122
164
return UserState .PUSH_STATUS_OUTDATED_ANDROID_SUPPORT_LIBRARY ;
123
165
}
@@ -127,7 +169,7 @@ Integer checkAndroidSupportLibrary(Context context) {
127
169
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O
128
170
&& getTargetSdkVersion (context ) >= Build .VERSION_CODES .O ) {
129
171
// Class was added in 26.0.0-beta2
130
- if (!classExists ( "android.support.v4.app.JobIntentService" )) {
172
+ if (!hasJobIntentService ( )) {
131
173
OneSignal .Log (OneSignal .LOG_LEVEL .FATAL , "The included Android Support Library is to old or incomplete. Please update to the 26.0.0 revision or newer." );
132
174
return UserState .PUSH_STATUS_OUTDATED_ANDROID_SUPPORT_LIBRARY ;
133
175
}
@@ -136,6 +178,17 @@ && getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
136
178
return null ;
137
179
}
138
180
181
+ private static String getAndroidSupporVersionFromMetaFile () {
182
+ InputStream inputStream = Object .class .getClassLoader ().getResourceAsStream ("META-INF/com.android.support_support-v4.version" );
183
+ if (inputStream == null )
184
+ return null ;
185
+
186
+ Scanner scanner = new Scanner (inputStream , "UTF-8" );
187
+ String version = scanner .useDelimiter ("\\ A" ).hasNext () ? scanner .next () : null ;
188
+ scanner .close ();
189
+ return version ;
190
+ }
191
+
139
192
int getDeviceType () {
140
193
try {
141
194
// Class only available on the FireOS and only when the following is in the AndroidManifest.xml.
@@ -317,13 +370,4 @@ static void sleep(int ms) {
317
370
e .printStackTrace ();
318
371
}
319
372
}
320
-
321
- static boolean classExists (String classWithNamespace ) {
322
- try {
323
- Class .forName (classWithNamespace );
324
- return true ;
325
- } catch (ClassNotFoundException e ) {
326
- return false ;
327
- }
328
- }
329
373
}
0 commit comments