(Telecom-system part 4) Move telecom to the system server process.

Also remove TelecomApp since it is no longer run/necessary after this
change.

Bug: 18112269
Change-Id: I901023a2333b5c664cd2dc314d2df638355e9d41
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4021d07..970b35b 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -19,7 +19,7 @@
         package="com.android.server.telecom"
         android:debuggable="true"
         coreApp="true"
-        android:sharedUserId="android.uid.phone">
+        android:sharedUserId="android.uid.system">
 
     <!-- Prevents the activity manager from delaying any activity-start
          requests by this package, including requests immediately after
@@ -28,6 +28,7 @@
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
     <uses-permission android:name="android.permission.MANAGE_USERS" />
+    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
     <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
     <uses-permission android:name="android.permission.READ_CALL_LOG" />
@@ -53,8 +54,7 @@
          can check for incompatible APIs. -->
     <uses-sdk android:minSdkVersion="19" />
 
-    <application android:name="TelecomApp"
-            android:persistent="true"
+    <application android:persistent="true"
             android:label="@string/telecommAppLabel"
             android:icon="@mipmap/ic_launcher_phone"
             android:allowBackup="false"
@@ -79,7 +79,8 @@
         <activity android:name="CallActivity"
                 android:theme="@style/Theme.Telecomm.Transparent"
                 android:permission="android.permission.CALL_PHONE"
-                android:excludeFromRecents="true">
+                android:excludeFromRecents="true"
+                android:process=":ui">
             <!-- CALL action intent filters for the various ways of initiating an outgoing call. -->
             <intent-filter>
                 <action android:name="android.intent.action.CALL" />
@@ -115,7 +116,8 @@
              the system from processing this intent (b/8871505). -->
         <activity-alias android:name="PrivilegedCallActivity"
                 android:targetActivity="CallActivity"
-                android:permission="android.permission.CALL_PRIVILEGED">
+                android:permission="android.permission.CALL_PRIVILEGED"
+                android:process=":ui">
             <intent-filter android:priority="1000">
                 <action android:name="android.intent.action.CALL_PRIVILEGED" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -149,7 +151,8 @@
              that scheme be removed from this activity? -->
         <activity-alias android:name="EmergencyCallActivity"
                 android:targetActivity="CallActivity"
-                android:permission="android.permission.CALL_PRIVILEGED">
+                android:permission="android.permission.CALL_PRIVILEGED"
+                android:process=":ui">
             <intent-filter android:priority="1000">
                 <action android:name="android.intent.action.CALL_EMERGENCY" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -174,7 +177,8 @@
             </intent-filter>
         </activity-alias>
 
-        <receiver android:name="TelecomBroadcastReceiver" android:exported="false">
+        <receiver android:name="TelecomBroadcastReceiver" android:exported="false"
+                android:process="system">
             <intent-filter>
                 <action android:name="com.android.server.telecom.ACTION_CALL_BACK_FROM_NOTIFICATION" />
                 <action android:name="com.android.server.telecom.ACTION_CALL_BACK_FROM_NOTIFICATION" />
@@ -182,7 +186,8 @@
             </intent-filter>
         </receiver>
 
-        <receiver android:name="PhoneAccountBroadcastReceiver">
+        <receiver android:name="PhoneAccountBroadcastReceiver"
+                android:process="system">
             <intent-filter>
                 <action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
                 <data android:scheme="package" />
@@ -192,7 +197,8 @@
         <activity android:name=".RespondViaSmsSettings$Settings"
                   android:label="@string/respond_via_sms_setting_title"
                   android:configChanges="orientation|screenSize|keyboardHidden"
-                  android:theme="@style/Theme.Telecom.DialerSettings">
+                  android:theme="@style/Theme.Telecom.DialerSettings"
+                  android:process=":ui">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
             </intent-filter>
@@ -202,22 +208,27 @@
                 android:configChanges="orientation|screenSize|keyboardHidden"
                 android:excludeFromRecents="true"
                 android:launchMode="singleInstance"
-                android:theme="@style/Theme.Telecomm.Transparent">
+                android:theme="@style/Theme.Telecomm.Transparent"
+                android:process=":ui">
         </activity>
 
         <receiver android:name=".CallReceiver"
-            android:exported="false">
+                android:exported="true"
+                android:permission="android.permission.MODIFY_PHONE_STATE"
+                android:process="system">
         </receiver>
 
         <service android:name="BluetoothPhoneService"
-                android:singleUser="true">
+                android:singleUser="true"
+                android:process="system">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothHeadsetPhone" />
             </intent-filter>
         </service>
 
         <service android:name=".TelecomService"
-                android:singleUser="true">
+                android:singleUser="true"
+                android:process="system">
             <intent-filter>
                 <android android:name="android.telecom.ITelecomService" />
             </intent-filter>
diff --git a/src/com/android/server/telecom/CallActivity.java b/src/com/android/server/telecom/CallActivity.java
index 5ce0eb7..37e24f6 100644
--- a/src/com/android/server/telecom/CallActivity.java
+++ b/src/com/android/server/telecom/CallActivity.java
@@ -123,7 +123,6 @@
         }
 
         intent.putExtra(CallReceiver.KEY_IS_DEFAULT_DIALER, isDefaultDialer());
-
         sendBroadcastToReceiver(intent);
     }
 
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 602ab4b..dc65c9e 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -77,7 +77,7 @@
     /**
      * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
      */
-    private static CallsManager INSTANCE = null;
+    private static CallsManager sInstance = null;
 
     private static final String TAG = "CallsManager";
 
