Merge "WifiManager(aidl): Remove pingSupplicant binder call" into oc-dev
diff --git a/api/current.txt b/api/current.txt
index 8d27905..e6363db 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -12695,7 +12695,7 @@
     enum_constant public static final android.graphics.Canvas.VertexMode TRIANGLE_STRIP;
   }
 
-  public class Color {
+  public final class Color {
     ctor public Color();
     method public static int HSVToColor(float[]);
     method public static int HSVToColor(int, float[]);
@@ -12720,6 +12720,7 @@
     method public float getComponent(int);
     method public int getComponentCount();
     method public float[] getComponents();
+    method public float[] getComponents(float[]);
     method public android.graphics.ColorSpace.Model getModel();
     method public float green();
     method public static float green(long);
diff --git a/api/system-current.txt b/api/system-current.txt
index badd31e..2b2502d 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -13421,7 +13421,7 @@
     enum_constant public static final android.graphics.Canvas.VertexMode TRIANGLE_STRIP;
   }
 
-  public class Color {
+  public final class Color {
     ctor public Color();
     method public static int HSVToColor(float[]);
     method public static int HSVToColor(int, float[]);
@@ -13446,6 +13446,7 @@
     method public float getComponent(int);
     method public int getComponentCount();
     method public float[] getComponents();
+    method public float[] getComponents(float[]);
     method public android.graphics.ColorSpace.Model getModel();
     method public float green();
     method public static float green(long);
diff --git a/api/test-current.txt b/api/test-current.txt
index 3a23cd9..b09136e 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -12745,7 +12745,7 @@
     enum_constant public static final android.graphics.Canvas.VertexMode TRIANGLE_STRIP;
   }
 
-  public class Color {
+  public final class Color {
     ctor public Color();
     method public static int HSVToColor(float[]);
     method public static int HSVToColor(int, float[]);
@@ -12770,6 +12770,7 @@
     method public float getComponent(int);
     method public int getComponentCount();
     method public float[] getComponents();
+    method public float[] getComponents(float[]);
     method public android.graphics.ColorSpace.Model getModel();
     method public float green();
     method public static float green(long);
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index b4e6bd5..e89dc0b 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -808,11 +808,6 @@
         public final void scheduleReceiver(Intent intent, ActivityInfo info,
                 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
                 boolean sync, int sendingUser, int processState) {
-            // TODO: Debugging added for bug:36406078 . Remove when done
-            if (Log.isLoggable("36406078", Log.DEBUG)) {
-                Log.d(TAG, "scheduleReceiver");
-            }
-
             updateProcessState(processState, false);
             ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
                     sync, false, mAppThread.asBinder(), sendingUser);
@@ -899,11 +894,6 @@
                 CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                 String buildSerial) {
 
-            // TODO: Debugging added for bug:36406078 . Remove when done
-            if (Log.isLoggable("36406078", Log.DEBUG)) {
-                Log.d(TAG, "bindApplication: " + processName);
-            }
-
             if (services != null) {
                 // Setup the service cache in the ServiceManager
                 ServiceManager.initServiceCache(services);
@@ -3239,10 +3229,6 @@
         if (receiver.getPendingResult() != null) {
             data.finish();
         }
-        // TODO: Debugging added for bug:36406078 . Remove when done
-        if (Log.isLoggable("36406078", Log.DEBUG)) {
-            Log.d(TAG, "handleReceiver done");
-        }
     }
 
     // Instantiate a BackupAgent and tell it that it's alive
@@ -5778,10 +5764,6 @@
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
-        // TODO: Debugging added for bug:36406078 . Remove when done
-        if (Log.isLoggable("36406078", Log.DEBUG)) {
-            Log.d(TAG, "handleBindApplication done");
-        }
     }
 
     /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
diff --git a/core/java/android/app/ApplicationLoaders.java b/core/java/android/app/ApplicationLoaders.java
index ef2db4a..2062930 100644
--- a/core/java/android/app/ApplicationLoaders.java
+++ b/core/java/android/app/ApplicationLoaders.java
@@ -18,6 +18,7 @@
 
 import android.os.Build;
 import android.os.Trace;
+import android.text.TextUtils;
 import android.util.ArrayMap;
 import com.android.internal.os.PathClassLoaderFactory;
 import dalvik.system.PathClassLoader;
@@ -29,8 +30,16 @@
     }
 
     ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled,
-                                      String librarySearchPath, String libraryPermittedPath,
-                                      ClassLoader parent) {
+                               String librarySearchPath, String libraryPermittedPath,
+                               ClassLoader parent) {
+        // For normal usage the cache key used is the same as the zip path.
+        return getClassLoader(zip, targetSdkVersion, isBundled, librarySearchPath,
+                              libraryPermittedPath, parent, zip);
+    }
+
+    private ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled,
+                                       String librarySearchPath, String libraryPermittedPath,
+                                       ClassLoader parent, String cacheKey) {
         /*
          * This is the parent we use if they pass "null" in.  In theory
          * this should be the "system" class loader; in practice we
@@ -50,7 +59,7 @@
              * new ClassLoader for the zip archive.
              */
             if (parent == baseParent) {
-                ClassLoader loader = mLoaders.get(zip);
+                ClassLoader loader = mLoaders.get(cacheKey);
                 if (loader != null) {
                     return loader;
                 }
@@ -71,7 +80,7 @@
                 setupVulkanLayerPath(pathClassloader, librarySearchPath);
                 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
 
-                mLoaders.put(zip, pathClassloader);
+                mLoaders.put(cacheKey, pathClassloader);
                 return pathClassloader;
             }
 
@@ -87,12 +96,16 @@
      * by this class. This is used in the WebView zygote, where its presence in the cache speeds up
      * startup and enables memory sharing.
      */
-    public ClassLoader createAndCacheWebViewClassLoader(String packagePath, String libsPath) {
-      // The correct paths are calculated by WebViewZygote in the system server and passed to
-      // us here. We hardcode the other parameters: WebView always targets the current SDK,
-      // does not need to use non-public system libraries, and uses the base classloader as its
-      // parent to permit usage of the cache.
-      return getClassLoader(packagePath, Build.VERSION.SDK_INT, false, libsPath, null, null);
+    public ClassLoader createAndCacheWebViewClassLoader(String packagePath, String libsPath,
+                                                        String cacheKey) {
+        // The correct paths are calculated by WebViewZygote in the system server and passed to
+        // us here. We hardcode the other parameters: WebView always targets the current SDK,
+        // does not need to use non-public system libraries, and uses the base classloader as its
+        // parent to permit usage of the cache.
+        // The cache key is passed separately to enable the stub WebView to be cached under the
+        // stub's APK path, when the actual package path is the donor APK.
+        return getClassLoader(packagePath, Build.VERSION.SDK_INT, false, libsPath, null, null,
+                              cacheKey);
     }
 
     private static native void setupVulkanLayerPath(ClassLoader classLoader, String librarySearchPath);
diff --git a/core/java/android/content/pm/PackageManagerInternal.java b/core/java/android/content/pm/PackageManagerInternal.java
index 6272822..b3e25d1 100644
--- a/core/java/android/content/pm/PackageManagerInternal.java
+++ b/core/java/android/content/pm/PackageManagerInternal.java
@@ -172,6 +172,7 @@
      * @param packageName The package name.
      * @param userId The user for which to check.
      * @return Whether was launched.
+     * @throws IllegalArgumentException if the package is not found
      */
     public abstract boolean wasPackageEverLaunched(String packageName, int userId);
 
diff --git a/core/java/android/os/ZygoteProcess.java b/core/java/android/os/ZygoteProcess.java
index b3366d8..8208438 100644
--- a/core/java/android/os/ZygoteProcess.java
+++ b/core/java/android/os/ZygoteProcess.java
@@ -487,11 +487,11 @@
      * Instructs the zygote to pre-load the classes and native libraries at the given paths
      * for the specified abi. Not all zygotes support this function.
      */
-    public void preloadPackageForAbi(String packagePath, String libsPath, String abi)
-            throws ZygoteStartFailedEx, IOException {
+    public void preloadPackageForAbi(String packagePath, String libsPath, String cacheKey,
+                                     String abi) throws ZygoteStartFailedEx, IOException {
         synchronized(mLock) {
             ZygoteState state = openZygoteSocketIfNeeded(abi);
-            state.writer.write("3");
+            state.writer.write("4");
             state.writer.newLine();
 
             state.writer.write("--preload-package");
@@ -503,6 +503,9 @@
             state.writer.write(libsPath);
             state.writer.newLine();
 
+            state.writer.write(cacheKey);
+            state.writer.newLine();
+
             state.writer.flush();
         }
     }
diff --git a/core/java/android/service/quicksettings/TileService.java b/core/java/android/service/quicksettings/TileService.java
index 2116847..8e01030 100644
--- a/core/java/android/service/quicksettings/TileService.java
+++ b/core/java/android/service/quicksettings/TileService.java
@@ -433,6 +433,7 @@
     public static final void requestListeningState(Context context, ComponentName component) {
         Intent intent = new Intent(ACTION_REQUEST_LISTENING);
         intent.putExtra(EXTRA_COMPONENT, component);
+        intent.setPackage("com.android.systemui");
         context.sendBroadcast(intent, Manifest.permission.BIND_QUICK_SETTINGS_TILE);
     }
 }
diff --git a/core/java/android/view/inputmethod/InputMethodSubtype.java b/core/java/android/view/inputmethod/InputMethodSubtype.java
index 44309a7..28c2d01 100644
--- a/core/java/android/view/inputmethod/InputMethodSubtype.java
+++ b/core/java/android/view/inputmethod/InputMethodSubtype.java
@@ -520,27 +520,27 @@
     }
 
     private HashMap<String, String> getExtraValueHashMap() {
-        if (mExtraValueHashMapCache == null) {
-            synchronized(this) {
-                if (mExtraValueHashMapCache == null) {
-                    mExtraValueHashMapCache = new HashMap<String, String>();
-                    final String[] pairs = mSubtypeExtraValue.split(EXTRA_VALUE_PAIR_SEPARATOR);
-                    final int N = pairs.length;
-                    for (int i = 0; i < N; ++i) {
-                        final String[] pair = pairs[i].split(EXTRA_VALUE_KEY_VALUE_SEPARATOR);
-                        if (pair.length == 1) {
-                            mExtraValueHashMapCache.put(pair[0], null);
-                        } else if (pair.length > 1) {
-                            if (pair.length > 2) {
-                                Slog.w(TAG, "ExtraValue has two or more '='s");
-                            }
-                            mExtraValueHashMapCache.put(pair[0], pair[1]);
-                        }
+        synchronized (this) {
+            HashMap<String, String> extraValueMap = mExtraValueHashMapCache;
+            if (extraValueMap != null) {
+                return extraValueMap;
+            }
+            extraValueMap = new HashMap<>();
+            final String[] pairs = mSubtypeExtraValue.split(EXTRA_VALUE_PAIR_SEPARATOR);
+            for (int i = 0; i < pairs.length; ++i) {
+                final String[] pair = pairs[i].split(EXTRA_VALUE_KEY_VALUE_SEPARATOR);
+                if (pair.length == 1) {
+                    extraValueMap.put(pair[0], null);
+                } else if (pair.length > 1) {
+                    if (pair.length > 2) {
+                        Slog.w(TAG, "ExtraValue has two or more '='s");
                     }
+                    extraValueMap.put(pair[0], pair[1]);
                 }
             }
+            mExtraValueHashMapCache = extraValueMap;
+            return extraValueMap;
         }
-        return mExtraValueHashMapCache;
     }
 
     /**
diff --git a/core/java/android/webkit/WebViewFactory.java b/core/java/android/webkit/WebViewFactory.java
index 81c2f5d..71db6b1 100644
--- a/core/java/android/webkit/WebViewFactory.java
+++ b/core/java/android/webkit/WebViewFactory.java
@@ -280,6 +280,44 @@
         }
     }
 
+    /**
+     * If the ApplicationInfo provided is for a stub WebView, fix up the object to include the
+     * required values from the donor package. If the ApplicationInfo is for a full WebView,
+     * leave it alone. Throws MissingWebViewPackageException if the donor is missing.
+     */
+    private static void fixupStubApplicationInfo(ApplicationInfo ai, PackageManager pm) {
+        String donorPackageName = null;
+        if (ai.metaData != null) {
+            donorPackageName = ai.metaData.getString("com.android.webview.WebViewDonorPackage");
+        }
+        if (donorPackageName != null) {
+            PackageInfo donorPackage;
+            try {
+                donorPackage = pm.getPackageInfo(
+                        donorPackageName,
+                        PackageManager.GET_SHARED_LIBRARY_FILES
+                        | PackageManager.MATCH_DEBUG_TRIAGED_MISSING
+                        | PackageManager.MATCH_UNINSTALLED_PACKAGES
+                        | PackageManager.MATCH_FACTORY_ONLY);
+            } catch (PackageManager.NameNotFoundException e) {
+                throw new MissingWebViewPackageException("Failed to find donor package: " +
+                                                         donorPackageName);
+            }
+            ApplicationInfo donorInfo = donorPackage.applicationInfo;
+
+            // Replace the stub's code locations with the donor's.
+            ai.sourceDir = donorInfo.sourceDir;
+            ai.splitSourceDirs = donorInfo.splitSourceDirs;
+            ai.nativeLibraryDir = donorInfo.nativeLibraryDir;
+            ai.secondaryNativeLibraryDir = donorInfo.secondaryNativeLibraryDir;
+
+            // Copy the donor's primary and secondary ABIs, since the stub doesn't have native code
+            // and so they are unset.
+            ai.primaryCpuAbi = donorInfo.primaryCpuAbi;
+            ai.secondaryCpuAbi = donorInfo.secondaryCpuAbi;
+        }
+    }
+
     private static Context getWebViewContextAndSetProvider() {
         Application initialApplication = AppGlobals.getInitialApplication();
         try {
@@ -307,9 +345,10 @@
             }
             // Fetch package info and verify it against the chosen package
             PackageInfo newPackageInfo = null;
+            PackageManager pm = initialApplication.getPackageManager();
             Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()");
             try {
-                newPackageInfo = initialApplication.getPackageManager().getPackageInfo(
+                newPackageInfo = pm.getPackageInfo(
                     response.packageInfo.packageName,
                     PackageManager.GET_SHARED_LIBRARY_FILES
                     | PackageManager.MATCH_DEBUG_TRIAGED_MISSING
@@ -328,12 +367,15 @@
             // failure
             verifyPackageInfo(response.packageInfo, newPackageInfo);
 
+            ApplicationInfo ai = newPackageInfo.applicationInfo;
+            fixupStubApplicationInfo(ai, pm);
+
             Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
                     "initialApplication.createApplicationContext");
             try {
                 // Construct an app context to load the Java code into the current app.
                 Context webViewContext = initialApplication.createApplicationContext(
-                        newPackageInfo.applicationInfo,
+                        ai,
                         Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
                 sPackageInfo = newPackageInfo;
                 return webViewContext;
@@ -449,7 +491,11 @@
      */
     public static int onWebViewProviderChanged(PackageInfo packageInfo) {
         String[] nativeLibs = null;
+        String originalSourceDir = packageInfo.applicationInfo.sourceDir;
         try {
+            fixupStubApplicationInfo(packageInfo.applicationInfo,
+                                     AppGlobals.getInitialApplication().getPackageManager());
+
             nativeLibs = WebViewFactory.getWebViewNativeLibraryPaths(packageInfo);
             if (nativeLibs != null) {
                 long newVmSize = 0L;
@@ -498,7 +544,7 @@
             Log.e(LOGTAG, "error preparing webview native library", t);
         }
 
-        WebViewZygote.onWebViewProviderChanged(packageInfo);
+        WebViewZygote.onWebViewProviderChanged(packageInfo, originalSourceDir);
 
         return prepareWebViewInSystemServer(nativeLibs);
     }
diff --git a/core/java/android/webkit/WebViewZygote.java b/core/java/android/webkit/WebViewZygote.java
index f78d622..2123deb 100644
--- a/core/java/android/webkit/WebViewZygote.java
+++ b/core/java/android/webkit/WebViewZygote.java
@@ -67,6 +67,13 @@
     private static PackageInfo sPackage;
 
     /**
+     * Cache key for the selected WebView package's classloader. This is set from
+     * #onWebViewProviderChanged().
+     */
+    @GuardedBy("sLock")
+    private static String sPackageCacheKey;
+
+    /**
      * Flag for whether multi-process WebView is enabled. If this is false, the zygote
      * will not be started.
      */
@@ -118,9 +125,10 @@
         }
     }
 
-    public static void onWebViewProviderChanged(PackageInfo packageInfo) {
+    public static void onWebViewProviderChanged(PackageInfo packageInfo, String cacheKey) {
         synchronized (sLock) {
             sPackage = packageInfo;
+            sPackageCacheKey = cacheKey;
 
             // If multi-process is not enabled, then do not start the zygote service.
             if (!sMultiprocessEnabled) {
@@ -210,7 +218,8 @@
                     TextUtils.join(File.pathSeparator, zipPaths);
 
             Log.d(LOGTAG, "Preloading package " + zip + " " + librarySearchPath);
-            sZygote.preloadPackageForAbi(zip, librarySearchPath, Build.SUPPORTED_ABIS[0]);
+            sZygote.preloadPackageForAbi(zip, librarySearchPath, sPackageCacheKey,
+                                         Build.SUPPORTED_ABIS[0]);
         } catch (Exception e) {
             Log.e(LOGTAG, "Error connecting to " + serviceName, e);
             sZygote = null;
diff --git a/core/java/com/android/internal/os/WebViewZygoteInit.java b/core/java/com/android/internal/os/WebViewZygoteInit.java
index f27c0d4..cc3f58c 100644
--- a/core/java/com/android/internal/os/WebViewZygoteInit.java
+++ b/core/java/com/android/internal/os/WebViewZygoteInit.java
@@ -26,6 +26,7 @@
 import android.webkit.WebViewFactory;
 import android.webkit.WebViewFactoryProvider;
 
+import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 
@@ -67,16 +68,20 @@
         }
 
         @Override
-        protected boolean handlePreloadPackage(String packagePath, String libsPath) {
+        protected boolean handlePreloadPackage(String packagePath, String libsPath,
+                                               String cacheKey) {
             // Ask ApplicationLoaders to create and cache a classloader for the WebView APK so that
             // our children will reuse the same classloader instead of creating their own.
             // This enables us to preload Java and native code in the webview zygote process and
             // have the preloaded versions actually be used post-fork.
             ClassLoader loader = ApplicationLoaders.getDefault().createAndCacheWebViewClassLoader(
-                    packagePath, libsPath);
+                    packagePath, libsPath, cacheKey);
 
             // Add the APK to the Zygote's list of allowed files for children.
-            Zygote.nativeAllowFileAcrossFork(packagePath);
+            String[] packageList = TextUtils.split(packagePath, File.pathSeparator);
+            for (String packageEntry : packageList) {
+                Zygote.nativeAllowFileAcrossFork(packageEntry);
+            }
 
             // Once we have the classloader, look up the WebViewFactoryProvider implementation and
             // call preloadInZygote() on it to give it the opportunity to preload the native library
diff --git a/core/java/com/android/internal/os/ZygoteConnection.java b/core/java/com/android/internal/os/ZygoteConnection.java
index e2485e9..a9bec41 100644
--- a/core/java/com/android/internal/os/ZygoteConnection.java
+++ b/core/java/com/android/internal/os/ZygoteConnection.java
@@ -177,7 +177,7 @@
 
             if (parsedArgs.preloadPackage != null) {
                 return handlePreloadPackage(parsedArgs.preloadPackage,
-                        parsedArgs.preloadPackageLibs);
+                        parsedArgs.preloadPackageLibs, parsedArgs.preloadPackageCacheKey);
             }
 
             if (parsedArgs.permittedCapabilities != 0 || parsedArgs.effectiveCapabilities != 0) {
@@ -314,7 +314,7 @@
         return ZygoteInit.isPreloadComplete();
     }
 
-    protected boolean handlePreloadPackage(String packagePath, String libsPath) {
+    protected boolean handlePreloadPackage(String packagePath, String libsPath, String cacheKey) {
         throw new RuntimeException("Zyogte does not support package preloading");
     }
 
@@ -428,6 +428,7 @@
          */
         String preloadPackage;
         String preloadPackageLibs;
+        String preloadPackageCacheKey;
 
         /**
          * Whether this is a request to start preloading the default resources and classes.
@@ -599,6 +600,7 @@
                 } else if (arg.equals("--preload-package")) {
                     preloadPackage = args[++curArg];
                     preloadPackageLibs = args[++curArg];
+                    preloadPackageCacheKey = args[++curArg];
                 } else if (arg.equals("--preload-default")) {
                     preloadDefault = true;
                 } else {
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 868e256..4afa8dc 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -189,22 +189,23 @@
     <!-- Displayed to tell the user that they cannot change the caller ID setting. -->
     <string name="CLIRPermanent">You can\'t change the caller ID setting.</string>
 
-    <!-- Displayed to tell the user that data service is blocked by access control. -->
-    <string name="RestrictedOnData">Data service is blocked.</string>
-    <!-- Displayed to tell the user that emergency service is blocked by access control. -->
-    <string name="RestrictedOnEmergency">Emergency service is blocked.</string>
-    <!-- Displayed to tell the user that normal service is blocked by access control. -->
-    <string name="RestrictedOnNormal">Voice service is blocked.</string>
-    <!-- Displayed to tell the user that all emergency and normal voice services are blocked by access control. -->
-    <string name="RestrictedOnAllVoice">All voice services are blocked.</string>
-    <!-- Displayed to tell the user that sms service is blocked by access control. -->
-    <string name="RestrictedOnSms">SMS service is blocked.</string>
-    <!-- Displayed to tell the user that voice/data service is blocked by access control. -->
-    <string name="RestrictedOnVoiceData">Voice/data services are blocked.</string>
-    <!-- Displayed to tell the user that voice and sms service are blocked by access control. -->
-    <string name="RestrictedOnVoiceSms">Voice/SMS services are blocked.</string>
-    <!-- Displayed to tell the user that all service is blocked by access control. -->
-    <string name="RestrictedOnAll">All voice/data/SMS services are blocked.</string>
+    <!-- Notification title to tell the user that data service is blocked by access control. -->
+    <string name="RestrictedOnDataTitle">No data service</string>
+    <!-- Notification title to tell the user that emergency service is blocked by access control. -->
+    <string name="RestrictedOnEmergencyTitle">No emergency service</string>
+    <!-- Notification title to tell the user that normal service is blocked by access control. -->
+    <string name="RestrictedOnNormalTitle">No voice service</string>
+    <!-- Notification title to tell the user that all emergency and normal voice services are blocked by access control. -->
+    <string name="RestrictedOnAllVoiceTitle">No voice/emergency service</string>
+
+    <!-- Notification content to tell the user that data service is blocked by access control. -->
+    <string name="RestrictedOnDataContent">Your carrier has temporarily suspended data service at this location</string>
+    <!-- Notification content to tell the user that emergency service is blocked by access control. -->
+    <string name="RestrictedOnEmergencyContent">Your carrier has temporarily suspended emergency calls at this location</string>
+    <!-- Notification content to tell the user that normal service is blocked by access control. -->
+    <string name="RestrictedOnNormalContent">Your carrier has temporarily suspended voice calls at this location</string>
+    <!-- Notification content to tell the user that all emergency and normal voice services are blocked by access control. -->
+    <string name="RestrictedOnAllVoiceContent">Your carrier has temporarily suspended voice and emergency calls at this location</string>
 
     <!-- Displayed to tell the user that they should switch their network preference. -->
     <string name="NetworkPreferenceSwitchTitle">Can\u2019t reach network</string>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index b23c96c..3c4d782 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -507,12 +507,16 @@
   <java-symbol type="string" name="Noon" />
   <java-symbol type="string" name="PinMmi" />
   <java-symbol type="string" name="PwdMmi" />
-  <java-symbol type="string" name="RestrictedOnAllVoice" />
-  <java-symbol type="string" name="RestrictedOnData" />
-  <java-symbol type="string" name="RestrictedOnEmergency" />
-  <java-symbol type="string" name="RestrictedOnNormal" />
   <java-symbol type="string" name="NetworkPreferenceSwitchSummary" />
   <java-symbol type="string" name="NetworkPreferenceSwitchTitle" />
+  <java-symbol type="string" name="RestrictedOnAllVoiceTitle" />
+  <java-symbol type="string" name="RestrictedOnDataTitle" />
+  <java-symbol type="string" name="RestrictedOnEmergencyTitle" />
+  <java-symbol type="string" name="RestrictedOnNormalTitle" />
+  <java-symbol type="string" name="RestrictedOnAllVoiceContent" />
+  <java-symbol type="string" name="RestrictedOnDataContent" />
+  <java-symbol type="string" name="RestrictedOnEmergencyContent" />
+  <java-symbol type="string" name="RestrictedOnNormalContent" />
   <java-symbol type="string" name="SetupCallDefault" />
   <java-symbol type="string" name="accept" />
   <java-symbol type="string" name="activity_chooser_view_see_all" />
diff --git a/graphics/java/android/graphics/Color.java b/graphics/java/android/graphics/Color.java
index 33d19d4..d69f67d 100644
--- a/graphics/java/android/graphics/Color.java
+++ b/graphics/java/android/graphics/Color.java
@@ -22,6 +22,7 @@
 import android.annotation.HalfFloat;
 import android.annotation.IntRange;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.annotation.Size;
 
 import android.util.Half;
@@ -288,7 +289,7 @@
  * and <code>(1.0, 0.0, 0.0, 0.5)</code>.</p>
  */
 @AnyThread
-public class Color {
+public final class Color {
     @ColorInt public static final int BLACK       = 0xFF000000;
     @ColorInt public static final int DKGRAY      = 0xFF444444;
     @ColorInt public static final int GRAY        = 0xFF888888;
@@ -415,7 +416,7 @@
      * to this color space's color model, plus one extra component for
      * alpha.
      *
-     * @return An integer between 4 and 5
+     * @return The integer 4 or 5
      */
     @IntRange(from = 4, to = 5)
     public int getComponentCount() {
@@ -560,7 +561,37 @@
     @NonNull
     @Size(min = 4, max = 5)
     public float[] getComponents() {
-        return Arrays.copyOf(mComponents, mColorSpace.getComponentCount() + 1);
+        return Arrays.copyOf(mComponents, mComponents.length);
+    }
+
+    /**
+     * Copies this color's components in the supplied array. The last element of the
+     * array is always the alpha component.
+     *
+     * @param components An array of floats whose size must be at least
+     *                  {@link #getComponentCount()}, can be null
+     * @return The array passed as a parameter if not null, or a new array of length
+     *         {@link #getComponentCount()}
+     *
+     * @see #getComponent(int)
+     *
+     * @throws IllegalArgumentException If the specified array's length is less than
+     * {@link #getComponentCount()}
+     */
+    @NonNull
+    @Size(min = 4)
+    public float[] getComponents(@Nullable @Size(min = 4) float[] components) {
+        if (components == null) {
+            return Arrays.copyOf(mComponents, mComponents.length);
+        }
+
+        if (components.length < mComponents.length) {
+            throw new IllegalArgumentException("The specified array's length must be at "
+                    + "least " + mComponents.length);
+        }
+
+        System.arraycopy(mComponents, 0, components, 0, mComponents.length);
+        return components;
     }
 
     /**
@@ -570,7 +601,7 @@
      *
      * <p>If the requested component index is {@link #getComponentCount()},
      * this method returns the alpha component, always in the range
-     * \([0..1\).</p>
+     * \([0..1]\).</p>
      *
      * @see #getComponents()
      *
diff --git a/packages/SystemUI/res/layout/qs_detail.xml b/packages/SystemUI/res/layout/qs_detail.xml
index 25d5226..1c087b3 100644
--- a/packages/SystemUI/res/layout/qs_detail.xml
+++ b/packages/SystemUI/res/layout/qs_detail.xml
@@ -43,6 +43,8 @@
         android:alpha="0"
         android:background="@color/qs_detail_progress_track"
         android:src="@drawable/indeterminate_anim"
+        android:scaleType="fitXY"
+        android:translationY="16dp"
         />
 
     <com.android.systemui.qs.NonInterceptingScrollView
@@ -57,6 +59,6 @@
             android:layout_height="match_parent"/>
     </com.android.systemui.qs.NonInterceptingScrollView>
 
-    <include layout="@layout/qs_detail_buttons"/>
+    <include layout="@layout/qs_detail_buttons" />
 
 </com.android.systemui.qs.QSDetail>
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
index a30b03b..6b50764 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
@@ -230,12 +230,8 @@
                 firstPageBuilder.addFloat(brightness, "translationY", heightDiff, 0);
                 mBrightnessAnimator = new TouchAnimator.Builder()
                         .addFloat(brightness, "alpha", 0, 1)
-                        .addFloat(mQsPanel.getPageIndicator(), "alpha", 0, 1)
-                        .addFloat(mQsPanel.getDivider(), "alpha", 0, 1)
                         .setStartDelay(.5f)
                         .build();
-                mAllViews.add(mQsPanel.getPageIndicator());
-                mAllViews.add(mQsPanel.getDivider());
                 mAllViews.add(brightness);
             } else {
                 mBrightnessAnimator = null;
@@ -247,7 +243,11 @@
             mFirstPageDelayedAnimator = new TouchAnimator.Builder()
                     .setStartDelay(EXPANDED_TILE_DELAY)
                     .addFloat(tileLayout, "alpha", 0, 1)
+                    .addFloat(mQsPanel.getPageIndicator(), "alpha", 0, 1)
+                    .addFloat(mQsPanel.getDivider(), "alpha", 0, 1)
                     .addFloat(mQsPanel.getFooter().getView(), "alpha", 0, 1).build();
+            mAllViews.add(mQsPanel.getPageIndicator());
+            mAllViews.add(mQsPanel.getDivider());
             mAllViews.add(mQsPanel.getFooter().getView());
             float px = 0;
             float py = 1;
@@ -264,6 +264,8 @@
         }
         mNonfirstPageAnimator = new TouchAnimator.Builder()
                 .addFloat(mQuickQsPanel, "alpha", 1, 0)
+                .addFloat(mQsPanel.getPageIndicator(), "alpha", 0, 1)
+                .addFloat(mQsPanel.getDivider(), "alpha", 0, 1)
                 .setListener(mNonFirstPageListener)
                 .setEndDelay(.5f)
                 .build();
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java b/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
index a84138d..716816d 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
@@ -162,11 +162,10 @@
         final Builder builder = new Builder()
                 .addFloat(mSettingsContainer, "translationX", -(remaining - defSpace), 0)
                 .addFloat(mSettingsButton, "rotation", -120, 0)
-                .addFloat(mAlarmStatus, "alpha", 0, 1)
-                .addFloat(mAlarmStatus, "translationX", 0, -mDate.getWidth())
-                .addFloat(mAlarmStatusCollapsed, "translationX", 0, -mDate.getWidth());
+                .addFloat(mAlarmStatus, "alpha", 0, 1);
         if (mAlarmShowing) {
-            builder.addFloat(mDate, "alpha", 1, 0);
+            builder.addFloat(mDate, "alpha", 1, 0)
+                    .addFloat(mDateTimeGroup, "translationX", 0, -mDate.getWidth());
         }
         mAnimator = builder.build();
         setExpansion(mExpansionAmount);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java b/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
index 63563b2..406f107 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
@@ -21,6 +21,7 @@
 import android.content.res.Configuration;
 import android.graphics.Rect;
 import android.os.Bundle;
+import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -39,6 +40,8 @@
 public class QSFragment extends Fragment implements QS {
     private static final String TAG = "QS";
     private static final boolean DEBUG = false;
+    private static final String EXTRA_EXPANDED = "expanded";
+    private static final String EXTRA_LISTENING = "listening";
 
     private final Rect mQsBounds = new Rect();
     private boolean mQsExpanded;
@@ -85,6 +88,35 @@
 
         mQSCustomizer = view.findViewById(R.id.qs_customize);
         mQSCustomizer.setQs(this);
+        if (savedInstanceState != null) {
+            setExpanded(savedInstanceState.getBoolean(EXTRA_EXPANDED));
+            setListening(savedInstanceState.getBoolean(EXTRA_LISTENING));
+        }
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        if (mListening) {
+            setListening(false);
+        }
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putBoolean(EXTRA_EXPANDED, mQsExpanded);
+        outState.putBoolean(EXTRA_LISTENING, mListening);
+    }
+
+    @VisibleForTesting
+    boolean isListening() {
+        return mListening;
+    }
+
+    @VisibleForTesting
+    boolean isExpanded() {
+        return mQsExpanded;
     }
 
     @Override
@@ -221,7 +253,7 @@
         }
 
         // Set bounds on the QS panel so it doesn't run over the header.
-        mQsBounds.top = (int) (mHeader.getBottom() * (1 - expansion));
+        mQsBounds.top = (int) (mQSPanel.getHeight() * (1 - expansion));
         mQsBounds.right = mQSPanel.getWidth();
         mQsBounds.bottom = mQSPanel.getHeight();
         mQSPanel.setClipBounds(mQsBounds);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java
index a751ef4..6e2add4 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java
@@ -84,12 +84,14 @@
 
     protected void updateIcon(ImageView iv, State state) {
         if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag))) {
+            boolean shouldAnimate = iv.isShown() && mAnimationEnabled
+                    && iv.getDrawable() != null;
             Drawable d = state.icon != null
-                    ? iv.isShown() && mAnimationEnabled ? state.icon.getDrawable(mContext)
+                    ? shouldAnimate ? state.icon.getDrawable(mContext)
                     : state.icon.getInvisibleDrawable(mContext) : null;
             int padding = state.icon != null ? state.icon.getPadding() : 0;
             if (d != null) {
-                d.setAutoMirrored(true);
+                d.setAutoMirrored(false);
             }
             iv.setImageDrawable(d);
             iv.setTag(R.id.qs_icon_tag, state.icon);
@@ -114,7 +116,7 @@
         if (state.state != mState) {
             int color = getColor(state.state);
             mState = state.state;
-            if (iv.isShown()) {
+            if (iv.isShown() && mTint != 0) {
                 animateGrayScale(mTint, color, iv);
                 mTint = color;
             } else {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/BatterySaverTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/BatterySaverTile.java
index c7979d8..8209ee2 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/BatterySaverTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/BatterySaverTile.java
@@ -41,8 +41,8 @@
 import com.android.systemui.Dependency;
 import com.android.systemui.R;
 import com.android.systemui.plugins.qs.DetailAdapter;
-import com.android.systemui.qs.QSHost;
 import com.android.systemui.plugins.qs.QSTile.BooleanState;
+import com.android.systemui.qs.QSHost;
 import com.android.systemui.qs.tileimpl.QSTileImpl;
 import com.android.systemui.statusbar.policy.BatteryController;
 
@@ -102,7 +102,7 @@
 
     @Override
     public CharSequence getTileLabel() {
-        return mContext.getString(R.string.battery);
+        return mContext.getString(R.string.battery_detail_switch_title);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
index 3ee01de..21a96e2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
@@ -19,6 +19,7 @@
 import android.annotation.Nullable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
+import android.view.Gravity;
 
 /**
  * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
@@ -40,6 +41,9 @@
 
     private KeyButtonDrawable(Drawable[] drawables) {
         super(drawables);
+        for (int i = 0; i < drawables.length; i++) {
+            setLayerGravity(i, Gravity.CENTER);
+        }
         mutate();
         mHasDarkDrawable = drawables.length > 1;
         setDarkIntensity(0f);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/QSFragmentTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/QSFragmentTest.java
index d77ed3d..673ffc5 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/QSFragmentTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/QSFragmentTest.java
@@ -15,8 +15,11 @@
 package com.android.systemui.qs;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
+import android.app.FragmentController;
+import android.app.FragmentManagerNonConfig;
 import android.os.Looper;
 
 import com.android.internal.logging.MetricsLogger;
@@ -24,6 +27,7 @@
 import com.android.systemui.Dependency;
 import com.android.systemui.R;
 
+import android.os.Parcelable;
 import android.testing.AndroidTestingRunner;
 
 import com.android.systemui.SysuiBaseFragmentTest;
@@ -90,4 +94,23 @@
         host.destroy();
         processAllMessages();
     }
+
+    @Test
+    public void testSaveState() {
+        QSFragment qs = (QSFragment) mFragment;
+
+        mFragments.dispatchResume();
+        processAllMessages();
+
+        qs.setListening(true);
+        qs.setExpanded(true);
+        processAllMessages();
+        recreateFragment();
+        processAllMessages();
+
+        // Get the reference to the new fragment.
+        qs = (QSFragment) mFragment;
+        assertTrue(qs.isListening());
+        assertTrue(qs.isExpanded());
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java
new file mode 100644
index 0000000..59483f2
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.systemui.qs.tileimpl;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.res.ColorStateList;
+import android.graphics.drawable.Drawable;
+import android.service.quicksettings.Tile;
+import android.testing.AndroidTestingRunner;
+import android.testing.UiThreadTest;
+import android.widget.ImageView;
+
+import com.android.systemui.SysuiTestCase;
+import com.android.systemui.plugins.qs.QSTile.Icon;
+import com.android.systemui.plugins.qs.QSTile.State;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentMatcher;
+
+@RunWith(AndroidTestingRunner.class)
+@UiThreadTest
+public class QSIconViewImplTest extends SysuiTestCase {
+
+    private QSIconViewImpl mIconView;
+
+    @Before
+    public void setup() {
+        mIconView = new QSIconViewImpl(mContext);
+    }
+
+    @Test
+    public void testNoFirstAnimation() {
+        ImageView iv = mock(ImageView.class);
+        State s = new State();
+        when(iv.isShown()).thenReturn(true);
+
+        // No current icon, only the static drawable should be used.
+        s.icon = mock(Icon.class);
+        when(iv.getDrawable()).thenReturn(null);
+        mIconView.updateIcon(iv, s);
+        verify(s.icon, never()).getDrawable(any());
+        verify(s.icon).getInvisibleDrawable(any());
+
+        // Has icon, should use the standard (animated) form.
+        s.icon = mock(Icon.class);
+        when(iv.getDrawable()).thenReturn(mock(Drawable.class));
+        mIconView.updateIcon(iv, s);
+        verify(s.icon).getDrawable(any());
+        verify(s.icon, never()).getInvisibleDrawable(any());
+    }
+
+    @Test
+    public void testNoFirstFade() {
+        ImageView iv = mock(ImageView.class);
+        State s = new State();
+        s.state = Tile.STATE_ACTIVE;
+        int desiredColor = mIconView.getColor(s.state);
+        when(iv.isShown()).thenReturn(true);
+
+        mIconView.setIcon(iv, s);
+        verify(iv).setImageTintList(argThat(stateList -> stateList.getColors()[0] == desiredColor));
+    }
+}
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 7a83436..ce9a904 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -18277,6 +18277,10 @@
             return record.info.isInstantApp();
         }
         // Otherwise check with PackageManager.
+        if (callerPackage == null) {
+            Slog.e(TAG, "isInstantApp with an application's uid, no record, and no package name");
+            throw new IllegalArgumentException("Calling application did not provide package name");
+        }
         mAppOpsService.checkPackage(uid, callerPackage);
         try {
             IPackageManager pm = AppGlobals.getPackageManager();
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index bbad493..1a27a39 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -907,7 +907,12 @@
                     Bundle finalExtras = new Bundle(extras);
                     String packageName = syncAdapterInfo.componentName.getPackageName();
                     // If the app did not run and has no account access, done
-                    if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
+                    try {
+                        if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
+                            continue;
+                        }
+                    } catch (IllegalArgumentException e) {
+                        // Package not found, race with an uninstall
                         continue;
                     }
                     mAccountManagerInternal.requestAccountAccess(account.account,
diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java
index 7f75c83..53a8092 100644
--- a/services/core/java/com/android/server/media/MediaSessionRecord.java
+++ b/services/core/java/com/android/server/media/MediaSessionRecord.java
@@ -772,7 +772,12 @@
     private final class SessionStub extends ISession.Stub {
         @Override
         public void destroy() {
-            mService.destroySession(MediaSessionRecord.this);
+            final long token = Binder.clearCallingIdentity();
+            try {
+                mService.destroySession(MediaSessionRecord.this);
+            } finally {
+                Binder.restoreCallingIdentity(token);
+            }
         }
 
         @Override
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/NetworkLoggingHandler.java b/services/devicepolicy/java/com/android/server/devicepolicy/NetworkLoggingHandler.java
index 9b4de043..70c7e58 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/NetworkLoggingHandler.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/NetworkLoggingHandler.java
@@ -26,6 +26,7 @@
 import android.os.Message;
 import android.os.SystemClock;
 import android.util.Log;
+import android.util.LongSparseArray;
 
 import com.android.internal.annotations.GuardedBy;
 
@@ -44,12 +45,21 @@
 
     // If this value changes, update DevicePolicyManager#retrieveNetworkLogs() javadoc
     private static final int MAX_EVENTS_PER_BATCH = 1200;
-    private static final long BATCH_FINALIZATION_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(90);
-    private static final long BATCH_FINALIZATION_TIMEOUT_ALARM_INTERVAL_MS =
-            TimeUnit.MINUTES.toMillis(30);
+
+    /**
+     * Maximum number of batches to store in memory. If more batches are generated and the DO
+     * doesn't fetch them, we will discard the oldest one.
+     */
+    private static final int MAX_BATCHES = 5;
+
+    private static final long BATCH_FINALIZATION_TIMEOUT_MS = 90 * 60 * 1000; // 1.5h
+    private static final long BATCH_FINALIZATION_TIMEOUT_ALARM_INTERVAL_MS = 30 * 60 * 1000; // 30m
 
     private static final String NETWORK_LOGGING_TIMEOUT_ALARM_TAG = "NetworkLogging.batchTimeout";
 
+    /** Delay after which older batches get discarded after a retrieval. */
+    private static final long RETRIEVED_BATCH_DISCARD_DELAY_MS = 5 * 60 * 1000; // 5m
+
     private final DevicePolicyManagerService mDpm;
     private final AlarmManager mAlarmManager;
 
@@ -66,22 +76,27 @@
 
     static final int LOG_NETWORK_EVENT_MSG = 1;
 
-    // threadsafe as it's Handler's thread confined
+    /** Network events accumulated so far to be finalized into a batch at some point. */
     @GuardedBy("this")
-    private ArrayList<NetworkEvent> mNetworkEvents = new ArrayList<NetworkEvent>();
+    private ArrayList<NetworkEvent> mNetworkEvents = new ArrayList<>();
 
+    /**
+     * Up to {@code MAX_BATCHES} finalized batches of logs ready to be retrieved by the DO. Already
+     * retrieved batches are discarded after {@code RETRIEVED_BATCH_DISCARD_DELAY_MS}.
+     */
     @GuardedBy("this")
-    private ArrayList<NetworkEvent> mFullBatch;
+    private final LongSparseArray<ArrayList<NetworkEvent>> mBatches =
+            new LongSparseArray<>(MAX_BATCHES);
 
     @GuardedBy("this")
     private boolean mPaused = false;
 
     // each full batch is represented by its token, which the DPC has to provide back to retrieve it
     @GuardedBy("this")
-    private long mCurrentFullBatchToken;
+    private long mCurrentBatchToken;
 
     @GuardedBy("this")
-    private long mLastRetrievedFullBatchToken;
+    private long mLastRetrievedBatchToken;
 
     NetworkLoggingHandler(Looper looper, DevicePolicyManagerService dpm) {
         super(looper);
@@ -93,7 +108,7 @@
     public void handleMessage(Message msg) {
         switch (msg.what) {
             case LOG_NETWORK_EVENT_MSG: {
-                NetworkEvent networkEvent = msg.getData().getParcelable(NETWORK_EVENT_KEY);
+                final NetworkEvent networkEvent = msg.getData().getParcelable(NETWORK_EVENT_KEY);
                 if (networkEvent != null) {
                     synchronized (NetworkLoggingHandler.this) {
                         mNetworkEvents.add(networkEvent);
@@ -113,6 +128,8 @@
 
     void scheduleBatchFinalization() {
         final long when = SystemClock.elapsedRealtime() + BATCH_FINALIZATION_TIMEOUT_MS;
+        // We use alarm manager and not just postDelayed here to ensure the batch gets finalized
+        // even if the device goes to sleep.
         mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, when,
                 BATCH_FINALIZATION_TIMEOUT_ALARM_INTERVAL_MS, NETWORK_LOGGING_TIMEOUT_ALARM_TAG,
                 mBatchTimeoutAlarmListener, this);
@@ -131,62 +148,80 @@
             return;
         }
 
-        Log.d(TAG, "Resumed network logging. Current batch="
-                + mCurrentFullBatchToken + ", LastRetrievedBatch=" + mLastRetrievedFullBatchToken);
+        Log.d(TAG, "Resumed network logging. Current batch=" + mCurrentBatchToken
+                + ", LastRetrievedBatch=" + mLastRetrievedBatchToken);
         mPaused = false;
 
-        // If there is a full batch ready that the device owner hasn't been notified about, do it
-        // now.
-        if (mFullBatch != null && mFullBatch.size() > 0
-                && mLastRetrievedFullBatchToken != mCurrentFullBatchToken) {
+        // If there is a batch ready that the device owner hasn't been notified about, do it now.
+        if (mBatches.size() > 0 && mLastRetrievedBatchToken != mCurrentBatchToken) {
             scheduleBatchFinalization();
             notifyDeviceOwnerLocked();
         }
     }
 
     synchronized void discardLogs() {
-        mFullBatch = null;
-        mNetworkEvents = new ArrayList<NetworkEvent>();
+        mBatches.clear();
+        mNetworkEvents = new ArrayList<>();
         Log.d(TAG, "Discarded all network logs");
     }
 
     @GuardedBy("this")
     private void finalizeBatchAndNotifyDeviceOwnerLocked() {
         if (mNetworkEvents.size() > 0) {
-            // finalize the batch and start a new one from scratch
-            mFullBatch = mNetworkEvents;
-            mCurrentFullBatchToken++;
-            mNetworkEvents = new ArrayList<NetworkEvent>();
+            // Finalize the batch and start a new one from scratch.
+            if (mBatches.size() >= MAX_BATCHES) {
+                // Remove the oldest batch if we hit the limit.
+                mBatches.removeAt(0);
+            }
+            mCurrentBatchToken++;
+            mBatches.append(mCurrentBatchToken, mNetworkEvents);
+            mNetworkEvents = new ArrayList<>();
             if (!mPaused) {
                 notifyDeviceOwnerLocked();
             }
         } else {
-            // don't notify the DO, since there are no events; DPC can still retrieve
+            // Don't notify the DO, since there are no events; DPC can still retrieve
             // the last full batch if not paused.
             Log.d(TAG, "Was about to finalize the batch, but there were no events to send to"
-                    + " the DPC, the batchToken of last available batch: "
-                    + mCurrentFullBatchToken);
+                    + " the DPC, the batchToken of last available batch: " + mCurrentBatchToken);
         }
-        // regardless of whether the batch was non-empty schedule a new finalization after timeout
+        // Regardless of whether the batch was non-empty schedule a new finalization after timeout.
         scheduleBatchFinalization();
     }
 
+    /** Sends a notification to the DO. Should only be called when there is a batch available. */
     @GuardedBy("this")
     private void notifyDeviceOwnerLocked() {
-        Bundle extras = new Bundle();
-        extras.putLong(DeviceAdminReceiver.EXTRA_NETWORK_LOGS_TOKEN, mCurrentFullBatchToken);
-        extras.putInt(DeviceAdminReceiver.EXTRA_NETWORK_LOGS_COUNT, mFullBatch.size());
+        final Bundle extras = new Bundle();
+        final int lastBatchSize = mBatches.valueAt(mBatches.size() - 1).size();
+        extras.putLong(DeviceAdminReceiver.EXTRA_NETWORK_LOGS_TOKEN, mCurrentBatchToken);
+        extras.putInt(DeviceAdminReceiver.EXTRA_NETWORK_LOGS_COUNT, lastBatchSize);
         Log.d(TAG, "Sending network logging batch broadcast to device owner, batchToken: "
-                + mCurrentFullBatchToken);
+                + mCurrentBatchToken);
         mDpm.sendDeviceOwnerCommand(DeviceAdminReceiver.ACTION_NETWORK_LOGS_AVAILABLE, extras);
     }
 
-    synchronized List<NetworkEvent> retrieveFullLogBatch(long batchToken) {
-        if (batchToken != mCurrentFullBatchToken) {
+    synchronized List<NetworkEvent> retrieveFullLogBatch(final long batchToken) {
+        final int index = mBatches.indexOfKey(batchToken);
+        if (index < 0) {
+            // Invalid token or batch has already been discarded.
             return null;
         }
-        mLastRetrievedFullBatchToken = mCurrentFullBatchToken;
-        return mFullBatch;
+
+        // Schedule this and older batches to be discarded after a delay to lessen memory load
+        // without interfering with the admin's ability to collect logs out-of-order.
+        // It isn't critical and we allow it to be delayed further if the phone sleeps, so we don't
+        // use the alarm manager here.
+        postDelayed(() -> {
+            synchronized(this) {
+                while (mBatches.size() > 0 && mBatches.keyAt(0) <= batchToken) {
+                    mBatches.removeAt(0);
+                }
+            }
+        }, RETRIEVED_BATCH_DISCARD_DELAY_MS);
+
+        mLastRetrievedBatchToken = batchToken;
+        return mBatches.valueAt(index);
     }
 }
 
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index f1cf441..d69f933 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -554,7 +554,7 @@
                 boolean usbDataUnlocked) {
             if (DEBUG) {
                 Slog.d(TAG, "setEnabledFunctions functions=" + functions + ", "
-                        + "forceRestart=" + forceRestart);
+                        + "forceRestart=" + forceRestart + ", usbDataUnlocked=" + usbDataUnlocked);
             }
 
             if (usbDataUnlocked != mUsbDataUnlocked) {
@@ -878,7 +878,12 @@
                     setEnabledFunctions(functions, false, msg.arg1 == 1);
                     break;
                 case MSG_UPDATE_USER_RESTRICTIONS:
-                    setEnabledFunctions(mCurrentFunctions, false, mUsbDataUnlocked);
+                    // Restart the USB stack if USB transfer is enabled but no longer allowed.
+                    final boolean forceRestart = mUsbDataUnlocked
+                            && isUsbDataTransferActive()
+                            && !isUsbTransferAllowed();
+                    setEnabledFunctions(
+                            mCurrentFunctions, forceRestart, mUsbDataUnlocked && !forceRestart);
                     break;
                 case MSG_SYSTEM_READY:
                     updateUsbNotification();
@@ -902,12 +907,10 @@
                 case MSG_USER_SWITCHED: {
                     if (mCurrentUser != msg.arg1) {
                         // Restart the USB stack and re-apply user restrictions for MTP or PTP.
-                        final boolean active = UsbManager.containsFunction(mCurrentFunctions,
-                                UsbManager.USB_FUNCTION_MTP)
-                                || UsbManager.containsFunction(mCurrentFunctions,
-                                UsbManager.USB_FUNCTION_PTP);
-                        if (mUsbDataUnlocked && active && mCurrentUser != UserHandle.USER_NULL) {
-                            Slog.v(TAG, "Current user switched to " + mCurrentUser
+                        if (mUsbDataUnlocked
+                                && isUsbDataTransferActive()
+                                && mCurrentUser != UserHandle.USER_NULL) {
+                            Slog.v(TAG, "Current user switched to " + msg.arg1
                                     + "; resetting USB host stack for MTP or PTP");
                             // avoid leaking sensitive data from previous user
                             setEnabledFunctions(mCurrentFunctions, true, false);
@@ -928,6 +931,11 @@
             }
         }
 
+        private boolean isUsbDataTransferActive() {
+            return UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)
+                    || UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_PTP);
+        }
+
         public UsbAccessory getCurrentAccessory() {
             return mCurrentAccessory;
         }
diff --git a/tests/testables/src/android/testing/BaseFragmentTest.java b/tests/testables/src/android/testing/BaseFragmentTest.java
index 53841d5..b09bcde 100644
--- a/tests/testables/src/android/testing/BaseFragmentTest.java
+++ b/tests/testables/src/android/testing/BaseFragmentTest.java
@@ -133,14 +133,7 @@
     public void testRecreate() {
         mFragments.dispatchResume();
         processAllMessages();
-        mFragments.dispatchPause();
-        Parcelable p = mFragments.saveAllState();
-        mFragments.dispatchDestroy();
-
-        mFragments = FragmentController.createController(new HostCallbacks());
-        mFragments.attachHost(null);
-        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
-        mFragments.dispatchResume();
+        recreateFragment();
         processAllMessages();
     }
 
@@ -154,6 +147,18 @@
         processAllMessages();
     }
 
+    protected void recreateFragment() {
+        mFragments.dispatchPause();
+        Parcelable p = mFragments.saveAllState();
+        mFragments.dispatchDestroy();
+
+        mFragments = FragmentController.createController(new HostCallbacks());
+        mFragments.attachHost(null);
+        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
+        mFragments.dispatchResume();
+        mFragment = mFragments.getFragmentManager().findFragmentById(VIEW_ID);
+    }
+
     protected void attachFragmentToWindow() {
         ViewUtils.attachView(mView);
         TestableLooper.get(this).processMessages(1);