1 /*
2  * Copyright (C) 2020 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.sim.smartForwarding;
18 
19 import static com.android.settings.sim.smartForwarding.MDNHandlerHeaderFragment.KEY_SLOT0_PHONE_NUMBER;
20 import static com.android.settings.sim.smartForwarding.MDNHandlerHeaderFragment.KEY_SLOT1_PHONE_NUMBER;
21 
22 import android.app.AlertDialog;
23 import android.app.settings.SettingsEnums;
24 import android.os.Bundle;
25 import android.text.TextUtils;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.Button;
30 
31 import androidx.fragment.app.Fragment;
32 import androidx.fragment.app.FragmentManager;
33 
34 import com.android.settings.R;
35 import com.android.settingslib.core.instrumentation.Instrumentable;
36 
37 public class MDNHandlerFragment extends Fragment implements Instrumentable {
38 
39     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)40     public View onCreateView(LayoutInflater inflater, ViewGroup container,
41                              Bundle savedInstanceState) {
42         View view = inflater.inflate(R.xml.smart_forwarding_mdn_handler, container, false);
43         getActivity().getActionBar().setTitle(
44                 getResources().getString(R.string.smart_forwarding_input_mdn_title));
45 
46         Button processBtn = view.findViewById(R.id.process);
47         processBtn.setOnClickListener((View v)-> {
48             pressButtonOnClick();
49         });
50 
51         Button cancelBtn = view.findViewById(R.id.cancel);
52         cancelBtn.setOnClickListener((View v)-> {
53             switchToMainFragment(true);
54         });
55         return view;
56     }
57 
pressButtonOnClick()58     private void pressButtonOnClick() {
59         // Get the phone number from the UI
60         MDNHandlerHeaderFragment fragment = (MDNHandlerHeaderFragment) this
61                 .getChildFragmentManager()
62                 .findFragmentById(R.id.fragment_settings);
63 
64         String slot0Number = "";
65         String slot1Number = "";
66         if (fragment != null) {
67             slot0Number = fragment.findPreference(KEY_SLOT0_PHONE_NUMBER)
68                     .getSummary().toString();
69             slot1Number = fragment.findPreference(KEY_SLOT1_PHONE_NUMBER)
70                     .getSummary().toString();
71         }
72         final String[] phoneNumber = {slot1Number, slot0Number};
73 
74         // If phone number is empty, popup an alert dialog
75         if(TextUtils.isEmpty(phoneNumber[0])
76                 || TextUtils.isEmpty(phoneNumber[1])) {
77             new AlertDialog.Builder(getActivity())
78                     .setTitle(R.string.smart_forwarding_failed_title)
79                     .setMessage(R.string.smart_forwarding_missing_mdn_text)
80                     .setPositiveButton(
81                             R.string.smart_forwarding_missing_alert_dialog_text,
82                             (dialog, which) -> { dialog.dismiss(); })
83                     .create()
84                     .show();
85         } else {
86             switchToMainFragment(false);
87             ((SmartForwardingActivity)getActivity()).enableSmartForwarding(phoneNumber);
88         }
89     }
90 
switchToMainFragment(boolean turnoffSwitch)91     private void switchToMainFragment(boolean turnoffSwitch) {
92         FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
93         fragmentManager.beginTransaction()
94                 .replace(R.id.content_frame, new SmartForwardingFragment(turnoffSwitch))
95                 .commit();
96     }
97 
98     @Override
getMetricsCategory()99     public int getMetricsCategory() {
100         return SettingsEnums.MOBILE_NETWORK;
101     }
102 }
103