Merge "Update permission names." into oc-dr1-dev
diff --git a/Android.mk b/Android.mk
index f514ffc..24fb423 100644
--- a/Android.mk
+++ b/Android.mk
@@ -20,7 +20,6 @@
         android-support-v7-preference \
         android-support-v7-recyclerview \
         android-support-v14-preference \
-        colorextraction \
         guava \
         volley
 
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 20c21ca..93829ed 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -24,7 +24,7 @@
 
     <uses-sdk
         android:minSdkVersion="23"
-        android:targetSdkVersion="23" />
+        android:targetSdkVersion="26" />
 
     <original-package android:name="com.android.phone" />
 
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index daafe87..fd64ff2 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -53,13 +53,12 @@
 import android.view.WindowManager;
 import android.widget.EditText;
 
+import com.android.internal.colorextraction.ColorExtractor;
+import com.android.internal.colorextraction.ColorExtractor.GradientColors;
+import com.android.internal.colorextraction.drawable.GradientDrawable;
 import com.android.phone.common.dialpad.DialpadKeyButton;
 import com.android.phone.common.util.ViewUtil;
 
-import com.google.android.colorextraction.ColorExtractor;
-import com.google.android.colorextraction.ColorExtractor.GradientColors;
-import com.google.android.colorextraction.drawable.GradientDrawable;
-
 /**
  * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
  *
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 31bca92..61bf860 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -16,8 +16,6 @@
 
 package com.android.phone;
 
-import static android.Manifest.permission.READ_PHONE_STATE;
-
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -52,6 +50,7 @@
 import android.util.Log;
 import android.widget.Toast;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.TelephonyCapabilities;
@@ -62,6 +61,8 @@
 import java.util.List;
 import java.util.Set;
 
+import static android.Manifest.permission.READ_PHONE_STATE;
+
 /**
  * NotificationManager-related utility code for the Phone app.
  *
@@ -543,7 +544,8 @@
      * appears when you lose data connectivity because you're roaming and
      * you have the "data roaming" feature turned off.
      */
