Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c760cf41 authored by Will Leshner's avatar Will Leshner
Browse files

Add a "restrict to wireless" option to when to dream/hub settings.

Bug: 403335785
Test: atest RadioButtonPickerExtraSwitchControllerTest
Flag: android.service.dreams.dreams_v2
Change-Id: Icf5e384f7cc3b87f1bb0c0116f7be54ca6fa4e84
parent 10c60497
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -3283,6 +3283,8 @@
    <string name="screensaver_settings_when_to_dream">When to start</string>
    <!-- [CHAR LIMIT=40] Display settings screen, setting option name to change whether the device wakes up when a lift gesture is detected. -->
    <string name="lift_to_wake_title">Lift to wake</string>
    <!-- Title of toggle preference in screensaver "when to start" screen to restrict to starting only when charging wirelessly [CHAR LIMIT=NONE] -->
    <string name="screensaver_restrict_to_wireless_charging_title">Restrict to wireless charging</string>
    <!-- [CHAR LIMIT=30] Title of the preference that opens the Ambient display settings screen. -->
    <string name="ambient_display_screen_title">Ambient display</string>
@@ -3699,6 +3701,9 @@
    <!-- Summary for when to automatically show hub mode (widgets on lockscreen): docked  [CHAR LIMIT=100] -->
    <string name="when_to_show_hubmode_docked">While docked</string>
    <!-- Title of toggle preference in glanceable hub "when to start" screen to restrict to starting only when charging wirelessly [CHAR LIMIT=NONE] -->
    <string name="hub_restrict_to_wireless_charging_title">Restrict to wireless charging</string>
    <!-- APN Settings -->
    <!-- APN settings screen title -->
    <string name="apn_settings">APNs</string>
+3 −1
Original line number Diff line number Diff line
@@ -17,4 +17,6 @@

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/screensaver_settings_when_to_dream" />
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/screensaver_settings_when_to_dream"
    settings:staticPreferenceLocation="append" />
+3 −1
Original line number Diff line number Diff line
@@ -16,4 +16,6 @@

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/when_to_auto_show_hubmode_title" />
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/when_to_auto_show_hubmode_title"
    settings:staticPreferenceLocation="append" />
+41 −0
Original line number Diff line number Diff line
@@ -29,8 +29,10 @@ import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.dream.RadioButtonPickerExtraSwitchController;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.widget.CandidateInfo;

@@ -49,6 +51,33 @@ public class WhenToStartHubPicker extends RadioButtonPickerFragment {

    private Context mContext;

    @Nullable
    private RadioButtonPickerExtraSwitchController mRestrictToWirelessChargingController = null;

    private final RadioButtonPickerExtraSwitchController.PreferenceAccessor
            mWirelessChargingPreferenceAccessor =
            new RadioButtonPickerExtraSwitchController.PreferenceAccessor() {
                @Override
                public void setValue(boolean value) {
                    Settings.Secure.putInt(
                            mContext.getContentResolver(),
                            Settings.Secure.GLANCEABLE_HUB_RESTRICT_TO_WIRELESS_CHARGING,
                            value ? 1 : 0);
                }

                @Override
                public boolean getValue() {
                    final int defaultValue = mContext.getResources().getBoolean(
                            com.android.internal.R.bool
                                    .config_onlyShowGlanceableHubWhenWirelessChargingDefault)
                            ? 1 : 0;
                    return Settings.Secure.getInt(
                            mContext.getContentResolver(),
                            Settings.Secure.GLANCEABLE_HUB_RESTRICT_TO_WIRELESS_CHARGING,
                            defaultValue) == 1;
                }
            };

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
@@ -85,6 +114,18 @@ public class WhenToStartHubPicker extends RadioButtonPickerFragment {
        return candidates;
    }

    @Override
    protected void addStaticPreferences(PreferenceScreen screen) {
        if (mRestrictToWirelessChargingController == null) {
            mRestrictToWirelessChargingController =
                    new RadioButtonPickerExtraSwitchController(
                            mContext,
                            R.string.hub_restrict_to_wireless_charging_title,
                            mWirelessChargingPreferenceAccessor);
            mRestrictToWirelessChargingController.addToScreen(screen);
        }
    }

    private String[] entries() {
        return getResources().getStringArray(R.array.when_to_start_hubmode_entries);
    }
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.dream;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceScreen;

import com.android.settingslib.RestrictedSwitchPreference;

/**
 * Controller for a switch preference that can be added to a
 * {@link com.android.settings.widget.RadioButtonPickerFragment}.
 */
public class RadioButtonPickerExtraSwitchController implements OnPreferenceChangeListener {
    private static final String PREFERENCE_KEY = "restricted_to_wireless_charging";

    private final RestrictedSwitchPreference mPreference;
    private final PreferenceAccessor mPreferenceAccessor;

    /**
     * Interface for an object that is responsible for setting and getting the preference for which
     * this controller is responsible.
     */
    public interface PreferenceAccessor {
        /** Set the preference to the given value. */
        void setValue(boolean value);

        /** Get the current value of the preference. */
        boolean getValue();
    }

    public RadioButtonPickerExtraSwitchController(
            @NonNull Context context,
            int titleResId,
            @NonNull PreferenceAccessor preferenceAccessor) {
        mPreferenceAccessor = preferenceAccessor;

        mPreference = new RestrictedSwitchPreference(context);
        if (titleResId != 0) {
            mPreference.setTitle(titleResId);
        }
        mPreference.setChecked(mPreferenceAccessor.getValue());
        mPreference.setKey(PREFERENCE_KEY);
        mPreference.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) {
        mPreferenceAccessor.setValue((Boolean) newValue);
        return true;
    }

    /** Add this controller's preference to the given {@link PreferenceScreen}. */
    public void addToScreen(@NonNull PreferenceScreen screen) {
        screen.addPreference(mPreference);
    }
}
Loading