Wire up multi-SIM experience (4/4)

Change-Id: Ibfcf8a0d68bf48bd32cd0e1a155a5fa7719e53e2
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index ee47f18..c0cdc3e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -585,5 +585,11 @@
             </intent-filter>
         </provider>
 
+        <receiver android:name="com.android.services.telephony.AddAccountsReceiver">
+            <intent-filter>
+                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </receiver>
     </application>
 </manifest>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2825a7e..70dcfc2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -125,7 +125,7 @@
     <!-- Label for the "Network settings" screen in the Settings UI -->
     <string name="settings_label">Network settings</string>
     <!-- Label for connection service setting -->
-    <string name="connection_service">Connection service to use</string>
+    <string name="phone_accounts">Phone account settings</string>
     <!-- Built-in label for the default connection service setting. -->
     <string name="connection_service_default_label">Built-in connection service</string>
     <!-- Call settings screen, setting option name -->
diff --git a/res/xml/call_feature_setting.xml b/res/xml/call_feature_setting.xml
index 6c1b570..30811e4 100644
--- a/res/xml/call_feature_setting.xml
+++ b/res/xml/call_feature_setting.xml
@@ -41,11 +41,13 @@
       android:title="@string/other_settings"
       android:persistent="false" />
 
-  <ListPreference
-      android:key="button_connection_service"
-      android:title="@string/connection_service"
-      android:defaultValue="@string/connection_service_default"
-      android:persistent="true" />
+    <Preference
+            android:key="phone_accounts"
+            android:title="@string/phone_accounts" >
+        <intent
+                android:targetClass="com.android.telecomm.PhoneAccountPreferencesActivity"
+                android:targetPackage="com.android.telecomm" />
+    </Preference>
 
   <PreferenceScreen
       android:key="button_voicemail_category_key"
diff --git a/sip/src/com/android/services/telephony/sip/SipConnectionService.java b/sip/src/com/android/services/telephony/sip/SipConnectionService.java
index a46180d..a87b398 100644
--- a/sip/src/com/android/services/telephony/sip/SipConnectionService.java
+++ b/sip/src/com/android/services/telephony/sip/SipConnectionService.java
@@ -48,9 +48,7 @@
     static PhoneAccount getPhoneAccount(Context context) {
         return new PhoneAccount(
                 new ComponentName(context, SipConnectionService.class),
-                null /* id */,
-                null /* handle */,
-                PhoneAccount.CAPABILITY_CALL_PROVIDER);
+                null /* id */);
     }
 
     @Override
