Skip to content

Commit ff37eff

Browse files
Merge pull request #653 from BranchMetrics/INTENG-4460/systemobserver_exception
INTENG-4460 -- Protection around getSystemService
2 parents 4a7eff0 + 8db50c2 commit ff37eff

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

Branch-SDK/src/io/branch/referral/SystemObserver.java

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.Manifest;
44
import android.app.UiModeManager;
55
import android.content.Context;
6-
import android.content.pm.ApplicationInfo;
76
import android.content.pm.PackageInfo;
87
import android.content.pm.PackageManager;
98
import android.content.pm.PackageManager.NameNotFoundException;
@@ -15,8 +14,6 @@
1514
import android.view.Display;
1615
import android.view.WindowManager;
1716

18-
import java.io.IOException;
19-
import java.io.InputStream;
2017
import java.lang.reflect.Method;
2118
import java.net.InetAddress;
2219
import java.net.NetworkInterface;
@@ -26,7 +23,6 @@
2623
import java.util.UUID;
2724
import java.util.concurrent.CountDownLatch;
2825
import java.util.concurrent.TimeUnit;
29-
import java.util.jar.JarFile;
3026

3127
import static android.content.Context.UI_MODE_SERVICE;
3228

@@ -112,11 +108,13 @@ boolean hasRealHardwareId() {
112108
*/
113109
String getPackageName() {
114110
String packageName = "";
115-
try {
116-
PackageInfo info = context_.getPackageManager().getPackageInfo(context_.getPackageName(), 0);
117-
packageName = info.packageName;
118-
} catch (NameNotFoundException e) {
119-
e.printStackTrace();
111+
if (context_ != null) {
112+
try {
113+
PackageInfo info = context_.getPackageManager().getPackageInfo(context_.getPackageName(), 0);
114+
packageName = info.packageName;
115+
} catch (NameNotFoundException e) {
116+
e.printStackTrace();
117+
}
120118
}
121119
return packageName;
122120
}
@@ -129,11 +127,12 @@ String getPackageName() {
129127
*/
130128
String getAppVersion() {
131129
try {
132-
PackageInfo packageInfo = context_.getPackageManager().getPackageInfo(context_.getPackageName(), 0);
133-
if (packageInfo.versionName != null)
134-
return packageInfo.versionName;
135-
else
136-
return BLANK;
130+
if (context_ != null) {
131+
PackageInfo packageInfo = context_.getPackageManager().getPackageInfo(context_.getPackageName(), 0);
132+
if (packageInfo.versionName != null) {
133+
return packageInfo.versionName;
134+
}
135+
}
137136
} catch (NameNotFoundException ignored) {
138137
}
139138
return BLANK;
@@ -239,8 +238,13 @@ int getOSVersion() {
239238
*/
240239
DisplayMetrics getScreenDisplay() {
241240
DisplayMetrics displayMetrics = new DisplayMetrics();
242-
Display display = ((WindowManager) context_.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
243-
display.getMetrics(displayMetrics);
241+
if (context_ != null) {
242+
WindowManager windowManager = (WindowManager) context_.getSystemService(Context.WINDOW_SERVICE);
243+
if (windowManager != null) {
244+
Display display = windowManager.getDefaultDisplay();
245+
display.getMetrics(displayMetrics);
246+
}
247+
}
244248
return displayMetrics;
245249
}
246250

@@ -261,9 +265,12 @@ DisplayMetrics getScreenDisplay() {
261265
*/
262266
@SuppressWarnings("MissingPermission")
263267
public boolean getWifiConnected() {
264-
if (PackageManager.PERMISSION_GRANTED == context_.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
268+
if (context_ != null && PackageManager.PERMISSION_GRANTED == context_.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
265269
ConnectivityManager connManager = (ConnectivityManager) context_.getSystemService(Context.CONNECTIVITY_SERVICE);
266-
NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
270+
NetworkInfo wifiInfo = null;
271+
if (connManager != null) {
272+
wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
273+
}
267274
return ((wifiInfo != null) && wifiInfo.isConnected());
268275
}
269276
return false;
@@ -278,11 +285,13 @@ public boolean getWifiConnected() {
278285
*/
279286
private Object getAdInfoObject() {
280287
Object adInfoObj = null;
281-
try {
282-
Class<?> AdvertisingIdClientClass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
283-
Method getAdvertisingIdInfoMethod = AdvertisingIdClientClass.getMethod("getAdvertisingIdInfo", Context.class);
284-
adInfoObj = getAdvertisingIdInfoMethod.invoke(null, context_);
285-
} catch (Throwable ignore) {
288+
if (context_ != null) {
289+
try {
290+
Class<?> AdvertisingIdClientClass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
291+
Method getAdvertisingIdInfoMethod = AdvertisingIdClientClass.getMethod("getAdvertisingIdInfo", Context.class);
292+
adInfoObj = getAdvertisingIdInfoMethod.invoke(null, context_);
293+
} catch (Throwable ignore) {
294+
}
286295
}
287296
return adInfoObj;
288297
}
@@ -429,32 +438,42 @@ static String getLocalIPAddress() {
429438
* {#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}.
430439
*/
431440
String getUIMode() {
432-
String mode;
433-
switch (((UiModeManager) context_.getSystemService(UI_MODE_SERVICE)).getCurrentModeType()) {
434-
case 0:
435-
mode = "UI_MODE_TYPE_UNDEFINED";
436-
break;
437-
case 1:
438-
mode = "UI_MODE_TYPE_NORMAL";
439-
break;
440-
case 2:
441-
mode = "UI_MODE_TYPE_DESK";
442-
break;
443-
case 3:
444-
mode = "UI_MODE_TYPE_CAR";
445-
break;
446-
case 4:
447-
mode = "UI_MODE_TYPE_TELEVISION";
448-
break;
449-
case 5:
450-
mode = "UI_MODE_TYPE_APPLIANCE";
451-
break;
452-
case 6:
453-
mode = "UI_MODE_TYPE_WATCH";
454-
break;
455-
default:
456-
mode = "UI_MODE_TYPE_UNDEFINED";
457-
break;
441+
String mode = "UI_MODE_TYPE_UNDEFINED";
442+
UiModeManager modeManager = null;
443+
444+
try {
445+
if (context_ != null) {
446+
modeManager = (UiModeManager) context_.getSystemService(UI_MODE_SERVICE);
447+
}
448+
449+
if (modeManager != null) {
450+
switch (modeManager.getCurrentModeType()) {
451+
case 1:
452+
mode = "UI_MODE_TYPE_NORMAL";
453+
break;
454+
case 2:
455+
mode = "UI_MODE_TYPE_DESK";
456+
break;
457+
case 3:
458+
mode = "UI_MODE_TYPE_CAR";
459+
break;
460+
case 4:
461+
mode = "UI_MODE_TYPE_TELEVISION";
462+
break;
463+
case 5:
464+
mode = "UI_MODE_TYPE_APPLIANCE";
465+
break;
466+
case 6:
467+
mode = "UI_MODE_TYPE_WATCH";
468+
break;
469+
470+
case 0:
471+
default:
472+
break;
473+
}
474+
}
475+
} catch (Exception e) {
476+
// Have seen reports of "DeadSystemException" from UiModeManager.
458477
}
459478
return mode;
460479
}

0 commit comments

Comments
 (0)