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

Commit b7996b16 authored by Danesh M's avatar Danesh M
Browse files

LiveDisplayController : Reload settings for current user

CYNGNOS-1166

Change-Id: I1dd91a4c7a605b5aa7f23ce43b770ebdc201af60
parent 564f10b8
Loading
Loading
Loading
Loading
+11 −10
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.util.Slog;

import com.android.server.LocalServices;
import com.android.server.accessibility.DisplayAdjustmentUtils;
import com.android.server.pm.UserContentObserver;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
@@ -134,13 +135,6 @@ public class LiveDisplayController {
        mDefaultOutdoorLux = mContext.getResources().getInteger(
                org.cyanogenmod.platform.internal.R.integer.config_outdoorAmbientLux);

        // Counter used to determine when we should tell the user about this feature.
        // If it's not used after 3 sunsets, we'll show the hint once.
        mHintCounter = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                CMSettings.System.LIVE_DISPLAY_HINTED,
                -3,
                UserHandle.USER_CURRENT);

        mUseOutdoorMode =
                mHardware.isSupported(CMHardwareManager.FEATURE_SUNLIGHT_ENHANCEMENT);
        mOutdoorModeIsSelfManaged = mUseOutdoorMode ?
@@ -187,6 +181,12 @@ public class LiveDisplayController {
                CMSettings.System.DISPLAY_TEMPERATURE_MODE,
                MODE_OFF,
                UserHandle.USER_CURRENT);
        // Counter used to determine when we should tell the user about this feature.
        // If it's not used after 3 sunsets, we'll show the hint once.
        mHintCounter = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                CMSettings.System.LIVE_DISPLAY_HINTED,
                -3,
                UserHandle.USER_CURRENT);

        // Clear the hint forever
        if (mMode != MODE_OFF) {
@@ -216,7 +216,7 @@ public class LiveDisplayController {
        updateLiveDisplay(mCurrentLux);
    }

    private final class SettingsObserver extends ContentObserver {
    private final class SettingsObserver extends UserContentObserver {
        private final Uri DISPLAY_TEMPERATURE_DAY_URI =
                CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_DAY);
        private final Uri DISPLAY_TEMPERATURE_NIGHT_URI =
@@ -245,14 +245,15 @@ public class LiveDisplayController {
                cr.registerContentObserver(DISPLAY_LOW_POWER_URI, false, this, UserHandle.USER_ALL);
                cr.registerContentObserver(DISPLAY_COLOR_ENHANCE_URI, false, this, UserHandle.USER_ALL);
                cr.registerContentObserver(DISPLAY_COLOR_ADJUSTMENT_URI, false, this, UserHandle.USER_ALL);
                observe();
            } else {
                cr.unregisterContentObserver(this);
                unobserve();
            }
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange,  uri);
        protected void update() {
            updateSettings();
        }
    }
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.server.pm;

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;

    /**
     * Content observer that tracks user switches
     * to allow clients to re-load settings for current user
     */
    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);
        }
    }

    /**
     *  Called to notify of registered uri changes and user switches.
     *  Always invoked on the handler passed in at construction
     */
    protected abstract void update();

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