aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ims/RcsFeatureConnection.java
blob: 626854369c8405e2ad354f0c9896fa81c930e5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 2019 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.ims;

import android.annotation.NonNull;
import android.content.Context;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.telephony.ims.RcsContactUceCapability;
import android.telephony.ims.aidl.IImsCapabilityCallback;
import android.telephony.ims.aidl.IImsConfig;
import android.telephony.ims.aidl.IImsRcsFeature;
import android.telephony.ims.aidl.IImsRegistration;
import android.telephony.ims.aidl.IImsRegistrationCallback;
import android.telephony.ims.aidl.IRcsFeatureListener;
import android.telephony.ims.feature.CapabilityChangeRequest;
import android.telephony.ims.feature.ImsFeature;

import com.android.internal.annotations.VisibleForTesting;
import com.android.telephony.Rlog;
import java.util.List;

/**
 * A container of the IImsServiceController binder, which implements all of the RcsFeatures that
 * the platform currently supports: RCS
 */
public class RcsFeatureConnection extends FeatureConnection {
    private static final String TAG = "RcsFeatureConnection";

    public class AvailabilityCallbackManager extends
            ImsCallbackAdapterManager<IImsCapabilityCallback> {

        AvailabilityCallbackManager(Context context) {
            super(context, new Object() /* Lock object */, mSlotId);
        }

        @Override
        public void registerCallback(IImsCapabilityCallback localCallback) {
            try {
                addCapabilityCallback(localCallback);
            } catch (RemoteException e) {
                loge("Register capability callback error: " + e);
                throw new IllegalStateException(
                        " CapabilityCallbackManager: Register callback error");
            }
        }

        @Override
        public void unregisterCallback(IImsCapabilityCallback localCallback) {
            try {
                removeCapabilityCallback(localCallback);
            } catch (RemoteException e) {
                loge("Cannot remove capability callback: " + e);
            }
        }
    }

    private class RegistrationCallbackManager extends
            ImsCallbackAdapterManager<IImsRegistrationCallback> {

        public RegistrationCallbackManager(Context context) {
            super(context, new Object() /* Lock object */, mSlotId);
        }

        @Override
        public void registerCallback(IImsRegistrationCallback localCallback) {
            IImsRegistration imsRegistration = getRegistration();
            if (imsRegistration == null) {
                loge("Register IMS registration callback: ImsRegistration is null");
                throw new IllegalStateException("RegistrationCallbackAdapter: RcsFeature is"
                        + " not available!");
            }

            try {
                imsRegistration.addRegistrationCallback(localCallback);
            } catch (RemoteException e) {
                throw new IllegalStateException("RegistrationCallbackAdapter: RcsFeature"
                        + " binder is dead.");
            }
        }

        @Override
        public void unregisterCallback(IImsRegistrationCallback localCallback) {
            IImsRegistration imsRegistration = getRegistration();
            if (imsRegistration == null) {
                logi("Unregister IMS registration callback: ImsRegistration is null");
                return;
            }

            try {
                imsRegistration.removeRegistrationCallback(localCallback);
            } catch (RemoteException e) {
                loge("Cannot remove registration callback: " + e);
            }
        }
    }

    public static @NonNull RcsFeatureConnection create(Context context , int slotId,
            IFeatureUpdate callback) {

        RcsFeatureConnection serviceProxy = new RcsFeatureConnection(context, slotId, callback);

        if (!ImsManager.isImsSupportedOnDevice(context)) {
            // Return empty service proxy in the case that IMS is not supported.
            sImsSupportedOnDevice = false;
            Rlog.w(TAG, "create: IMS is not supported");
            return serviceProxy;
        }

        TelephonyManager tm =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm == null) {
            Rlog.w(TAG, "create: TelephonyManager is null");
            return serviceProxy;
        }