-    /* package */ void showDataDisconnectedRoaming() {
+    @VisibleForTesting
+    public void showDataDisconnectedRoaming() {
         if (DBG) log("showDataDisconnectedRoaming()...");
 
         // "Mobile network settings" screen / dialog
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 2ed5139..1d61c8e 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -680,10 +680,12 @@
                 // if (a) you have the "data roaming" feature turned off, and
                 // (b) you just lost data connectivity because you're roaming.
                 // (c) if we haven't shown the notification for this disconnection earlier.
+                // (d) if data was enabled for the sim
                 if (!mDataDisconnectedDueToRoaming
                         && PhoneConstants.DataState.DISCONNECTED.name().equals(state)
                         && Phone.REASON_ROAMING_ON.equals(reason)
-                        && !phone.getDataRoamingEnabled()) {
+                        && !phone.getDataRoamingEnabled()
+                        && phone.getDataEnabled()) {
                     // Notify the user that data call is disconnected due to roaming. Note that
                     // calling this multiple times will not cause multiple notifications.
                     mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_DISCONNECTED);
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 0a1d073..f793a57 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -26,6 +26,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.ComponentInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.net.NetworkStats;
@@ -96,6 +97,7 @@
 import com.android.internal.telephony.RIL;
 import com.android.internal.telephony.RILConstants;
 import com.android.internal.telephony.SubscriptionController;
+import com.android.internal.telephony.euicc.EuiccConnector;
 import com.android.internal.telephony.uicc.IccIoResult;
 import com.android.internal.telephony.uicc.IccUtils;
 import com.android.internal.telephony.uicc.SIMRecords;
@@ -193,6 +195,9 @@
     private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
     private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
 
+    // The AID of ISD-R.
+    private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
+
     private NetworkScanRequestTracker mNetworkScanRequestTracker;
 
     /**
@@ -2397,12 +2402,25 @@
     }
 
     @Override
-    public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
+    public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
+            int subId, String callingPackage, String aid, int p2) {
         enforceModifyPermissionOrCarrierPrivilege(subId);
 
-        if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
+        if (TextUtils.equals(ISDR_AID, aid)) {
+            // Only allows LPA to open logical channel to ISD-R.
+            mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
+            ComponentInfo bestComponent =
+                    EuiccConnector.findBestComponent(mPhone.getContext().getPackageManager());
+            if (bestComponent == null
+                    || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
+                loge("The calling package is not allowed to access ISD-R.");
+                throw new SecurityException("The calling package is not allowed to access ISD-R.");
+            }
+        }
+
+        if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
         IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
-            CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
+                CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), subId);
         if (DBG) log("iccOpenLogicalChannel: " + response);
         return response;
     }
diff --git a/tests/src/com/android/TelephonyTestBase.java b/tests/src/com/android/TelephonyTestBase.java
index 044f26b..edf575b 100644
--- a/tests/src/com/android/TelephonyTestBase.java
+++ b/tests/src/com/android/TelephonyTestBase.java
@@ -20,6 +20,7 @@
 import android.os.Handler;
 import android.os.Looper;
 import android.support.test.InstrumentationRegistry;
+import android.util.Log;
 
 import org.mockito.MockitoAnnotations;
 
@@ -68,4 +69,12 @@
             }
         }
     }
+
+    protected void waitForMs(long ms) {
+        try {
+            Thread.sleep(ms);
+        } catch (InterruptedException e) {
+            Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
+        }
+    }
 }
diff --git a/tests/src/com/android/phone/PhoneGlobalsTest.java b/tests/src/com/android/phone/PhoneGlobalsTest.java
new file mode 100644
index 0000000..b6d216e
--- /dev/null
+++ b/tests/src/com/android/phone/PhoneGlobalsTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.phone;
+
+import android.content.Intent;
+import android.support.test.runner.AndroidJUnit4;
+
+import com.android.TelephonyTestBase;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.TelephonyIntents;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.lang.reflect.Field;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(AndroidJUnit4.class)
+public class PhoneGlobalsTest extends TelephonyTestBase {
+
+    private Phone mPhone = PhoneFactory.getDefaultPhone();
+
+    private PhoneGlobals mPhoneGlobals = PhoneGlobals.getInstance();
+
+    private NotificationMgr mNotificationMgr = mock(NotificationMgr.class);
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        Field notificationMgr = PhoneGlobals.class.getDeclaredField("notificationMgr");
+        notificationMgr.setAccessible(true);
+        notificationMgr.set(mPhoneGlobals, mNotificationMgr);
+    }
+
+    @Test
+    public void testDataDisconnectedNotification() {
+        mPhone.setDataRoamingEnabled(false);
+        // Test no notification sent out when data is disabled, data raoming enabled and data
+        // disconnected because of roaming.
+        mPhone.setDataEnabled(false);
+        Intent intent = new Intent(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
+        intent.putExtra(PhoneConstants.DATA_APN_TYPE_KEY,
+                PhoneConstants.APN_TYPE_DEFAULT);
+        intent.putExtra(PhoneConstants.STATE_KEY, PhoneConstants.DataState.DISCONNECTED.name());
+        intent.putExtra(PhoneConstants.STATE_CHANGE_REASON_KEY, Phone.REASON_ROAMING_ON);
+        mContext.sendBroadcast(intent);
+
+        waitForMs(200);
+
+        verify(mNotificationMgr, times(0)).showDataDisconnectedRoaming();
+
+        // Test notification sent out when data is enabled, data raoming enabled and data
+        // disconnected because of roaming.
+        mPhone.setDataEnabled(true);
+        mContext.sendBroadcast(intent);
+
+        waitForMs(200);
+
+        verify(mNotificationMgr, times(1)).showDataDisconnectedRoaming();
+    }
+}