Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Created September 15, 2016 18:11
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save renaudcerrato/746e039700ac5eeaaea40808666e239f to your computer and use it in GitHub Desktop.
Save renaudcerrato/746e039700ac5eeaaea40808666e239f to your computer and use it in GitHub Desktop.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.view.View;
/**
* Created by Cerrato Renaud <renaud.cerrato@gmail.com>
* https://github.com/renaudcerrato
* 9/7/16
*/
public class InteractiveViewHelper {
private final View mView;
private final Callback mCallback;
private final PowerManager mPowerManager;
private BroadcastReceiver mReceiver;
private IntentFilter mScreenIntentFilter;
private boolean isVisible;
private boolean isWindowVisible;
private boolean isAttachedToWindow;
private boolean isViewInteractive;
public interface Callback {
void onInteractivityChanged(boolean isInteractive);
}
public InteractiveViewHelper(@NonNull View view, @NonNull Callback callback) {
mView = view;
mCallback = callback;
mPowerManager = (PowerManager) view.getContext().getSystemService(Context.POWER_SERVICE);
isVisible = view.isShown();
isWindowVisible = view.getWindowVisibility() == View.VISIBLE;
isAttachedToWindow = ViewCompat.isAttachedToWindow(view);
update(false);
}
public boolean isViewInteractive() {
return isViewInteractive;
}
public void onAttachedToWindow() {
isAttachedToWindow = true;
update(true);
}
public void onDetachedFromWindow() {
isAttachedToWindow = false;
update(true);
}
@SuppressWarnings("UnusedParameters")
public void onVisibilityChanged(@NonNull View changedView, int visibility) {
isVisible = visibility == View.VISIBLE;
update(true);
}
public void onWindowVisibilityChanged(int visibility) {
isWindowVisible = visibility == View.VISIBLE;
update(true);
}
private void update(boolean notify) {
final boolean isViewVisible = isVisible && isAttachedToWindow && isWindowVisible;
final boolean interactive = isViewVisible && isScreenInteractive();
maybeRegisterScreenReceiver(isViewVisible);
if(interactive != isViewInteractive) {
isViewInteractive = interactive;
if (notify) {
mCallback.onInteractivityChanged(isViewInteractive);
}
}
}
private void maybeRegisterScreenReceiver(boolean register) {
if(register) {
if(mReceiver == null) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
update(true);
}
};
if(mScreenIntentFilter == null) {
mScreenIntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
mScreenIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
}
mView.getContext().registerReceiver(mReceiver, mScreenIntentFilter);
}
}else if(mReceiver != null) {
mView.getContext().unregisterReceiver(mReceiver);
mReceiver = null;
}
}
private boolean isScreenInteractive() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) return mPowerManager.isInteractive();
return mPowerManager.isScreenOn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment