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

Commit b70179fd authored by Sam Mortimer's avatar Sam Mortimer Committed by Gerrit Code Review
Browse files

[1/2] Forward port power connect/disconnect notification support

Original commit info:

    [1/2] Power connect/disconnect notification support

    part 1/2: frameworks/base PowerUI and Settings
    http://review.cyanogenmod.org/#/c/35241/

    part 2/2: packages/apps/Settings Sound settings
    http://review.cyanogenmod.org/#/c/35242/

    Change-Id: I36a6b9f924d2cd52191a8e83a744745b37c5b068

Change-Id: Ib865e1c296cb8e1d957b2a48616c88ceaba5c008
parent f2fa2d8d
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.systemui.power;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
@@ -66,6 +68,9 @@ public class PowerUI extends SystemUI {

    private long mScreenOffTime = -1;

    // For filtering ACTION_POWER_DISCONNECTED on boot
    boolean mIgnoreFirstPowerEvent = true;

    public void start() {

        mLowBatteryAlertCloseLevel = mContext.getResources().getInteger(
@@ -83,6 +88,8 @@ public class PowerUI extends SystemUI {
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
    }

@@ -130,6 +137,10 @@ public class PowerUI extends SystemUI {
                final boolean plugged = mPlugType != 0;
                final boolean oldPlugged = oldPlugType != 0;

                if (mIgnoreFirstPowerEvent && plugged) {
                    mIgnoreFirstPowerEvent = false;
                }

                int oldBucket = findBatteryLevelBucket(oldBatteryLevel);
                int bucket = findBatteryLevelBucket(mBatteryLevel);

@@ -175,6 +186,18 @@ public class PowerUI extends SystemUI {
                mScreenOffTime = SystemClock.elapsedRealtime();
            } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
                mScreenOffTime = -1;
            } else if (Intent.ACTION_POWER_CONNECTED.equals(action)
                    || Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
                final ContentResolver cr = mContext.getContentResolver();

                if (mIgnoreFirstPowerEvent) {
                    mIgnoreFirstPowerEvent = false;
                } else {
                    if (Settings.Global.getInt(cr,
                            Settings.Global.POWER_NOTIFICATIONS_ENABLED, 0) == 1) {
                        playPowerNotificationSound();
                    }
                }
            } else {
                Slog.w(TAG, "unknown intent: " + intent);
            }
@@ -308,6 +331,34 @@ public class PowerUI extends SystemUI {
        mInvalidChargerDialog = d;
    }

    void playPowerNotificationSound() {
        final ContentResolver cr = mContext.getContentResolver();
        final String soundPath =
                Settings.Global.getString(cr, Settings.Global.POWER_NOTIFICATIONS_RINGTONE);

        NotificationManager notificationManager =
                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        Notification powerNotify=new Notification();
        powerNotify.defaults = Notification.DEFAULT_ALL;
        if (soundPath != null) {
            powerNotify.sound = Uri.parse(soundPath);
            if (powerNotify.sound != null) {
                // DEFAULT_SOUND overrides so flip off
                powerNotify.defaults &= ~Notification.DEFAULT_SOUND;
            }
        }
        if (Settings.Global.getInt(cr,
                Settings.Global.POWER_NOTIFICATIONS_VIBRATE, 0) == 0) {
            powerNotify.defaults &= ~Notification.DEFAULT_VIBRATE;
        }

        notificationManager.notify(0, powerNotify);
    }

    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        pw.print("mLowBatteryAlertCloseLevel=");
        pw.println(mLowBatteryAlertCloseLevel);