summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/settings/NotificationBrightnessController.java
blob: 617ef8ffe41c98be2e5e45db0a94e7170ba0a26e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2012-2015 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.settings;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.UserHandle;
import android.provider.Settings;

import com.android.systemui.R;

import java.lang.Exception;
import java.util.ArrayList;

import cyanogenmod.providers.CMSettings;

public class NotificationBrightnessController implements ToggleSlider.Listener {
    private static final String TAG = "StatusBar.NotificationBrightnessController";

    public static final int LIGHT_BRIGHTNESS_MINIMUM = 1;
    public static final int LIGHT_BRIGHTNESS_MAXIMUM = 255;

    // Minimum delay between LED notification updates
    private final static long LED_UPDATE_DELAY_MS = 250;

    private int mCurrentBrightness;
    private final int mMinimumBrightness;
    private final int mMaximumBrightness;

    private final Context mContext;
    private final ToggleSlider mControl;
    private final CurrentUserTracker mUserTracker;
    private final Handler mHandler;
    private final NotificationBrightnessObserver mBrightnessObserver;

    private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
            new ArrayList<BrightnessStateChangeCallback>();

    private boolean mListening;
    private boolean mExternalChange;

    private boolean mNotificationAllow;
    private final Bundle mNotificationBundle;
    private final Notification.Builder mNotificationBuilder;
    private NotificationManager mNotificationManager;

    public interface BrightnessStateChangeCallback {
        public void onBrightnessLevelChanged();
    }

    /** ContentObserver to watch brightness **/
    private class NotificationBrightnessObserver extends ContentObserver {

        private final Uri NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL_URI =
                CMSettings.System.getUriFor(CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL);

        public NotificationBrightnessObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            onChange(selfChange, null);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            if (selfChange) return;
            try {
                mExternalChange = true;
                updateSlider();
                for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
                    cb.onBrightnessLevelChanged();
                }
            } finally {
                mExternalChange = false;
            }
        }

        public void startObserving() {
            final ContentResolver cr = mContext.getContentResolver();
            cr.unregisterContentObserver(this);
            cr.registerContentObserver(
                    NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL_URI,
                    false, this, UserHandle.USER_ALL);
        }

        public void stopObserving() {
            final ContentResolver cr = mContext.getContentResolver();
            cr.unregisterContentObserver(this);
        }

    }

    public NotificationBrightnessController(Context context, ToggleSlider control) {
        mContext = context;
        mControl = control;
        mHandler = new Handler();
        mUserTracker = new CurrentUserTracker(mContext) {
            @Override
            public void onUserSwitched(int newUserId) {
                updateSlider();
            }
        };
        mBrightnessObserver = new NotificationBrightnessObserver(mHandler);

        mMinimumBrightness = LIGHT_BRIGHTNESS_MINIMUM;
        mMaximumBrightness = LIGHT_BRIGHTNESS_MAXIMUM;
        mCurrentBrightness = LIGHT_BRIGHTNESS_MAXIMUM;

        mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationBundle = new Bundle();
        mNotificationBuilder = new Notification.Builder(mContext);

        mNotificationBundle.putBoolean(Notification.EXTRA_FORCE_SHOW_LIGHTS, true);
        mNotificationBuilder.setExtras(mNotificationBundle)
                .setContentTitle(mContext.getString(R.string.led_notification_title))
                .setContentText(mContext.getString(R.string.led_notification_text))
                .setSmallIcon(R.drawable.ic_settings)
                .setOngoing(true);
    }

    private Handler mLedHandler = new Handler() {
        public void handleMessage(Message msg) {
            updateNotification();
        }
    };

    public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
        mChangeCallbacks.add(cb);
    }

    public boolean removeStateChangedCallback(BrightnessStateChangeCallback cb) {
        return mChangeCallbacks.remove(cb);
    }

    @Override
    public void onInit(ToggleSlider control) {
        // Do nothing
    }

    public void registerCallbacks() {
        if (mListening) {
            return;
        }

        // Update the slider and mode before attaching the listener so we don't
        // receive the onChanged notifications for the initial values.
        mNotificationAllow = true;
        updateSlider();

        mBrightnessObserver.startObserving();
        mUserTracker.startTracking();

        mControl.setOnChangedListener(this);
        mListening = true;
    }

    /** Unregister all call backs, both to and from the controller */
    public void unregisterCallbacks() {
        if (!mListening) {
            return;
        }

        mNotificationAllow = false;
        mBrightnessObserver.stopObserving();
        mUserTracker.stopTracking();
        mControl.setOnChangedListener(null);
        mNotificationManager.cancel(1);
        mListening = false;

        CMSettings.System.putIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mCurrentBrightness, UserHandle.USER_CURRENT);
    }

    @Override
    public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value,
            boolean stopTracking) {
        if (mExternalChange) return;

        mCurrentBrightness = value + mMinimumBrightness;
        updateNotification();

        for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
            cb.onBrightnessLevelChanged();
        }
    }

    /** Fetch the brightness from the system settings and update the slider */
    private void updateSlider() {
        mCurrentBrightness = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mMaximumBrightness, UserHandle.USER_CURRENT);

        CMSettings.System.putIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mMaximumBrightness, UserHandle.USER_CURRENT);

        mControl.setMax(mMaximumBrightness - mMinimumBrightness);
        mControl.setValue(mCurrentBrightness - mMinimumBrightness);
        updateNotification();
    }

    /** Fetch the brightness from the system settings and update the slider */
    private void updateNotification() {
        // Dampen rate of consecutive LED changes
        if (mLedHandler.hasMessages(0)) {
            return;
        }

        if (mNotificationAllow) {
            mLedHandler.sendEmptyMessageDelayed(0, LED_UPDATE_DELAY_MS);

            // Instead of canceling the notification, force it to update with the color.
            // Use a white light for a better preview of the brightness.
            int notificationColor = 0xFFFFFF | (mCurrentBrightness << 24);
            mNotificationBuilder.setLights(notificationColor, 1, 0);
            mNotificationManager.notify(1, mNotificationBuilder.build());
        }
    }

}