blob: 56ee2c16c9a6dc276ea55b53d7347ef225621d7a [file] [log] [blame]
Cheuksan Wangf9c50c42014-10-21 15:58:23 -07001/*
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.service.carriermessaging;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24
25import com.android.internal.util.Preconditions;
26
27/**
28 * Provides basic structure for platform to connect to the carrier messaging service.
29 * <p>
30 * <code>
31 * CarrierMessagingServiceManager carrierMessagingServiceManager =
32 * new CarrierMessagingServiceManagerImpl();
33 * if (carrierMessagingServiceManager.bindToCarrierMessagingService(context, carrierPackageName)) {
34 * // wait for onServiceReady callback
35 * } else {
36 * // Unable to bind: handle error.
37 * }
38 * </code>
39 * <p> Upon completion {@link #disposeConnection} should be called to unbind the
40 * CarrierMessagingService.
41 * @hide
42 */
43public abstract class CarrierMessagingServiceManager {
44 // Populated by bindToCarrierMessagingService. bindToCarrierMessagingService must complete
45 // prior to calling disposeConnection so that mCarrierMessagingServiceConnection is initialized.
46 private volatile CarrierMessagingServiceConnection mCarrierMessagingServiceConnection;
47
48 /**
49 * Binds to the carrier messaging service under package {@code carrierPackageName}. This method
50 * should be called exactly once.
51 *
52 * @param context the context
53 * @param carrierPackageName the carrier package name
54 * @return true upon successfully binding to a carrier messaging service, false otherwise
55 */
56 public boolean bindToCarrierMessagingService(Context context, String carrierPackageName) {
57 Preconditions.checkState(mCarrierMessagingServiceConnection == null);
58
59 Intent intent = new Intent(CarrierMessagingService.SERVICE_INTERFACE);
60 intent.setPackage(carrierPackageName);
61 mCarrierMessagingServiceConnection = new CarrierMessagingServiceConnection();
62 return context.bindService(intent, mCarrierMessagingServiceConnection,
63 Context.BIND_AUTO_CREATE);
64 }
65
66 /**
67 * Unbinds the carrier messaging service. This method should be called exactly once.
68 *
69 * @param context the context
70 */
71 public void disposeConnection(Context context) {
72 Preconditions.checkNotNull(mCarrierMessagingServiceConnection);
73 context.unbindService(mCarrierMessagingServiceConnection);
74 mCarrierMessagingServiceConnection = null;
75 }
76
77 /**
78 * Implemented by subclasses to use the carrier messaging service once it is ready.
79 *
80 * @param carrierMessagingService the carirer messaing service interface
81 */
82 protected abstract void onServiceReady(ICarrierMessagingService carrierMessagingService);
83
84 /**
85 * A basic {@link ServiceConnection}.
86 */
87 private final class CarrierMessagingServiceConnection implements ServiceConnection {
88 @Override
89 public void onServiceConnected(ComponentName name, IBinder service) {
90 onServiceReady(ICarrierMessagingService.Stub.asInterface(service));
91 }
92
93 @Override
94 public void onServiceDisconnected(ComponentName name) {
95 }
96 }
97}