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