Merge "Daydream -> screen saver" into nyc-dev
diff --git a/api/system-current.txt b/api/system-current.txt
index 013d8f1..dd2f18b 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -32920,6 +32920,7 @@
     method public abstract void onConnected();
     method public abstract void onDisconnected();
     method public final void updateRecommendations(java.util.List<android.printservice.recommendation.RecommendationInfo>);
+    field public static final java.lang.String SERVICE_INTERFACE = "android.printservice.recommendation.RecommendationService";
   }
 
 }
diff --git a/core/java/android/net/INetworkPolicyListener.aidl b/core/java/android/net/INetworkPolicyListener.aidl
index 31dc965a..812f1fe 100644
--- a/core/java/android/net/INetworkPolicyListener.aidl
+++ b/core/java/android/net/INetworkPolicyListener.aidl
@@ -22,5 +22,6 @@
     void onUidRulesChanged(int uid, int uidRules);
     void onMeteredIfacesChanged(in String[] meteredIfaces);
     void onRestrictBackgroundChanged(boolean restrictBackground);
+    void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted);
 
 }
diff --git a/core/java/android/printservice/recommendation/RecommendationService.java b/core/java/android/printservice/recommendation/RecommendationService.java
index b7ea512..733629a 100644
--- a/core/java/android/printservice/recommendation/RecommendationService.java
+++ b/core/java/android/printservice/recommendation/RecommendationService.java
@@ -27,7 +27,6 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.util.Log;
-import com.android.internal.annotations.GuardedBy;
 
 import java.util.List;
 
@@ -46,8 +45,6 @@
     /**
      * The {@link Intent} action that must be declared as handled by a service in its manifest for
      * the system to recognize it as a print service recommendation service.
-     *
-     * @hide
      */
     public static final String SERVICE_INTERFACE =
             "android.printservice.recommendation.RecommendationService";
diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/FragmentTuner.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/FragmentTuner.java
index e3eae33..ecdbe63 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/FragmentTuner.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/FragmentTuner.java
@@ -154,7 +154,8 @@
             MenuItem rename = menu.findItem(R.id.menu_rename);
             MenuItem selectAll = menu.findItem(R.id.menu_select_all);
 
-            open.setVisible(true);
+            open.setVisible(mState.action == ACTION_GET_CONTENT ||
+                    mState.action == ACTION_OPEN);
             share.setVisible(false);
             delete.setVisible(false);
             rename.setVisible(false);
diff --git a/packages/PrintServiceRecommendationService/Android.mk b/packages/PrintRecommendationService/Android.mk
similarity index 100%
rename from packages/PrintServiceRecommendationService/Android.mk
rename to packages/PrintRecommendationService/Android.mk
diff --git a/packages/PrintServiceRecommendationService/AndroidManifest.xml b/packages/PrintRecommendationService/AndroidManifest.xml
similarity index 100%
rename from packages/PrintServiceRecommendationService/AndroidManifest.xml
rename to packages/PrintRecommendationService/AndroidManifest.xml
diff --git a/packages/PrintRecommendationService/CleanSpec.mk b/packages/PrintRecommendationService/CleanSpec.mk
new file mode 100644
index 0000000..c087cb8
--- /dev/null
+++ b/packages/PrintRecommendationService/CleanSpec.mk
@@ -0,0 +1,49 @@
+# Copyright (C) 2016 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.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list.  These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+#     $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+#     $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list.  E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
diff --git a/packages/PrintServiceRecommendationService/MODULE_LICENSE_APACHE2 b/packages/PrintRecommendationService/MODULE_LICENSE_APACHE2
similarity index 100%
rename from packages/PrintServiceRecommendationService/MODULE_LICENSE_APACHE2
rename to packages/PrintRecommendationService/MODULE_LICENSE_APACHE2
diff --git a/packages/PrintServiceRecommendationService/NOTICE b/packages/PrintRecommendationService/NOTICE
similarity index 100%
rename from packages/PrintServiceRecommendationService/NOTICE
rename to packages/PrintRecommendationService/NOTICE
diff --git a/packages/PrintServiceRecommendationService/res/values/donottranslate.xml b/packages/PrintRecommendationService/res/values/donottranslate.xml
similarity index 100%
rename from packages/PrintServiceRecommendationService/res/values/donottranslate.xml
rename to packages/PrintRecommendationService/res/values/donottranslate.xml
diff --git a/packages/PrintServiceRecommendationService/res/values/strings.xml b/packages/PrintRecommendationService/res/values/strings.xml
similarity index 100%
rename from packages/PrintServiceRecommendationService/res/values/strings.xml
rename to packages/PrintRecommendationService/res/values/strings.xml
diff --git a/packages/PrintServiceRecommendationService/res/xml/vendorconfigs.xml b/packages/PrintRecommendationService/res/xml/vendorconfigs.xml
similarity index 100%
rename from packages/PrintServiceRecommendationService/res/xml/vendorconfigs.xml
rename to packages/PrintRecommendationService/res/xml/vendorconfigs.xml
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java
diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java
similarity index 100%
rename from packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java
rename to packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java
diff --git a/packages/SettingsLib/res/layout/drawer_category.xml b/packages/SettingsLib/res/layout/drawer_category.xml
index b7b50d0..582821b 100644
--- a/packages/SettingsLib/res/layout/drawer_category.xml
+++ b/packages/SettingsLib/res/layout/drawer_category.xml
@@ -28,7 +28,8 @@
     <TextView
         android:id="@android:id/title"
         android:layout_width="match_parent"
-        android:layout_height="48dp"
+        android:layout_height="wrap_content"
+        android:minHeight="48dp"
         android:paddingTop="16dp"
         android:paddingBottom="16dp"
         android:paddingStart="16dp"
diff --git a/packages/SettingsLib/res/layout/drawer_item.xml b/packages/SettingsLib/res/layout/drawer_item.xml
index 4b53049..e1f1ae5 100644
--- a/packages/SettingsLib/res/layout/drawer_item.xml
+++ b/packages/SettingsLib/res/layout/drawer_item.xml
@@ -17,7 +17,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tile_item"
     android:layout_width="match_parent"
-    android:layout_height="48dp"
+    android:layout_height="wrap_content"
+    android:minHeight="48dp"
     android:orientation="horizontal" >
 
     <ImageView
diff --git a/packages/SystemUI/res/layout/qs_detail.xml b/packages/SystemUI/res/layout/qs_detail.xml
index af2a285..32839d8 100644
--- a/packages/SystemUI/res/layout/qs_detail.xml
+++ b/packages/SystemUI/res/layout/qs_detail.xml
@@ -44,11 +44,15 @@
         android:layout_height="wrap_content"
         />
 
-    <FrameLayout
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1">
+        <FrameLayout
             android:id="@android:id/content"
             android:layout_width="match_parent"
-            android:layout_height="0dp"
-            android:layout_weight="1" />
+            android:layout_height="wrap_content" />
+    </ScrollView>
 
     <include layout="@layout/qs_detail_buttons" />
 
