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

Commit c4fd8e65 authored by Santos Cordon's avatar Santos Cordon
Browse files

Add permissions check for brightness changes via shell.

Also
- Adjusts some code structure around DisplayManagerShellCommand.
- Remove clearIdentity() on other display shell commands.

Test: Run all shell options for display in adb root and unroot.
Test: Verify that 'adb shell cmd display set-brightness *' still works.
Test: atest DisplayTest
Bug: 144920149
Change-Id: I9c6f584537a89a99f38871e280796fe66feeb312
parent 44e5637c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -230,6 +230,9 @@
    <!-- Permission required for CTS test - CtsOsTestCases -->
    <uses-permission android:name="android.permission.MANAGE_CRATES"/>

    <!-- Allows setting brightness from the shell -->
    <uses-permission android:name="android.permission.CONTROL_DISPLAY_BRIGHTNESS"/>

    <application android:label="@string/app_label"
                android:theme="@android:style/Theme.DeviceDefault.DayNight"
                android:defaultToDeviceProtectedStorage="true"
+31 −42
Original line number Diff line number Diff line
@@ -77,7 +77,6 @@ import android.os.SystemProperties;
import android.os.Trace;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.IntArray;
import android.util.Pair;
@@ -1346,6 +1345,35 @@ public final class DisplayManagerService extends SystemService {
        return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
    }

    void resetBrightnessConfiguration() {
        setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
                mContext.getPackageName());
    }

    void setAutoBrightnessLoggingEnabled(boolean enabled) {
        if (mDisplayPowerController != null) {
            synchronized (mSyncRoot) {
                mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
            }
        }
    }

    void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
        if (mDisplayPowerController != null) {
            synchronized (mSyncRoot) {
                mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
            }
        }
    }

    void setAmbientColorTemperatureOverride(float cct) {
        if (mDisplayPowerController != null) {
            synchronized (mSyncRoot) {
                mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
            }
        }
    }

    private void onDesiredDisplayModeSpecsChangedInternal() {
        boolean changed = false;
        synchronized (mSyncRoot) {
@@ -2249,13 +2277,8 @@ public final class DisplayManagerService extends SystemService {
        public void onShellCommand(FileDescriptor in, FileDescriptor out,
                FileDescriptor err, String[] args, ShellCallback callback,
                ResultReceiver resultReceiver) {
            final long token = Binder.clearCallingIdentity();
            try {
                DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
                command.exec(this, in, out, err, args, callback, resultReceiver);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
            new DisplayManagerShellCommand(DisplayManagerService.this).exec(this, in, out, err,
                    args, callback, resultReceiver);
        }

        @Override // Binder call
@@ -2278,40 +2301,6 @@ public final class DisplayManagerService extends SystemService {
            }
        }

        void setBrightness(int brightness) {
            Settings.System.putIntForUser(mContext.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
        }

        void resetBrightnessConfiguration() {
            setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
                    mContext.getPackageName());
        }

        void setAutoBrightnessLoggingEnabled(boolean enabled) {
            if (mDisplayPowerController != null) {
                synchronized (mSyncRoot) {
                    mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
                }
            }
        }

        void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
            if (mDisplayPowerController != null) {
                synchronized (mSyncRoot) {
                    mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
                }
            }
        }

        void setAmbientColorTemperatureOverride(float cct) {
            if (mDisplayPowerController != null) {
                synchronized (mSyncRoot) {
                    mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
                }
            }
        }

        private boolean validatePackageName(int uid, String packageName) {
            if (packageName != null) {
                String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
+20 −3
Original line number Diff line number Diff line
@@ -16,17 +16,22 @@

package com.android.server.display;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.ShellCommand;
import android.os.UserHandle;
import android.provider.Settings;

import java.io.PrintWriter;

class DisplayManagerShellCommand extends ShellCommand {
    private static final String TAG = "DisplayManagerShellCommand";

    private final DisplayManagerService.BinderService mService;
    private final DisplayManagerService mService;

    DisplayManagerShellCommand(DisplayManagerService.BinderService service) {
    DisplayManagerShellCommand(DisplayManagerService service) {
        mService = service;
    }

@@ -96,7 +101,19 @@ class DisplayManagerShellCommand extends ShellCommand {
            getErrPrintWriter().println("Error: brightness should be a number between 0 and 1");
            return 1;
        }
        mService.setBrightness((int) (brightness * 255));

        final Context context = mService.getContext();
        context.enforceCallingOrSelfPermission(
                Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
                "Permission required to set the display's brightness");
        final long token = Binder.clearCallingIdentity();
        try {
            Settings.System.putIntForUser(context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS, (int) (brightness * 255),
                    UserHandle.USER_CURRENT);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        return 0;
    }