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