cmsdk: Move UserContentObserver from framework

* This is used by LiveDisplay, but may be useful for other purposes.
   Move it into the internal/common package instead of keeping an
   external dependency.

Change-Id: I87c0af11e19ecaea4a1a5b736fc286cd9a8ac55c
This commit is contained in:
Steve Kondik
2016-08-29 01:53:13 -07:00
parent 5501335568
commit 613ec77559
3 changed files with 94 additions and 2 deletions

View File

@@ -0,0 +1,92 @@
/*
* 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 org.cyanogenmod.platform.internal.common;
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();
}
}

View File

@@ -27,9 +27,9 @@ import android.os.Handler;
import android.os.UserHandle;
import android.util.Log;
import com.android.server.pm.UserContentObserver;
import com.android.server.twilight.TwilightState;
import org.cyanogenmod.platform.internal.common.UserContentObserver;
import org.cyanogenmod.platform.internal.display.LiveDisplayService.State;
import java.io.PrintWriter;

View File

@@ -45,13 +45,13 @@ import android.view.Display;
import com.android.internal.util.ArrayUtils;
import com.android.server.LocalServices;
import com.android.server.ServiceThread;
import com.android.server.pm.UserContentObserver;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
import org.cyanogenmod.internal.util.QSConstants;
import org.cyanogenmod.internal.util.QSUtils;
import org.cyanogenmod.platform.internal.common.UserContentObserver;
import org.cyanogenmod.platform.internal.CMSystemService;
import org.cyanogenmod.platform.internal.R;