1 /* 2 * Copyright (C) 2019 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.settings.sim; 18 19 import android.app.Dialog; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.os.Bundle; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.SubscriptionManager; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.BaseAdapter; 30 import android.widget.ImageView; 31 import android.widget.TextView; 32 33 import androidx.annotation.NonNull; 34 import androidx.annotation.Nullable; 35 import androidx.annotation.VisibleForTesting; 36 import androidx.appcompat.app.AlertDialog; 37 38 import com.android.settings.R; 39 import com.android.settings.Utils; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 /** 45 * Shows a dialog consisting of a list of SIMs (aka subscriptions), possibly including an additional 46 * entry indicating "ask me every time". 47 */ 48 public class SimListDialogFragment extends SimDialogFragment implements 49 DialogInterface.OnClickListener { 50 protected static final String KEY_INCLUDE_ASK_EVERY_TIME = "include_ask_every_time"; 51 52 protected SelectSubscriptionAdapter mAdapter; 53 @VisibleForTesting 54 List<SubscriptionInfo> mSubscriptions; 55 newInstance(int dialogType, int titleResId, boolean includeAskEveryTime)56 public static SimListDialogFragment newInstance(int dialogType, int titleResId, 57 boolean includeAskEveryTime) { 58 final SimListDialogFragment fragment = new SimListDialogFragment(); 59 final Bundle args = initArguments(dialogType, titleResId); 60 args.putBoolean(KEY_INCLUDE_ASK_EVERY_TIME, includeAskEveryTime); 61 fragment.setArguments(args); 62 return fragment; 63 } 64 65 @NonNull 66 @Override onCreateDialog(@ullable Bundle savedInstanceState)67 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 68 mSubscriptions = new ArrayList<>(); 69 70 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 71 builder.setTitle(getTitleResId()); 72 73 mAdapter = new SelectSubscriptionAdapter(builder.getContext(), mSubscriptions); 74 75 setAdapter(builder); 76 final Dialog dialog = builder.create(); 77 updateDialog(); 78 return dialog; 79 } 80 81 @Override onClick(DialogInterface dialog, int selectionIndex)82 public void onClick(DialogInterface dialog, int selectionIndex) { 83 if (selectionIndex >= 0 && selectionIndex < mSubscriptions.size()) { 84 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 85 final SubscriptionInfo subscription = mSubscriptions.get(selectionIndex); 86 if (subscription != null) { 87 subId = subscription.getSubscriptionId(); 88 } 89 final SimDialogActivity activity = (SimDialogActivity) getActivity(); 90 activity.onSubscriptionSelected(getDialogType(), subId); 91 } 92 } 93 getCurrentSubscriptions()94 protected List<SubscriptionInfo> getCurrentSubscriptions() { 95 final SubscriptionManager manager = getContext().getSystemService( 96 SubscriptionManager.class); 97 return manager.getActiveSubscriptionInfoList(true); 98 } 99 100 @Override updateDialog()101 public void updateDialog() { 102 List<SubscriptionInfo> currentSubscriptions = getCurrentSubscriptions(); 103 if (currentSubscriptions == null) { 104 dismiss(); 105 return; 106 } 107 if (getArguments().getBoolean(KEY_INCLUDE_ASK_EVERY_TIME)) { 108 final List<SubscriptionInfo> tmp = new ArrayList<>(currentSubscriptions.size() + 1); 109 tmp.add(null); 110 tmp.addAll(currentSubscriptions); 111 currentSubscriptions = tmp; 112 } 113 if (currentSubscriptions.equals(mSubscriptions)) { 114 return; 115 } 116 mSubscriptions.clear(); 117 mSubscriptions.addAll(currentSubscriptions); 118 mAdapter.notifyDataSetChanged(); 119 } 120 121 @VisibleForTesting setAdapter(AlertDialog.Builder builder)122 void setAdapter(AlertDialog.Builder builder) { 123 builder.setAdapter(mAdapter, this); 124 } 125 126 @Override getMetricsCategory()127 public int getMetricsCategory() { 128 return SettingsEnums.DIALOG_SIM_LIST; 129 } 130 131 private static class SelectSubscriptionAdapter extends BaseAdapter { 132 private Context mContext; 133 private LayoutInflater mInflater; 134 List<SubscriptionInfo> mSubscriptions; 135 SelectSubscriptionAdapter(Context context, List<SubscriptionInfo> subscriptions)136 public SelectSubscriptionAdapter(Context context, List<SubscriptionInfo> subscriptions) { 137 mSubscriptions = subscriptions; 138 mContext = context; 139 } 140 141 @Override getCount()142 public int getCount() { 143 return mSubscriptions.size(); 144 } 145 146 @Override getItem(int position)147 public SubscriptionInfo getItem(int position) { 148 return mSubscriptions.get(position); 149 } 150 151 @Override getItemId(int position)152 public long getItemId(int position) { 153 final SubscriptionInfo info = mSubscriptions.get(position); 154 if (info == null) { 155 return SubscriptionManager.INVALID_SUBSCRIPTION_ID; 156 } 157 return info.getSubscriptionId(); 158 } 159 160 @Override getView(int position, View convertView, ViewGroup parent)161 public View getView(int position, View convertView, ViewGroup parent) { 162 if (convertView == null) { 163 if (mInflater == null) { 164 mInflater = LayoutInflater.from(parent.getContext()); 165 } 166 convertView = mInflater.inflate(R.layout.select_account_list_item, parent, false); 167 } 168 final SubscriptionInfo sub = getItem(position); 169 170 final TextView title = convertView.findViewById(R.id.title); 171 final TextView summary = convertView.findViewById(R.id.summary); 172 final ImageView icon = convertView.findViewById(R.id.icon); 173 174 if (sub == null) { 175 title.setText(R.string.sim_calls_ask_first_prefs_title); 176 summary.setText(""); 177 icon.setImageDrawable(mContext.getDrawable(R.drawable.ic_feedback_24dp)); 178 icon.setImageTintList( 179 Utils.getColorAttr(mContext, android.R.attr.textColorSecondary)); 180 } else { 181 title.setText(sub.getDisplayName()); 182 summary.setText(sub.getNumber()); 183 icon.setImageBitmap(sub.createIconBitmap(mContext)); 184 185 } 186 return convertView; 187 } 188 } 189 } 190