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