(Telecom-system part 1) Move global state from TelecomApp to TelecomGlobals.

TelecomApp will no longer function once telecom moves to a shared
process so we need an alternative for initializing global state. This
change moves everything in TelecomApp to TelecomGlobals.

Bug: 18112269
Change-Id: I79b5e7a3337adb6bae55ebe22c5a8f8506391ef3
diff --git a/src/com/android/server/telecom/BluetoothPhoneService.java b/src/com/android/server/telecom/BluetoothPhoneService.java
index bf28699..da39524 100644
--- a/src/com/android/server/telecom/BluetoothPhoneService.java
+++ b/src/com/android/server/telecom/BluetoothPhoneService.java
@@ -851,8 +851,11 @@
      * phone account for PhoneAccount.SCHEME_TEL.
      */
     private PhoneAccount getBestPhoneAccount() {
-        TelecomApp app = (TelecomApp) getApplication();
-        PhoneAccountRegistrar registry = app.getPhoneAccountRegistrar();
+        PhoneAccountRegistrar registry = TelecomGlobals.getInstance().getPhoneAccountRegistrar();
+        if (registry == null) {
+            return null;
+        }
+
         Call call = getCallsManager().getForegroundCall();
 
         PhoneAccount account = null;
diff --git a/src/com/android/server/telecom/TelecomApp.java b/src/com/android/server/telecom/TelecomApp.java
index 09f62c7..f9df53c 100644
--- a/src/com/android/server/telecom/TelecomApp.java
+++ b/src/com/android/server/telecom/TelecomApp.java
@@ -17,82 +17,19 @@
 package com.android.server.telecom;
 
 import android.app.Application;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.os.ServiceManager;
 import android.os.UserHandle;
-import android.os.UserManager;
 
 /**
  * Top-level Application class for Telecom.
  */
 public final class TelecomApp extends Application {
-    private static final IntentFilter USER_SWITCHED_FILTER =
-            new IntentFilter(Intent.ACTION_USER_SWITCHED);
-
-    /**
-     * The Telecom service implementation.
-     */
-    private TelecomServiceImpl mTelecomService;
-
-    /**
-     * Missed call notifier. Exists here so that the instance can be shared with
-     * {@link TelecomBroadcastReceiver}.
-     */
-    private MissedCallNotifier mMissedCallNotifier;
-
-    /**
-     * Maintains the list of registered {@link android.telecom.PhoneAccountHandle}s.
-     */
-    private PhoneAccountRegistrar mPhoneAccountRegistrar;
-
-    /**
-     * The calls manager for the Telecom service.
-     */
-    private CallsManager mCallsManager;
-
-    private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
-            UserHandle currentUserHandle = new UserHandle(userHandleId);
-            mPhoneAccountRegistrar.setCurrentUserHandle(currentUserHandle);
-        }
-    };
-
     /** {@inheritDoc} */
     @Override
     public void onCreate() {
         super.onCreate();
 
         if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
-            // Note: This style of initialization mimics what will be performed once Telecom is
-            // moved
-            // to run in the system service. The emphasis is on ensuring that initialization of all
-            // telecom classes happens in one place without relying on Singleton initialization.
-            mMissedCallNotifier = new MissedCallNotifier(this);
-            mPhoneAccountRegistrar = new PhoneAccountRegistrar(this);
-
-            mCallsManager = new CallsManager(this, mMissedCallNotifier, mPhoneAccountRegistrar);
-            CallsManager.initialize(mCallsManager);
-
-            mTelecomService = new TelecomServiceImpl(mMissedCallNotifier, mPhoneAccountRegistrar,
-                    mCallsManager, this);
-            ServiceManager.addService(Context.TELECOM_SERVICE, mTelecomService);
-
-            // Start the BluetoothPhoneService
-            BluetoothPhoneService.start(this);
+            TelecomGlobals.getInstance().initialize(this);
         }
-        registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
-    }
-
-    MissedCallNotifier getMissedCallNotifier() {
-        return mMissedCallNotifier;
-    }
-
-    PhoneAccountRegistrar getPhoneAccountRegistrar() {
-        return mPhoneAccountRegistrar;
     }
 }
diff --git a/src/com/android/server/telecom/TelecomGlobals.java b/src/com/android/server/telecom/TelecomGlobals.java
new file mode 100644
index 0000000..e30fee1
--- /dev/null
+++ b/src/com/android/server/telecom/TelecomGlobals.java
@@ -0,0 +1,109 @@
+/*
+ * 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.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+
+/**
+ * Top-level Application class for Telecom.
+ */
+public final class TelecomGlobals {
+    private static final String TAG = TelecomGlobals.class.getSimpleName();
+
+    private static final IntentFilter USER_SWITCHED_FILTER =
+            new IntentFilter(Intent.ACTION_USER_SWITCHED);
+
+    private static final TelecomGlobals INSTANCE = new TelecomGlobals();
+
+    /**
+     * The Telecom service implementation.
+     */
+    private TelecomServiceImpl mTelecomService;
+
+    /**
+     * Missed call notifier. Exists here so that the instance can be shared with
+     * {@link TelecomBroadcastReceiver}.
+     */
+    private MissedCallNotifier mMissedCallNotifier;
+
+    /**
+     * Maintains the list of registered {@link android.telecom.PhoneAccountHandle}s.
+     */
+    private PhoneAccountRegistrar mPhoneAccountRegistrar;
+
+    /**
+     * The calls manager for the Telecom service.
+     */
+    private CallsManager mCallsManager;
+
+    /**
+     * The application context.
+     */
+    private Context mContext;
+
+    private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
+            UserHandle currentUserHandle = new UserHandle(userHandleId);
+            mPhoneAccountRegistrar.setCurrentUserHandle(currentUserHandle);
+        }
+    };
+
+    static TelecomGlobals getInstance() {
+        return INSTANCE;
+    }
+
+    void initialize(Context context) {
+        if (mContext != null) {
+            Log.e(TAG, null, "Attempting to intialize TelecomGlobals a second time.");
+            return;
+        } else {
+            Log.i(TAG, "TelecomGlobals initializing");
+        }
+        mContext = context;
+
+        mMissedCallNotifier = new MissedCallNotifier(mContext);
+        mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
+
+        mCallsManager = new CallsManager(mContext, mMissedCallNotifier, mPhoneAccountRegistrar);
+        CallsManager.initialize(mCallsManager);
+
+        mTelecomService = new TelecomServiceImpl(mMissedCallNotifier, mPhoneAccountRegistrar,
+                mCallsManager, mContext);
+        ServiceManager.addService(Context.TELECOM_SERVICE, mTelecomService);
+
+        // Start the BluetoothPhoneService
+        BluetoothPhoneService.start(mContext);
+
+        mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
+    }
+
+    MissedCallNotifier getMissedCallNotifier() {
+        return mMissedCallNotifier;
+    }
+
+    PhoneAccountRegistrar getPhoneAccountRegistrar() {
+        return mPhoneAccountRegistrar;
+    }
+}