Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f987d1a6 authored by Santos Cordon's avatar Santos Cordon
Browse files

(Telecom-system part 2) Move telecom intialization from application to service.

Instead of having TelecomApp initialize the global state of telecom, it
will bind to the telecom service and the service itself will
initialize the global state. This prepares telecom to run in the system
process since at that point TelecomAppw will stop running and the
framework will need to use the telecom service to start the global state.

Bug: 18112269
Change-Id: I76cd520e55ba2f58915ea2a7938cffd3cc101bab
parent 0b5cb4d0
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -216,5 +216,12 @@
            </intent-filter>
        </service>

        <service android:name=".TelecomService"
                android:singleUser="true">
            <intent-filter>
                <android android:name="android.telecom.ITelecomService" />
            </intent-filter>
        </service>

    </application>
</manifest>
+49 −1
Original line number Diff line number Diff line
@@ -17,19 +17,67 @@
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) {
            TelecomGlobals.getInstance().initialize(this);
            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.");
        }
    }
}
+7 −8
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.ServiceManager;
import android.os.UserHandle;

/**
@@ -38,7 +37,7 @@ public final class TelecomGlobals {
    /**
     * The Telecom service implementation.
     */
    private TelecomServiceImpl mTelecomService;
    private TelecomService mTelecomService;

    /**
     * Missed call notifier. Exists here so that the instance can be shared with
@@ -76,12 +75,12 @@ public final class TelecomGlobals {

    void initialize(Context context) {
        if (mContext != null) {
            Log.e(TAG, null, "Attempting to intialize TelecomGlobals a second time.");
            Log.e(TAG, new Exception(), "Attempting to intialize TelecomGlobals a second time.");
            return;
        } else {
            Log.i(TAG, "TelecomGlobals initializing");
        }
        mContext = context;
        mContext = context.getApplicationContext();

        mMissedCallNotifier = new MissedCallNotifier(mContext);
        mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
@@ -89,10 +88,6 @@ public final class TelecomGlobals {
        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);

@@ -106,4 +101,8 @@ public final class TelecomGlobals {
    PhoneAccountRegistrar getPhoneAccountRegistrar() {
        return mPhoneAccountRegistrar;
    }

    CallsManager getCallsManager() {
        return mCallsManager;
    }
}
+956 −0
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
package com.android.server.telecom;

import android.Manifest;
import android.annotation.SdkConstant;
import android.app.AppOpsManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -37,12 +39,10 @@ import android.telecom.CallState;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;


// TODO: Needed for move to system service: import com.android.internal.R;
import com.android.internal.telecom.ITelecomService;
import com.android.internal.util.IndentingPrintWriter;
@@ -55,16 +55,16 @@ import java.util.List;
/**
 * Implementation of the ITelecom interface.
 */
public class TelecomServiceImpl extends ITelecomService.Stub {
public class TelecomService extends Service {
    /**
     * The {@link Intent} that must be declared as handled by the service.
     */
    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
    public static final String SERVICE_INTERFACE = "android.telecom.ITelecomService";

    /** The context. */
    private Context mContext;

    /** ${inheritDoc} */
    @Override
    public IBinder asBinder() {
        return super.asBinder();
    }

    /**
     * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
     * request after sending. The main thread will notify the request when it is complete.
@@ -77,9 +77,9 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
    }

    /**
     * A handler that processes messages on the main thread in the phone process. Since many
     * of the Phone calls are not thread safe this is needed to shuttle the requests from the
     * inbound binder threads to the main thread in the phone process.
     * A handler that processes messages on the main thread. Since many of the method calls are not
     * thread safe this is needed to shuttle the requests from the inbound binder threads to the
     * main thread.
     */
    private final class MainThreadHandler extends Handler {
        @Override
@@ -128,8 +128,7 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
        }
    }

    /** Private constructor; @see init() */
    private static final String TAG = TelecomServiceImpl.class.getSimpleName();
    private static final String TAG = TelecomService.class.getSimpleName();

    private static final String SERVICE_NAME = "telecom";

@@ -142,33 +141,46 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
    private static final int MSG_GET_CURRENT_TTY_MODE = 7;
    private static final int MSG_NEW_INCOMING_CALL = 8;

    /** The singleton instance. */
    private static TelecomServiceImpl sInstance;

    private final MainThreadHandler mMainThreadHandler = new MainThreadHandler();
    private final CallsManager mCallsManager;
    private final MissedCallNotifier mMissedCallNotifier;
    private final PhoneAccountRegistrar mPhoneAccountRegistrar;
    private final AppOpsManager mAppOpsManager;
    private final UserManager mUserManager;
    private final PackageManager mPackageManager;

    public TelecomServiceImpl(
            MissedCallNotifier missedCallNotifier, PhoneAccountRegistrar phoneAccountRegistrar,
            CallsManager callsManager, Context context) {
        mMissedCallNotifier = missedCallNotifier;
        mPhoneAccountRegistrar = phoneAccountRegistrar;
        mCallsManager = callsManager;
        mContext = context;

    private CallsManager mCallsManager;
    private MissedCallNotifier mMissedCallNotifier;
    private PhoneAccountRegistrar mPhoneAccountRegistrar;
    private AppOpsManager mAppOpsManager;
    private UserManager mUserManager;
    private PackageManager mPackageManager;
    private TelecomServiceImpl mServiceImpl;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(this, "onCreate");
        mContext = this;
        mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
        mServiceImpl = new TelecomServiceImpl();

        TelecomGlobals globals = TelecomGlobals.getInstance();
        globals.initialize(this);

        mMissedCallNotifier = globals.getMissedCallNotifier();
        mPhoneAccountRegistrar = globals.getPhoneAccountRegistrar();
        mCallsManager = globals.getCallsManager();
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mPackageManager = mContext.getPackageManager();
    }

    //
    // Implementation of the ITelecomService interface.
    //
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(this, "onBind");
        return mServiceImpl;
    }

    /**
     * Implementation of the ITelecomService interface.
     * TODO: Reorganize this inner class to top of file.
     */
    class TelecomServiceImpl extends ITelecomService.Stub {
        @Override
        public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
            try {
@@ -656,6 +668,7 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
                        + " to add new unknown call.");
            }
        }
    }

    //
    // Supporting methods for the ITelecomService interface implementation.
@@ -856,7 +869,7 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
    }

    private boolean isDefaultDialerCalling() {
        ComponentName defaultDialerComponent = getDefaultPhoneApp();
        ComponentName defaultDialerComponent = getDefaultPhoneAppInternal();
        if (defaultDialerComponent != null) {
            try {
                mAppOpsManager.checkPackage(
@@ -869,6 +882,13 @@ public class TelecomServiceImpl extends ITelecomService.Stub {
        return false;
    }

    private ComponentName getDefaultPhoneAppInternal() {
        Resources resources = mContext.getResources();
        return new ComponentName(
                resources.getString(R.string.ui_default_package),
                resources.getString(R.string.dialer_default_class));
    }

    private TelephonyManager getTelephonyManager() {
        return (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
    }