1 /* 2 * Copyright (C) 2017 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.deviceinfo.simstatus; 18 19 import android.app.Dialog; 20 import android.app.settings.SettingsEnums; 21 import android.os.Bundle; 22 import android.text.TextUtils; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.WindowManager; 26 import android.widget.TextView; 27 28 import androidx.appcompat.app.AlertDialog; 29 import androidx.fragment.app.Fragment; 30 import androidx.fragment.app.FragmentManager; 31 32 import com.android.settings.R; 33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 34 import com.android.settings.deviceinfo.PhoneNumberUtil; 35 36 import java.util.Arrays; 37 import java.util.stream.IntStream; 38 39 public class SimStatusDialogFragment extends InstrumentedDialogFragment { 40 41 private static final String SIM_SLOT_BUNDLE_KEY = "arg_key_sim_slot"; 42 private static final String DIALOG_TITLE_BUNDLE_KEY = "arg_key_dialog_title"; 43 44 private static final String TAG = "SimStatusDialog"; 45 46 private View mRootView; 47 private SimStatusDialogController mController; 48 49 @Override getMetricsCategory()50 public int getMetricsCategory() { 51 return SettingsEnums.DIALOG_SIM_STATUS; 52 } 53 show(Fragment host, int slotId, String dialogTitle)54 public static void show(Fragment host, int slotId, String dialogTitle) { 55 final FragmentManager manager = host.getChildFragmentManager(); 56 if (manager.findFragmentByTag(TAG) == null) { 57 final Bundle bundle = new Bundle(); 58 bundle.putInt(SIM_SLOT_BUNDLE_KEY, slotId); 59 bundle.putString(DIALOG_TITLE_BUNDLE_KEY, dialogTitle); 60 final SimStatusDialogFragment dialog = 61 new SimStatusDialogFragment(); 62 dialog.setArguments(bundle); 63 dialog.show(manager, TAG); 64 } 65 } 66 67 @Override onCreateDialog(Bundle savedInstanceState)68 public Dialog onCreateDialog(Bundle savedInstanceState) { 69 final Bundle bundle = getArguments(); 70 final int slotId = bundle.getInt(SIM_SLOT_BUNDLE_KEY); 71 final String dialogTitle = bundle.getString(DIALOG_TITLE_BUNDLE_KEY); 72 mController = new SimStatusDialogController(this, mLifecycle, slotId); 73 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) 74 .setTitle(dialogTitle) 75 .setPositiveButton(android.R.string.ok, null /* onClickListener */); 76 mRootView = LayoutInflater.from(builder.getContext()) 77 .inflate(R.layout.dialog_sim_status, null /* parent */); 78 mController.initialize(); 79 80 Dialog dlg = builder.setView(mRootView).create(); 81 dlg.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, 82 WindowManager.LayoutParams.FLAG_SECURE); 83 84 return dlg; 85 } 86 87 @Override onDestroy()88 public void onDestroy() { 89 mController.deinitialize(); 90 super.onDestroy(); 91 } 92 removeSettingFromScreen(int viewId)93 public void removeSettingFromScreen(int viewId) { 94 final View view = mRootView.findViewById(viewId); 95 if (view != null) { 96 view.setVisibility(View.GONE); 97 } 98 } 99 100 /** 101 * View ID(s) which is digit format (instead of decimal number) text. 102 **/ 103 private static final int[] sViewIdsInDigitFormat = IntStream 104 .of(SimStatusDialogController.ICCID_INFO_VALUE_ID, 105 SimStatusDialogController.PHONE_NUMBER_VALUE_ID, 106 SimStatusDialogController.EID_INFO_VALUE_ID) 107 .sorted().toArray(); 108 setText(int viewId, CharSequence text)109 public void setText(int viewId, CharSequence text) { 110 final TextView textView = mRootView.findViewById(viewId); 111 if (textView == null) { 112 return; 113 } 114 if (TextUtils.isEmpty(text)) { 115 text = getResources().getString(R.string.device_info_default); 116 } else if (Arrays.binarySearch(sViewIdsInDigitFormat, viewId) >= 0) { 117 text = PhoneNumberUtil.expandByTts(text); 118 } 119 textView.setText(text); 120 } 121 } 122