-
Notifications
You must be signed in to change notification settings - Fork 2
/
HandleAnimations.kt
87 lines (64 loc) · 2.75 KB
/
HandleAnimations.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.kazucocoa.droidtesthelperlib
import android.content.Intent
import android.os.IBinder
import android.util.Log
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.Arrays
class HandleAnimations {
private val DISABLE = 0.0f
private val ENABLE = 1.0f
private lateinit var setAnimationScalesMethod: Method
private lateinit var getAnimationScalesMethod: Method
private lateinit var windowManagerObject: Any
private fun animationHandler() {
try {
val asInterface = Class.forName("android.view.IWindowManager\$Stub").getDeclaredMethod("asInterface", IBinder::class.java)
val getService = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String::class.java)
val windowManagerClazz = Class.forName("android.view.IWindowManager")
setAnimationScalesMethod = Class.forName("android.view.IWindowManager").getDeclaredMethod("setAnimationScales", FloatArray::class.java)
getAnimationScalesMethod = windowManagerClazz.getDeclaredMethod("getAnimationScales")
windowManagerObject = asInterface.invoke(null, getService.invoke(null, "window") as IBinder)
} catch (e: Exception) {
throw RuntimeException("Failed to access animation methods", e)
}
}
private fun disableAnimations() {
try {
setAnimationScaleWith(DISABLE)
} catch (e: Exception) {
Log.e(TAG, "Failed to disable animations", e)
}
}
fun enableAnimations() {
try {
setAnimationScaleWith(ENABLE)
} catch (e: Exception) {
Log.e(TAG, "Failed to enable animations", e)
}
}
@Throws(InvocationTargetException::class, IllegalAccessException::class)
private fun setAnimationScaleWith(scaleFactor: Float) {
(getAnimationScalesMethod.invoke(windowManagerObject) as FloatArray).let {
Arrays.fill(it, scaleFactor)
setAnimationScalesMethod.invoke(windowManagerObject, it)
}
}
companion object {
private val TAG = HandleAnimations::class.java.simpleName
private const val animationExtra = "ANIMATION"
fun hasExtraRegardingAnimation(intent: Intent): Boolean {
return intent.hasExtra(animationExtra)
}
fun enableAnimationsWithIntent(intent: Intent) {
val enableAnimation = intent.getBooleanExtra(animationExtra, false)
val handleAnimations = HandleAnimations()
handleAnimations.animationHandler()
if (enableAnimation) {
handleAnimations.enableAnimations()
} else {
handleAnimations.disableAnimations()
}
}
}
}