diff --git a/packages/SystemUI/res/values-sw410dp/config.xml b/packages/SystemUI/res/values-sw410dp/config.xml
index 08b2f88..049a535 100644
--- a/packages/SystemUI/res/values-sw410dp/config.xml
+++ b/packages/SystemUI/res/values-sw410dp/config.xml
@@ -21,4 +21,6 @@
      for different hardware and product builds. -->
 <resources>
     <integer name="quick_settings_num_rows">2</integer>
+
+    <bool name="quick_settings_show_date">true</bool>
 </resources>
diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml
index c54db37..ef84c76 100644
--- a/packages/SystemUI/res/values/config.xml
+++ b/packages/SystemUI/res/values/config.xml
@@ -268,5 +268,7 @@
     <!-- Nav bar button default ordering/layout -->
     <string name="config_navBarLayout" translatable="false">space,back;home;recent,menu_ime</string>
 
+    <bool name="quick_settings_show_date">false</bool>
+
 </resources>
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index 1149c59..74f1b80 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -70,6 +70,7 @@
 
     private QSCustomizer mCustomizePanel;
     private Record mDetailRecord;
+    private boolean mTriggeredExpand;
 
     public QSPanel(Context context) {
         this(context, null);
@@ -384,8 +385,16 @@
     }
 
     protected void handleShowDetail(Record r, boolean show) {
-        if (show && !mExpanded) {
-            mHost.animateExpandQS();
+        if (show) {
+            if (!mExpanded) {
+                mTriggeredExpand = true;
+                mHost.animateToggleQSExpansion();
+            } else {
+                mTriggeredExpand = false;
+            }
+        } else if (mTriggeredExpand) {
+            mHost.animateToggleQSExpansion();
+            mTriggeredExpand = false;
         }
         if (r instanceof TileRecord) {
             handleShowDetailTile((TileRecord) r, show);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
index 3e32905..7692598 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
@@ -389,7 +389,7 @@
         void startRunnableDismissingKeyguard(Runnable runnable);
         void warn(String message, Throwable t);
         void collapsePanels();
-        void animateExpandQS();
+        void animateToggleQSExpansion();
         void openPanels();
         Looper getLooper();
         Context getContext();
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
index faa5283..41b49d8 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
@@ -279,6 +279,7 @@
 
     private void selectPosition(int position, View v) {
         // Remove the placeholder.
+        notifyItemRemoved(mEditIndex);
         mTiles.remove(mEditIndex--);
         mAccessibilityMoving = false;
         move(mAccessibilityFromIndex, position, v);
@@ -323,7 +324,7 @@
     }
 
     private boolean move(int from, int to, View v) {
-        if (to >= mEditIndex) {
+        if (to > mEditIndex) {
             if (from >= mEditIndex) {
                 return false;
             }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
index d04a2fc..9bc85c8 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
@@ -144,14 +144,18 @@
                 String packageName = info.serviceInfo.packageName;
                 ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name);
                 String spec = CustomTile.toSpec(componentName);
+                if (info.serviceInfo.icon == 0) {
+                    continue;
+                }
                 Drawable icon = info.serviceInfo.loadIcon(pm);
                 if (!permission.BIND_QUICK_SETTINGS_TILE.equals(info.serviceInfo.permission)) {
                     continue;
                 }
-                if (icon != null) {
-                    icon.mutate();
-                    icon.setTint(mContext.getColor(android.R.color.white));
+                if (icon == null) {
+                    continue;
                 }
+                icon.mutate();
+                icon.setTint(mContext.getColor(android.R.color.white));
                 CharSequence label = info.serviceInfo.loadLabel(pm);
                 final CharSequence appLabel = info.serviceInfo.applicationInfo.loadLabel(pm);
                 addTile(spec, icon, label != null ? label.toString() : "null", appLabel, mContext);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
index 6b20681..54dd015 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
@@ -36,7 +36,6 @@
 import android.view.IWindowManager;
 import android.view.WindowManager;
 import android.view.WindowManagerGlobal;
-
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.systemui.R;
@@ -59,6 +58,7 @@
     private final IQSTileService mService;
     private final TileServiceManager mServiceManager;
     private final int mUser;
+    private final android.graphics.drawable.Icon mDefaultIcon;
 
     private boolean mListening;
     private boolean mBound;
@@ -73,15 +73,19 @@
         mService = mServiceManager.getTileService();
         mTile = new Tile(mComponent);
         mUser = ActivityManager.getCurrentUser();
+        android.graphics.drawable.Icon defaultIcon;
         try {
             PackageManager pm = mContext.getPackageManager();
             ServiceInfo info = pm.getServiceInfo(mComponent,
                     PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE);
-            mTile.setIcon(android.graphics.drawable.Icon
-                    .createWithResource(mComponent.getPackageName(), info.icon));
+            defaultIcon = info.icon != 0 ? android.graphics.drawable.Icon
+                    .createWithResource(mComponent.getPackageName(), info.icon) : null;
+            mTile.setIcon(defaultIcon);
             mTile.setLabel(info.loadLabel(pm));
         } catch (Exception e) {
+            defaultIcon = null;
         }
+        mDefaultIcon = defaultIcon;
         try {
             mService.setQSTile(mTile);
         } catch (RemoteException e) {
@@ -91,13 +95,7 @@
 
     @Override
     public boolean isAvailable() {
-        try {
-            ServiceInfo info = mContext.getPackageManager().getServiceInfo(mComponent,
-                    PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE);
-            return true;
-        } catch (Exception e) {
-            return false;
-        }
+        return mDefaultIcon != null;
     }
 
     public int getUser() {
@@ -227,11 +225,18 @@
 
     @Override
     protected void handleUpdateState(State state, Object arg) {
-        Drawable drawable = mTile.getIcon().loadDrawable(mContext);
         int tileState = mTile.getState();
         if (mServiceManager.hasPendingBind()) {
             tileState = Tile.STATE_UNAVAILABLE;
         }
+        Drawable drawable;
+        try {
+            drawable = mTile.getIcon().loadDrawable(mContext);
+        } catch (Exception e) {
+            Log.w(TAG, "Invalid icon, forcing into unavailable state");
+            tileState = Tile.STATE_UNAVAILABLE;
+            drawable = mDefaultIcon.loadDrawable(mContext);
+        }
         int color = mContext.getColor(getColor(tileState));
         drawable.setTint(color);
         state.icon = new DrawableIcon(drawable);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
index 5a26a4a..5903218 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
@@ -148,13 +148,20 @@
         if (DEBUG) Log.d(TAG, "onServiceConnected " + name);
         // Got a connection, set the binding count to 0.
         mBindTryCount = 0;
-        mWrapper = new QSTileServiceWrapper(Stub.asInterface(service));
+        final QSTileServiceWrapper wrapper = new QSTileServiceWrapper(Stub.asInterface(service));
         try {
             service.linkToDeath(this, 0);
         } catch (RemoteException e) {
         }
-        setQSService(mService);
-        setQSTile(mTile);
+        if (!wrapper.setQSService(mService)) {
+            handleDeath();
+            return;
+        }
+        if (!wrapper.setQSTile(mTile)) {
+            handleDeath();
+            return;
+        }
+        mWrapper = wrapper;
         handlePendingMessages();
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
index 82496ac..493b23f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
@@ -209,7 +209,7 @@
         // already logged
     }
 
-    public void animateExpandQS() {
+    public void animateToggleQSExpansion() {
         // TODO: Better path to animated panel expansion.
         mHeader.performClick();
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
index 2b03dfb..b8e069d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
@@ -106,6 +106,8 @@
         mDateTimeGroup = (ViewGroup) findViewById(R.id.date_time_group);
         mDateTimeGroup.setPivotX(0);
         mDateTimeGroup.setPivotY(0);
+        boolean showDate = getResources().getBoolean(R.bool.quick_settings_show_date);
+        findViewById(R.id.date).setVisibility(showDate ? View.VISIBLE : View.GONE);
 
         mExpandIndicator = (ExpandableIndicator) findViewById(R.id.expand_indicator);
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/DataSaverController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/DataSaverController.java
index c4c64e7..e6e189f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/DataSaverController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/DataSaverController.java
@@ -89,6 +89,10 @@
                 }
             });
         }
+
+        @Override
+        public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
+        }
     };
 
     public interface Listener {
diff --git a/rs/java/android/renderscript/Element.java b/rs/java/android/renderscript/Element.java
index 50226ac..9d2f750 100644
--- a/rs/java/android/renderscript/Element.java
+++ b/rs/java/android/renderscript/Element.java
@@ -311,8 +311,12 @@
      * @return Element
      */
     public static Element BOOLEAN(RenderScript rs) {
-        if(rs.mElement_BOOLEAN == null) {
-            rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
+        if (rs.mElement_BOOLEAN == null) {
+            synchronized (rs) {
+                if (rs.mElement_BOOLEAN == null) {
+                    rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
+                }
+            }
         }
         return rs.mElement_BOOLEAN;
     }
@@ -325,8 +329,12 @@
      * @return Element
      */
     public static Element U8(RenderScript rs) {
-        if(rs.mElement_U8 == null) {
-            rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
+        if (rs.mElement_U8 == null) {
+            synchronized (rs) {
+                if (rs.mElement_U8 == null) {
+                    rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
+                }
+            }
         }
         return rs.mElement_U8;
     }
@@ -339,436 +347,683 @@
      * @return Element
      */
     public static Element I8(RenderScript rs) {
-        if(rs.mElement_I8 == null) {
-            rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
+        if (rs.mElement_I8 == null) {
+            synchronized (rs) {
+                if (rs.mElement_I8 == null) {
+                    rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
+                }
+            }
         }
         return rs.mElement_I8;
     }
 
     public static Element U16(RenderScript rs) {
-        if(rs.mElement_U16 == null) {
-            rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
+        if (rs.mElement_U16 == null) {
+            synchronized (rs) {
+                if (rs.mElement_U16 == null) {
+                    rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
+                }
+            }
         }
         return rs.mElement_U16;
     }
 
     public static Element I16(RenderScript rs) {
-        if(rs.mElement_I16 == null) {
-            rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
+        if (rs.mElement_I16 == null) {
+            synchronized (rs) {
+                if (rs.mElement_I16 == null) {
+                    rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
+                }
+            }
         }
         return rs.mElement_I16;
     }
 
     public static Element U32(RenderScript rs) {
-        if(rs.mElement_U32 == null) {
-            rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
+        if (rs.mElement_U32 == null) {
+            synchronized (rs) {
+                if (rs.mElement_U32 == null) {
+                    rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
+                }
+            }
         }
         return rs.mElement_U32;
     }
 
     public static Element I32(RenderScript rs) {
-        if(rs.mElement_I32 == null) {
-            rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
+        if (rs.mElement_I32 == null) {
+            synchronized (rs) {
+                if (rs.mElement_I32 == null) {
+                    rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
+                }
+            }
         }
         return rs.mElement_I32;
     }
 
     public static Element U64(RenderScript rs) {
-        if(rs.mElement_U64 == null) {
-            rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
+        if (rs.mElement_U64 == null) {
+            synchronized (rs) {
+                if (rs.mElement_U64 == null) {
+                    rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
+                }
+            }
         }
         return rs.mElement_U64;
     }
 
     public static Element I64(RenderScript rs) {
-        if(rs.mElement_I64 == null) {
-            rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
+        if (rs.mElement_I64 == null) {
+            synchronized (rs) {
+                if (rs.mElement_I64 == null) {
+                    rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
+                }
+            }
         }
         return rs.mElement_I64;
     }
 
     public static Element F16(RenderScript rs) {
-        if(rs.mElement_F16 == null) {
-            rs.mElement_F16 = createUser(rs, DataType.FLOAT_16);
+        if (rs.mElement_F16 == null) {
+            synchronized (rs) {
+                if (rs.mElement_F16 == null) {
+                    rs.mElement_F16 = createUser(rs, DataType.FLOAT_16);
+                }
+            }
         }
         return rs.mElement_F16;
     }
 
     public static Element F32(RenderScript rs) {
-        if(rs.mElement_F32 == null) {
-            rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
+        if (rs.mElement_F32 == null) {
+            synchronized (rs) {
+                if (rs.mElement_F32 == null) {
+                    rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
+                }
+            }
         }
         return rs.mElement_F32;
     }
 
     public static Element F64(RenderScript rs) {
-        if(rs.mElement_F64 == null) {
-            rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
+        if (rs.mElement_F64 == null) {
+            synchronized (rs) {
+                if (rs.mElement_F64 == null) {
+                    rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
+                }
+            }
         }
         return rs.mElement_F64;
     }
 
     public static Element ELEMENT(RenderScript rs) {
-        if(rs.mElement_ELEMENT == null) {
-            rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
+        if (rs.mElement_ELEMENT == null) {
+            synchronized (rs) {
+                if (rs.mElement_ELEMENT == null) {
+                    rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
+                }
+            }
         }
         return rs.mElement_ELEMENT;
     }
 
     public static Element TYPE(RenderScript rs) {
-        if(rs.mElement_TYPE == null) {
-            rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
+        if (rs.mElement_TYPE == null) {
+            synchronized (rs) {
+                if (rs.mElement_TYPE == null) {
+                    rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
+                }
+            }
         }
         return rs.mElement_TYPE;
     }
 
     public static Element ALLOCATION(RenderScript rs) {
-        if(rs.mElement_ALLOCATION == null) {
-            rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
+        if (rs.mElement_ALLOCATION == null) {
+            synchronized (rs) {
+                if (rs.mElement_ALLOCATION == null) {
+                    rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
+                }
+            }
         }
         return rs.mElement_ALLOCATION;
     }
 
     public static Element SAMPLER(RenderScript rs) {
-        if(rs.mElement_SAMPLER == null) {
-            rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
+        if (rs.mElement_SAMPLER == null) {
+            synchronized (rs) {
+                if (rs.mElement_SAMPLER == null) {
+                    rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
+                }
+            }
         }
         return rs.mElement_SAMPLER;
     }
 
     public static Element SCRIPT(RenderScript rs) {
-        if(rs.mElement_SCRIPT == null) {
-            rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
+        if (rs.mElement_SCRIPT == null) {
+            synchronized (rs) {
+                if (rs.mElement_SCRIPT == null) {
+                    rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
+                }
+            }
         }
         return rs.mElement_SCRIPT;
     }
 
     public static Element MESH(RenderScript rs) {
-        if(rs.mElement_MESH == null) {
-            rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
+        if (rs.mElement_MESH == null) {
+            synchronized (rs) {
+                if (rs.mElement_MESH == null) {
+                    rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
+                }
+            }
         }
         return rs.mElement_MESH;
     }
 
     public static Element PROGRAM_FRAGMENT(RenderScript rs) {
-        if(rs.mElement_PROGRAM_FRAGMENT == null) {
-            rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
+        if (rs.mElement_PROGRAM_FRAGMENT == null) {
+            synchronized (rs) {
+                if (rs.mElement_PROGRAM_FRAGMENT == null) {
+                    rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
+                }
+            }
         }
         return rs.mElement_PROGRAM_FRAGMENT;
     }
 
     public static Element PROGRAM_VERTEX(RenderScript rs) {
-        if(rs.mElement_PROGRAM_VERTEX == null) {
-            rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
+        if (rs.mElement_PROGRAM_VERTEX == null) {
+            synchronized (rs) {
+                if (rs.mElement_PROGRAM_VERTEX == null) {
+                    rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
+                }
+            }
         }
         return rs.mElement_PROGRAM_VERTEX;
     }
 
     public static Element PROGRAM_RASTER(RenderScript rs) {
-        if(rs.mElement_PROGRAM_RASTER == null) {
-            rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
+        if (rs.mElement_PROGRAM_RASTER == null) {
+            synchronized (rs) {
+                if (rs.mElement_PROGRAM_RASTER == null) {
+                    rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
+                }
+            }
         }
         return rs.mElement_PROGRAM_RASTER;
     }
 
     public static Element PROGRAM_STORE(RenderScript rs) {
-        if(rs.mElement_PROGRAM_STORE == null) {
-            rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
+        if (rs.mElement_PROGRAM_STORE == null) {
+            synchronized (rs) {
+                if (rs.mElement_PROGRAM_STORE == null) {
+                    rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
+                }
+            }
         }
         return rs.mElement_PROGRAM_STORE;
     }
 
     public static Element FONT(RenderScript rs) {
-        if(rs.mElement_FONT == null) {
-            rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
+        if (rs.mElement_FONT == null) {
+            synchronized (rs) {
+                if (rs.mElement_FONT == null) {
+                    rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
+                }
+            }
         }
         return rs.mElement_FONT;
     }
 
-
     public static Element A_8(RenderScript rs) {
-        if(rs.mElement_A_8 == null) {
-            rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
+        if (rs.mElement_A_8 == null) {
+            synchronized (rs) {
+                if (rs.mElement_A_8 == null) {
+                    rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
+                }
+            }
         }
         return rs.mElement_A_8;
     }
 
     public static Element RGB_565(RenderScript rs) {
-        if(rs.mElement_RGB_565 == null) {
-            rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
+        if (rs.mElement_RGB_565 == null) {
+            synchronized (rs) {
+                if (rs.mElement_RGB_565 == null) {
+                    rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
+                }
+            }
         }
         return rs.mElement_RGB_565;
     }
 
     public static Element RGB_888(RenderScript rs) {
-        if(rs.mElement_RGB_888 == null) {
-            rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
+        if (rs.mElement_RGB_888 == null) {
+            synchronized (rs) {
+                if (rs.mElement_RGB_888 == null) {
+                    rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
+                }
+            }
         }
         return rs.mElement_RGB_888;
     }
 
     public static Element RGBA_5551(RenderScript rs) {
-        if(rs.mElement_RGBA_5551 == null) {
-            rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
+        if (rs.mElement_RGBA_5551 == null) {
+            synchronized (rs) {
+                if (rs.mElement_RGBA_5551 == null) {
+                    rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
+                }
+            }
         }
         return rs.mElement_RGBA_5551;
     }
 
     public static Element RGBA_4444(RenderScript rs) {
-        if(rs.mElement_RGBA_4444 == null) {
-            rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
+        if (rs.mElement_RGBA_4444 == null) {
+            synchronized (rs) {
+                if (rs.mElement_RGBA_4444 == null) {
+                    rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
+                }
+            }
         }
         return rs.mElement_RGBA_4444;
     }
 
     public static Element RGBA_8888(RenderScript rs) {
-        if(rs.mElement_RGBA_8888 == null) {
-            rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
+        if (rs.mElement_RGBA_8888 == null) {
+            synchronized (rs) {
+                if (rs.mElement_RGBA_8888 == null) {
+                    rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
+                }
+            }
         }
         return rs.mElement_RGBA_8888;
     }
 
     public static Element F16_2(RenderScript rs) {
-        if(rs.mElement_HALF_2 == null) {
-            rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2);
+        if (rs.mElement_HALF_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_HALF_2 == null) {
+                    rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2);
+                }
+            }
         }
         return rs.mElement_HALF_2;
     }
 
     public static Element F16_3(RenderScript rs) {
-        if(rs.mElement_HALF_3 == null) {
-            rs.mElement_HALF_3 = createVector(rs, DataType.FLOAT_16, 3);
+        if (rs.mElement_HALF_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_HALF_3 == null) {
+                    rs.mElement_HALF_3 = createVector(rs, DataType.FLOAT_16, 3);
+                }
+            }
         }
         return rs.mElement_HALF_3;
     }
 
     public static Element F16_4(RenderScript rs) {
-        if(rs.mElement_HALF_4 == null) {
-            rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4);
+        if (rs.mElement_HALF_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_HALF_4 == null) {
+                    rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4);
+                }
+            }
         }
         return rs.mElement_HALF_4;
     }
 
     public static Element F32_2(RenderScript rs) {
-        if(rs.mElement_FLOAT_2 == null) {
-            rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
+        if (rs.mElement_FLOAT_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_FLOAT_2 == null) {
+                    rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
+                }
+            }
         }
         return rs.mElement_FLOAT_2;
     }
 
     public static Element F32_3(RenderScript rs) {
-        if(rs.mElement_FLOAT_3 == null) {
-            rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
+        if (rs.mElement_FLOAT_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_FLOAT_3 == null) {
+                    rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
+                }
+            }
         }
         return rs.mElement_FLOAT_3;
     }
 
     public static Element F32_4(RenderScript rs) {
-        if(rs.mElement_FLOAT_4 == null) {
-            rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
+        if (rs.mElement_FLOAT_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_FLOAT_4 == null) {
+                    rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
+                }
+            }
         }
         return rs.mElement_FLOAT_4;
     }
 
     public static Element F64_2(RenderScript rs) {
-        if(rs.mElement_DOUBLE_2 == null) {
-            rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
+        if (rs.mElement_DOUBLE_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_DOUBLE_2 == null) {
+                    rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
+                }
+            }
         }
         return rs.mElement_DOUBLE_2;
     }
 
     public static Element F64_3(RenderScript rs) {
-        if(rs.mElement_DOUBLE_3 == null) {
-            rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
+        if (rs.mElement_DOUBLE_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_DOUBLE_3 == null) {
+                    rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
+                }
+            }
         }
         return rs.mElement_DOUBLE_3;
     }
 
     public static Element F64_4(RenderScript rs) {
-        if(rs.mElement_DOUBLE_4 == null) {
-            rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
+        if (rs.mElement_DOUBLE_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_DOUBLE_4 == null) {
+                    rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
+                }
+            }
         }
         return rs.mElement_DOUBLE_4;
     }
 
     public static Element U8_2(RenderScript rs) {
-        if(rs.mElement_UCHAR_2 == null) {
-            rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
+        if (rs.mElement_UCHAR_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UCHAR_2 == null) {
+                    rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
+                }
+            }
         }
         return rs.mElement_UCHAR_2;
     }
 
     public static Element U8_3(RenderScript rs) {
-        if(rs.mElement_UCHAR_3 == null) {
-            rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
+        if (rs.mElement_UCHAR_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UCHAR_3 == null) {
+                    rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
+                }
+            }
         }
         return rs.mElement_UCHAR_3;
     }
 
     public static Element U8_4(RenderScript rs) {
-        if(rs.mElement_UCHAR_4 == null) {
-            rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
+        if (rs.mElement_UCHAR_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UCHAR_4 == null) {
+                    rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
+                }
+            }
         }
         return rs.mElement_UCHAR_4;
     }
 
     public static Element I8_2(RenderScript rs) {
-        if(rs.mElement_CHAR_2 == null) {
-            rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
+        if (rs.mElement_CHAR_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_CHAR_2 == null) {
+                    rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
+                }
+            }
         }
         return rs.mElement_CHAR_2;
     }
 
     public static Element I8_3(RenderScript rs) {
-        if(rs.mElement_CHAR_3 == null) {
-            rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
+        if (rs.mElement_CHAR_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_CHAR_3 == null) {
+                    rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
+                }
+            }
         }
         return rs.mElement_CHAR_3;
     }
 
     public static Element I8_4(RenderScript rs) {
-        if(rs.mElement_CHAR_4 == null) {
-            rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
+        if (rs.mElement_CHAR_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_CHAR_4 == null) {
+                    rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
+                }
+            }
         }
         return rs.mElement_CHAR_4;
     }
 
     public static Element U16_2(RenderScript rs) {
-        if(rs.mElement_USHORT_2 == null) {
-            rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
+        if (rs.mElement_USHORT_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_USHORT_2 == null) {
+                    rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
+                }
+            }
         }
         return rs.mElement_USHORT_2;
     }
 
     public static Element U16_3(RenderScript rs) {
-        if(rs.mElement_USHORT_3 == null) {
-            rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
+        if (rs.mElement_USHORT_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_USHORT_3 == null) {
+                    rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
+                }
+            }
         }
         return rs.mElement_USHORT_3;
     }
 
     public static Element U16_4(RenderScript rs) {
-        if(rs.mElement_USHORT_4 == null) {
-            rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
+        if (rs.mElement_USHORT_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_USHORT_4 == null) {
+                    rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
+                }
+            }
         }
         return rs.mElement_USHORT_4;
     }
 
     public static Element I16_2(RenderScript rs) {
-        if(rs.mElement_SHORT_2 == null) {
-            rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
+        if (rs.mElement_SHORT_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_SHORT_2 == null) {
+                    rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
+                }
+            }
         }
         return rs.mElement_SHORT_2;
     }
 
     public static Element I16_3(RenderScript rs) {
-        if(rs.mElement_SHORT_3 == null) {
-            rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
+        if (rs.mElement_SHORT_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_SHORT_3 == null) {
+                    rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
+                }
+            }
         }
         return rs.mElement_SHORT_3;
     }
 
     public static Element I16_4(RenderScript rs) {
-        if(rs.mElement_SHORT_4 == null) {
-            rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
+        if (rs.mElement_SHORT_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_SHORT_4 == null) {
+                    rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
+                }
+            }
         }
         return rs.mElement_SHORT_4;
     }
 
     public static Element U32_2(RenderScript rs) {
-        if(rs.mElement_UINT_2 == null) {
-            rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
+        if (rs.mElement_UINT_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UINT_2 == null) {
+                    rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
+                }
+            }
         }
         return rs.mElement_UINT_2;
     }
 
     public static Element U32_3(RenderScript rs) {
-        if(rs.mElement_UINT_3 == null) {
-            rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
+        if (rs.mElement_UINT_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UINT_3 == null) {
+                    rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
+                }
+            }
         }
         return rs.mElement_UINT_3;
     }
 
     public static Element U32_4(RenderScript rs) {
-        if(rs.mElement_UINT_4 == null) {
-            rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
+        if (rs.mElement_UINT_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_UINT_4 == null) {
+                    rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
+                }
+            }
         }
         return rs.mElement_UINT_4;
     }
 
     public static Element I32_2(RenderScript rs) {
-        if(rs.mElement_INT_2 == null) {
-            rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
+        if (rs.mElement_INT_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_INT_2 == null) {
+                    rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
+                }
+            }
         }
         return rs.mElement_INT_2;
     }
 
     public static Element I32_3(RenderScript rs) {
-        if(rs.mElement_INT_3 == null) {
-            rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
+        if (rs.mElement_INT_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_INT_3 == null) {
+                    rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
+                }
+            }
         }
         return rs.mElement_INT_3;
     }
 
     public static Element I32_4(RenderScript rs) {
-        if(rs.mElement_INT_4 == null) {
-            rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
+        if (rs.mElement_INT_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_INT_4 == null) {
+                    rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
+                }
+            }
         }
         return rs.mElement_INT_4;
     }
 
     public static Element U64_2(RenderScript rs) {
-        if(rs.mElement_ULONG_2 == null) {
-            rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
+        if (rs.mElement_ULONG_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_ULONG_2 == null) {
+                    rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
+                }
+            }
         }
         return rs.mElement_ULONG_2;
     }
 
     public static Element U64_3(RenderScript rs) {
-        if(rs.mElement_ULONG_3 == null) {
-            rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
+        if (rs.mElement_ULONG_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_ULONG_3 == null) {
+                    rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
+                }
+            }
         }
         return rs.mElement_ULONG_3;
     }
 
     public static Element U64_4(RenderScript rs) {
-        if(rs.mElement_ULONG_4 == null) {
-            rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
+        if (rs.mElement_ULONG_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_ULONG_4 == null) {
+                    rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
+                }
+            }
         }
         return rs.mElement_ULONG_4;
     }
 
     public static Element I64_2(RenderScript rs) {
-        if(rs.mElement_LONG_2 == null) {
-            rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
+        if (rs.mElement_LONG_2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_LONG_2 == null) {
+                    rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
+                }
+            }
         }
         return rs.mElement_LONG_2;
     }
 
     public static Element I64_3(RenderScript rs) {
-        if(rs.mElement_LONG_3 == null) {
-            rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
+        if (rs.mElement_LONG_3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_LONG_3 == null) {
+                    rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
+                }
+            }
         }
         return rs.mElement_LONG_3;
     }
 
     public static Element I64_4(RenderScript rs) {
-        if(rs.mElement_LONG_4 == null) {
-            rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
+        if (rs.mElement_LONG_4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_LONG_4 == null) {
+                    rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
+                }
+            }
         }
         return rs.mElement_LONG_4;
     }
 
     public static Element YUV(RenderScript rs) {
         if (rs.mElement_YUV == null) {
-            rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
+            synchronized (rs) {
+                if (rs.mElement_YUV == null) {
+                    rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
+                }
+            }
         }
         return rs.mElement_YUV;
     }
 
     public static Element MATRIX_4X4(RenderScript rs) {
-        if(rs.mElement_MATRIX_4X4 == null) {
-            rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
+        if (rs.mElement_MATRIX_4X4 == null) {
+            synchronized (rs) {
+                if (rs.mElement_MATRIX_4X4 == null) {
+                    rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
+                }
+            }
         }
         return rs.mElement_MATRIX_4X4;
     }
@@ -780,15 +1035,23 @@
     }
 
     public static Element MATRIX_3X3(RenderScript rs) {
-        if(rs.mElement_MATRIX_3X3 == null) {
-            rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
+        if (rs.mElement_MATRIX_3X3 == null) {
+            synchronized (rs) {
+                if (rs.mElement_MATRIX_3X3 == null) {
+                    rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
+                }
+            }
         }
         return rs.mElement_MATRIX_3X3;
     }
 
     public static Element MATRIX_2X2(RenderScript rs) {
-        if(rs.mElement_MATRIX_2X2 == null) {
-            rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
+        if (rs.mElement_MATRIX_2X2 == null) {
+            synchronized (rs) {
+                if (rs.mElement_MATRIX_2X2 == null) {
+                    rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
+                }
+            }
         }
         return rs.mElement_MATRIX_2X2;
     }
diff --git a/rs/java/android/renderscript/RenderScript.java b/rs/java/android/renderscript/RenderScript.java
index 2650e5a..3d1370a 100644
--- a/rs/java/android/renderscript/RenderScript.java
+++ b/rs/java/android/renderscript/RenderScript.java
@@ -1037,97 +1037,97 @@
     @SuppressWarnings({"FieldCanBeLocal"})
     MessageThread mMessageThread;
 
-    Element mElement_U8;
-    Element mElement_I8;
-    Element mElement_U16;
-    Element mElement_I16;
-    Element mElement_U32;
-    Element mElement_I32;
-    Element mElement_U64;
-    Element mElement_I64;
-    Element mElement_F16;
-    Element mElement_F32;
-    Element mElement_F64;
-    Element mElement_BOOLEAN;
+    volatile Element mElement_U8;
+    volatile Element mElement_I8;
+    volatile Element mElement_U16;
+    volatile Element mElement_I16;
+    volatile Element mElement_U32;
+    volatile Element mElement_I32;
+    volatile Element mElement_U64;
+    volatile Element mElement_I64;
+    volatile Element mElement_F16;
+    volatile Element mElement_F32;
+    volatile Element mElement_F64;
+    volatile Element mElement_BOOLEAN;
 
-    Element mElement_ELEMENT;
-    Element mElement_TYPE;
-    Element mElement_ALLOCATION;
-    Element mElement_SAMPLER;
-    Element mElement_SCRIPT;
-    Element mElement_MESH;
-    Element mElement_PROGRAM_FRAGMENT;
-    Element mElement_PROGRAM_VERTEX;
-    Element mElement_PROGRAM_RASTER;
-    Element mElement_PROGRAM_STORE;
-    Element mElement_FONT;
+    volatile Element mElement_ELEMENT;
+    volatile Element mElement_TYPE;
+    volatile Element mElement_ALLOCATION;
+    volatile Element mElement_SAMPLER;
+    volatile Element mElement_SCRIPT;
+    volatile Element mElement_MESH;
+    volatile Element mElement_PROGRAM_FRAGMENT;
+    volatile Element mElement_PROGRAM_VERTEX;
+    volatile Element mElement_PROGRAM_RASTER;
+    volatile Element mElement_PROGRAM_STORE;
+    volatile Element mElement_FONT;
 
-    Element mElement_A_8;
-    Element mElement_RGB_565;
-    Element mElement_RGB_888;
-    Element mElement_RGBA_5551;
-    Element mElement_RGBA_4444;
-    Element mElement_RGBA_8888;
+    volatile Element mElement_A_8;
+    volatile Element mElement_RGB_565;
+    volatile Element mElement_RGB_888;
+    volatile Element mElement_RGBA_5551;
+    volatile Element mElement_RGBA_4444;
+    volatile Element mElement_RGBA_8888;
 
-    Element mElement_HALF_2;
-    Element mElement_HALF_3;
-    Element mElement_HALF_4;
+    volatile Element mElement_HALF_2;
+    volatile Element mElement_HALF_3;
+    volatile Element mElement_HALF_4;
 
-    Element mElement_FLOAT_2;
-    Element mElement_FLOAT_3;
-    Element mElement_FLOAT_4;
+    volatile Element mElement_FLOAT_2;
+    volatile Element mElement_FLOAT_3;
+    volatile Element mElement_FLOAT_4;
 
-    Element mElement_DOUBLE_2;
-    Element mElement_DOUBLE_3;
-    Element mElement_DOUBLE_4;
+    volatile Element mElement_DOUBLE_2;
+    volatile Element mElement_DOUBLE_3;
+    volatile Element mElement_DOUBLE_4;
 
-    Element mElement_UCHAR_2;
-    Element mElement_UCHAR_3;
-    Element mElement_UCHAR_4;
+    volatile Element mElement_UCHAR_2;
+    volatile Element mElement_UCHAR_3;
+    volatile Element mElement_UCHAR_4;
 
-    Element mElement_CHAR_2;
-    Element mElement_CHAR_3;
-    Element mElement_CHAR_4;
+    volatile Element mElement_CHAR_2;
+    volatile Element mElement_CHAR_3;
+    volatile Element mElement_CHAR_4;
 
-    Element mElement_USHORT_2;
-    Element mElement_USHORT_3;
-    Element mElement_USHORT_4;
+    volatile Element mElement_USHORT_2;
+    volatile Element mElement_USHORT_3;
+    volatile Element mElement_USHORT_4;
 
-    Element mElement_SHORT_2;
-    Element mElement_SHORT_3;
-    Element mElement_SHORT_4;
+    volatile Element mElement_SHORT_2;
+    volatile Element mElement_SHORT_3;
+    volatile Element mElement_SHORT_4;
 
-    Element mElement_UINT_2;
-    Element mElement_UINT_3;
-    Element mElement_UINT_4;
+    volatile Element mElement_UINT_2;
+    volatile Element mElement_UINT_3;
+    volatile Element mElement_UINT_4;
 
-    Element mElement_INT_2;
-    Element mElement_INT_3;
-    Element mElement_INT_4;
+    volatile Element mElement_INT_2;
+    volatile Element mElement_INT_3;
+    volatile Element mElement_INT_4;
 
-    Element mElement_ULONG_2;
-    Element mElement_ULONG_3;
-    Element mElement_ULONG_4;
+    volatile Element mElement_ULONG_2;
+    volatile Element mElement_ULONG_3;
+    volatile Element mElement_ULONG_4;
 
-    Element mElement_LONG_2;
-    Element mElement_LONG_3;
-    Element mElement_LONG_4;
+    volatile Element mElement_LONG_2;
+    volatile Element mElement_LONG_3;
+    volatile Element mElement_LONG_4;
 
-    Element mElement_YUV;
+    volatile Element mElement_YUV;
 
-    Element mElement_MATRIX_4X4;
-    Element mElement_MATRIX_3X3;
-    Element mElement_MATRIX_2X2;
+    volatile Element mElement_MATRIX_4X4;
+    volatile Element mElement_MATRIX_3X3;
+    volatile Element mElement_MATRIX_2X2;
 
-    Sampler mSampler_CLAMP_NEAREST;
-    Sampler mSampler_CLAMP_LINEAR;
-    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
-    Sampler mSampler_WRAP_NEAREST;
-    Sampler mSampler_WRAP_LINEAR;
-    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
-    Sampler mSampler_MIRRORED_REPEAT_NEAREST;
-    Sampler mSampler_MIRRORED_REPEAT_LINEAR;
-    Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
+    volatile Sampler mSampler_CLAMP_NEAREST;
+    volatile Sampler mSampler_CLAMP_LINEAR;
+    volatile Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
+    volatile Sampler mSampler_WRAP_NEAREST;
+    volatile Sampler mSampler_WRAP_LINEAR;
+    volatile Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
+    volatile Sampler mSampler_MIRRORED_REPEAT_NEAREST;
+    volatile Sampler mSampler_MIRRORED_REPEAT_LINEAR;
+    volatile Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
 
     ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
     ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
diff --git a/rs/java/android/renderscript/Sampler.java b/rs/java/android/renderscript/Sampler.java
index 5c4bae9..70e88bc 100644
--- a/rs/java/android/renderscript/Sampler.java
+++ b/rs/java/android/renderscript/Sampler.java
@@ -98,13 +98,17 @@
      * @return Sampler
      */
     public static Sampler CLAMP_NEAREST(RenderScript rs) {
-        if(rs.mSampler_CLAMP_NEAREST == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.NEAREST);
-            b.setMagnification(Value.NEAREST);
-            b.setWrapS(Value.CLAMP);
-            b.setWrapT(Value.CLAMP);
-            rs.mSampler_CLAMP_NEAREST = b.create();
+        if (rs.mSampler_CLAMP_NEAREST == null) {
+            synchronized (rs) {
+                if (rs.mSampler_CLAMP_NEAREST == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.NEAREST);
+                    b.setMagnification(Value.NEAREST);
+                    b.setWrapS(Value.CLAMP);
+                    b.setWrapT(Value.CLAMP);
+                    rs.mSampler_CLAMP_NEAREST = b.create();
+                }
+            }
         }
         return rs.mSampler_CLAMP_NEAREST;
     }
@@ -118,13 +122,17 @@
      * @return Sampler
      */
     public static Sampler CLAMP_LINEAR(RenderScript rs) {
-        if(rs.mSampler_CLAMP_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.CLAMP);
-            b.setWrapT(Value.CLAMP);
-            rs.mSampler_CLAMP_LINEAR = b.create();
+        if (rs.mSampler_CLAMP_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_CLAMP_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.CLAMP);
+                    b.setWrapT(Value.CLAMP);
+                    rs.mSampler_CLAMP_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_CLAMP_LINEAR;
     }
@@ -138,13 +146,17 @@
      * @return Sampler
      */
     public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
-        if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR_MIP_LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.CLAMP);
-            b.setWrapT(Value.CLAMP);
-            rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
+        if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR_MIP_LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.CLAMP);
+                    b.setWrapT(Value.CLAMP);
+                    rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
     }
