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