Merge changes from topics "pass-res", "remove-isProvisioningNeeded"

* changes:
  Protect invalid entitlement app configuration
  Pass entitlement configuration to Settings for entitlement check
diff --git a/ApiDocs.bp b/ApiDocs.bp
index 029699e..2d12e2d 100644
--- a/ApiDocs.bp
+++ b/ApiDocs.bp
@@ -91,6 +91,7 @@
     arg_files: [
         "core/res/AndroidManifest.xml",
     ],
+    sandbox: true,
     args: metalava_framework_docs_args,
     write_sdk_values: true,
 }
diff --git a/StubLibraries.bp b/StubLibraries.bp
index bb13eaa..54f4937 100644
--- a/StubLibraries.bp
+++ b/StubLibraries.bp
@@ -60,6 +60,7 @@
     ],
     api_levels_annotations_enabled: false,
     filter_packages: packages_to_document,
+    sandbox: true,
 }
 
 stubs_defaults {
@@ -377,6 +378,7 @@
         "core/java/android/os/RemoteException.java",
         "core/java/android/util/AndroidException.java",
     ],
+    sandbox: true,
     installable: false,
     sdk_version: "core_platform",
     annotations_enabled: true,
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto
index 3311829..c8f2efa 100644
--- a/cmds/statsd/src/atoms.proto
+++ b/cmds/statsd/src/atoms.proto
@@ -41,6 +41,7 @@
 import "frameworks/base/core/proto/android/service/procstats_enum.proto";
 import "frameworks/base/core/proto/android/service/usb.proto";
 import "frameworks/base/core/proto/android/stats/connectivity/network_stack.proto";
+import "frameworks/base/core/proto/android/stats/connectivity/tethering.proto";
 import "frameworks/base/core/proto/android/stats/dnsresolver/dns_resolver.proto";
 import "frameworks/base/core/proto/android/stats/devicepolicy/device_policy.proto";
 import "frameworks/base/core/proto/android/stats/devicepolicy/device_policy_enums.proto";
@@ -364,6 +365,7 @@
         NetworkDhcpRenewReported network_dhcp_renew_reported = 291 [(log_from_module) = "network_stack"];
         NetworkValidationReported network_validation_reported = 292 [(log_from_module) = "network_stack"];
         NetworkStackQuirkReported network_stack_quirk_reported = 293 [(log_from_module) = "network_stack"];
+        NetworkTetheringReported  network_tethering_reported = 303 [(log_from_module) =  "network_tethering"];
     }
 
     // Pulled events will start at field 10000.