@@ -158,13 +170,17 @@
      * @return Sampler
      */
     public static Sampler WRAP_NEAREST(RenderScript rs) {
-        if(rs.mSampler_WRAP_NEAREST == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.NEAREST);
-            b.setMagnification(Value.NEAREST);
-            b.setWrapS(Value.WRAP);
-            b.setWrapT(Value.WRAP);
-            rs.mSampler_WRAP_NEAREST = b.create();
+        if (rs.mSampler_WRAP_NEAREST == null) {
+            synchronized (rs) {
+                if (rs.mSampler_WRAP_NEAREST == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.NEAREST);
+                    b.setMagnification(Value.NEAREST);
+                    b.setWrapS(Value.WRAP);
+                    b.setWrapT(Value.WRAP);
+                    rs.mSampler_WRAP_NEAREST = b.create();
+                }
+            }
         }
         return rs.mSampler_WRAP_NEAREST;
     }
@@ -178,13 +194,17 @@
      * @return Sampler
      */
     public static Sampler WRAP_LINEAR(RenderScript rs) {
-        if(rs.mSampler_WRAP_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.WRAP);
-            b.setWrapT(Value.WRAP);
-            rs.mSampler_WRAP_LINEAR = b.create();
+        if (rs.mSampler_WRAP_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_WRAP_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.WRAP);
+                    b.setWrapT(Value.WRAP);
+                    rs.mSampler_WRAP_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_WRAP_LINEAR;
     }