diff --git a/src/com/android/services/telephony/AddAccountsReceiver.java b/src/com/android/services/telephony/AddAccountsReceiver.java
new file mode 100644
index 0000000..a05698c
--- /dev/null
+++ b/src/com/android/services/telephony/AddAccountsReceiver.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2014 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.services.telephony;
+
+import com.android.phone.R;
+import com.android.services.telephony.sip.SipConnectionService;
+
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.telecomm.PhoneAccount;
+import android.telecomm.PhoneAccountMetadata;
+import android.telecomm.TelecommManager;
+
+public class AddAccountsReceiver extends BroadcastReceiver {
+
+    private static String SCHEME_TEL = "tel";
+
+    private static final ComponentName PSTN_SERVICE_COMPONENT_NAME = new ComponentName(
+            "com.android.phone",
+            TelephonyConnectionService.class.getName());
+
+    private static final ComponentName SIP_SERVICE_COMPONENT_NAME = new ComponentName(
+            "com.android.phone",
+            SipConnectionService.class.getName());
+
+    public static final PhoneAccountMetadata[] PHONE_ACCOUNTS = new PhoneAccountMetadata[] {
+            new PhoneAccountMetadata(
+                    new PhoneAccount(PSTN_SERVICE_COMPONENT_NAME, "SIM card zero"),
+                    Uri.fromParts(SCHEME_TEL, "650-555-1212", null),
+                    PhoneAccountMetadata.CAPABILITY_CALL_PROVIDER,
+                    R.drawable.fab_ic_call,
+                    "Label for SIM card zero",
+                    "Short description for SIM card zero",
+                    false),
+            new PhoneAccountMetadata(
+                    new PhoneAccount(PSTN_SERVICE_COMPONENT_NAME, "SIM card one"),
+                    Uri.fromParts(SCHEME_TEL, "650-555-1234", null),
+                    PhoneAccountMetadata.CAPABILITY_CALL_PROVIDER,
+                    R.drawable.fab_ic_call,
+                    "Label for SIM card one",
+                    "Short description for SIM card one",
+                    false),
+            new PhoneAccountMetadata(
+                    new PhoneAccount(SIP_SERVICE_COMPONENT_NAME, "SIP Account"),
+                    Uri.fromParts(SCHEME_TEL, "650-555-1111", null),
+                    PhoneAccountMetadata.CAPABILITY_CALL_PROVIDER,
+                    R.drawable.fab_ic_call,
+                    "Label for SIP Account",
+                    "Short description for SIP Account",
+                    false)
+    };
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.d(this, "onReceive");
+        try {
+            TelecommManager telecommManager = TelecommManager.from(context);
+            telecommManager.clearAccounts(PSTN_SERVICE_COMPONENT_NAME.getPackageName());
+            for (int i = 0; i < PHONE_ACCOUNTS.length; i++) {
+                telecommManager.registerPhoneAccount(PHONE_ACCOUNTS[i]);
+            }
+        } catch (Exception e) {
+            Log.e(this, e, "onReceive");
+            throw e;
+        }
+    }
+}
diff --git a/src/com/android/services/telephony/PstnIncomingCallNotifier.java b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
index e19991a..d030576 100644
--- a/src/com/android/services/telephony/PstnIncomingCallNotifier.java
+++ b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
@@ -20,7 +20,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.Message;
@@ -157,7 +156,7 @@
         Intent intent = new Intent(TelecommConstants.ACTION_INCOMING_CALL);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.putExtra(TelecommConstants.EXTRA_PHONE_ACCOUNT,
-                TelephonyConnectionService.getPhoneAccount(context));
+                AddAccountsReceiver.PHONE_ACCOUNTS[0].getAccount());
 
         Log.d(this, "Sending incoming call intent: %s", intent);
         context.startActivityAsUser(intent, UserHandle.CURRENT);
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index d82b712..04b016b 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -20,6 +20,8 @@
 import android.content.Context;
 import android.net.Uri;
 import android.os.Debug;
+import android.telecomm.PhoneAccountMetadata;
+import android.telecomm.TelecommManager;
 import android.telephony.DisconnectCause;
 import android.telephony.ServiceState;
 import android.text.TextUtils;
@@ -45,14 +47,6 @@
 
     private EmergencyCallHelper mEmergencyCallHelper;
 
-    static PhoneAccount getPhoneAccount(Context context) {
-        return new PhoneAccount(
-                new ComponentName(context, TelephonyConnectionService.class),
-                null /* id */,
-                null,
-                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
-    }
-
     @Override
     protected void onCreateOutgoingConnection(
             final ConnectionRequest request,
@@ -176,7 +170,7 @@
 
         Uri handle = getHandleFromAddress(originalConnection.getAddress());
         ConnectionRequest telephonyRequest = new ConnectionRequest(
-                getPhoneAccount(this),
+                request.getAccount(),
                 request.getCallId(),
                 handle,
                 originalConnection.getNumberPresentation(),
@@ -231,7 +225,7 @@
         }
 
         ConnectionRequest telephonyRequest = new ConnectionRequest(
-                getPhoneAccount(this),
+                request.getAccount(),
                 request.getCallId(),
                 request.getHandle(),
                 request.getHandlePresentation(),