1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.data;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.util.SimpleArrayMap;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
22991c2e5ab8be3638116a335f6ddc9e13aac4912cPaul Duffinimport com.android.messaging.util.Assert;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
24991c2e5ab8be3638116a335f6ddc9e13aac4912cPaul Duffinimport com.google.common.annotations.VisibleForTesting;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.ArrayList;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.Iterator;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.NoSuchElementException;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * A class that contains the list of all participants potentially involved in a conversation.
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Includes both the participant records for each participant referenced in conversation
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * participants table (i.e. "other" phone numbers) plus all participants representing self
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * (i.e. one per sim recorded in the subscription manager db).
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ConversationParticipantsData implements Iterable<ParticipantData> {
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // A map from a participant id to a participant
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final SimpleArrayMap<String, ParticipantData> mConversationParticipantsMap;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mParticipantCountExcludingSelf = 0;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ConversationParticipantsData() {
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationParticipantsMap = new SimpleArrayMap<String, ParticipantData>();
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void bind(final Cursor cursor) {
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationParticipantsMap.clear();
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParticipantCountExcludingSelf = 0;
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (cursor != null) {
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            while (cursor.moveToNext()) {
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final ParticipantData newParticipant = ParticipantData.getFromCursor(cursor);
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (!newParticipant.isSelf()) {
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mParticipantCountExcludingSelf++;
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mConversationParticipantsMap.put(newParticipant.getId(), newParticipant);
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @VisibleForTesting
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    ParticipantData getParticipantById(final String participantId) {
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mConversationParticipantsMap.get(participantId);
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    ArrayList<ParticipantData> getParticipantListExcludingSelf() {
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ArrayList<ParticipantData> retList =
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                new ArrayList<ParticipantData>(mConversationParticipantsMap.size());
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (int i = 0; i < mConversationParticipantsMap.size(); i++) {
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ParticipantData participant = mConversationParticipantsMap.valueAt(i);
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (!participant.isSelf()) {
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                retList.add(participant);
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return retList;
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * For a 1:1 conversation return the other (not self) participant
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ParticipantData getOtherParticipant() {
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mParticipantCountExcludingSelf == 1) {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            for (int i = 0; i < mConversationParticipantsMap.size(); i++) {
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final ParticipantData participant = mConversationParticipantsMap.valueAt(i);
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (!participant.isSelf()) {
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    return participant;
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
87991c2e5ab8be3638116a335f6ddc9e13aac4912cPaul Duffin            Assert.fail("Could not find other participant");
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public int getNumberOfParticipantsExcludingSelf() {
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mParticipantCountExcludingSelf;
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean isLoaded() {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return !mConversationParticipantsMap.isEmpty();
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public Iterator<ParticipantData> iterator() {
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return new Iterator<ParticipantData>() {
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            private int mCurrentIndex = -1;
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public boolean hasNext() {
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return mCurrentIndex < mConversationParticipantsMap.size() - 1;
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public ParticipantData next() {
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mCurrentIndex++;
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (mCurrentIndex >= mConversationParticipantsMap.size()) {
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    throw new NoSuchElementException();
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return mConversationParticipantsMap.valueAt(mCurrentIndex);
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public void remove() {
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                throw new UnsupportedOperationException();
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        };
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
126