@@ -198,13 +218,17 @@
      * @return Sampler
      */
     public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
-        if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR_MIP_LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.WRAP);
-            b.setWrapT(Value.WRAP);
-            rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
+        if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR_MIP_LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.WRAP);
+                    b.setWrapT(Value.WRAP);
+                    rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
     }
@@ -218,13 +242,17 @@
      * @return Sampler
      */
     public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
-        if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.NEAREST);
-            b.setMagnification(Value.NEAREST);
-            b.setWrapS(Value.MIRRORED_REPEAT);
-            b.setWrapT(Value.MIRRORED_REPEAT);
-            rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
+        if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
+            synchronized (rs) {
+                if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.NEAREST);
+                    b.setMagnification(Value.NEAREST);
+                    b.setWrapS(Value.MIRRORED_REPEAT);
+                    b.setWrapT(Value.MIRRORED_REPEAT);
+                    rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
+                }
+            }
         }
         return rs.mSampler_MIRRORED_REPEAT_NEAREST;
     }
@@ -238,13 +266,17 @@
      * @return Sampler
      */
     public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
-        if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.MIRRORED_REPEAT);
-            b.setWrapT(Value.MIRRORED_REPEAT);
-            rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
+        if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.MIRRORED_REPEAT);
+                    b.setWrapT(Value.MIRRORED_REPEAT);
+                    rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_MIRRORED_REPEAT_LINEAR;
     }
