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