Skip to content

Commit

Permalink
Added Choreographer alternative for ancient devices
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Jul 18, 2016
1 parent 0786513 commit 07ce249
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

package org.quantumbadger.redreader.views;

import android.view.Choreographer;
public abstract class RRAnimation implements RRChoreographer.Callback {

public abstract class RRAnimation implements Choreographer.FrameCallback {

private static final Choreographer CHOREOGRAPHER = Choreographer.getInstance();
private static final RRChoreographer CHOREOGRAPHER = RRChoreographer.getInstance();

private long mFirstFrameNanos = -1;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.views;

import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import org.quantumbadger.redreader.common.AndroidApi;

public abstract class RRChoreographer {

public interface Callback {
void doFrame(long frameTimeNanos);
}

@NonNull
public static RRChoreographer getInstance() {

if(AndroidApi.isGreaterThanOrEqualTo(Build.VERSION_CODES.JELLY_BEAN)) {
Log.i("RRChoreographer", "Using modern Choreographer");
return RRChoreographerModern.INSTANCE;
} else {
Log.i("RRChoreographer", "Using legacy Choreographer");
return RRChoreographerLegacy.INSTANCE;
}
}

public abstract void postFrameCallback(@NonNull final Callback callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.views;

import android.support.annotation.NonNull;
import org.quantumbadger.redreader.common.AndroidApi;

public class RRChoreographerLegacy extends RRChoreographer implements Runnable {

static final RRChoreographerLegacy INSTANCE = new RRChoreographerLegacy();

private final Callback[] mCallbacks = new Callback[128];
private int mCallbackCount = 0;
private boolean mPosted = false;

private RRChoreographerLegacy() {}

@Override
public void postFrameCallback(@NonNull final Callback callback) {
mCallbacks[mCallbackCount] = callback;
mCallbackCount++;

if(!mPosted) {
AndroidApi.UI_THREAD_HANDLER.postDelayed(this, 1000 / 60);
mPosted = true;
}
}

@Override
public void run() {

final long frameTimeNanos = System.nanoTime();

final int callbackCount = mCallbackCount;
mPosted = false;
mCallbackCount = 0;

for(int i = 0; i < callbackCount; i++) {
mCallbacks[i].doFrame(frameTimeNanos);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.views;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Choreographer;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class RRChoreographerModern extends RRChoreographer implements Choreographer.FrameCallback {

static final RRChoreographerModern INSTANCE = new RRChoreographerModern();

private static final Choreographer CHOREOGRAPHER = Choreographer.getInstance();

private final Callback[] mCallbacks = new Callback[128];
private int mCallbackCount = 0;
private boolean mPosted = false;

private RRChoreographerModern() {}

@Override
public void postFrameCallback(@NonNull final Callback callback) {
mCallbacks[mCallbackCount] = callback;
mCallbackCount++;

if(!mPosted) {
CHOREOGRAPHER.postFrameCallback(this);
mPosted = true;
}
}

@Override
public void doFrame(final long frameTimeNanos) {

final int callbackCount = mCallbackCount;
mPosted = false;
mCallbackCount = 0;

for(int i = 0; i < callbackCount; i++) {
mCallbacks[i].doFrame(frameTimeNanos);
}


}
}

0 comments on commit 07ce249

Please sign in to comment.