Merge "Toggle switch back when user dismisses dialog by tapping screen" into udc-dev

This commit is contained in:
Yuxin Hu
2023-05-16 18:24:14 +00:00
committed by Android (Google) Code Review
4 changed files with 338 additions and 140 deletions

View File

@@ -28,11 +28,9 @@ import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
/**
* Dialog fragment for reboot confirmation when enabling certain features.
*/
/** Dialog fragment for reboot confirmation when enabling certain features. */
public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
implements DialogInterface.OnClickListener {
implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
private static final String TAG = "FreeformPrefRebootDlg";
@@ -40,18 +38,17 @@ public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
private final int mCancelButtonId;
private final RebootConfirmationDialogHost mHost;
/**
* Show an instance of this dialog.
*/
/** Show an instance of this dialog. */
public static void show(Fragment fragment, int messageId, RebootConfirmationDialogHost host) {
show(fragment, messageId, R.string.reboot_dialog_reboot_later, host);
}
/**
* Show an instance of this dialog with cancel button string set as cancelButtonId
*/
public static void show(Fragment fragment, int messageId,
int cancelButtonId, RebootConfirmationDialogHost host) {
/** Show an instance of this dialog with cancel button string set as cancelButtonId */
public static void show(
Fragment fragment,
int messageId,
int cancelButtonId,
RebootConfirmationDialogHost host) {
final FragmentManager manager = fragment.getActivity().getSupportFragmentManager();
if (manager.findFragmentByTag(TAG) == null) {
final RebootConfirmationDialogFragment dialog =
@@ -60,8 +57,8 @@ public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
}
}
private RebootConfirmationDialogFragment(int messageId,
int cancelButtonId, RebootConfirmationDialogHost host) {
private RebootConfirmationDialogFragment(
int messageId, int cancelButtonId, RebootConfirmationDialogHost host) {
mMessageId = messageId;
mCancelButtonId = cancelButtonId;
mHost = host;
@@ -89,4 +86,10 @@ public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
mHost.onRebootCancelled();
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
mHost.onRebootDialogDismissed();
}
}

View File

@@ -20,22 +20,20 @@ import android.content.Context;
import android.content.Intent;
/**
* Host of {@link RebootConfirmationDialogFragment} that provides callback when user
* interacts with the UI.
* Host of {@link RebootConfirmationDialogFragment} that provides callback when user interacts with
* the UI.
*/
public interface RebootConfirmationDialogHost {
/**
* Called when user made a decision to reboot the device.
*/
/** Called when user made a decision to reboot the device. */
default void onRebootConfirmed(Context context) {
// user presses button "Reboot now", reboot the device
final Intent intent = new Intent(Intent.ACTION_REBOOT);
context.startActivity(intent);
}
/**
* Called when user made a decision to cancel the reboot
* Default to do nothing
*/
/** Called when user made a decision to cancel the reboot Default to do nothing */
default void onRebootCancelled() {}
/** Called when reboot dialog is dismissed Default to do nothing */
default void onRebootDialogDismissed() {}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.development.graphicsdriver;
import android.content.Context;
import android.content.Intent;
import android.os.GraphicsEnvironment;
import android.os.SystemProperties;
import android.text.TextUtils;
@@ -33,9 +34,7 @@ import com.android.settings.development.RebootConfirmationDialogFragment;
import com.android.settings.development.RebootConfirmationDialogHost;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
/**
* Controller to handle the events when user toggles this developer option switch: Enable ANGLE
*/
/** Controller to handle the events when user toggles this developer option switch: Enable ANGLE */
public class GraphicsDriverEnableAngleAsSystemDriverController
extends DeveloperOptionsPreferenceController
implements Preference.OnPreferenceChangeListener,
@@ -50,14 +49,15 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
private final GraphicsDriverSystemPropertiesWrapper mSystemProperties;
private boolean mShouldToggleSwitchBackOnRebootDialogDismiss;
@VisibleForTesting
static final String PROPERTY_RO_GFX_ANGLE_SUPPORTED = "ro.gfx.angle.supported";
@VisibleForTesting
static final String PROPERTY_PERSISTENT_GRAPHICS_EGL = "persist.graphics.egl";
@VisibleForTesting
static final String ANGLE_DRIVER_SUFFIX = "angle";
@VisibleForTesting static final String ANGLE_DRIVER_SUFFIX = "angle";
@VisibleForTesting
static class Injector {
@@ -87,6 +87,10 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
super(context);
mFragment = fragment;
mSystemProperties = injector.createSystemPropertiesWrapper();
// By default, when the reboot dialog is dismissed we want to toggle the switch back.
// Exception is when user chooses to reboot now, the switch should keep its current value
// and persist its' state over reboot.
mShouldToggleSwitchBackOnRebootDialogDismiss = true;
}
@Override
@@ -108,11 +112,12 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
@VisibleForTesting
void showRebootDialog() {
RebootConfirmationDialogFragment.show(
mFragment, R.string.reboot_dialog_enable_angle_as_system_driver,
R.string.cancel, this);
mFragment,
R.string.reboot_dialog_enable_angle_as_system_driver,
R.string.cancel,
this);
}
@Override
public void updateState(Preference preference) {
// set switch on if "persist.graphics.egl" is "angle" and angle is built in /vendor
@@ -120,8 +125,9 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
final String currentGlesDriver =
mSystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
final boolean isAngle = TextUtils.equals(ANGLE_DRIVER_SUFFIX, currentGlesDriver);
final boolean isAngleSupported = TextUtils
.equals(mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
final boolean isAngleSupported =
TextUtils.equals(
mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
((SwitchPreference) mPreference).setChecked(isAngle && isAngleSupported);
((SwitchPreference) mPreference).setEnabled(isAngleSupported);
}
@@ -130,8 +136,9 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
protected void onDeveloperOptionsSwitchEnabled() {
// only enable the switch if ro.gfx.angle.supported is true
// we use ro.gfx.angle.supported to indicate if ANGLE libs are installed under /vendor
final boolean isAngleSupported = TextUtils
.equals(mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
final boolean isAngleSupported =
TextUtils.equals(
mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
((SwitchPreference) mPreference).setEnabled(isAngleSupported);
}
@@ -145,9 +152,7 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
((SwitchPreference) mPreference).setEnabled(false);
}
@Override
public void onRebootCancelled() {
// if user presses button "Cancel", do not reboot the device, and toggles switch back
void toggleSwitchBack() {
final String currentGlesDriver =
mSystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
if (TextUtils.equals(ANGLE_DRIVER_SUFFIX, currentGlesDriver)) {
@@ -169,4 +174,40 @@ public class GraphicsDriverEnableAngleAsSystemDriverController
// if persist.graphics.egl holds values other than the above two, log error message
Log.e(TAG, "Invalid persist.graphics.egl property value");
}
@VisibleForTesting
void rebootDevice(Context context) {
final Intent intent = new Intent(Intent.ACTION_REBOOT);
context.startActivity(intent);
}
@Override
public void onRebootConfirmed(Context context) {
// User chooses to reboot now, do not toggle switch back
mShouldToggleSwitchBackOnRebootDialogDismiss = false;
// Reboot the device
rebootDevice(context);
}
@Override
public void onRebootCancelled() {
// User chooses to cancel reboot, toggle switch back
mShouldToggleSwitchBackOnRebootDialogDismiss = true;
}
@Override
public void onRebootDialogDismissed() {
// If reboot dialog is dismissed either from
// 1) User clicks cancel
// 2) User taps phone screen area outside of reboot dialog
// do not reboot the device, and toggles switch back.
if (mShouldToggleSwitchBackOnRebootDialogDismiss) {
toggleSwitchBack();
}
// Reset the flag so that the default option is to toggle switch back
// on reboot dialog dismissed.
mShouldToggleSwitchBackOnRebootDialogDismiss = true;
}
}