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.SmartForwardingUtils.TAG;
20 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.getPhoneNumber;
21 
22 import android.app.settings.SettingsEnums;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 import androidx.fragment.app.FragmentManager;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceFragmentCompat;
30 import androidx.preference.TwoStatePreference;
31 
32 import com.android.settings.R;
33 import com.android.settingslib.core.instrumentation.Instrumentable;
34 
35 public class SmartForwardingFragment extends PreferenceFragmentCompat
36         implements Preference.OnPreferenceChangeListener, Instrumentable {
37 
38     private static final String KEY_SMART_FORWARDING_SWITCH = "smart_forwarding_switch";
39     private boolean turnOffSwitch;
40 
SmartForwardingFragment()41     public SmartForwardingFragment() { }
42 
SmartForwardingFragment(boolean turnOffSwitch)43     public SmartForwardingFragment(boolean turnOffSwitch) {
44         this.turnOffSwitch = turnOffSwitch;
45     }
46 
47     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)48     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
49         setPreferencesFromResource(R.xml.smart_forwarding_switch, rootKey);
50 
51         String title = getResources().getString(R.string.smart_forwarding_title);
52         getActivity().getActionBar().setTitle(title);
53 
54         TwoStatePreference smartForwardingSwitch = findPreference(KEY_SMART_FORWARDING_SWITCH);
55         if (turnOffSwitch) {
56             smartForwardingSwitch.setChecked(false);
57         }
58         smartForwardingSwitch.setOnPreferenceChangeListener(this);
59     }
60 
61     @Override
onPreferenceChange(Preference preference, Object newValue)62     public boolean onPreferenceChange(Preference preference, Object newValue) {
63         boolean value = (boolean) newValue;
64 
65         Log.d(TAG, "onPreferenceChange. Update value to " + value);
66 
67         if (value) {
68             String slot0PhoneNumber = getPhoneNumber(getContext(), 0);
69             String slot1PhoneNumber = getPhoneNumber(getContext(), 1);
70 
71             String[] phoneNumber = new String[]{slot1PhoneNumber, slot0PhoneNumber};
72 
73             if (TextUtils.isEmpty(slot0PhoneNumber) || TextUtils.isEmpty(slot1PhoneNumber)) {
74                 Log.d(TAG, "Slot 0 or Slot 1 phone number missing.");
75                 switchToMDNFragment();
76             } else {
77                 ((SmartForwardingActivity) getActivity()).enableSmartForwarding(phoneNumber);
78             }
79             return false;
80         } else {
81             ((SmartForwardingActivity) getActivity()).disableSmartForwarding();
82         }
83 
84         return true;
85     }
86 
87 
switchToMDNFragment()88     private void switchToMDNFragment() {
89         FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
90         fragmentManager.beginTransaction()
91                 .replace(R.id.content_frame, new MDNHandlerFragment())
92                 .commit();
93     }
94 
turnOnSwitchPreference()95     public void turnOnSwitchPreference() {
96         TwoStatePreference smartForwardingSwitch = findPreference(KEY_SMART_FORWARDING_SWITCH);
97         smartForwardingSwitch.setChecked(true);
98     }
99 
100     @Override
getMetricsCategory()101     public int getMetricsCategory() {
102         return SettingsEnums.MOBILE_NETWORK;
103     }
104 }
105