[Provider Model] Add Airplane-safe Networks preference

- Hide the preference when airplane mode is off.

- Reuse PrimarySwitchPreference for RestrictedSwitchPreference

- Screenshot
  https://screenshot.googleplex.com/8HiC5sc8xaqnhz5

Bug: 174022082
Test: atest -c AirplaneSafeNetworksPreferenceControllerTest
Change-Id: I06a56e8e815778cebf200c4a486c414e92f4cb87
This commit is contained in:
Weng Su
2020-11-23 16:36:14 +00:00
parent c48eacaa82
commit 845db3499d
5 changed files with 265 additions and 14 deletions

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import android.content.Context;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceScreen;
import com.android.settings.AirplaneModeEnabler;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.PrimarySwitchController;
import com.android.settings.wifi.WifiEnabler;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.AbstractPreferenceController;
public class AirplaneSafeNetworksPreferenceController extends AbstractPreferenceController
implements LifecycleObserver, AirplaneModeEnabler.OnAirplaneModeChangedListener {
private static final String PREFERENCE_KEY = "airplane_safe_networks";
private RestrictedSwitchPreference mPreference;
private AirplaneModeEnabler mAirplaneModeEnabler;
private WifiEnabler mWifiEnabler;
public AirplaneSafeNetworksPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
if (lifecycle == null) {
throw new IllegalArgumentException("Lifecycle must be set");
}
mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, this);
lifecycle.addObserver(this);
}
@Override
public String getPreferenceKey() {
return PREFERENCE_KEY;
}
@Override
public boolean isAvailable() {
return mAirplaneModeEnabler.isAirplaneModeOn();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mAirplaneModeEnabler.start();
if (mPreference != null) {
mWifiEnabler = new WifiEnabler(mContext, new PrimarySwitchController(mPreference),
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider());
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mAirplaneModeEnabler.stop();
if (mWifiEnabler != null) {
mWifiEnabler.teardownSwitchController();
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
if (mWifiEnabler != null) {
mWifiEnabler.resume(mContext);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
if (mWifiEnabler != null) {
mWifiEnabler.pause();
}
}
@Override
public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
if (mPreference != null) {
mPreference.setVisible(isAirplaneModeOn);
}
}
}

View File

@@ -147,6 +147,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
controllers.add(privateDnsPreferenceController);
if (Utils.isProviderModelEnabled(context)) {
controllers.add(new NetworkProviderCallsSmsController(context, lifecycle));
controllers.add(new AirplaneSafeNetworksPreferenceController(context, lifecycle));
}
return controllers;
}