OSDN Git Service

Fix crash in monodroid apps
authorJason Monk <jmonk@google.com>
Fri, 14 Jul 2017 14:22:36 +0000 (10:22 -0400)
committerJason Monk <jmonk@google.com>
Fri, 14 Jul 2017 17:23:58 +0000 (13:23 -0400)
Don't getApplicationContext if we already have an application.

Test: launch a monodroid app
Change-Id: I0c1558463053de3db0f1cd4693088e7c639ce5fa
Fixes: 63666480

core/java/android/app/ActivityThread.java

index 204df63..abf48a8 100644 (file)
@@ -6480,9 +6480,9 @@ public final class ActivityThread {
     private <T> T instantiate(ClassLoader cl, String className, Context c,
             Instantiator<T> instantiator)
             throws ClassNotFoundException, IllegalAccessException, InstantiationException {
-        if (c.getApplicationContext() instanceof Application) {
-            T a = instantiator.instantiate((Application) c.getApplicationContext(),
-                    cl, className);
+        Application app = getApp(c);
+        if (app != null) {
+            T a = instantiator.instantiate(app, cl, className);
             if (a != null) return a;
         }
         return (T) cl.loadClass(className).newInstance();
@@ -6491,14 +6491,25 @@ public final class ActivityThread {
     private <T> T instantiate(ClassLoader cl, String className, Intent intent, Context c,
             IntentInstantiator<T> instantiator)
             throws ClassNotFoundException, IllegalAccessException, InstantiationException {
-        if (c.getApplicationContext() instanceof Application) {
-            T a = instantiator.instantiate((Application) c.getApplicationContext(),
-                    cl, className, intent);
+        Application app = getApp(c);
+        if (app != null) {
+            T a = instantiator.instantiate(app, cl, className, intent);
             if (a != null) return a;
         }
         return (T) cl.loadClass(className).newInstance();
     }
 
+    private Application getApp(Context c) {
+        // We need this shortcut to avoid actually calling getApplicationContext() on an Application
+        // because the Application may not return itself for getApplicationContext() because the
+        // API doesn't enforce it.
+        if (c instanceof Application) return (Application) c;
+        if (c.getApplicationContext() instanceof Application) {
+            return (Application) c.getApplicationContext();
+        }
+        return null;
+    }
+
     private interface Instantiator<T> {
         T instantiate(Application app, ClassLoader cl, String className)
                 throws ClassNotFoundException, IllegalAccessException, InstantiationException;