1 /*
2  * Copyright (C) 2019 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 
17 package android.telephony.ims.cts;
18 
19 import android.content.Context;
20 import android.telephony.ims.feature.CapabilityChangeRequest;
21 import android.telephony.ims.feature.RcsFeature;
22 import android.telephony.ims.stub.CapabilityExchangeEventListener;
23 import android.telephony.ims.stub.ImsRegistrationImplBase;
24 import android.telephony.ims.stub.RcsCapabilityExchangeImplBase;
25 import android.util.Log;
26 
27 import java.util.List;
28 import java.util.concurrent.Executor;
29 
30 public class TestRcsFeature extends RcsFeature {
31 
32     private static final String TAG = "CtsTestImsService";
33 
34     private final TestImsService.ReadyListener mReadyListener;
35     private final TestImsService.RemovedListener mRemovedListener;
36     private final TestImsService.RcsCapabilityExchangeEventListener mCapExchangeEventListener;
37 
38     private final RcsImsCapabilities mRcsCapabilitiesLte;
39     private final RcsImsCapabilities mRcsCapabilitiesIWan;
40     private final TestImsService.CapabilitiesSetListener mRcsCapabilityChangedListener;
41 
42     private TestRcsCapabilityExchangeImpl mCapExchangeImpl;
43     private CapabilityExchangeEventListener mCapEventListener;
44     private TestImsService.DeviceCapPublishListener mDeviceCapPublishListener;
45 
TestRcsFeature(Context context, TestImsService.ReadyListener readyListener, TestImsService.RemovedListener removedListener, TestImsService.CapabilitiesSetListener setListener, TestImsService.RcsCapabilityExchangeEventListener capExchangeEventListener)46     TestRcsFeature(Context context,
47             TestImsService.ReadyListener readyListener,
48             TestImsService.RemovedListener removedListener,
49             TestImsService.CapabilitiesSetListener setListener,
50             TestImsService.RcsCapabilityExchangeEventListener capExchangeEventListener) {
51         super(context.getMainExecutor());
52 
53         mReadyListener = readyListener;
54         mRemovedListener = removedListener;
55         mCapExchangeEventListener = capExchangeEventListener;
56 
57         mRcsCapabilityChangedListener = setListener;
58         mRcsCapabilitiesLte = new RcsImsCapabilities(RcsImsCapabilities.CAPABILITY_TYPE_NONE);
59         mRcsCapabilitiesIWan = new RcsImsCapabilities(RcsImsCapabilities.CAPABILITY_TYPE_NONE);
60 
61         setFeatureState(STATE_READY);
62     }
63 
setDeviceCapPublishListener(TestImsService.DeviceCapPublishListener listener)64     public void setDeviceCapPublishListener(TestImsService.DeviceCapPublishListener listener) {
65         mDeviceCapPublishListener = listener;
66     }
67 
68     @Override
onFeatureReady()69     public void onFeatureReady() {
70         if (ImsUtils.VDBG) {
71             Log.d(TAG, "TestRcsFeature.onFeatureReady called");
72         }
73         mReadyListener.onReady();
74     }
75 
76     @Override
onFeatureRemoved()77     public void onFeatureRemoved() {
78         if (ImsUtils.VDBG) {
79             Log.d(TAG, "TestRcsFeature.onFeatureRemoved called");
80         }
81         mRemovedListener.onRemoved();
82     }
83 
createCapabilityExchangeImpl(Executor executor, CapabilityExchangeEventListener listener)84     public RcsCapabilityExchangeImplBase createCapabilityExchangeImpl(Executor executor,
85             CapabilityExchangeEventListener listener) {
86         return createCapabilityExchangeImpl(listener);
87     }
88 
89     @Override
createCapabilityExchangeImpl( CapabilityExchangeEventListener listener)90     public RcsCapabilityExchangeImplBase createCapabilityExchangeImpl(
91             CapabilityExchangeEventListener listener) {
92         if (ImsUtils.VDBG) {
93             Log.d(TAG, "TestRcsFeature.createCapabilityExchangeImpl called");
94         }
95         mCapEventListener = listener;
96         mCapExchangeImpl = new TestRcsCapabilityExchangeImpl(mDeviceCapPublishListener);
97         mCapExchangeEventListener.onSet();
98         return mCapExchangeImpl;
99     }
100 
101     @Override
destroyCapabilityExchangeImpl(RcsCapabilityExchangeImplBase capExchangeImpl)102     public void destroyCapabilityExchangeImpl(RcsCapabilityExchangeImplBase capExchangeImpl) {
103         if (ImsUtils.VDBG) {
104             Log.d(TAG, "TestRcsFeature.destroyCapabilityExchangeImpl called");
105         }
106         mCapEventListener = null;
107         mCapExchangeEventListener.onSet();
108     }
109 
getEventListener()110     public CapabilityExchangeEventListener getEventListener() {
111         return mCapEventListener;
112     }
113 
getRcsCapabilityExchangeImpl()114     public TestRcsCapabilityExchangeImpl getRcsCapabilityExchangeImpl() {
115         return mCapExchangeImpl;
116     }
117 
118     @Override
changeEnabledCapabilities(CapabilityChangeRequest request, CapabilityCallbackProxy c)119     public void changeEnabledCapabilities(CapabilityChangeRequest request,
120             CapabilityCallbackProxy c) {
121 
122         // Enabled RCS capabilities
123         List<CapabilityChangeRequest.CapabilityPair> pairs = request.getCapabilitiesToEnable();
124         for (CapabilityChangeRequest.CapabilityPair pair : pairs) {
125             if (pair.getRadioTech() == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
126                 mRcsCapabilitiesLte.addCapabilities(pair.getCapability());
127             } else if (pair.getRadioTech() == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
128                 mRcsCapabilitiesIWan.addCapabilities(pair.getCapability());
129             }
130         }
131 
132         // Disabled RCS capabilities
133         pairs = request.getCapabilitiesToDisable();
134         for (CapabilityChangeRequest.CapabilityPair pair : pairs) {
135             if (pair.getRadioTech() == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
136                 mRcsCapabilitiesLte.removeCapabilities(pair.getCapability());
137             } else if (pair.getRadioTech() == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
138                 mRcsCapabilitiesIWan.removeCapabilities(pair.getCapability());
139             }
140         }
141         mRcsCapabilityChangedListener.onSet();
142     }
143 
144     @Override
queryCapabilityConfiguration(int capability, int radioTech)145     public boolean queryCapabilityConfiguration(int capability, int radioTech) {
146         if (ImsUtils.VDBG) {
147             Log.d(TAG, "TestRcsFeature.queryCapabilityConfiguration capability: " + capability);
148         }
149         if (radioTech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
150             return mRcsCapabilitiesLte.isCapable(capability);
151         } else if (radioTech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
152             return mRcsCapabilitiesIWan.isCapable(capability);
153         }
154         return false;
155     }
156 }
157