@@ -138,7 +138,7 @@
 
     /** Singleton accessor. */
     static CallsManager getInstance() {
-        return INSTANCE;
+        return sInstance;
     }
 
     /**
@@ -147,7 +147,7 @@
      * @param instance The instance to set.
      */
     static void initialize(CallsManager instance) {
-        INSTANCE = instance;
+        sInstance = instance;
     }
 
     /**
diff --git a/src/com/android/server/telecom/TelecomApp.java b/src/com/android/server/telecom/TelecomApp.java
deleted file mode 100644
index 8f46b49..0000000
--- a/src/com/android/server/telecom/TelecomApp.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * 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.server.telecom;
-
-import android.app.Application;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-import android.content.ServiceConnection;
-import android.os.IBinder;
-import android.os.ServiceManager;
-import android.os.UserHandle;
-
-/**
- * Top-level Application class for Telecom.
- */
-public final class TelecomApp extends Application {
-
-    /**
-     * Used to bind to the telecom service. Once created, the telecom service will start the telecom
-     * global state.
-     */
-    private class TelecomServiceConnection implements ServiceConnection {
-        /** {@inheritDoc} */
-        @Override public void onServiceConnected(ComponentName name, IBinder service) {
-            Log.i(this, "onServiceConnected: %s", name);
-            ServiceManager.addService(Context.TELECOM_SERVICE, service);
-        }
-
-        /** {@inheritDoc} */
-        @Override public void onServiceDisconnected(ComponentName name) {
-            Log.i(this, "onDisconnected: %s", name);
-            bindToService();
-        }
-    }
-
-    private ServiceConnection mServiceConnection;
-
-    /** {@inheritDoc} */
-    @Override
-    public void onCreate() {
-        super.onCreate();
-
-        if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
-            bindToService();
-        }
-    }
-
-    private void bindToService() {
-        if (mServiceConnection != null) {
-            unbindService(mServiceConnection);
-            mServiceConnection = null;
-        }
-
-        ComponentName componentName = new ComponentName(this, TelecomService.class);
-        Intent intent = new Intent(TelecomService.SERVICE_INTERFACE);
-        intent.setComponent(componentName);
-        int bindFlags = Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT;
-
-        Log.i(this, "binding to TelecomService.");
-        ServiceConnection serviceConnection = new TelecomServiceConnection();
-        if (bindServiceAsUser(intent, serviceConnection, bindFlags, UserHandle.OWNER)) {
-            mServiceConnection = serviceConnection;
-            Log.i(this, "TelecomService binding successful");
-        } else {
-            Log.e(this, null, "Failed to bind to TelecomService.");
-        }
-    }
-}
diff --git a/src/com/android/server/telecom/TelecomGlobals.java b/src/com/android/server/telecom/TelecomGlobals.java
index a11b470..cf0936c 100644
--- a/src/com/android/server/telecom/TelecomGlobals.java
+++ b/src/com/android/server/telecom/TelecomGlobals.java
@@ -87,6 +87,7 @@
 
         mCallsManager = new CallsManager(mContext, mMissedCallNotifier, mPhoneAccountRegistrar);
         CallsManager.initialize(mCallsManager);
+        Log.i(this, "CallsManager initialized");
 
         // Start the BluetoothPhoneService
         BluetoothPhoneService.start(mContext);
diff --git a/src/com/android/server/telecom/TelecomService.java b/src/com/android/server/telecom/TelecomService.java
index eab7947..0a792b6 100644
--- a/src/com/android/server/telecom/TelecomService.java
+++ b/src/com/android/server/telecom/TelecomService.java
@@ -183,6 +183,8 @@
     class TelecomServiceImpl extends ITelecomService.Stub {
         @Override
         public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
+            enforceReadPermission();
+            long token = Binder.clearCallingIdentity();
             try {
                 PhoneAccountHandle defaultOutgoingPhoneAccount =
                         mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(uriScheme);
@@ -196,6 +198,8 @@
             } catch (Exception e) {
                 Log.e(this, e, "getDefaultOutgoingPhoneAccount");
                 throw e;
+            } finally {
+                Binder.restoreCallingIdentity(token);
             }
         }
 
@@ -230,23 +234,31 @@
 
         @Override
         public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
+            enforceReadPermission();
+            long token = Binder.clearCallingIdentity();
             try {
                 return filterForAccountsVisibleToCaller(
                         mPhoneAccountRegistrar.getCallCapablePhoneAccounts());
             } catch (Exception e) {
                 Log.e(this, e, "getCallCapablePhoneAccounts");
                 throw e;
+            } finally {
+                Binder.restoreCallingIdentity(token);
             }
         }
 
         @Override
         public List<PhoneAccountHandle> getPhoneAccountsSupportingScheme(String uriScheme) {
+            enforceReadPermission();
+            long token = Binder.clearCallingIdentity();
             try {
                 return filterForAccountsVisibleToCaller(
                         mPhoneAccountRegistrar.getCallCapablePhoneAccounts(uriScheme));
             } catch (Exception e) {
                 Log.e(this, e, "getPhoneAccountsSupportingScheme %s", uriScheme);
                 throw e;
+            } finally {
+                Binder.restoreCallingIdentity(token);
             }
         }
 
@@ -343,12 +355,16 @@
 
         @Override
         public List<PhoneAccountHandle> getSimCallManagers() {
+            enforceReadPermission();
+            long token = Binder.clearCallingIdentity();
             try {
                 return filterForAccountsVisibleToCaller(
                         mPhoneAccountRegistrar.getConnectionManagerPhoneAccounts());
             } catch (Exception e) {
                 Log.e(this, e, "getSimCallManagers");
                 throw e;
+            } finally {
+                Binder.restoreCallingIdentity(token);
             }
         }