1/*
2 * Copyright (C) 2015 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 com.android.messaging.datamodel.data;
18
19import android.database.Cursor;
20import android.support.v4.util.ArrayMap;
21
22import java.util.ArrayList;
23import java.util.List;
24
25import com.android.messaging.util.OsUtil;
26
27/**
28 * A class that contains the list of all self participants potentially involved in a conversation.
29 * This class contains both active/inactive self entries when there is multi-SIM support.
30 */
31public class SelfParticipantsData {
32    /**
33     * The map from self participant ids to self-participant data entries in the participants table.
34     * This includes both active, inactive and default (with subId ==
35     * {@link ParticipantData#DEFAULT_SELF_SUB_ID}) subscriptions.
36     */
37    private final ArrayMap<String, ParticipantData> mSelfParticipantMap;
38
39    public SelfParticipantsData() {
40        mSelfParticipantMap = new ArrayMap<String, ParticipantData>();
41    }
42
43    public void bind(final Cursor cursor) {
44        mSelfParticipantMap.clear();
45        if (cursor != null) {
46            while (cursor.moveToNext()) {
47                final ParticipantData newParticipant = ParticipantData.getFromCursor(cursor);
48                mSelfParticipantMap.put(newParticipant.getId(), newParticipant);
49            }
50        }
51    }
52
53    /**
54     * Gets the list of self participants for all subscriptions.
55     * @param activeOnly if set, returns active self entries only (i.e. those with SIMs plugged in).
56     */
57    public List<ParticipantData> getSelfParticipants(final boolean activeOnly) {
58         List<ParticipantData> list = new ArrayList<ParticipantData>();
59        for (final ParticipantData self : mSelfParticipantMap.values()) {
60            if (!activeOnly || self.isActiveSubscription()) {
61                list.add(self);
62            }
63        }
64        return list;
65    }
66
67    /**
68     * Gets the self participant corresponding to the given self id.
69     */
70    ParticipantData getSelfParticipantById(final String selfId) {
71        return mSelfParticipantMap.get(selfId);
72    }
73
74    /**
75     * Returns if a given self id represents the default self.
76     */
77    boolean isDefaultSelf(final String selfId) {
78        if (!OsUtil.isAtLeastL_MR1()) {
79            return true;
80        }
81        final ParticipantData self = getSelfParticipantById(selfId);
82        return self == null ? false : self.getSubId() == ParticipantData.DEFAULT_SELF_SUB_ID;
83    }
84
85    public int getSelfParticipantsCountExcludingDefault(final boolean activeOnly) {
86        int count = 0;
87        for (final ParticipantData self : mSelfParticipantMap.values()) {
88            if (!self.isDefaultSelf() && (!activeOnly || self.isActiveSubscription())) {
89                count++;
90            }
91        }
92        return count;
93    }
94
95    public ParticipantData getDefaultSelfParticipant() {
96        for (final ParticipantData self : mSelfParticipantMap.values()) {
97            if (self.isDefaultSelf()) {
98                return self;
99            }
100        }
101        return null;
102    }
103
104    boolean isLoaded() {
105        return !mSelfParticipantMap.isEmpty();
106    }
107}
108