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

Commit 095f9ea3 authored by Zhao Wei Liew's avatar Zhao Wei Liew
Browse files

SystemUI: Use Tuner API for CM settings

Get rid of all the excess code by implementing TunerService.Tunable
and observing any changes made to the settings through it.

Remove UserContentObserver as the Tuner API handles user switches.

Also remove some unused imports that were introduced in earlier
CM commits, but were never removed since.

Change-Id: Iecafafabdaec82b3b3c72293bea865de48f0e90a
parent d0adc72a
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.view.View;
import android.widget.ImageView;

import com.android.systemui.statusbar.phone.StatusBarIconController;
import com.android.systemui.cm.UserContentObserver;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.tuner.TunerService;

+0 −89
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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.cm;

import android.app.ActivityManagerNative;
import android.app.IUserSwitchObserver;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IRemoteCallback;
import android.os.RemoteException;
import android.util.Log;

/**
 * Simple extension of ContentObserver that also listens for user switch events to call update
 */
public abstract class UserContentObserver extends ContentObserver {
    private static final String TAG = "UserContentObserver";

    private Runnable mUpdateRunnable;

    private IUserSwitchObserver mUserSwitchObserver = new IUserSwitchObserver.Stub() {
        @Override
        public void onUserSwitching(int newUserId, IRemoteCallback reply) {
        }
        @Override
        public void onUserSwitchComplete(int newUserId) throws RemoteException {
            mHandler.post(mUpdateRunnable);
        }
        @Override
        public void onForegroundProfileSwitch(int newProfileId) {
        }
    };

    private Handler mHandler;

    public UserContentObserver(Handler handler) {
        super(handler);
        mHandler = handler;
        mUpdateRunnable = new Runnable() {
            @Override
            public void run() {
                update();
            }
        };
    }

    protected void observe() {
        try {
            ActivityManagerNative.getDefault().registerUserSwitchObserver(mUserSwitchObserver);
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to register user switch observer!", e);
        }
    }

    protected void unobserve() {
        try {
            mHandler.removeCallbacks(mUpdateRunnable);
            ActivityManagerNative.getDefault().unregisterUserSwitchObserver(mUserSwitchObserver);
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to unregister user switch observer!", e);
        }
    }

    protected abstract void update();

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

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        update();
    }
}
+26 −69
Original line number Diff line number Diff line
@@ -18,27 +18,28 @@ 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 com.android.systemui.tuner.TunerService;

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

import cyanogenmod.providers.CMSettings;

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

    private static final String NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL =
            "cmsystem:" + CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL;

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

@@ -51,9 +52,6 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {

    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>();
@@ -70,61 +68,9 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {
        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;
@@ -171,10 +117,9 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {
        // Update the slider and mode before attaching the listener so we don't
        // receive the onChanged notifications for the initial values.
        mNotificationAllow = true;
        updateSlider();
        updateSlider(null);

        mBrightnessObserver.startObserving();
        mUserTracker.startTracking();
        TunerService.get(mContext).addTunable(this, NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL);

        mControl.setOnChangedListener(this);
        mListening = true;
@@ -187,8 +132,7 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {
        }

        mNotificationAllow = false;
        mBrightnessObserver.stopObserving();
        mUserTracker.stopTracking();
        TunerService.get(mContext).removeTunable(this);
        mControl.setOnChangedListener(null);
        mNotificationManager.cancel(1);
        mListening = false;
@@ -212,10 +156,8 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {
    }

    /** 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);
    private void updateSlider(final String value) {
        mCurrentBrightness = value == null ? mMaximumBrightness : Integer.parseInt(value);

        CMSettings.System.putIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
@@ -244,4 +186,19 @@ public class NotificationBrightnessController implements ToggleSlider.Listener {
        }
    }

    @Override
    public void onTuningChanged(String key, String newValue) {
        if (!NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL.equals(key)) {
            return;
        }
        try {
            mExternalChange = true;
            updateSlider(newValue);
            for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
                cb.onBrightnessLevelChanged();
            }
        } finally {
            mExternalChange = false;
        }
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.android.systemui.R;
import com.android.systemui.cm.UserContentObserver;

import java.util.ArrayList;
import java.util.List;
+21 −11
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
@@ -40,7 +41,7 @@ import android.view.accessibility.AccessibilityEvent;

import com.android.internal.statusbar.StatusBarIcon;
import com.android.systemui.R;
import com.android.systemui.cm.UserContentObserver;
import com.android.systemui.settings.CurrentUserTracker;

import java.text.NumberFormat;
import java.util.ArrayList;
@@ -390,10 +391,11 @@ public class StatusBarIconView extends AnimatedImageView {
        return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
    }

    static class GlobalSettingsObserver extends UserContentObserver {
    static class GlobalSettingsObserver extends ContentObserver {
        private static GlobalSettingsObserver sInstance;
        private ArrayList<StatusBarIconView> mIconViews = new ArrayList<StatusBarIconView>();
        private Context mContext;
        private CurrentUserTracker mUserTracker;

        GlobalSettingsObserver(Handler handler, Context context) {
            super(handler);
@@ -421,24 +423,32 @@ public class StatusBarIconView extends AnimatedImageView {
            }
        }

        @Override
        protected void observe() {
            super.observe();

        void observe() {
            mContext.getContentResolver().registerContentObserver(
                    CMSettings.System.getUriFor(CMSettings.System.STATUS_BAR_NOTIF_COUNT),
                    false, this);
        }

            mUserTracker = new CurrentUserTracker(mContext) {
                @Override
        protected void unobserve() {
            super.unobserve();
                public void onUserSwitched(int newUserId) {
                    update();
                }
            };
            mUserTracker.startTracking();
        }

        void unobserve() {
            mUserTracker.stopTracking();
            mContext.getContentResolver().unregisterContentObserver(this);
        }

        @Override
        public void update() {
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            update();
        }

        private void update() {
            boolean showIconCount = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                    CMSettings.System.STATUS_BAR_NOTIF_COUNT, 0, UserHandle.USER_CURRENT) == 1;
            for (StatusBarIconView sbiv : mIconViews) {
Loading