Files
app_Settings/src/com/android/settings/network/AirplaneModePreferenceController.java
Jason Chiu 1766b3c01d Fix indexing error of mobile related controllers
There were several errors in indexing mobile related sliceable
controllers, which led them not able to display on Panel and as inline
controls in search result.

It's because indexing mechanism is running in a worker thread and trying
to construct each controller instance, but the failed controllers'
constructors need to initilize something in the main thread.

- Give the main looper to the classes which only can be initialized in
  the main thread to fix the indexing.
- Guard null pointer exception in SlicesIndexer after fixing the
  indexing.
- Use onStart/onStop in AirplaneModePreferenceController to start/stop
  listener.

Fixes: 149720345
Test: robotest
Change-Id: Ibe5a8d6cc713eeddf26eceaabc05e6d1faa45507
2020-04-10 10:56:31 +00:00

166 lines
5.3 KiB
Java

/*
* Copyright (C) 2016 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 static android.provider.SettingsSlicesContract.KEY_AIRPLANE_MODE;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.SettingsSlicesContract;
import android.telephony.TelephonyManager;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.AirplaneModeEnabler;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
public class AirplaneModePreferenceController extends TogglePreferenceController
implements LifecycleObserver, OnStart, OnStop,
AirplaneModeEnabler.OnAirplaneModeChangedListener {
public static final int REQUEST_CODE_EXIT_ECM = 1;
/**
* Uri for Airplane mode Slice.
*/
public static final Uri SLICE_URI = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(SettingsSlicesContract.KEY_AIRPLANE_MODE)
.build();
private static final String EXIT_ECM_RESULT = "exit_ecm_result";
private Fragment mFragment;
private AirplaneModeEnabler mAirplaneModeEnabler;
private SwitchPreference mAirplaneModePreference;
public AirplaneModePreferenceController(Context context, String key) {
super(context, key);
if (isAvailable(mContext)) {
mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, this);
}
}
public void setFragment(Fragment hostFragment) {
mFragment = hostFragment;
}
@VisibleForTesting
void setAirplaneModeEnabler(AirplaneModeEnabler airplaneModeEnabler) {
mAirplaneModeEnabler = airplaneModeEnabler;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (KEY_AIRPLANE_MODE.equals(preference.getKey())
&& mAirplaneModeEnabler.isInEcmMode()) {
// In ECM mode launch ECM app dialog
if (mFragment != null) {
mFragment.startActivityForResult(
new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
REQUEST_CODE_EXIT_ECM);
}
return true;
}
return false;
}
@Override
public Uri getSliceUri() {
return SLICE_URI;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mAirplaneModePreference = screen.findPreference(getPreferenceKey());
}
public static boolean isAvailable(Context context) {
return context.getResources().getBoolean(R.bool.config_show_toggle_airplane)
&& !context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
}
@Override
public boolean isPublicSlice() {
return true;
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return isAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void onStart() {
if (isAvailable()) {
mAirplaneModeEnabler.start();
}
}
@Override
public void onStop() {
if (isAvailable()) {
mAirplaneModeEnabler.stop();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EXIT_ECM) {
final boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
// Set Airplane mode based on the return value and checkbox state
mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
mAirplaneModePreference.isChecked());
}
}
@Override
public boolean isChecked() {
return mAirplaneModeEnabler.isAirplaneModeOn();
}
@Override
public boolean setChecked(boolean isChecked) {
if (isChecked() == isChecked) {
return false;
}
mAirplaneModeEnabler.setAirplaneMode(isChecked);
return true;
}
@Override
public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
if (mAirplaneModePreference != null) {
mAirplaneModePreference.setChecked(isAirplaneModeOn);
}
}
}