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 android.app.Fragment; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.os.SystemProperties; 23 import android.support.v14.preference.SwitchPreference; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 27 import com.android.internal.telephony.TelephonyIntents; 28 import com.android.internal.telephony.TelephonyProperties; 29 import com.android.settings.AirplaneModeEnabler; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 32 import com.android.settings.overlay.FeatureFactory; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 import com.android.settingslib.core.lifecycle.LifecycleObserver; 35 import com.android.settingslib.core.lifecycle.events.OnPause; 36 import com.android.settingslib.core.lifecycle.events.OnResume; 37 38 public class AirplaneModePreferenceController extends AbstractPreferenceController 39 implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause { 40 41 public static final int REQUEST_CODE_EXIT_ECM = 1; 42 43 public static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane"; 44 45 private static final String EXIT_ECM_RESULT = "exit_ecm_result"; 46 47 private final Fragment mFragment; 48 private final MetricsFeatureProvider mMetricsFeatureProvider; 49 private AirplaneModeEnabler mAirplaneModeEnabler; 50 private SwitchPreference mAirplaneModePreference; 51 52 AirplaneModePreferenceController(Context context, Fragment hostFragment)53 public AirplaneModePreferenceController(Context context, Fragment hostFragment) { 54 super(context); 55 mFragment = hostFragment; 56 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 57 } 58 59 @Override handlePreferenceTreeClick(Preference preference)60 public boolean handlePreferenceTreeClick(Preference preference) { 61 if (KEY_TOGGLE_AIRPLANE.equals(preference.getKey()) && Boolean.parseBoolean( 62 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { 63 // In ECM mode launch ECM app dialog 64 if (mFragment != null) { 65 mFragment.startActivityForResult( 66 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null), 67 REQUEST_CODE_EXIT_ECM); 68 } 69 return true; 70 } 71 72 return false; 73 } 74 75 @Override displayPreference(PreferenceScreen screen)76 public void displayPreference(PreferenceScreen screen) { 77 if (isAvailable()) { 78 mAirplaneModePreference = (SwitchPreference) screen.findPreference(getPreferenceKey()); 79 if (mAirplaneModePreference != null) { 80 mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, mAirplaneModePreference, 81 mMetricsFeatureProvider); 82 } 83 } else { 84 removePreference(screen, getPreferenceKey()); 85 } 86 } 87 88 @Override isAvailable()89 public boolean isAvailable() { 90 return isAvailable(mContext); 91 } 92 isAvailable(Context context)93 public static boolean isAvailable(Context context) { 94 return !context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION); 95 } 96 97 @Override getPreferenceKey()98 public String getPreferenceKey() { 99 return KEY_TOGGLE_AIRPLANE; 100 } 101 onResume()102 public void onResume() { 103 mAirplaneModeEnabler.resume(); 104 } 105 106 @Override onPause()107 public void onPause() { 108 mAirplaneModeEnabler.pause(); 109 } 110 onActivityResult(int requestCode, int resultCode, Intent data)111 public void onActivityResult(int requestCode, int resultCode, Intent data) { 112 if (requestCode == REQUEST_CODE_EXIT_ECM) { 113 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false); 114 // Set Airplane mode based on the return value and checkbox state 115 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes, 116 mAirplaneModePreference.isChecked()); 117 } 118 } 119 } 120