blob: 03ade5f2abd8dc277c411d3818e0fadc9b0fec60 [file] [log] [blame]
/*
* Copyright (C) 2020 The Android Open Source 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.location.injector;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import com.android.internal.util.Preconditions;
import com.android.server.FgThread;
/**
* Provides accessors and listeners for screen interactive state (screen on/off).
*/
public class SystemScreenInteractiveHelper extends ScreenInteractiveHelper {
private final Context mContext;
private boolean mReady;
private volatile boolean mIsInteractive;
public SystemScreenInteractiveHelper(Context context) {
mContext = context;
}
/** Called when system is ready. */
public void onSystemReady() {
if (mReady) {
return;
}
IntentFilter screenIntentFilter = new IntentFilter();
screenIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
screenIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
mContext.registerReceiverAsUser(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean interactive;
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
interactive = true;
} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
interactive = false;
} else {
return;
}
onScreenInteractiveChanged(interactive);
}
}, UserHandle.ALL, screenIntentFilter, null, FgThread.getHandler());
mReady = true;
}
void onScreenInteractiveChanged(boolean interactive) {
if (interactive == mIsInteractive) {
return;
}
mIsInteractive = interactive;
notifyScreenInteractiveChanged(interactive);
}
@Override
public boolean isInteractive() {
Preconditions.checkState(mReady);
return mIsInteractive;
}
}