OSDN Git Service

Change the default behavior when MCC/MNC changes.
authorHolly Jiuyu Sun <jiuyu@google.com>
Thu, 12 Jan 2017 01:34:03 +0000 (17:34 -0800)
committerHolly Jiuyu Sun <jiuyu@google.com>
Fri, 20 Jan 2017 21:50:51 +0000 (13:50 -0800)
By default, we don't restart the activity when MCC/MNC changes
even when they are not set in configChanges. If they want to
restart, set mcc or mnc in the new attribute restartOnConfigChanges.

Bug: 34258948
Test: Test in unit test(testGetActivityConfigChanges() in
      PackageParserTest.java) and on real device with
      changing the SIM card.

Change-Id: Icd6899597c9b8f2e5706e74373a0280d19150092

api/current.txt
api/system-current.txt
api/test-current.txt
core/java/android/content/pm/PackageParser.java
core/res/res/values/attrs_manifest.xml
core/res/res/values/public.xml
core/tests/coretests/src/android/content/pm/PackageParserTest.java

index b9cd7d0..64a935f 100644 (file)
@@ -1052,6 +1052,7 @@ package android {
     field public static final int resizeable = 16843405; // 0x101028d
     field public static final int resizeableActivity = 16844022; // 0x10104f6
     field public static final int resource = 16842789; // 0x1010025
+    field public static final int restartOnConfigChanges = 16844105; // 0x1010549
     field public static final int restoreAnyVersion = 16843450; // 0x10102ba
     field public static final deprecated int restoreNeedsApplication = 16843421; // 0x101029d
     field public static final int restrictedAccountType = 16843733; // 0x10103d5
index 75b0a34..a1684d9 100644 (file)
@@ -1161,6 +1161,7 @@ package android {
     field public static final int resizeable = 16843405; // 0x101028d
     field public static final int resizeableActivity = 16844022; // 0x10104f6
     field public static final int resource = 16842789; // 0x1010025
+    field public static final int restartOnConfigChanges = 16844105; // 0x1010549
     field public static final int restoreAnyVersion = 16843450; // 0x10102ba
     field public static final deprecated int restoreNeedsApplication = 16843421; // 0x101029d
     field public static final int restrictedAccountType = 16843733; // 0x10103d5
index de86022..750f064 100644 (file)
@@ -1052,6 +1052,7 @@ package android {
     field public static final int resizeable = 16843405; // 0x101028d
     field public static final int resizeableActivity = 16844022; // 0x10104f6
     field public static final int resource = 16842789; // 0x1010025
+    field public static final int restartOnConfigChanges = 16844105; // 0x1010549
     field public static final int restoreAnyVersion = 16843450; // 0x10102ba
     field public static final deprecated int restoreNeedsApplication = 16843421; // 0x101029d
     field public static final int restrictedAccountType = 16843733; // 0x10103d5
index 083e4cc..2fdc527 100644 (file)
@@ -179,6 +179,13 @@ public class PackageParser {
     private static final String TAG_PACKAGE = "package";
     private static final String TAG_RESTRICT_UPDATE = "restrict-update";
 
+    /**
+     * Bit mask of all the valid bits that can be set in restartOnConfigChanges.
+     * @hide
+     */
+    private static final int RESTART_ON_CONFIG_CHANGES_MASK =
+            ActivityInfo.CONFIG_MCC | ActivityInfo.CONFIG_MNC;
+
     // These are the tags supported by child packages
     private static final Set<String> CHILD_PACKAGE_TAGS = new ArraySet<>();
     static {
@@ -3855,7 +3862,9 @@ public class PackageParser {
             a.info.maxRecents = sa.getInt(
                     R.styleable.AndroidManifestActivity_maxRecents,
                     ActivityManager.getDefaultAppRecentsLimitStatic());
-            a.info.configChanges = sa.getInt(R.styleable.AndroidManifestActivity_configChanges, 0);
+            a.info.configChanges = getActivityConfigChanges(
+                    sa.getInt(R.styleable.AndroidManifestActivity_configChanges, 0),
+                    sa.getInt(R.styleable.AndroidManifestActivity_restartOnConfigChanges, 0));
             a.info.softInputMode = sa.getInt(
                     R.styleable.AndroidManifestActivity_windowSoftInputMode, 0);
 
@@ -4083,6 +4092,17 @@ public class PackageParser {
         }
     }
 
+    /**
+     * @param configChanges The bit mask of configChanges fetched from AndroidManifest.xml.
+     * @param restartOnConfigChanges The bit mask restartOnConfigChanges fetched from
+     *                               AndroidManifest.xml.
+     * @hide Exposed for unit testing only.
+     */
+    @TestApi
+    public static int getActivityConfigChanges(int configChanges, int restartOnConfigChanges) {
+        return configChanges | ((~restartOnConfigChanges) & RESTART_ON_CONFIG_CHANGES_MASK);
+    }
+
     private void parseLayout(Resources res, AttributeSet attrs, Activity a) {
         TypedArray sw = res.obtainAttributes(attrs,
                 com.android.internal.R.styleable.AndroidManifestLayout);
index b961394..34e2f4f 100644 (file)
         <enum name="locked" value="14" />
     </attr>
 
+    <!-- Specify the configuration changes that trigger the system to restart the
+         current activity if any of these configuration changes happen in the system.
+         The valid configuration changes include mcc and mnc which are the same with
+         those in configChanges. By default from Android O, we don't restart the activity
+         even the app doesn't specify mcc or mnc in configChanges. If the app wants to
+         restart, specify them in restartOnConfigChanges. -->
+    <attr name="restartOnConfigChanges">
+        <!-- The IMSI MCC has changed, that is a SIM has been detected and
+             updated the Mobile Country Code. -->
+        <flag name="mcc" value="0x0001" />
+        <!-- The IMSI MNC has changed, that is a SIM has been detected and
+             updated the Mobile Network Code. -->
+        <flag name="mnc" value="0x0002" />
+    </attr>
+
     <!-- Specify one or more configuration changes that the activity will
          handle itself.  If not specified, the activity will be restarted
          if any of these configuration changes happen in the system.  Otherwise,
          include/utils/ResourceTypes.h. -->
     <attr name="configChanges">
         <!-- The IMSI MCC has changed, that is a SIM has been detected and
-             updated the Mobile Country Code. -->
+             updated the Mobile Country Code. By default from Android O, we
+             don't restart the activity even the app doesn't specify mcc in
+             configChanges. If the app wants to restart, specify mcc in
+             restartOnConfigChanges. -->
         <flag name="mcc" value="0x0001" />
         <!-- The IMSI MNC has changed, that is a SIM has been detected and
-             updated the Mobile Network Code. -->
+             updated the Mobile Network Code. By default from Android O, we
+             don't restart the activity even the app doesn't specify mnc in
+             configChanges. If the app wants to restart, specify mnc in
+             restartOnConfigChanges. -->
         <flag name="mnc" value="0x0002" />
         <!-- The locale has changed, that is the user has selected a new
              language that text should be displayed in. -->
         <attr name="launchMode" />
         <attr name="screenOrientation" />
         <attr name="configChanges" />
+        <attr name="restartOnConfigChanges" />
         <attr name="permission" />
         <attr name="multiprocess" />
         <attr name="process" />
index 064d31e..38c7f4d 100644 (file)
         <public name="appCategory" />
         <public name="autoSizeMaxTextSize" />
         <public name="supportsDismissingWindow" />
+        <public name="restartOnConfigChanges" />
     </public-group>
 
     <public-group type="style" first-id="0x010302e0">
index 2a3c22c..ca4141a 100644 (file)
@@ -215,4 +215,46 @@ public class PackageParserTest {
         // DEV: Released API 20
         verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, true, -1);
     }
+
+    /**
+     * Unit test for PackageParser.getActivityConfigChanges().
+     * If the bit is 1 in the original configChanges, it is still 1 in the final configChanges.
+     * If the bit is 0 in the original configChanges and the bit is not set to 1 in
+     * restartOnConfigChanges, the bit is changed to 1 in the final configChanges by default.
+     */
+    @Test
+    public void testGetActivityConfigChanges() {
+        // Not set in either configChanges or restartOnConfigChanges.
+        int configChanges = 0x0000; // 00000000.
+        int restartOnConfigChanges = 0x0000; // 00000000.
+        int finalConfigChanges =
+                PackageParser.getActivityConfigChanges(configChanges, restartOnConfigChanges);
+        assertEquals(0x0003, finalConfigChanges); // Should be 00000011.
+
+        // Not set in configChanges, but set in restartOnConfigChanges.
+        configChanges = 0x0000; // 00000000.
+        restartOnConfigChanges = 0x0003; // 00000011.
+        finalConfigChanges =
+                PackageParser.getActivityConfigChanges(configChanges, restartOnConfigChanges);
+        assertEquals(0x0000, finalConfigChanges); // Should be 00000000.
+
+        // Set in configChanges.
+        configChanges = 0x0003; // 00000011.
+        restartOnConfigChanges = 0X0000; // 00000000.
+        finalConfigChanges =
+                PackageParser.getActivityConfigChanges(configChanges, restartOnConfigChanges);
+        assertEquals(0x0003, finalConfigChanges); // Should be 00000011.
+
+        restartOnConfigChanges = 0x0003; // 00000011.
+        finalConfigChanges =
+                PackageParser.getActivityConfigChanges(configChanges, restartOnConfigChanges);
+        assertEquals(0x0003, finalConfigChanges); // Should still be 00000011.
+
+        // Other bit set in configChanges.
+        configChanges = 0x0080; // 10000000, orientation.
+        restartOnConfigChanges = 0x0000; // 00000000.
+        finalConfigChanges =
+                PackageParser.getActivityConfigChanges(configChanges, restartOnConfigChanges);
+        assertEquals(0x0083, finalConfigChanges); // Should be 10000011.
+    }
 }