@@ -258,13 +290,17 @@
      * @return Sampler
      */
     public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
-        if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
-            Builder b = new Builder(rs);
-            b.setMinification(Value.LINEAR_MIP_LINEAR);
-            b.setMagnification(Value.LINEAR);
-            b.setWrapS(Value.MIRRORED_REPEAT);
-            b.setWrapT(Value.MIRRORED_REPEAT);
-            rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
+        if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
+            synchronized (rs) {
+                if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
+                    Builder b = new Builder(rs);
+                    b.setMinification(Value.LINEAR_MIP_LINEAR);
+                    b.setMagnification(Value.LINEAR);
+                    b.setWrapS(Value.MIRRORED_REPEAT);
+                    b.setWrapT(Value.MIRRORED_REPEAT);
+                    rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
+                }
+            }
         }
         return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
     }
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
index 2b29c6b..1b85016 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -2296,11 +2296,16 @@
         public boolean bindLocked() {
             UserState userState = getUserStateLocked(mUserId);
             if (!mIsAutomation) {
-                if (mService == null && mContext.bindServiceAsUser(
-                        mIntent, this,
-                        Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
-                        new UserHandle(mUserId))) {
-                    userState.mBindingServices.add(mComponentName);
+                final long identity = Binder.clearCallingIdentity();
+                try {
+                    if (mService == null && mContext.bindServiceAsUser(
+                            mIntent, this,
+                            Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
+                            new UserHandle(mUserId))) {
+                        userState.mBindingServices.add(mComponentName);
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(identity);
                 }
             } else {
                 userState.mBindingServices.add(mComponentName);
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 40d44b4..71506be 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -1419,6 +1419,15 @@
                 log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")");
             }
         }
+
+        @Override
+        public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
+            if (LOGD_RULES) {
+                // caller is NPMS, since we only register with them
+                log("onRestrictBackgroundWhitelistChanged(uid=" + uid + ", whitelisted="
+                        + whitelisted + ")");
+            }
+        }
     };
 
     /**
diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
index 3fb786b..433c056 100644
--- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
+++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
@@ -1678,7 +1678,7 @@
         // Checks if app was added or removed to the blacklist.
         if ((oldPolicy == POLICY_NONE && policy == POLICY_REJECT_METERED_BACKGROUND)
                 || (oldPolicy == POLICY_REJECT_METERED_BACKGROUND && policy == POLICY_NONE)) {
-            mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
+            mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 1, null)
                     .sendToTarget();
         }
     }
@@ -1970,10 +1970,9 @@
             // ...but always persists the whitelist request.
             writePolicyLocked();
         }
-        if (mRestrictBackground && !oldStatus && needFirewallRules) {
-            mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
-                    .sendToTarget();
-        }
+        int changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0;
+        mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed,
+                Boolean.TRUE).sendToTarget();
     }
 
     @Override
@@ -1983,10 +1982,8 @@
         synchronized (mRulesLock) {
             changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true);
         }
-        if (changed) {
-            mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
-                    .sendToTarget();
-        }
+        mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed ? 1 : 0,
+                Boolean.FALSE).sendToTarget();
     }
 
     /**
@@ -2924,10 +2921,40 @@
                     return true;
                 }
                 case MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED: {
+                    // MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED can be called in 2 occasions:
+                    // - when an app is whitelisted
+                    // - when an app is blacklisted
+                    //
+                    // Whether the internal listeners (INetworkPolicyListener implementations) or
+                    // app broadcast receivers are notified depend on the following rules:
+                    //
+                    // - App receivers are only notified when the app status changed (msg.arg2 = 1)
+                    // - Listeners are only notified when app was whitelisted (msg.obj is not null),
+                    //   since blacklist notifications are handled through MSG_RULES_CHANGED).
                     final int uid = msg.arg1;
+                    final boolean changed = msg.arg2 == 1;
+                    final Boolean whitelisted = (Boolean) msg.obj;
+
+                    if (whitelisted != null) {
+                        final int length = mListeners.beginBroadcast();
+                        for (int i = 0; i < length; i++) {
+                            // First notify internal listeners...
+                            final INetworkPolicyListener listener = mListeners.getBroadcastItem(i);
+                            if (listener != null) {
+                                try {
+                                    listener.onRestrictBackgroundWhitelistChanged(uid,
+                                            whitelisted.booleanValue());
+                                } catch (RemoteException e) {
+                                }
+                            }
+                        }
+                        mListeners.finishBroadcast();
+                    }
+
                     final PackageManager pm = mContext.getPackageManager();
                     final String[] packages = pm.getPackagesForUid(uid);
-                    if (packages != null) {
+                    if (changed && packages != null) {
+                        // ...then notify apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED
                         final int userId = UserHandle.getUserId(uid);
                         for (String packageName : packages) {
                             final Intent intent = new Intent(
@@ -2936,8 +2963,6 @@
                             intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
                             mContext.sendBroadcastAsUser(intent, UserHandle.of(userId));
                         }
-                    } else {
-                        Slog.w(TAG, "no packages for uid " + uid);
                     }
                     return true;
                 }
diff --git a/services/core/java/com/android/server/wm/DragState.java b/services/core/java/com/android/server/wm/DragState.java
index aace5e7..ffe0336 100644
--- a/services/core/java/com/android/server/wm/DragState.java
+++ b/services/core/java/com/android/server/wm/DragState.java
@@ -420,12 +420,7 @@
     void notifyLocationLw(float x, float y) {
         // Tell the affected window
         WindowState touchedWin = mDisplayContent.getTouchableWinAtPointLocked(x, y);
-        if (touchedWin == null) {
-            if (DEBUG_DRAG) Slog.d(TAG_WM, "No touched win at x=" + x + " y=" + y);
-            return;
-        }
-
-        if (!isWindowNotified(touchedWin)) {
+        if (touchedWin != null && !isWindowNotified(touchedWin)) {
             // The drag point is over a window which was not notified about a drag start.
             // Pretend it's over empty space.
             touchedWin = null;
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
index 837b4a4..386c3aa 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
@@ -128,6 +128,12 @@
     }
 
     @Override
+    public void onUnlockUser(int userHandle) {
+        mServiceStub.initForUser(userHandle);
+        mServiceStub.switchImplementationIfNeeded(false);
+    }
+
+    @Override
     public void onSwitchUser(int userHandle) {
         mServiceStub.switchUser(userHandle);
     }
@@ -362,6 +368,12 @@
             }
         }
 
+        void switchImplementationIfNeeded(boolean force) {
+            synchronized (this) {
+                switchImplementationIfNeededLocked(force);
+            }
+        }
+
         void switchImplementationIfNeededLocked(boolean force) {
             if (!mSafeMode) {
                 String curService = Settings.Secure.getStringForUser(