aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ims/rcs/uce/presence/publish/PublishServiceDescTracker.java
blob: a107b1ade4a243ab0152d4f036c5a2fc88546eb9 (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
/*
 * Copyright (C) 2021 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.rcs.uce.presence.publish;

import android.telephony.CarrierConfigManager;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IndentingPrintWriter;
import android.util.Log;

import com.android.ims.rcs.uce.util.FeatureTags;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Parses the Android Carrier Configuration for service-description -> feature tag mappings and
 * tracks the IMS registration to pass in the
 * to determine capabilities for features that the framework does not manage.
 *
 * @see CarrierConfigManager.Ims#KEY_PUBLISH_SERVICE_DESC_FEATURE_TAG_MAP_OVERRIDE_STRING_ARRAY for
 * more information on the format of this key.
 */
public class PublishServiceDescTracker {
    private static final String TAG = "PublishServiceDescTracker";

    /**
     * Map from (service-id, version) to the feature tags required in registration required in order
     * for the RCS feature to be considered "capable".
     * <p>
     * See {@link
     * CarrierConfigManager.Ims#KEY_PUBLISH_SERVICE_DESC_FEATURE_TAG_MAP_OVERRIDE_STRING_ARRAY}
     * for more information on how this can be overridden/extended.
     */
    private static final Map<ServiceDescription, Set<String>> DEFAULT_SERVICE_DESCRIPTION_MAP;
    static {
        ArrayMap<ServiceDescription, Set<String>> map = new ArrayMap<>(21);
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHAT_IM,
                Collections.singleton(FeatureTags.FEATURE_TAG_CHAT_IM));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHAT_SESSION,
                Collections.singleton(FeatureTags.FEATURE_TAG_CHAT_SESSION));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_FT,
                Collections.singleton(FeatureTags.FEATURE_TAG_FILE_TRANSFER));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_FT_SMS,
                Collections.singleton(FeatureTags.FEATURE_TAG_FILE_TRANSFER_VIA_SMS));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_PRESENCE,
                Collections.singleton(FeatureTags.FEATURE_TAG_PRESENCE));
        // Same service-ID & version for MMTEL, but different description.
        map.put(ServiceDescription.SERVICE_DESCRIPTION_MMTEL_VOICE,
                Collections.singleton(FeatureTags.FEATURE_TAG_MMTEL));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_MMTEL_VOICE_VIDEO, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_MMTEL, FeatureTags.FEATURE_TAG_VIDEO)));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_GEOPUSH,
                Collections.singleton(FeatureTags.FEATURE_TAG_GEO_PUSH));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_GEOPUSH_SMS,
                Collections.singleton(FeatureTags.FEATURE_TAG_GEO_PUSH_VIA_SMS));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CALL_COMPOSER,
                Collections.singleton(FeatureTags.FEATURE_TAG_CALL_COMPOSER_ENRICHED_CALLING));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CALL_COMPOSER_MMTEL,
                Collections.singleton(FeatureTags.FEATURE_TAG_CALL_COMPOSER_VIA_TELEPHONY));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_POST_CALL,
                Collections.singleton(FeatureTags.FEATURE_TAG_POST_CALL));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_SHARED_MAP,
                Collections.singleton(FeatureTags.FEATURE_TAG_SHARED_MAP));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_SHARED_SKETCH,
                Collections.singleton(FeatureTags.FEATURE_TAG_SHARED_SKETCH));
        // A map has one key and one value. And if the same key is used, the value is replaced
        // with a new one.
        // The service description between SERVICE_DESCRIPTION_CHATBOT_SESSION and
        // SERVICE_DESCRIPTION_CHATBOT_SESSION_V1 is the same, but this is for botVersion=#1 .
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SESSION, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_SUPPORTED)));
        // This is the service description for botVersion=#1,#2 .
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SESSION_V1, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_V2_SUPPORTED)));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SESSION_V2, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_V2_SUPPORTED)));
        // The service description between SERVICE_DESCRIPTION_CHATBOT_SA_SESSION and
        // SERVICE_DESCRIPTION_CHATBOT_SA_SESSION_V1 is the same, but this is for botVersion=#1 .
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SA_SESSION, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_STANDALONE_MSG,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_SUPPORTED)));
        // This is the service description for botVersion=#1,#2 .
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SA_SESSION_V1, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_STANDALONE_MSG,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_V2_SUPPORTED)));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_SA_SESSION_V2, new ArraySet<>(
                Arrays.asList(FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_STANDALONE_MSG,
                        FeatureTags.FEATURE_TAG_CHATBOT_VERSION_V2_SUPPORTED)));
        map.put(ServiceDescription.SERVICE_DESCRIPTION_CHATBOT_ROLE,
                Collections.singleton(FeatureTags.FEATURE_TAG_CHATBOT_ROLE));
        DEFAULT_SERVICE_DESCRIPTION_MAP = Collections.unmodifiableMap(map);
    }

    // Maps from ServiceDescription to the set of feature tags required to consider the feature
    // capable for PUBLISH.
    private final Map<ServiceDescription, Set<String>> mServiceDescriptionFeatureTagMap;
    // Handles cases where multiple ServiceDescriptions match a subset of the same feature tags.
    // This will be used to only include the feature tags where the
    private final Set<ServiceDescription> mServiceDescriptionPartialMatches = new ArraySet<>();
    // The capabilities calculated based off of the last IMS registration.
    private final Set<ServiceDescription> mRegistrationCapabilities = new ArraySet<>();
    // Contains the feature tags used in the last update to IMS registration.
    private Set<String> mRegistrationFeatureTags = new ArraySet<>();

    /**
     * Create a new instance, which incorporates any carrier config overrides of the default
     * mapping.
     */
    public static PublishServiceDescTracker fromCarrierConfig(String[] carrierConfig) {
        Map<ServiceDescription, Set<String>> elements = new ArrayMap<>();
        for (Map.Entry<ServiceDescription, Set<String>> entry :
                DEFAULT_SERVICE_DESCRIPTION_MAP.entrySet()) {

            elements.put(entry.getKey(), entry.getValue().stream()
                    .map(PublishServiceDescTracker::removeInconsistencies)
                    .collect(Collectors.toSet()));
        }
        if (carrierConfig != null) {
            for (String entry : carrierConfig) {
                String[] serviceDesc = entry.split("\\|");
                if (serviceDesc.length < 4) {
                    Log.w(TAG, "fromCarrierConfig: error parsing " + entry);
                    continue;
                }
                elements.put(new ServiceDescription(serviceDesc[0].trim(), serviceDesc[1].trim(),
                        serviceDesc[2].trim()), parseFeatureTags(serviceDesc[3]));
            }
        }
        return new PublishServiceDescTracker(elements);
    }

    /**
     * Parse the feature tags in the string, which will be separated by ";".
     */
    private static Set<String> parseFeatureTags(String featureTags) {
        // First, split feature tags into individual params
        String[] featureTagSplit = featureTags.split(";");
        if (featureTagSplit.length == 0) {
            return Collections.emptySet();
        }
        ArraySet<String> tags = new ArraySet<>(featureTagSplit.length);
        // Add each tag, first trying to remove inconsistencies in string matching that may cause
        // it to fail.
        for (String tag : featureTagSplit) {
            tags.add(removeInconsistencies(tag));
        }
        return tags;
    }

    private PublishServiceDescTracker(Map<ServiceDescription, Set<String>> serviceFeatureTagMap) {
        mServiceDescriptionFeatureTagMap = serviceFeatureTagMap;
        Set<ServiceDescription> keySet = mServiceDescriptionFeatureTagMap.keySet();
        // Go through and collect any ServiceDescriptions that have the same service-id & version
        // (but not the same description) and add them to a "partial match" list.
        for (ServiceDescription c : keySet) {
            mServiceDescriptionPartialMatches.addAll(keySet.stream()
                    .filter(s -> !Objects.equals(s, c) && isSimilar(c , s))
                    .collect(Collectors.toList()));
        }
    }

    /**
     * Update the IMS registration associated with this tracker.
     * @param imsRegistration A List of feature tags that were associated with the last IMS
     *                        registration.
     */
    public void updateImsRegistration(Set<String> imsRegistration) {
        Set<String> sanitizedTags = imsRegistration.stream()
                // Ensure formatting passed in is the same as format stored here.
                .map(PublishServiceDescTracker::parseFeatureTags)
                // Each entry should only contain one feature tag.
                .map(s -> s.iterator().next()).collect(Collectors.toSet());
        // For aliased service descriptions (service-id && version is the same, but desc is
        // different), Keep a "score" of the number of feature tags that the service description
        // has associated with it. If another is found with a higher score, replace this one.
        Map<ServiceDescription, Integer> aliasedServiceDescScore = new ArrayMap<>();
        synchronized (mRegistrationCapabilities) {
            mRegistrationFeatureTags = imsRegistration;
            mRegistrationCapabilities.clear();
            for (Map.Entry<ServiceDescription, Set<String>> desc :
                    mServiceDescriptionFeatureTagMap.entrySet()) {
                boolean found = true;
                for (String tag : desc.getValue()) {
                    if (!sanitizedTags.contains(tag)) {
                        found = false;
                        break;
                    }
                }
                if (found) {
                    // There may be ambiguity with multiple entries having the same service-id &&
                    // version, but not the same description. In this case, we need to find any
                    // other entries with the same id & version and replace it with the new entry
                    // if it matches more "completely", i.e. match "mmtel;video" over "mmtel" if the
                    // registration set includes "mmtel;video". Skip putting that in for now and
                    // instead track the match with the most feature tags associated with it that
                    // are all found in the IMS registration.
                    if (mServiceDescriptionPartialMatches.contains(desc.getKey())) {
                        ServiceDescription aliasedDesc = aliasedServiceDescScore.keySet().stream()
                                .filter(s -> isSimilar(s, desc.getKey()))
                                .findFirst().orElse(null);
                        if (aliasedDesc != null) {
                            Integer prevEntrySize = aliasedServiceDescScore.get(aliasedDesc);
                            if (prevEntrySize != null
                                    // Overrides are added below the original map, so prefer those.
                                    && (prevEntrySize <= desc.getValue().size())) {
                                aliasedServiceDescScore.remove(aliasedDesc);
                                aliasedServiceDescScore.put(desc.getKey(), desc.getValue().size());
                            }
                        } else {
                            aliasedServiceDescScore.put(desc.getKey(), desc.getValue().size());
                        }
                    } else {
                        mRegistrationCapabilities.add(desc.getKey());
                    }
                }
            }
            // Collect the highest "scored" ServiceDescriptions and add themto registration caps.
            mRegistrationCapabilities.addAll(aliasedServiceDescScore.keySet());
        }
    }

    /**
     * @return A copy of the service-description pairs (service-id, version) that are associated
     * with the last IMS registration update in {@link #updateImsRegistration(Set)}
     */
    public Set<ServiceDescription> copyRegistrationCapabilities() {
        synchronized (mRegistrationCapabilities) {
            return new ArraySet<>(mRegistrationCapabilities);
        }
    }

    /**
     * @return A copy of the last update to the IMS feature tags via {@link #updateImsRegistration}.
     */
    public Set<String> copyRegistrationFeatureTags() {
        synchronized (mRegistrationCapabilities) {
            return new ArraySet<>(mRegistrationFeatureTags);
        }
    }

    /**
     * Dumps the current state of this tracker.
     */
    public void dump(PrintWriter printWriter) {
        IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
        pw.println("PublishServiceDescTracker");
        pw.increaseIndent();

        pw.println("ServiceDescription -> Feature Tag Map:");
        pw.increaseIndent();
        for (Map.Entry<ServiceDescription, Set<String>> entry :
                mServiceDescriptionFeatureTagMap.entrySet()) {
            pw.print(entry.getKey());
            pw.print("->");
            pw.println(entry.getValue());
        }
        pw.println();
        pw.decreaseIndent();

        if (!mServiceDescriptionPartialMatches.isEmpty()) {
            pw.println("Similar ServiceDescriptions:");
            pw.increaseIndent();
            for (ServiceDescription entry : mServiceDescriptionPartialMatches) {
                pw.println(entry);
            }
            pw.decreaseIndent();
        } else {
            pw.println("No Similar ServiceDescriptions:");
        }
        pw.println();

        pw.println("Last IMS registration update:");
        pw.increaseIndent();
        for (String entry : mRegistrationFeatureTags) {
            pw.println(entry);
        }
        pw.println();
        pw.decreaseIndent();

        pw.println("Capabilities:");
        pw.increaseIndent();
        for (ServiceDescription entry : mRegistrationCapabilities) {
            pw.println(entry);
        }
        pw.println();
        pw.decreaseIndent();

        pw.decreaseIndent();
    }

    /**
     * Test if two ServiceDescriptions are similar, meaning service-id && version are equal.
     */
    private static boolean isSimilar(ServiceDescription a, ServiceDescription b) {
        return (a.serviceId.equals(b.serviceId) && a.version.equals(b.version));
    }

    /**
     * Remove any formatting inconsistencies that could make string matching difficult.
     */
    private static String removeInconsistencies(String tag) {
        tag = tag.toLowerCase();
        tag = tag.replaceAll("\\s+", "");
        return tag;
    }
}