[automerger skipped] Merge "Revert "Use TelephonyManager#isEmergencyNumber() instead"" am: a281046577 am: d2eb366490 am: cef53cebae -s ours

am skip reason: Merged-In I7eca63e80394ed606a61bfc3a60b5f068e6192ca with SHA-1 524867d42f is already in history. Merged-In was found from reverted change.

Reverted change: https://android-review.googlesource.com/c/platform/external/ims/+/1453740

Original change: https://android-review.googlesource.com/c/platform/external/ims/+/1702032

Change-Id: I2d74562804fb38db1cb6caf87a486f9547b7ab9a
diff --git a/rcs/presencepolling/AndroidManifest.xml b/rcs/presencepolling/AndroidManifest.xml
index 9ee86ef..2ffcc5c 100644
--- a/rcs/presencepolling/AndroidManifest.xml
+++ b/rcs/presencepolling/AndroidManifest.xml
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<?xml version="1.0" encoding="utf-8"?>
 <!--
  * Copyright (c) 2015, Motorola Mobility LLC
  * All rights reserved.
@@ -28,20 +28,20 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
-    package="com.android.service.ims.presence"
-    android:sharedUserId="android.uid.phone"
-    coreApp="true">
+     xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
+     package="com.android.service.ims.presence"
+     android:sharedUserId="android.uid.phone"
+     coreApp="true">
 
     <uses-sdk android:minSdkVersion="19"/>
 
     <permission android:name="com.android.rcs.eab.permission.READ_WRITE_EAB"
-            android:protectionLevel="signatureOrSystem" />
+         android:protectionLevel="signatureOrSystem"/>
 
-    <protected-broadcast android:name="android.provider.rcs.eab.EAB_NEW_CONTACT_INSERTED" />
-    <protected-broadcast android:name="android.provider.rcs.eab.EAB_DATABASE_RESET" />
-    <protected-broadcast android:name="com.android.service.ims.presence.capability_polling_retry" />
-    <protected-broadcast android:name="com.android.service.ims.presence.periodical_capability_discovery" />
+    <protected-broadcast android:name="android.provider.rcs.eab.EAB_NEW_CONTACT_INSERTED"/>
+    <protected-broadcast android:name="android.provider.rcs.eab.EAB_DATABASE_RESET"/>
+    <protected-broadcast android:name="com.android.service.ims.presence.capability_polling_retry"/>
+    <protected-broadcast android:name="com.android.service.ims.presence.periodical_capability_discovery"/>
 
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
@@ -51,64 +51,65 @@
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
     <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
-    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
+    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
     <uses-permission android:name="com.android.rcs.eab.permission.READ_WRITE_EAB"/>
     <uses-permission android:name="android.permission.READ_PROFILE"/>
     <uses-permission android:name="com.android.ims.rcs.permission.STATUS_CHANGED"/>
     <uses-permission android:name="com.android.ims.permission.PRESENCE_ACCESS"/>
 
-    <application
-        android:label="@string/app_label"
-        android:singleUser="true"
-        android:process="com.android.ims.rcsservice">
+    <application android:label="@string/app_label"
+         android:singleUser="true"
+         android:process="com.android.ims.rcsservice">
 
-        <service
-            android:name=".PollingService"
-            android:excludeFromRecents="true"
-            android:singleUser="true"
-            android:permission="com.android.ims.permission.PRESENCE_ACCESS">
+        <service android:name=".PollingService"
+             android:excludeFromRecents="true"
+             android:singleUser="true"
+             android:permission="com.android.ims.permission.PRESENCE_ACCESS">
         </service>
 
-        <receiver android:name=".DeviceBoot" androidprv:systemUserOnly="true">
+        <receiver android:name=".DeviceBoot"
+             androidprv:systemUserOnly="true"
+             android:exported="true">
             <intent-filter android:priority="103">
                  <action android:name="android.intent.action.BOOT_COMPLETED"/>
             </intent-filter>
         </receiver>
 
