1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.telephony;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24import android.service.carrier.CarrierMessagingService;
25import android.service.carrier.ICarrierMessagingService;
26
27import com.android.internal.util.Preconditions;
28
29/**
30 * Provides basic structure for platform to connect to the carrier messaging service.
31 * <p>
32 * <code>
33 * CarrierMessagingServiceManager carrierMessagingServiceManager =
34 *     new CarrierMessagingServiceManagerImpl();
35 * if (carrierMessagingServiceManager.bindToCarrierMessagingService(context, carrierPackageName)) {
36 *   // wait for onServiceReady callback
37 * } else {
38 *   // Unable to bind: handle error.
39 * }
40 * </code>
41 * <p> Upon completion {@link #disposeConnection} should be called to unbind the
42 * CarrierMessagingService.
43 * @hide
44 */
45public abstract class CarrierMessagingServiceManager {
46    // Populated by bindToCarrierMessagingService. bindToCarrierMessagingService must complete
47    // prior to calling disposeConnection so that mCarrierMessagingServiceConnection is initialized.
48    private volatile CarrierMessagingServiceConnection mCarrierMessagingServiceConnection;
49
50    /**
51     * Binds to the carrier messaging service under package {@code carrierPackageName}. This method
52     * should be called exactly once.
53     *
54     * @param context the context
55     * @param carrierPackageName the carrier package name
56     * @return true upon successfully binding to a carrier messaging service, false otherwise
57     */
58    public boolean bindToCarrierMessagingService(Context context, String carrierPackageName) {
59        Preconditions.checkState(mCarrierMessagingServiceConnection == null);
60
61        Intent intent = new Intent(CarrierMessagingService.SERVICE_INTERFACE);
62        intent.setPackage(carrierPackageName);
63        mCarrierMessagingServiceConnection = new CarrierMessagingServiceConnection();
64        return context.bindService(intent, mCarrierMessagingServiceConnection,
65                Context.BIND_AUTO_CREATE);
66    }
67
68    /**
69     * Unbinds the carrier messaging service. This method should be called exactly once.
70     *
71     * @param context the context
72     */
73    public void disposeConnection(Context context) {
74        Preconditions.checkNotNull(mCarrierMessagingServiceConnection);
75        context.unbindService(mCarrierMessagingServiceConnection);
76        mCarrierMessagingServiceConnection = null;
77    }
78
79    /**
80     * Implemented by subclasses to use the carrier messaging service once it is ready.
81     *
82     * @param carrierMessagingService the carrier messaing service interface
83     */
84    protected abstract void onServiceReady(ICarrierMessagingService carrierMessagingService);
85
86    /**
87     * A basic {@link ServiceConnection}.
88     */
89    private final class CarrierMessagingServiceConnection implements ServiceConnection {
90        @Override
91        public void onServiceConnected(ComponentName name, IBinder service) {
92            onServiceReady(ICarrierMessagingService.Stub.asInterface(service));
93        }
94
95        @Override
96        public void onServiceDisconnected(ComponentName name) {
97        }
98    }
99}
100