        IImsRcsFeature binder = tm.getImsRcsFeatureAndListen(slotId, serviceProxy.getListener());
        if (binder != null) {
            Rlog.d(TAG, "create: set binder");
            serviceProxy.setBinder(binder.asBinder());
            // Trigger the cache to be updated for feature status.
            serviceProxy.getFeatureState();
        } else {
            Rlog.i(TAG, "create: binder is null! Slot Id: " + slotId);
        }
        return serviceProxy;
    }

    @VisibleForTesting
    public AvailabilityCallbackManager mAvailabilityCallbackManager;
    @VisibleForTesting
    public RegistrationCallbackManager mRegistrationCallbackManager;

    private RcsFeatureConnection(Context context, int slotId, IFeatureUpdate callback) {
        super(context, slotId);
        setStatusCallback(callback);
        mAvailabilityCallbackManager = new AvailabilityCallbackManager(mContext);
        mRegistrationCallbackManager = new RegistrationCallbackManager(mContext);
    }

    public void close() {
        removeRcsFeatureListener();
        mAvailabilityCallbackManager.close();
        mRegistrationCallbackManager.close();
    }

    @Override
    protected void onRemovedOrDied() {
        removeImsFeatureCallback();
        super.onRemovedOrDied();
        synchronized (mLock) {
            close();
        }
    }

    private void removeImsFeatureCallback() {
        TelephonyManager tm = getTelephonyManager();
        if (tm != null) {
            tm.unregisterImsFeatureCallback(mSlotId, ImsFeature.FEATURE_RCS, getListener());
        }
    }

    @Override
    @VisibleForTesting
    public void handleImsFeatureCreatedCallback(int slotId, int feature) {
        logi("IMS feature created: slotId= " + slotId + ", feature=" + feature);
        if (!isUpdateForThisFeatureAndSlot(slotId, feature)) {
            return;
        }
        synchronized(mLock) {
            if (!mIsAvailable) {
                logi("RCS enabled on slotId: " + slotId);
                mIsAvailable = true;
            }
        }
    }

    @Override
    @VisibleForTesting
    public void handleImsFeatureRemovedCallback(int slotId, int feature) {
        logi("IMS feature removed: slotId= " + slotId + ", feature=" + feature);
        if (!isUpdateForThisFeatureAndSlot(slotId, feature)) {
            return;
        }
        synchronized(mLock) {
            logi("Rcs UCE removed on slotId: " + slotId);
            onRemovedOrDied();
        }
    }

    @Override
    @VisibleForTesting
    public void handleImsStatusChangedCallback(int slotId, int feature, int status) {
        logi("IMS status changed: slotId=" + slotId + ", feature=" + feature + ", status="
                + status);
        if (!isUpdateForThisFeatureAndSlot(slotId, feature)) {
            return;
        }
        synchronized(mLock) {
            mFeatureStateCached = status;
        }
    }

    private boolean isUpdateForThisFeatureAndSlot(int slotId, int feature) {
        if (mSlotId == slotId && feature == ImsFeature.FEATURE_RCS) {
            return true;
        }
        return false;
    }

    public void setRcsFeatureListener(IRcsFeatureListener listener) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).setListener(listener);
        }
    }

    public void removeRcsFeatureListener() {
        try {
            setRcsFeatureListener(null);
        } catch (RemoteException e) {
            // If we are not still connected, there is no need to fail removing.
        }
    }

    public int queryCapabilityStatus() throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            return getServiceInterface(mBinder).queryCapabilityStatus();
        }
    }

    public void addCallbackForSubscription(int subId, IImsCapabilityCallback cb) {
        mAvailabilityCallbackManager.addCallbackForSubscription(cb, subId);
    }

    public void addCallbackForSubscription(int subId, IImsRegistrationCallback cb) {
        mRegistrationCallbackManager.addCallbackForSubscription(cb, subId);
    }

    public void addCallback(IImsRegistrationCallback cb) {
        mRegistrationCallbackManager.addCallback(cb);
    }

    public void removeCallbackForSubscription(int subId, IImsCapabilityCallback cb) {
        mAvailabilityCallbackManager.removeCallbackForSubscription(cb, subId);
    }

    public void removeCallbackForSubscription(int subId, IImsRegistrationCallback cb) {
        mRegistrationCallbackManager.removeCallbackForSubscription(cb, subId);
    }

    public void removeCallback(IImsRegistrationCallback cb) {
        mRegistrationCallbackManager.removeCallback(cb);
    }

    // Add callback to remote service
    private void addCapabilityCallback(IImsCapabilityCallback callback) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).addCapabilityCallback(callback);
        }
    }

    // Remove callback to remote service
    private void removeCapabilityCallback(IImsCapabilityCallback callback) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).removeCapabilityCallback(callback);
        }
    }

    public void queryCapabilityConfiguration(int capability, int radioTech,
            IImsCapabilityCallback c) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).queryCapabilityConfiguration(capability, radioTech, c);
        }
    }

    public void changeEnabledCapabilities(CapabilityChangeRequest request,
            IImsCapabilityCallback callback) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).changeCapabilitiesConfiguration(request, callback);
        }
    }

    public void requestPublication(RcsContactUceCapability capabilities, int taskId)
            throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).updateCapabilities(capabilities, taskId);
        }
    }

    public void requestCapabilities(List<Uri> uris, int taskId) throws RemoteException {
        synchronized (mLock) {
            checkServiceIsReady();
            getServiceInterface(mBinder).requestCapabilities(uris, taskId);
        }
    }

    @Override
    @VisibleForTesting
    public Integer retrieveFeatureState() {
        if (mBinder != null) {
            try {
                return getServiceInterface(mBinder).getFeatureState();
            } catch (RemoteException e) {
                // Status check failed, don't update cache
            }
        }
        return null;
    }

    @Override
    protected IImsRegistration getRegistrationBinder() {
        TelephonyManager tm = getTelephonyManager();
        return  tm != null ? tm.getImsRegistration(mSlotId, ImsFeature.FEATURE_RCS) : null;
    }

    @Override
    protected IImsConfig getConfigBinder() {
        TelephonyManager tm = getTelephonyManager();
        return  tm != null ? tm.getImsConfig(mSlotId, ImsFeature.FEATURE_RCS) : null;
    }

    @VisibleForTesting
    public IImsRcsFeature getServiceInterface(IBinder b) {
        return IImsRcsFeature.Stub.asInterface(b);
    }
    private void log(String s) {
        Rlog.d(TAG + " [" + mSlotId + "]", s);
    }

    private void logi(String s) {
        Rlog.i(TAG + " [" + mSlotId + "]", s);
    }

    private void loge(String s) {
        Rlog.e(TAG + " [" + mSlotId + "]", s);
    }
}