-        <receiver android:name=".DeviceShutdown" androidprv:systemUserOnly="true">
+        <receiver android:name=".DeviceShutdown"
+             androidprv:systemUserOnly="true"
+             android:exported="true">
             <intent-filter>
                  <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
             </intent-filter>
         </receiver>
 
         <receiver android:name=".AlarmBroadcastReceiver"
-            androidprv:systemUserOnly="true"
-            android:permission="com.android.ims.permission.PRESENCE_ACCESS">
+             androidprv:systemUserOnly="true"
+             android:permission="com.android.ims.permission.PRESENCE_ACCESS"
+             android:exported="true">
             <intent-filter>
                 <action android:name="com.android.service.ims.presence.periodical_capability_discovery"/>
                 <action android:name="com.android.service.ims.presence.capability_polling_retry"/>
-                <action android:name="android.provider.rcs.eab.EAB_NEW_CONTACT_INSERTED" />
+                <action android:name="android.provider.rcs.eab.EAB_NEW_CONTACT_INSERTED"/>
             </intent-filter>
         </receiver>
 
         <service android:name=".PersistService"
-          android:exported="false"
-          android:permission="com.android.ims.permission.PRESENCE_ACCESS">
+             android:exported="false"
+             android:permission="com.android.ims.permission.PRESENCE_ACCESS">
             <intent-filter>
                 <action android:name="com.android.ims.ACTION_PRESENCE_CHANGED"/>
             </intent-filter>
         </service>
 
-        <service
-            android:name="com.android.service.ims.presence.EABService"
-            android:enabled="true">
+        <service android:name="com.android.service.ims.presence.EABService"
+             android:enabled="true">
         </service>
 
-        <provider
-            android:name=".EABProvider"
-            android:permission="com.android.rcs.eab.permission.READ_WRITE_EAB"
-            android:exported="true"
-            android:enabled="true"
-            android:authorities="com.android.rcs.eab" />
+        <provider android:name=".EABProvider"
+             android:permission="com.android.rcs.eab.permission.READ_WRITE_EAB"
+             android:exported="true"
+             android:enabled="true"
+             android:authorities="com.android.rcs.eab"/>
     </application>
 </manifest>
diff --git a/rcs/presencepolling/src/com/android/service/ims/presence/CapabilityPolling.java b/rcs/presencepolling/src/com/android/service/ims/presence/CapabilityPolling.java
index a92d133..5002b60 100644
--- a/rcs/presencepolling/src/com/android/service/ims/presence/CapabilityPolling.java
+++ b/rcs/presencepolling/src/com/android/service/ims/presence/CapabilityPolling.java
@@ -90,6 +90,7 @@
     private int mPublished = -1;
     private int mProvisioned = -1;
     private int mDefaultSubId;
+    private boolean isInitializing = false;
 
     private HandlerThread mDiscoveryThread;
     private Handler mDiscoveryHandler;
