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