1 /*
2  * Copyright (C) 2016 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 package com.android.settings.network;
17 
18 import static android.provider.SettingsSlicesContract.KEY_AIRPLANE_MODE;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 import androidx.preference.SwitchPreference;
28 
29 import com.android.internal.telephony.TelephonyIntents;
30 import com.android.settings.AirplaneModeEnabler;
31 import com.android.settings.R;
32 import com.android.settings.core.TogglePreferenceController;
33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
34 import com.android.settingslib.core.lifecycle.events.OnPause;
35 import com.android.settingslib.core.lifecycle.events.OnResume;
36 
37 public class AirplaneModePreferenceController extends TogglePreferenceController
38         implements LifecycleObserver, OnResume, OnPause,
39         AirplaneModeEnabler.OnAirplaneModeChangedListener {
40 
41     public static final int REQUEST_CODE_EXIT_ECM = 1;
42 
43     private static final String EXIT_ECM_RESULT = "exit_ecm_result";
44 
45     private Fragment mFragment;
46     private AirplaneModeEnabler mAirplaneModeEnabler;
47     private SwitchPreference mAirplaneModePreference;
48 
AirplaneModePreferenceController(Context context, String key)49     public AirplaneModePreferenceController(Context context, String key) {
50         super(context, key);
51 
52         if (isAvailable(mContext)) {
53             mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, this);
54         }
55     }
56 
setFragment(Fragment hostFragment)57     public void setFragment(Fragment hostFragment) {
58         mFragment = hostFragment;
59     }
60 
61     @Override
handlePreferenceTreeClick(Preference preference)62     public boolean handlePreferenceTreeClick(Preference preference) {
63         if (KEY_AIRPLANE_MODE.equals(preference.getKey())
64                 && mAirplaneModeEnabler.isInEcmMode()) {
65             // In ECM mode launch ECM app dialog
66             if (mFragment != null) {
67                 mFragment.startActivityForResult(
68                         new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
69                         REQUEST_CODE_EXIT_ECM);
70             }
71             return true;
72         }
73 
74         return false;
75     }
76 
77     @Override
displayPreference(PreferenceScreen screen)78     public void displayPreference(PreferenceScreen screen) {
79         super.displayPreference(screen);
80         mAirplaneModePreference = screen.findPreference(getPreferenceKey());
81     }
82 
isAvailable(Context context)83     public static boolean isAvailable(Context context) {
84         return context.getResources().getBoolean(R.bool.config_show_toggle_airplane)
85                 && !context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
86     }
87 
88     @Override
isSliceable()89     public boolean isSliceable() {
90         return true;
91     }
92 
93     @Override
94     @AvailabilityStatus
getAvailabilityStatus()95     public int getAvailabilityStatus() {
96         return isAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
97     }
98 
99     @Override
onResume()100     public void onResume() {
101         if (isAvailable()) {
102             mAirplaneModeEnabler.resume();
103         }
104     }
105 
106     @Override
onPause()107     public void onPause() {
108         if (isAvailable()) {
109             mAirplaneModeEnabler.pause();
110         }
111     }
112 
onActivityResult(int requestCode, int resultCode, Intent data)113     public void onActivityResult(int requestCode, int resultCode, Intent data) {
114         if (requestCode == REQUEST_CODE_EXIT_ECM) {
115             final boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
116             // Set Airplane mode based on the return value and checkbox state
117             mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
118                     mAirplaneModePreference.isChecked());
119         }
120     }
121 
122     @Override
isChecked()123     public boolean isChecked() {
124         return mAirplaneModeEnabler.isAirplaneModeOn();
125     }
126 
127     @Override
setChecked(boolean isChecked)128     public boolean setChecked(boolean isChecked) {
129         if (isChecked() == isChecked) {
130             return false;
131         }
132         mAirplaneModeEnabler.setAirplaneMode(isChecked);
133         return true;
134     }
135 
136     @Override
onAirplaneModeChanged(boolean isAirplaneModeOn)137     public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
138         if (mAirplaneModePreference != null) {
139             mAirplaneModePreference.setChecked(isAirplaneModeOn);
140         }
141     }
142 }
143