@@ -805,13 +806,16 @@
     // Track the default subscription (the closest we can get to MSIM).
     // call from main thread only.
     public void handleDefaultSubscriptionChanged(int newDefaultSubId) {
-        logger.print("registerImsCallbacksAndSetAssociatedSubscription: new default= "
+        logger.print("handleDefaultSubscriptionChanged: new default= "
                 + newDefaultSubId);
+
         if (!SubscriptionManager.isValidSubscriptionId(newDefaultSubId)) {
             return;
         }
-        if (mDefaultSubId == newDefaultSubId) {
+        if (isInitializing && (mDefaultSubId == newDefaultSubId)) {
             return;
+        } else {
+            isInitializing = true;
         }
         // unregister old default first
         if (SubscriptionManager.isValidSubscriptionId(mDefaultSubId)) {
diff --git a/rcs/presencepolling/src/com/android/service/ims/presence/PollingService.java b/rcs/presencepolling/src/com/android/service/ims/presence/PollingService.java
index ed511e7..8bfd8d1 100644
--- a/rcs/presencepolling/src/com/android/service/ims/presence/PollingService.java
+++ b/rcs/presencepolling/src/com/android/service/ims/presence/PollingService.java
@@ -209,6 +209,7 @@
             logger.info("Starting CapabilityPolling...");
             mCapabilityPolling = CapabilityPolling.getInstance(this);
             mCapabilityPolling.start();
+            mCapabilityPolling.handleDefaultSubscriptionChanged(mDefaultSubId);
         }
     }
 
diff --git a/rcs/rcsmanager/src/java/com/android/ims/internal/ContactNumberUtils.java b/rcs/rcsmanager/src/java/com/android/ims/internal/ContactNumberUtils.java
index 1ed03f9..094afb4 100644
--- a/rcs/rcsmanager/src/java/com/android/ims/internal/ContactNumberUtils.java
+++ b/rcs/rcsmanager/src/java/com/android/ims/internal/ContactNumberUtils.java
@@ -134,7 +134,9 @@
     public static int NUMBER_INVALID = 5;
 
     /**
-     * Check if it is a valid contact number for presence
+     * Check if it is a valid contact number for presence.
+     *
+     * Note: mContext must be set via setContext() before calling this method.
      *
      * @param phoneNumber read from contact db.
      * @return contact number error code.
@@ -161,7 +163,16 @@
             return NUMBER_INVALID;
         }
 
-        if (PhoneNumberUtils.isEmergencyNumber(number)) {
+        boolean isEmergencyNumber;
+        if (mContext == null) {
+            Log.e(TAG, "context is unexpectedly null to provide emergency identification service");
+            isEmergencyNumber = false;
+        } else {
+            TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
+            isEmergencyNumber = tm.isEmergencyNumber(number);
+        }
+
+        if (isEmergencyNumber) {
             return NUMBER_EMERGENCY;
         // TODO: To handle short code
         //} else if ((mContext != null) && PhoneNumberUtils.isN11Number(mContext, number)) {
diff --git a/rcs/rcsservice/AndroidManifest.xml b/rcs/rcsservice/AndroidManifest.xml
index 77921c3..6d108a1 100644
--- a/rcs/rcsservice/AndroidManifest.xml
+++ b/rcs/rcsservice/AndroidManifest.xml
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<?xml version="1.0" encoding="utf-8"?>
 <!--
  * Copyright (c) 2015, Motorola Mobility LLC
  * All rights reserved.
@@ -28,23 +28,22 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
- android:versionCode="1"
- android:versionName="2.4.6"
- coreApp="true"
- android:sharedUserId="android.uid.phone"
- package="com.android.service.ims">
+     xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
+     android:versionCode="1"
+     android:versionName="2.4.6"
+     coreApp="true"
+     android:sharedUserId="android.uid.phone"
+     package="com.android.service.ims">
 
     <uses-sdk android:minSdkVersion="19"/>
 
-    <permission
-        android:name="com.android.ims.rcs.permission.STATUS_CHANGED"
-        android:protectionLevel="signatureOrSystem" />
+    <permission android:name="com.android.ims.rcs.permission.STATUS_CHANGED"
+         android:protectionLevel="signatureOrSystem"/>
 
     <permission android:name="com.android.ims.permission.PRESENCE_ACCESS"
-        android:label="@string/ims_presence_permission"
-        android:description="@string/ims_ims_permission_desc"
-        android:protectionLevel="signatureOrSystem" />
+         android:label="@string/ims_presence_permission"
+         android:description="@string/ims_ims_permission_desc"
+         android:protectionLevel="signatureOrSystem"/>
 
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
     <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
@@ -55,24 +54,27 @@
     <uses-permission android:name="com.android.ims.permission.PRESENCE_ACCESS"/>
     <uses-permission android:name="com.android.rcs.eab.permission.READ_WRITE_EAB"/>
 
-    <application android:name="RcsServiceApp" android:persistent="true"
-        android:process="com.android.ims.rcsservice">
+    <application android:name="RcsServiceApp"
+         android:persistent="true"
+         android:process="com.android.ims.rcsservice">
         <service android:name="com.android.service.ims.RcsService"
-            android:exported="true"
-            android:enabled="true"
-            android:permission="com.android.ims.permission.PRESENCE_ACCESS">
+             android:exported="true"
+             android:enabled="true"
+             android:permission="com.android.ims.permission.PRESENCE_ACCESS">
         </service>
 
         <receiver android:name="com.android.service.ims.DeviceShutdown"
-            androidprv:systemUserOnly="true">
+             androidprv:systemUserOnly="true"
+             android:exported="true">
             <intent-filter>
                  <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
             </intent-filter>
         </receiver>
 
         <receiver android:name=".AlarmBroadcastReceiver"
-            android:permission="com.android.ims.permission.PRESENCE_ACCESS"
-            androidprv:systemUserOnly="true">
+             android:permission="com.android.ims.permission.PRESENCE_ACCESS"
+             androidprv:systemUserOnly="true"
+             android:exported="true">
             <intent-filter>
                 <action android:name="com.android.service.ims.presence.retry"/>
                 <action android:name="com.android.service.ims.presence.task.timeout"/>
diff --git a/rcs/rcsservice/src/com/android/service/ims/RcsStackAdaptor.java b/rcs/rcsservice/src/com/android/service/ims/RcsStackAdaptor.java
index c34bfde..e279c83 100644
--- a/rcs/rcsservice/src/com/android/service/ims/RcsStackAdaptor.java
+++ b/rcs/rcsservice/src/com/android/service/ims/RcsStackAdaptor.java
@@ -255,14 +255,16 @@
     public void updatePublisherState(@PresenceBase.PresencePublishState int publishState) {
         synchronized (mSyncObj) {
             logger.print("mPublishingState=" + mPublishingState + " publishState=" + publishState);
+            if (mPublishingState != publishState ) {
+                Intent publishIntent = new Intent(RcsPresence.ACTION_PUBLISH_STATE_CHANGED);
+                publishIntent.putExtra(RcsPresence.EXTRA_PUBLISH_STATE, publishState);
+                // Start PersistService and broadcast to other receivers that are listening
+                // dynamically.
+                mContext.sendStickyBroadcast(publishIntent);
+                launchPersistService(publishIntent);
+            }
             mPublishingState = publishState;
         }
-        Intent publishIntent = new Intent(RcsPresence.ACTION_PUBLISH_STATE_CHANGED);
-        publishIntent.putExtra(RcsPresence.EXTRA_PUBLISH_STATE, publishState);
-        // Start PersistService and broadcast to other receivers that are listening
-        // dynamically.
-        mContext.sendStickyBroadcast(publishIntent);
-        launchPersistService(publishIntent);
     }
 
     @Override
@@ -695,7 +697,7 @@
             intent.putExtra("times", times);
             intent.setPackage(mContext.getPackageName());
             mRetryAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent,
-                    PendingIntent.FLAG_UPDATE_CURRENT);
+                    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
 
             // Wait for 1s to ignore duplicate init request as possible as we can.
             long timeSkip = 1000;
diff --git a/rcs/rcsservice/src/com/android/service/ims/presence/PresencePublication.java b/rcs/rcsservice/src/com/android/service/ims/presence/PresencePublication.java
index 692ad0a..29391f7 100644
--- a/rcs/rcsservice/src/com/android/service/ims/presence/PresencePublication.java
+++ b/rcs/rcsservice/src/com/android/service/ims/presence/PresencePublication.java
@@ -1033,7 +1033,7 @@
         Intent intent = new Intent(ACTION_RETRY_PUBLISH_ALARM);
         intent.setPackage(mContext.getPackageName());
         mRetryAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent,
-                PendingIntent.FLAG_UPDATE_CURRENT);
+                PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);
 
         if(mAlarmManager == null) {
             mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);