@@ -5853,6 +5855,24 @@
 }
 
 /**
+ * Logs when a Tethering event occurs.
+ *
+ */
+message NetworkTetheringReported {
+  // tethering error code
+  optional android.stats.connectivity.ErrorCode error_code = 1;
+
+  // tethering downstream type
+  optional android.stats.connectivity.DownstreamType downstream_type = 2;
+
+  // transport type of upstream network
+  optional android.stats.connectivity.UpstreamType upstream_type = 3;
+
+  // The user type of Tethering
+  optional android.stats.connectivity.UserType user_type= 4;
+}
+
+/**
  * Logs a DNS lookup operation initiated by the system resolver on behalf of an application
  * invoking native APIs such as getaddrinfo() or Java APIs such as Network#getAllByName().
  *
diff --git a/config/preloaded-classes-blacklist b/config/preloaded-classes-blacklist
index 5e54559..48d579c 100644
--- a/config/preloaded-classes-blacklist
+++ b/config/preloaded-classes-blacklist
@@ -2,6 +2,7 @@
 android.net.ConnectivityThread$Singleton
 android.os.AsyncTask
 android.os.FileObserver
+android.os.NullVibrator
 android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask
 android.widget.Magnifier
-
+com.android.server.BootReceiver$2
diff --git a/core/proto/android/stats/connectivity/Android.bp b/core/proto/android/stats/connectivity/Android.bp
index 9cd233e..5e6ac3c 100644
--- a/core/proto/android/stats/connectivity/Android.bp
+++ b/core/proto/android/stats/connectivity/Android.bp
@@ -22,3 +22,17 @@
     ],
     sdk_version: "system_29",
 }
+
+java_library_static {
+    name: "tetheringprotos",
+    proto: {
+        type: "lite",
+    },
+    srcs: [
+        "tethering.proto",
+    ],
+    apex_available: [
+        "com.android.tethering",
+    ],
+    sdk_version: "system_current",
+}
diff --git a/core/proto/android/stats/connectivity/tethering.proto b/core/proto/android/stats/connectivity/tethering.proto
new file mode 100644
index 0000000..6303b7d
--- /dev/null
+++ b/core/proto/android/stats/connectivity/tethering.proto
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+syntax = "proto2";
+package android.stats.connectivity;
+option java_multiple_files = true;
+option java_outer_classname = "TetheringProto";
+
+enum ErrorCode {
+    EC_NO_ERROR = 0;
+    EC_UNKNOWN_IFACE = 1;
+    EC_SERVICE_UNAVAIL = 2;
+    EC_UNSUPPORTED = 3;
+    EC_UNAVAIL_IFACE = 4;
+    EC_INTERNAL_ERROR = 5;
+    EC_TETHER_IFACE_ERROR = 6;
+    EC_UNTETHER_IFACE_ERROR = 7;
+    EC_ENABLE_FORWARDING_ERROR = 8;
+    EC_DISABLE_FORWARDING_ERROR = 9;
+    EC_IFACE_CFG_ERROR = 10;
+    EC_PROVISIONING_FAILED = 11;
+    EC_DHCPSERVER_ERROR = 12;
+    EC_ENTITLEMENT_UNKNOWN = 13;
+    EC_NO_CHANGE_TETHERING_PERMISSION = 14;
+    EC_NO_ACCESS_TETHERING_PERMISSION = 15;
+    EC_UNKNOWN_TYPE = 16;
+}
+
+enum DownstreamType {
+    // Unspecific tethering type.
+    DS_UNSPECIFIED = 0;
+    // Wifi tethering type.
+    DS_TETHERING_WIFI = 1;
+    // USB tethering type.
+    DS_TETHERING_USB = 2;
+    // Bluetooth tethering type.
+    DS_TETHERING_BLUETOOTH = 3;
+    // Wifi P2p tethering type.
+    DS_TETHERING_WIFI_P2P = 4;
+    // NCM (Network Control Model) local tethering type.
+    DS_TETHERING_NCM = 5;
+    // Ethernet tethering type.
+    DS_TETHERING_ETHERNET = 6;
+}
+
+enum UpstreamType {
+    UT_UNKNOWN = 0;
+    // Indicates upstream using a Cellular transport.
+    UT_CELLULAR = 1;
+    // Indicates upstream using a Wi-Fi transport.
+    UT_WIFI = 2;
+    // Indicates upstream using a Bluetooth transport.
+    UT_BLUETOOTH = 3;
+    // Indicates upstream using an Ethernet transport.
+    UT_ETHERNET = 4;
+    // Indicates upstream using a Wi-Fi Aware transport.
+    UT_WIFI_AWARE = 5;
+    // Indicates upstream using a LoWPAN transport.
+    UT_LOWPAN = 6;
+    // Indicates upstream using a Cellular+VPN transport.
+    UT_CELLULAR_VPN = 7;
+    // Indicates upstream using a Wi-Fi+VPN transport.
+    UT_WIFI_VPN = 8;
+    // Indicates upstream using a Bluetooth+VPN transport.
+    UT_BLUETOOTH_VPN = 9;
+    // Indicates upstream using an Ethernet+VPN transport.
+    UT_ETHERNET_VPN = 10;
+    // Indicates upstream using a Wi-Fi+Cellular+VPN transport.
+    UT_WIFI_CELLULAR_VPN = 11;
+    // Indicates upstream using for test only.
+    UT_TEST = 12;
+    // Indicates upstream using DUN capability + Cellular transport.
+    UT_DUN_CELLULAR = 13;
+}
+
+enum UserType {
+    // Unknown.
+    USER_UNKOWNN = 0;
+    // Settings.
+    USER_SETTINGS = 1;
+    // System UI.
+    USER_SYSTEMUI = 2;
+    // Google mobile service.
+    USER_GMS = 3;
+}
diff --git a/data/etc/car/Android.bp b/data/etc/car/Android.bp
index ff8eb0c..f691610 100644
--- a/data/etc/car/Android.bp
+++ b/data/etc/car/Android.bp
@@ -122,6 +122,13 @@
 }
 
 prebuilt_etc {
+    name: "privapp_whitelist_com.android.car.companiondevicesupport",
+    sub_dir: "permissions",
+    src: "com.android.car.companiondevicesupport.xml",
+    filename_from_src: true,
+}
+
+prebuilt_etc {
     name: "privapp_whitelist_com.google.android.car.kitchensink",
     sub_dir: "permissions",
     src: "com.google.android.car.kitchensink.xml",
diff --git a/data/etc/car/com.android.car.companiondevicesupport.xml b/data/etc/car/com.android.car.companiondevicesupport.xml
new file mode 100644
index 0000000..2067bab
--- /dev/null
+++ b/data/etc/car/com.android.car.companiondevicesupport.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2019 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
+  -->
+<permissions>
+    <privapp-permissions package="com.android.car.companiondevicesupport">
+      <permission name="android.permission.INTERACT_ACROSS_USERS"/>
+      <permission name="android.permission.MANAGE_USERS"/>
+      <permission name="android.permission.PROVIDE_TRUST_AGENT"/>
+      <permission name="android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME"/>
+    </privapp-permissions>
+</permissions>
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index 5613b03..3b48c1d 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -39,6 +39,8 @@
     <uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+    <!-- ACCESS_BACKGROUND_LOCATION is needed for testing purposes only. -->
+    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
diff --git a/services/backup/java/com/android/server/backup/restore/FullRestoreEngineThread.java b/services/backup/java/com/android/server/backup/restore/FullRestoreEngineThread.java
index 7075608..71f1dbf 100644
--- a/services/backup/java/com/android/server/backup/restore/FullRestoreEngineThread.java
+++ b/services/backup/java/com/android/server/backup/restore/FullRestoreEngineThread.java
@@ -1,10 +1,10 @@
 package com.android.server.backup.restore;
 
 import android.os.ParcelFileDescriptor;
+import android.os.ParcelFileDescriptor.AutoCloseInputStream;
 
 import libcore.io.IoUtils;
 
-import java.io.FileInputStream;
 import java.io.InputStream;
 
 class FullRestoreEngineThread implements Runnable {
@@ -19,7 +19,7 @@
         // We *do* want this FileInputStream to own the underlying fd, so that
         // when we are finished with it, it closes this end of the pipe in a way
         // that signals its other end.
-        mEngineStream = new FileInputStream(engineSocket.getFileDescriptor(), true);
+        mEngineStream = new AutoCloseInputStream(engineSocket);
         // Tell it to be sure to leave the agent instance up after finishing
         mMustKillAgent = false;
     }
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index 7d1398b..6b498b5 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -7330,6 +7330,29 @@
     }
 
     /**
+     * Unregister a IImsServiceFeatureCallback previously associated with an ImsFeature through
+     * {@link #getImsMmTelFeatureAndListen(int, IImsServiceFeatureCallback)} or
+     * {@link #getImsRcsFeatureAndListen(int, IImsServiceFeatureCallback)}.
+     * @param slotIndex The SIM slot associated with the callback.
+     * @param featureType The {@link android.telephony.ims.feature.ImsFeature.FeatureType}
+     *                    associated with the callback.
+     * @param callback The callback to be unregistered.
+     * @hide
+     */
+    public void unregisterImsFeatureCallback(int slotIndex, int featureType,
+            IImsServiceFeatureCallback callback) {
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                telephony.unregisterImsFeatureCallback(slotIndex, featureType, callback);
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "unregisterImsFeatureCallback, RemoteException: "
+                    + e.getMessage());
+        }
+    }
+
+    /**
      * @return the {@IImsRegistration} interface that corresponds with the slot index and feature.
      * @param slotIndex The SIM slot corresponding to the ImsService ImsRegistration is active for.
      * @param feature An integer indicating the feature that we wish to get the ImsRegistration for.
diff --git a/telephony/java/android/telephony/ims/ImsMmTelManager.java b/telephony/java/android/telephony/ims/ImsMmTelManager.java
index 872f49d..c274ec4 100644
--- a/telephony/java/android/telephony/ims/ImsMmTelManager.java
+++ b/telephony/java/android/telephony/ims/ImsMmTelManager.java
@@ -519,9 +519,6 @@
      * @param executor The executor the callback events should be run on.
      * @param c The MmTel {@link CapabilityCallback} to be registered.
      * @see #unregisterMmTelCapabilityCallback(CapabilityCallback)
-     * @throws IllegalArgumentException if the subscription associated with this callback is not
-     * active (SIM is not inserted, ESIM inactive) or invalid, or a null {@link Executor} or
-     * {@link CapabilityCallback} callback.
      * @throws ImsException if the subscription associated with this callback is valid, but
      * the {@link ImsService} associated with the subscription is not available. This can happen if
      * the service crashed, for example. See {@link ImsException#getCode()} for a more detailed
@@ -544,18 +541,13 @@
         ITelephony iTelephony = getITelephony();
         if (iTelephony == null) {
             throw new ImsException("Could not find Telephony Service.",
-                    ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
+                    ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
         }
 
         try {
             iTelephony.registerMmTelCapabilityCallback(mSubId, c.getBinder());
         } catch (ServiceSpecificException e) {
-            if (e.errorCode == ImsException.CODE_ERROR_INVALID_SUBSCRIPTION) {
-                // Rethrow as runtime error to keep API compatible.
-                throw new IllegalArgumentException(e.getMessage());
-            } else {
-                throw new ImsException(e.getMessage(), e.errorCode);
-            }
+            throw new ImsException(e.getMessage(), e.errorCode);
         } catch (RemoteException e) {
             throw e.rethrowAsRuntimeException();
         }  catch (IllegalStateException e) {
diff --git a/telephony/java/android/telephony/ims/ImsRcsManager.java b/telephony/java/android/telephony/ims/ImsRcsManager.java
index 71bc68e..16e5483 100644
--- a/telephony/java/android/telephony/ims/ImsRcsManager.java
+++ b/telephony/java/android/telephony/ims/ImsRcsManager.java
@@ -56,14 +56,15 @@
      * Activity Action: Show the opt-in dialog for enabling or disabling RCS contact discovery
      * using User Capability Exchange (UCE).
      * <p>
-     * An application that depends on contact discovery being enabled may send this intent
+     * An application that depends on RCS contact discovery being enabled must send this intent
      * using {@link Context#startActivity(Intent)} to ask the user to opt-in for contacts upload for
-     * capability exchange if it is currently disabled. Whether or not this setting has been enabled
-     * can be queried using {@link RcsUceAdapter#isUceSettingEnabled()}.
+     * capability exchange if it is currently disabled. Whether or not RCS contact discovery has
+     * been enabled by the user can be queried using {@link RcsUceAdapter#isUceSettingEnabled()}.
      * <p>
-     * This intent should only be sent if the carrier supports RCS capability exchange, which can be
-     * queried using the key {@link CarrierConfigManager#KEY_USE_RCS_PRESENCE_BOOL}. Otherwise, the
-     * setting will not be present.
+     * This intent will always be handled by the system, however the application should only send
+     * this Intent if the carrier supports RCS contact discovery, which can be queried using the key
+     * {@link CarrierConfigManager#KEY_USE_RCS_PRESENCE_BOOL}. Otherwise, the RCS contact discovery
+     * opt-in dialog will not be shown.
      * <p>
      * Input: A mandatory {@link Settings#EXTRA_SUB_ID} extra containing the subscription that the
      * setting will be be shown for.
diff --git a/telephony/java/android/telephony/ims/RcsUceAdapter.java b/telephony/java/android/telephony/ims/RcsUceAdapter.java
index 01fc09a..27456f1 100644
--- a/telephony/java/android/telephony/ims/RcsUceAdapter.java
+++ b/telephony/java/android/telephony/ims/RcsUceAdapter.java
@@ -502,9 +502,10 @@
     /**
      * Change the user’s setting for whether or not UCE is enabled for the associated subscription.
      * <p>
-     * If an application Requires UCE, they may launch an Activity using the Intent
+     * If an application Requires UCE, they will launch an Activity using the Intent
      * {@link ImsRcsManager#ACTION_SHOW_CAPABILITY_DISCOVERY_OPT_IN}, which will ask the user if
-     * they wish to enable this feature.
+     * they wish to enable this feature. This setting should only be enabled after the user has
+     * opted-in to capability exchange.
      * <p>
      * Note: This setting does not affect whether or not the device publishes its service
      * capabilities if the subscription supports presence publication.
diff --git a/telephony/java/com/android/internal/telephony/ITelephony.aidl b/telephony/java/com/android/internal/telephony/ITelephony.aidl
index 5293efa..5a6b997 100644
--- a/telephony/java/com/android/internal/telephony/ITelephony.aidl
+++ b/telephony/java/com/android/internal/telephony/ITelephony.aidl
@@ -851,6 +851,14 @@
     IImsRcsFeature getRcsFeatureAndListen(int slotId, in IImsServiceFeatureCallback callback);
 
     /**
+     * Unregister a callback that was previously registered through
+     * {@link #getMmTelFeatureAndListen} or {@link #getRcsFeatureAndListen}. This should always be
+     * called when the callback is no longer being used.
+     */
+    void unregisterImsFeatureCallback(int slotId, int featureType,
+            in IImsServiceFeatureCallback callback);
+
+    /**
     * Returns the IImsRegistration associated with the slot and feature specified.
     */
     IImsRegistration getImsRegistration(int slotId, int feature);