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

Commit 49c6a637 authored by William Escande's avatar William Escande
Browse files

SystemServer: AutoOnFeature sync binder call

Bug: 323060869
Bug: 316946334
Test: manual testing
Change-Id: I08bdb871d9645255f2f2ec6c178d19d95ee0a181
parent 58d60d2b
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -95,8 +95,11 @@ public fun notifyBluetoothOn(resolver: ContentResolver) {

    if (!isFeatureSupportedForUser(resolver)) {
        val defaultFeatureValue = true
        if (!setFeatureEnabledForUserUnchecked(resolver, defaultFeatureValue)) {
            Log.e(TAG, "Failed to set feature to its default value ${defaultFeatureValue}")
        } else {
            Log.i(TAG, "Feature was set to its default value ${defaultFeatureValue}")
        setFeatureEnabledForUserUnchecked(resolver, defaultFeatureValue)
        }
    }
}

@@ -119,7 +122,9 @@ public fun setUserEnabled(
    if (!isUserSupported(context.contentResolver)) {
        throw IllegalStateException("AutoOnFeature not supported for user: ${context.getUser()}")
    }
    setFeatureEnabledForUserUnchecked(context.contentResolver, status)
    if (!setFeatureEnabledForUserUnchecked(context.contentResolver, status)) {
        throw IllegalStateException("AutoOnFeature database failure for user: ${context.getUser()}")
    }
    Counter.logIncrement(
        if (status) "bluetooth.value_auto_on_enabled" else "bluetooth.value_auto_on_disabled"
    )
@@ -267,8 +272,8 @@ private fun isFeatureSupportedForUser(resolver: ContentResolver): Boolean {
 *
 * @return whether the auto on feature is enabled for this user
 */
private fun setFeatureEnabledForUserUnchecked(resolver: ContentResolver, status: Boolean) {
    Settings.Secure.putInt(resolver, USER_SETTINGS_KEY, if (status) 1 else 0)
private fun setFeatureEnabledForUserUnchecked(resolver: ContentResolver, status: Boolean): Boolean {
    return Settings.Secure.putInt(resolver, USER_SETTINGS_KEY, if (status) 1 else 0)
}

// Listener is needed because code should be actionable prior to V API release
+35 −11
Original line number Diff line number Diff line
@@ -83,6 +83,8 @@ import com.android.server.BluetoothManagerServiceDumpProto;
import com.android.server.bluetooth.airplane.AirplaneModeListener;
import com.android.server.bluetooth.satellite.SatelliteModeListener;

import libcore.util.SneakyThrow;

import kotlin.Unit;
import kotlin.time.TimeSource;

@@ -101,7 +103,12 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@@ -2704,31 +2711,48 @@ class BluetoothManagerService {
                mLooper, mCurrentUserContext, mState, this::enableFromAutoOn);
    }

    private <T> T postAndWait(Callable<T> callable) {
        FutureTask<T> task = new FutureTask(callable);

        mHandler.post(task);
        try {
            return task.get(1, TimeUnit.SECONDS);
        } catch (TimeoutException | InterruptedException e) {
            SneakyThrow.sneakyThrow(e);
        } catch (ExecutionException e) {
            SneakyThrow.sneakyThrow(e.getCause());
        }
        return null;
    }

    boolean isAutoOnSupported() {
        return mDeviceConfigAllowAutoOn
                && AutoOnFeature.isUserSupported(mCurrentUserContext.getContentResolver());
                && postAndWait(
                        () ->
                                AutoOnFeature.isUserSupported(
                                        mCurrentUserContext.getContentResolver()));
    }

    boolean isAutoOnEnabled() {
        if (!mDeviceConfigAllowAutoOn) {
            throw new IllegalStateException("AutoOnFeature is not supported in current config");
        }
        return AutoOnFeature.isUserEnabled(mCurrentUserContext);
        return postAndWait(() -> AutoOnFeature.isUserEnabled(mCurrentUserContext));
    }

    void setAutoOnEnabled(boolean status) {
        if (!mDeviceConfigAllowAutoOn) {
            throw new IllegalStateException("AutoOnFeature is not supported in current config");
        }
        // Call coming from binder thread need to be posted before exec
        mHandler.post(
        postAndWait(
                Executors.callable(
                        () ->
                                AutoOnFeature.setUserEnabled(
                                        mLooper,
                                        mCurrentUserContext,
                                        mState,
                                        status,
                                this::enableFromAutoOn));
                                        this::enableFromAutoOn)));
    }

    /**