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.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 import android.provider.SettingsSlicesContract;
26 import android.telephony.TelephonyManager;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.fragment.app.Fragment;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 import androidx.preference.SwitchPreference;
33 
34 import com.android.settings.AirplaneModeEnabler;
35 import com.android.settings.R;
36 import com.android.settings.core.TogglePreferenceController;
37 import com.android.settingslib.core.lifecycle.LifecycleObserver;
38 import com.android.settingslib.core.lifecycle.events.OnDestroy;
39 import com.android.settingslib.core.lifecycle.events.OnStart;
40 import com.android.settingslib.core.lifecycle.events.OnStop;
41 
42 public class AirplaneModePreferenceController extends TogglePreferenceController
43         implements LifecycleObserver, OnStart, OnStop, OnDestroy,
44         AirplaneModeEnabler.OnAirplaneModeChangedListener {
45 
46     public static final int REQUEST_CODE_EXIT_ECM = 1;
47 
48     /**
49      * Uri for Airplane mode Slice.
50      */
51     public static final Uri SLICE_URI = new Uri.Builder()
52             .scheme(ContentResolver.SCHEME_CONTENT)
53             .authority(SettingsSlicesContract.AUTHORITY)
54             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
55             .appendPath(SettingsSlicesContract.KEY_AIRPLANE_MODE)
56             .build();
57     private static final String EXIT_ECM_RESULT = "exit_ecm_result";
58 
59     private Fragment mFragment;
60     private AirplaneModeEnabler mAirplaneModeEnabler;
61     private SwitchPreference mAirplaneModePreference;
62 
AirplaneModePreferenceController(Context context, String key)63     public AirplaneModePreferenceController(Context context, String key) {
64         super(context, key);
65 
66         if (isAvailable(mContext)) {
67             mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, this);
68         }
69     }
70 
setFragment(Fragment hostFragment)71     public void setFragment(Fragment hostFragment) {
72         mFragment = hostFragment;
73     }
74 
75     @VisibleForTesting
setAirplaneModeEnabler(AirplaneModeEnabler airplaneModeEnabler)76     void setAirplaneModeEnabler(AirplaneModeEnabler airplaneModeEnabler) {
77         mAirplaneModeEnabler = airplaneModeEnabler;
78     }
79 
80     @Override
handlePreferenceTreeClick(Preference preference)81     public boolean handlePreferenceTreeClick(Preference preference) {
82         if (KEY_AIRPLANE_MODE.equals(preference.getKey())
83                 && mAirplaneModeEnabler.isInEcmMode()) {
84             // In ECM mode launch ECM app dialog
85             if (mFragment != null) {
86                 mFragment.startActivityForResult(
87                         new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
88                         REQUEST_CODE_EXIT_ECM);
89             }
90             return true;
91         }
92 
93         return false;
94     }
95 
96     @Override
getSliceUri()97     public Uri getSliceUri() {
98         return SLICE_URI;
99     }
100 
101     @Override
displayPreference(PreferenceScreen screen)102     public void displayPreference(PreferenceScreen screen) {
103         super.displayPreference(screen);
104         mAirplaneModePreference = screen.findPreference(getPreferenceKey());
105     }
106 
isAvailable(Context context)107     public static boolean isAvailable(Context context) {
108         return context.getResources().getBoolean(R.bool.config_show_toggle_airplane)
109                 && !context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
110     }
111 
112     @Override
isPublicSlice()113     public boolean isPublicSlice() {
114         return true;
115     }
116 
117     @Override
118     @AvailabilityStatus
getAvailabilityStatus()119     public int getAvailabilityStatus() {
120         return isAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
121     }
122 
123     @Override
getSliceHighlightMenuRes()124     public int getSliceHighlightMenuRes() {
125         return R.string.menu_key_network;
126     }
127 
128     @Override
onStart()129     public void onStart() {
130         if (isAvailable()) {
131             mAirplaneModeEnabler.start();
132         }
133     }
134 
135     @Override
onStop()136     public void onStop() {
137         if (isAvailable()) {
138             mAirplaneModeEnabler.stop();
139         }
140     }
141 
142     @Override
onDestroy()143     public void onDestroy() {
144         mAirplaneModeEnabler.close();
145     }
146 
147 
onActivityResult(int requestCode, int resultCode, Intent data)148     public void onActivityResult(int requestCode, int resultCode, Intent data) {
149         if (requestCode == REQUEST_CODE_EXIT_ECM) {
150             final boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
151             // Set Airplane mode based on the return value and checkbox state
152             mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
153                     mAirplaneModePreference.isChecked());
154         }
155     }
156 
157     @Override
isChecked()158     public boolean isChecked() {
159         return mAirplaneModeEnabler.isAirplaneModeOn();
160     }
161 
162     @Override
setChecked(boolean isChecked)163     public boolean setChecked(boolean isChecked) {
164         if (isChecked() == isChecked) {
165             return false;
166         }
167         mAirplaneModeEnabler.setAirplaneMode(isChecked);
168         return true;
169     }
170 
171     @Override
onAirplaneModeChanged(boolean isAirplaneModeOn)172     public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
173         if (mAirplaneModePreference != null) {
174             mAirplaneModePreference.setChecked(isAirplaneModeOn);
175         }
176     }
177 }
178