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

Commit 1922856e authored by Sam Mortimer's avatar Sam Mortimer
Browse files

[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/

patchset 3:
    shrink a couple of long lines
    remove power_notifications_use_default option (folded into ringtone option)
    load default settings in global instead of system

patchset 5:
    support vibrate toggle
    ignore first power state change (on boot) if it's a DISCONNECT

patchset 6:
    fix ignore first power state change

patchset 7:
    change vibrate to default to off
    simplify first power state change ignore logic

Change-Id: I36a6b9f924d2cd52191a8e83a744745b37c5b068
parent d3361b7d
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -5261,6 +5261,24 @@ public final class Settings {
         */
        public static final String POWER_SOUNDS_ENABLED = "power_sounds_enabled";

        /**
         * Whether to sound when charger power is connected/disconnected
         * @hide
         */
        public static final String POWER_NOTIFICATIONS_ENABLED = "power_notifications_enabled";

        /**
         * Whether to vibrate when charger power is connected/disconnected
         * @hide
         */
        public static final String POWER_NOTIFICATIONS_VIBRATE = "power_notifications_vibrate";

        /**
         * URI for power notification sounds
         * @hide
         */
        public static final String POWER_NOTIFICATIONS_RINGTONE = "power_notifications_ringtone";

        /**
         * URI for the "wireless charging started" sound.
         * @hide
@@ -6330,6 +6348,9 @@ public final class Settings {
            AUTO_TIME,
            AUTO_TIME_ZONE,
            POWER_SOUNDS_ENABLED,
            POWER_NOTIFICATIONS_ENABLED,
            POWER_NOTIFICATIONS_VIBRATE,
            POWER_NOTIFICATIONS_RINGTONE,
            DOCK_SOUNDS_ENABLED,
            USB_MASS_STORAGE_ENABLED,
            ENABLE_ACCESSIBILITY_GLOBAL_GESTURE_ENABLED,
+4 −0
Original line number Diff line number Diff line
@@ -77,6 +77,10 @@
    <string name="def_unlock_sound" translatable="false">/system/media/audio/ui/Unlock.ogg</string>
    <string name="def_wireless_charging_started_sound" translatable="false">/system/media/audio/ui/WirelessChargingStarted.ogg</string>

    <bool name="def_power_notifications_enabled">false</bool>
    <bool name="def_power_notifications_vibrate">false</bool>
    <string name="def_power_notifications_ringtone" translatable="false">content://settings/system/notification_sound</string>

    <bool name="def_lockscreen_disabled">false</bool>
    <bool name="def_device_provisioned">false</bool>

+7 −0
Original line number Diff line number Diff line
@@ -2229,6 +2229,13 @@ public class DatabaseHelper extends SQLiteOpenHelper {
            loadStringSetting(stmt, Settings.Global.WIRELESS_CHARGING_STARTED_SOUND,
                    R.string.def_wireless_charging_started_sound);

            loadBooleanSetting(stmt, Settings.Global.POWER_NOTIFICATIONS_ENABLED,
                    R.bool.def_power_notifications_enabled);
            loadBooleanSetting(stmt, Settings.Global.POWER_NOTIFICATIONS_VIBRATE,
                    R.bool.def_power_notifications_vibrate);
            loadStringSetting(stmt, Settings.Global.POWER_NOTIFICATIONS_RINGTONE,
                    R.string.def_power_notifications_ringtone);

            loadSetting(stmt, Settings.Global.SET_INSTALL_LOCATION, 0);
            loadSetting(stmt, Settings.Global.DEFAULT_INSTALL_LOCATION,
                    PackageHelper.APP_INSTALL_AUTO);
+50 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import java.io.PrintWriter;
import java.util.Arrays;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
@@ -62,6 +64,9 @@ public class PowerUI extends SystemUI {
    AlertDialog mLowBatteryDialog;
    TextView mBatteryLevelTextView;

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

    public void start() {

        mLowBatteryAlertCloseLevel = mContext.getResources().getInteger(
@@ -75,6 +80,7 @@ public class PowerUI extends SystemUI {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
    }

@@ -122,6 +128,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);

@@ -163,6 +173,18 @@ public class PowerUI extends SystemUI {
                } else if (mBatteryLevelTextView != null) {
                    showLowBatteryWarning();
                }
            } else if (action.equals(Intent.ACTION_POWER_CONNECTED)
                    || action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                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);
            }
@@ -284,6 +306,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);