summaryrefslogtreecommitdiff
path: root/rcs/rcsservice/src/com/android/service/ims/PresenceInfoParser.java
blob: dac14013a8897dce05518d1726890c183e0fe52c (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
/*
 * Copyright (c) 2015, Motorola Mobility LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of Motorola Mobility nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY LLC BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

package com.android.service.ims;

import android.net.Uri;
import android.telephony.ims.RcsContactPresenceTuple;
import android.telephony.ims.RcsContactPresenceTuple.ServiceCapabilities;
import android.telephony.ims.RcsContactUceCapability;
import android.telephony.ims.RcsContactUceCapability.PresenceBuilder;

import java.lang.String;
import java.util.ArrayList;

import com.android.ims.internal.Logger;
import com.android.ims.internal.uce.presence.PresTupleInfo;
import com.android.ims.internal.uce.presence.PresRlmiInfo;
import com.android.ims.internal.uce.presence.PresResInfo;
import com.android.ims.internal.uce.presence.PresResInstanceInfo;
import com.android.ims.RcsPresenceInfo;
import com.android.ims.RcsPresenceInfo.ServiceType;
import com.android.ims.RcsPresenceInfo.ServiceState;
import com.android.service.ims.presence.PresenceUtils;

public class PresenceInfoParser{
    /*
     * The logger
     */
    static private Logger logger = Logger.getLogger("PresenceInfoParser");

    public PresenceInfoParser() {
    }

    static public RcsPresenceInfo getPresenceInfoFromTuple(String pPresentityURI,
            PresTupleInfo[] pTupleInfo){
        logger.debug("getPresenceInfoFromTuple: pPresentityURI=" + pPresentityURI +
                " pTupleInfo=" + pTupleInfo);

        if(pPresentityURI == null){
            logger.error("pPresentityURI=" + pPresentityURI);
            return null;
        }

        String contactNumber = getPhoneFromUri(pPresentityURI);
        // Now that we got notification if the content didn't indicate it is not Volte
        // then it should be Volte enabled. So default to Volte enabled.
        int volteStatus = RcsPresenceInfo.VolteStatus.VOLTE_ENABLED;

        int ipVoiceCallState = RcsPresenceInfo.ServiceState.UNKNOWN;
        String ipVoiceCallServiceNumber = null;
         // We use the timestamp which when we received it instead of the one in PDU
        long ipVoiceCallTimestamp = System.currentTimeMillis();

        int ipVideoCallState = RcsPresenceInfo.ServiceState.UNKNOWN;
        String ipVideoCallServiceNumber = null;
        long ipVideoCallTimestamp = System.currentTimeMillis();

        if( pTupleInfo == null){
            logger.debug("pTupleInfo=null");
            return (new RcsPresenceInfo(contactNumber, volteStatus,
                ipVoiceCallState, ipVoiceCallServiceNumber, ipVoiceCallTimestamp,
                ipVideoCallState, ipVideoCallServiceNumber, ipVideoCallTimestamp));
        }

        for(int i = 0; i < pTupleInfo.length; i++){
            // Video call has high priority. If it supports video call it will support voice call.
            String featureTag = pTupleInfo[i].getFeatureTag();
            logger.debug("getFeatureTag " + i + ": " + featureTag);
            if(featureTag.equals(
                "+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\";video") ||
                featureTag.equals("+g.gsma.rcs.telephony=\"cs,volte\";video")) {
                logger.debug("Got Volte and VT enabled tuple");
                ipVoiceCallState = RcsPresenceInfo.ServiceState.ONLINE;
                ipVideoCallState = RcsPresenceInfo.ServiceState.ONLINE;

                if(pTupleInfo[i].getContactUri() != null){
                    ipVideoCallServiceNumber = getPhoneFromUri(
                            pTupleInfo[i].getContactUri().toString());
                }
            } else if(featureTag.equals("+g.gsma.rcs.telephony=\"cs\";video")) {
                logger.debug("Got Volte disabled but VT enabled tuple");
                ipVoiceCallState = RcsPresenceInfo.ServiceState.OFFLINE;
                ipVideoCallState = RcsPresenceInfo.ServiceState.ONLINE;

                if(pTupleInfo[i].getContactUri() != null){
                    ipVideoCallServiceNumber = getPhoneFromUri(
                            pTupleInfo[i].getContactUri().toString());
                }
            }else{
                if(featureTag.equals(
                    "+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\"") ||
                    featureTag.equals("+g.gsma.rcs.telephony=\"cs,volte\"")) {

                    logger.debug("Got Volte only tuple");
                    ipVoiceCallState = RcsPresenceInfo.ServiceState.ONLINE;
                    // "OR" for multiple tuples.
                    if(RcsPresenceInfo.ServiceState.UNKNOWN == ipVideoCallState) {
                        ipVideoCallState = RcsPresenceInfo.ServiceState.OFFLINE;
                    }

                    if(pTupleInfo[i].getContactUri() != null){
                        ipVoiceCallServiceNumber = getPhoneFromUri(
                                pTupleInfo[i].getContactUri().toString());
                    }
                }else{
                    logger.debug("Ignoring feature tag: " + pTupleInfo[i].getFeatureTag());
                }
           }
        }

        RcsPresenceInfo retPresenceInfo = new RcsPresenceInfo(contactNumber,volteStatus,
                ipVoiceCallState, ipVoiceCallServiceNumber, ipVoiceCallTimestamp,
                ipVideoCallState, ipVideoCallServiceNumber, ipVideoCallTimestamp);

        logger.debug("getPresenceInfoFromTuple: " + retPresenceInfo);

        return retPresenceInfo;
    }

    static private void addPresenceInfo(ArrayList<RcsPresenceInfo> presenceInfoList,
            RcsPresenceInfo presenceInfo){
        logger.debug("addPresenceInfo presenceInfoList=" + presenceInfoList +
                    " presenceInfo=" + presenceInfo);

        if(presenceInfoList == null || presenceInfo == null){
            logger.debug("addPresenceInfo presenceInfoList=" + presenceInfoList +
                    " presenceInfo=" + presenceInfo);
            return;
        }

        for(int i=0; i< presenceInfoList.size(); i++){
            RcsPresenceInfo presenceInfoTmp = presenceInfoList.get(i);
            if((presenceInfoTmp != null) && (presenceInfoTmp.getContactNumber() != null) &&
                    presenceInfoTmp.getContactNumber().equalsIgnoreCase(
                    presenceInfo.getContactNumber())){
                // merge the information
                presenceInfoList.set(i, new RcsPresenceInfo(presenceInfoTmp.getContactNumber(),
                        presenceInfoTmp.getVolteStatus(),
                        ((ServiceState.ONLINE == presenceInfo.getServiceState(
                                ServiceType.VOLTE_CALL)) ||
                                (ServiceState.ONLINE == presenceInfoTmp.getServiceState(
                                ServiceType.VOLTE_CALL)))?ServiceState.ONLINE:
                                    presenceInfoTmp.getServiceState(ServiceType.VOLTE_CALL),
                        presenceInfoTmp.getServiceContact(ServiceType.VOLTE_CALL),
                        presenceInfoTmp.getTimeStamp(ServiceType.VOLTE_CALL),
                        ((ServiceState.ONLINE == presenceInfo.getServiceState(
                                ServiceType.VT_CALL)) ||
                                (ServiceState.ONLINE == presenceInfoTmp.getServiceState(
                                ServiceType.VT_CALL)))?ServiceState.ONLINE:
                                    presenceInfoTmp.getServiceState(ServiceType.VT_CALL),
                        presenceInfoTmp.getServiceContact(ServiceType.VT_CALL),
                        presenceInfoTmp.getTimeStamp(ServiceType.VT_CALL)));
                return;
            }
        }

        // didn't merge, so add the new one.
        presenceInfoList.add(presenceInfo);
    }

    static public RcsPresenceInfo[] getPresenceInfosFromPresenceRes(
            PresRlmiInfo pRlmiInfo, PresResInfo[] pRcsPresenceInfo) {
        if(pRcsPresenceInfo == null){
            logger.debug("getPresenceInfosFromPresenceRes pRcsPresenceInfo=null");
            return null;
        }

        ArrayList<RcsPresenceInfo> presenceInfoList = new ArrayList<RcsPresenceInfo>();
        for(int i=0; i < pRcsPresenceInfo.length; i++ ) {
            if(pRcsPresenceInfo[i].getInstanceInfo() == null){
                logger.error("invalid data getInstanceInfo = null");
                continue;
            }

            String resUri = pRcsPresenceInfo[i].getResUri();
            if(resUri == null){
                logger.error("invalid data getResUri = null");
                continue;
            }

            String contactNumber = getPhoneFromUri(resUri);
            if(contactNumber == null){
                logger.error("invalid data contactNumber=null");
                continue;
            }

            if(pRcsPresenceInfo[i].getInstanceInfo().getResInstanceState() ==
                    PresResInstanceInfo.UCE_PRES_RES_INSTANCE_STATE_TERMINATED){
                logger.debug("instatance state is terminated");
                String reason = pRcsPresenceInfo[i].getInstanceInfo().getReason();
                if(reason != null){
                    reason = reason.toLowerCase();
                    // Noresource: 404. Device shall consider the resource as non-EAB capable
                    // Rejected: 403. Device shall consider the resource as non-EAB  capable
                    // Deactivated: 408, 481, 603, other 4xx, 5xx 6xx. Ignore.
                    // Give Up: 480. Ignore.
                    // Probation: 503. Ignore.
                    if(reason.equals("rejected") || reason.equals("noresource")){
                        RcsPresenceInfo presenceInfo = new RcsPresenceInfo(contactNumber,
                                RcsPresenceInfo.VolteStatus.VOLTE_DISABLED,
                                RcsPresenceInfo.ServiceState.OFFLINE, null,
                                System.currentTimeMillis(),
                                RcsPresenceInfo.ServiceState.OFFLINE, null,
                                System.currentTimeMillis());
                        addPresenceInfo(presenceInfoList, presenceInfo);
                        logger.debug("reason=" + reason + " presenceInfo=" + presenceInfo);
                        continue;
                    }
                }
            }

            RcsPresenceInfo presenceInfo = getPresenceInfoFromTuple(resUri,
                    pRcsPresenceInfo[i].getInstanceInfo().getTupleInfo());
            if(presenceInfo != null){
                addPresenceInfo(presenceInfoList, presenceInfo);
            }else{
                logger.debug("presenceInfo["+ i + "] = null");
                addPresenceInfo(presenceInfoList, getPresenceInfoFromTuple(resUri, null));
            }
        }

        logger.debug("getPresenceInfoFromPresenceRes, presenceInfos:" + presenceInfoList);
        if(presenceInfoList.size() == 0){
            return null;
        }

        RcsPresenceInfo[] theArray = new RcsPresenceInfo[presenceInfoList.size()];
        return (RcsPresenceInfo[])presenceInfoList.toArray(theArray);
    }

    static public String getPhoneFromUri(String uriValue) {
        if(uriValue == null){
            return null;
        }

        int startIndex = uriValue.indexOf(":", 0);
        int endIndex = uriValue.indexOf("@", startIndex);

        logger.debug("getPhoneFromUri uriValue=" + uriValue +
            " startIndex=" + startIndex + " endIndex=" + endIndex);

        String number = uriValue;
        if(endIndex != -1){
            number = uriValue.substring(0, endIndex);
        }

        if (startIndex != -1) {
            return number = number.substring(startIndex + 1);
        }

        return number;
    }

    public static RcsContactUceCapability getUceCapability(RcsPresenceInfo info) {
        boolean volteCapable = false;
        if (ServiceState.ONLINE == info.getServiceState(ServiceType.VOLTE_CALL)) {
            volteCapable = true;
        }

        boolean vtCapable = false;
        if (ServiceState.ONLINE == info.getServiceState(ServiceType.VT_CALL)) {
            vtCapable = true;
        }

        ServiceCapabilities.Builder servCapsBuilder = new ServiceCapabilities.Builder(
            volteCapable, vtCapable);
        servCapsBuilder.addSupportedDuplexMode(ServiceCapabilities.DUPLEX_MODE_FULL);

        Uri contactUri = PresenceUtils.convertContactNumber(info.getContactNumber());

        RcsContactPresenceTuple.Builder tupleBuilder = new RcsContactPresenceTuple.Builder(
                RcsContactPresenceTuple.TUPLE_BASIC_STATUS_OPEN,
                RcsContactPresenceTuple.SERVICE_ID_MMTEL, "1.0");
        tupleBuilder.setContactUri(contactUri).setServiceCapabilities(servCapsBuilder.build());

        PresenceBuilder presenceBuilder = new PresenceBuilder(contactUri,
                RcsContactUceCapability.SOURCE_TYPE_CACHED,
                RcsContactUceCapability.REQUEST_RESULT_FOUND);
        presenceBuilder.addCapabilityTuple(tupleBuilder.build());

        return presenceBuilder.build();
    }

    public static RcsPresenceInfo getRcsPresenceInfo(RcsContactUceCapability capability) {
        int volteCapable = ServiceState.OFFLINE;
        int vtCapable = ServiceState.OFFLINE;
        RcsContactPresenceTuple presenceTuple = capability.getCapabilityTuple(
                RcsContactPresenceTuple.SERVICE_ID_MMTEL);
        if (presenceTuple != null) {
            ServiceCapabilities serviceCaps = presenceTuple.getServiceCapabilities();
            if (serviceCaps != null && serviceCaps.isAudioCapable()) {
                volteCapable = ServiceState.ONLINE;
            }
            if (serviceCaps != null && serviceCaps.isVideoCapable()) {
                vtCapable = ServiceState.ONLINE;
            }
        }
        return new RcsPresenceInfo(capability.getContactUri().getSchemeSpecificPart(),
                // Not sure what the difference is, just track voice capable.
                (volteCapable == ServiceState.ONLINE) ? RcsPresenceInfo.VolteStatus.VOLTE_ENABLED :
                        RcsPresenceInfo.VolteStatus.VOLTE_DISABLED, volteCapable,
                PresenceUtils.getNumber(capability.getContactUri()),
                // We always use system current time instead of time from server
                System.currentTimeMillis(), vtCapable,
                PresenceUtils.getNumber(capability.getContactUri()),
                // We always use system current time instead of time from server
                System.currentTimeMillis());
    }
}