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

Commit 2f01d7f6 authored by Bruno Martins's avatar Bruno Martins
Browse files

power: Re-introduce custom charging sounds

 * Mostly inspired by old implementation (see commit 72fe3994),
   but refactored to use most of the AOSP code.

 * Also adapted to the wireless charging feature introduced in P.

 Also include:

   power: Respect global vibration setting for charging sounds

    * Don't vibrate when in silent mode

   Change-Id: Ife14105e74a28856d74a4c153637344124686e1b

Change-Id: I5f6589002db51fe03ec2cf7bb33ed0e63948c55a
parent 5bb14d69
Loading
Loading
Loading
Loading
+30 −7
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ import com.android.server.inputmethod.InputMethodManagerInternal;
import com.android.server.policy.WindowManagerPolicy;
import com.android.server.statusbar.StatusBarManagerInternal;

import lineageos.providers.LineageSettings;

/**
 * Sends broadcasts about important power state changes.
 * <p>
@@ -94,6 +96,7 @@ public class Notifier {
    private static final int MSG_SCREEN_BRIGHTNESS_BOOST_CHANGED = 4;
    private static final int MSG_PROFILE_TIMED_OUT = 5;
    private static final int MSG_WIRED_CHARGING_STARTED = 6;
    private static final int MSG_WIRED_CHARGING_DISCONNECTED = 7;

    private static final long[] WIRELESS_VIBRATION_TIME = {
            40, 40, 40, 40, 40, 40, 40, 40, 40, // ramp-up sampling rate = 40ms
@@ -123,6 +126,7 @@ public class Notifier {
    @Nullable private final StatusBarManagerInternal mStatusBarManagerInternal;
    private final TrustManager mTrustManager;
    private final Vibrator mVibrator;
    private final AudioManager mAudioManager;

    private final NotifierHandler mHandler;
    private final Intent mScreenOnIntent;
@@ -171,6 +175,7 @@ public class Notifier {
        mStatusBarManagerInternal = LocalServices.getService(StatusBarManagerInternal.class);
        mTrustManager = mContext.getSystemService(TrustManager.class);
        mVibrator = mContext.getSystemService(Vibrator.class);
        mAudioManager = mContext.getSystemService(AudioManager.class);

        mHandler = new NotifierHandler(looper);
        mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
@@ -659,6 +664,21 @@ public class Notifier {
        mHandler.sendMessage(msg);
    }

    /**
     * Called when wired charging has been disconnected so as to provide user feedback
     */
    public void onWiredChargingDisconnected(@UserIdInt int userId) {
        if (DEBUG) {
            Slog.d(TAG, "onWiredChargingDisconnected");
        }

        mSuspendBlocker.acquire();
        Message msg = mHandler.obtainMessage(MSG_WIRED_CHARGING_DISCONNECTED);
        msg.setAsynchronous(true);
        msg.arg1 = userId;
        mHandler.sendMessage(msg);
    }

    private void updatePendingBroadcastLocked() {
        if (!mBroadcastInProgress
                && mPendingInteractiveState != INTERACTIVE_STATE_UNKNOWN
@@ -796,10 +816,10 @@ public class Notifier {
     */
    private void playChargingStartedFeedback(@UserIdInt int userId) {
        playChargingStartedVibration(userId);
        final String soundPath = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.CHARGING_STARTED_SOUND);
        if (isChargingFeedbackEnabled(userId) && soundPath != null) {
            final Uri soundUri = Uri.parse("file://" + soundPath);
        final String soundPath = LineageSettings.Global.getString(mContext.getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_RINGTONE);
        if (isChargingFeedbackEnabled(userId) && soundPath != null && !soundPath.equals("silent")) {
            final Uri soundUri = Uri.parse(soundPath);
            if (soundUri != null) {
                final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
                if (sfx != null) {
@@ -828,8 +848,8 @@ public class Notifier {
    }

    private void playChargingStartedVibration(@UserIdInt int userId) {
        final boolean vibrateEnabled = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                Settings.Secure.CHARGING_VIBRATION_ENABLED, 1, userId) != 0;
        final boolean vibrateEnabled = LineageSettings.Global.getInt(mContext.getContentResolver(),
                LineageSettings.Global.POWER_NOTIFICATIONS_VIBRATE, 0) == 1;
        if (vibrateEnabled && isChargingFeedbackEnabled(userId)) {
            mVibrator.vibrate(WIRELESS_CHARGING_VIBRATION_EFFECT, VIBRATION_ATTRIBUTES);
        }
@@ -841,7 +861,9 @@ public class Notifier {
        final boolean dndOff = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS)
                == Settings.Global.ZEN_MODE_OFF;
        return enabled && dndOff;
        final boolean silentMode = mAudioManager.getRingerModeInternal()
                == AudioManager.RINGER_MODE_SILENT;
        return enabled && dndOff && !silentMode;
    }

    private final class NotifierHandler extends Handler {
@@ -868,6 +890,7 @@ public class Notifier {
                    lockProfile(msg.arg1);
                    break;
                case MSG_WIRED_CHARGING_STARTED:
                case MSG_WIRED_CHARGING_DISCONNECTED:
                    showWiredChargingStarted(msg.arg1);
                    break;
            }
+2 −0
Original line number Diff line number Diff line
@@ -1901,6 +1901,8 @@ public final class PowerManagerService extends SystemService
                    if (mIsPowered && !BatteryManager.isPlugWired(oldPlugType)
                            && BatteryManager.isPlugWired(mPlugType)) {
                        mNotifier.onWiredChargingStarted(mForegroundProfile);
                    } else if (wasPowered && !mIsPowered && !dockedOnWirelessCharger) {
                        mNotifier.onWiredChargingDisconnected(mForegroundProfile);
                    } else if (dockedOnWirelessCharger) {
                        mNotifier.onWirelessChargingStarted(mBatteryLevel, mForegroundProfile);
                    }