Skip to content

Instantly share code, notes, and snippets.

@ronshapiro
Last active August 29, 2015 14:04
Show Gist options
  • Save ronshapiro/036293073ff5308ac25e to your computer and use it in GitHub Desktop.
Save ronshapiro/036293073ff5308ac25e to your computer and use it in GitHub Desktop.
An adapted version of https://code.google.com/p/android-test-kit/wiki/DisablingAnimations configured as a InstrumentationTestRunner
android {
defaultConfig {
testInstrumentationRunner 'com.venmo.tests.VenmoTestRunner'
// ...
}
}
package com.venmo.tests;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner;
import java.lang.reflect.Method;
@SuppressWarnings("unused")
public class VenmoTestRunner extends GoogleInstrumentationTestRunner {
private static final String ANIMATION_PERMISSION = "android.permission.SET_ANIMATION_SCALE";
private static final float DISABLED = 0.0f;
private static final float DEFAULT = 1.0f;
@Override
public void callActivityOnCreate(Activity activity, Bundle bundle) {
disableAll();
super.callActivityOnCreate(activity, bundle);
}
@Override
public void callActivityOnDestroy(Activity activity) {
super.callActivityOnDestroy(activity);
enableAll();
}
private void disableAll() {
setSystemAnimationsScale(DISABLED);
}
private void enableAll() {
setSystemAnimationsScale(DEFAULT);
}
private void setSystemAnimationsScale(float animationScale) {
int permStatus = getContext().checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
Method asInterface =
windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
Method setAnimationScales =
windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");
IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
for (int i = 0; i < currentScales.length; i++) {
currentScales[i] = animationScale;
}
setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
} catch (Exception e) {
Log.e(getClass().getSimpleName(),
"Could not change animation scale to " + animationScale + " :'(");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment