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 package com.android.messaging.datamodel.data; 17 18 import android.content.Context; 19 import android.net.Uri; 20 import android.text.TextUtils; 21 22 import com.android.messaging.R; 23 import com.android.messaging.util.Assert; 24 import com.android.messaging.util.AvatarUriUtil; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Locale; 29 30 /** 31 * This is a UI facing data model component that holds a list of 32 * {@link SubscriptionListData.SubscriptionListEntry}'s, one for each *active* subscriptions. 33 * 34 * This is used to: 35 * 1) Show a list of SIMs in the SIM Selector 36 * 2) Show the currently selected SIM in the compose message view 37 * 3) Show SIM indicators on conversation message views 38 * 39 * It builds on top of SelfParticipantsData and performs additional logic such as determining 40 * the set of icons to use for the individual Subs. 41 */ 42 public class SubscriptionListData { 43 /** 44 * Represents a single sub that backs UI. 45 */ 46 public static class SubscriptionListEntry { 47 public final String selfParticipantId; 48 public final Uri iconUri; 49 public final Uri selectedIconUri; 50 public final String displayName; 51 public final int displayColor; 52 public final String displayDestination; 53 54 private SubscriptionListEntry(final String selfParticipantId, final Uri iconUri, 55 final Uri selectedIconUri, final String displayName, final int displayColor, 56 final String displayDestination) { 57 this.selfParticipantId = selfParticipantId; 58 this.iconUri = iconUri; 59 this.selectedIconUri = selectedIconUri; 60 this.displayName = displayName; 61 this.displayColor = displayColor; 62 this.displayDestination = displayDestination; 63 } 64 65 static SubscriptionListEntry fromSelfParticipantData( 66 final ParticipantData selfParticipantData, final Context context) { 67 Assert.isTrue(selfParticipantData.isSelf()); 68 Assert.isTrue(selfParticipantData.isActiveSubscription()); 69 final int slotId = selfParticipantData.getDisplaySlotId(); 70 final String iconIdentifier = String.format(Locale.getDefault(), "%d", slotId); 71 final String subscriptionName = selfParticipantData.getSubscriptionName(); 72 final String displayName = TextUtils.isEmpty(subscriptionName) ? 73 context.getString(R.string.sim_slot_identifier, slotId) : subscriptionName; 74 return new SubscriptionListEntry(selfParticipantData.getId(), 75 AvatarUriUtil.createAvatarUri(selfParticipantData, iconIdentifier, 76 false /* selected */, false /* incoming */), 77 AvatarUriUtil.createAvatarUri(selfParticipantData, iconIdentifier, 78 true /* selected */, false /* incoming */), 79 displayName, selfParticipantData.getSubscriptionColor(), 80 selfParticipantData.getDisplayDestination()); 81 } 82 } 83 84 private final List<SubscriptionListEntry> mEntriesExcludingDefault; 85 private SubscriptionListEntry mDefaultEntry; 86 private final Context mContext; 87 88 public SubscriptionListData(final Context context) { 89 mEntriesExcludingDefault = new ArrayList<SubscriptionListEntry>(); 90 mContext = context; 91 } 92 93 public void bind(final List<ParticipantData> subs) { 94 mEntriesExcludingDefault.clear(); 95 mDefaultEntry = null; 96 for (final ParticipantData sub : subs) { 97 final SubscriptionListEntry entry = 98 SubscriptionListEntry.fromSelfParticipantData(sub, mContext); 99 if (!sub.isDefaultSelf()) { 100 mEntriesExcludingDefault.add(entry); 101 } else { 102 mDefaultEntry = entry; 103 } 104 } 105 } 106 107 public List<SubscriptionListEntry> getActiveSubscriptionEntriesExcludingDefault() { 108 return mEntriesExcludingDefault; 109 } 110 111 public SubscriptionListEntry getActiveSubscriptionEntryBySelfId(final String selfId, 112 final boolean excludeDefault) { 113 if (mDefaultEntry != null && TextUtils.equals(mDefaultEntry.selfParticipantId, selfId)) { 114 return excludeDefault ? null : mDefaultEntry; 115 } 116 117 for (final SubscriptionListEntry entry : mEntriesExcludingDefault) { 118 if (TextUtils.equals(entry.selfParticipantId, selfId)) { 119 return entry; 120 } 121 } 122 return null; 123 } 124 125 public boolean hasData() { 126 return !mEntriesExcludingDefault.isEmpty() || mDefaultEntry != null; 127 } 128 } 129