forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.kt
More file actions
102 lines (94 loc) · 3.74 KB
/
MainActivity.kt
File metadata and controls
102 lines (94 loc) · 3.74 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.lichess.mobileV2
import android.app.ActivityManager
import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.provider.Settings
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val GESTURES_CHANNEL = "mobile.lichess.org/gestures_exclusion"
private val SYSTEM_CHANNEL = "mobile.lichess.org/system"
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, GESTURES_CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "setSystemGestureExclusionRects") {
val arguments = call.arguments as List<Map<String, Int>>
val decodedRects = decodeExclusionRects(arguments)
ViewCompat.setSystemGestureExclusionRects(activity.window.decorView, decodedRects)
result.success(null)
} else {
result.notImplemented()
}
}
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SYSTEM_CHANNEL).setMethodCallHandler {
call, result ->
when (call.method) {
"clearApplicationUserData" -> {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
result.success(activityManager.clearApplicationUserData())
}
"getTotalRam" -> {
val memoryInfo = getAvailableMemory()
val totalMemInMb = memoryInfo.totalMem / 1048576L
result.success(totalMemInMb.toInt())
}
"areAnimationsEnabled" -> {
val animationsEnabled = areAnimationsEnabled()
result.success(animationsEnabled)
}
else -> {
result.notImplemented()
}
}
}
}
private fun decodeExclusionRects(inputRects: List<Map<String, Int>>): List<Rect> =
inputRects.mapIndexed { index, item ->
Rect(
item["left"] ?: error("rect at index $index doesn't contain 'left' property"),
item["top"] ?: error("rect at index $index doesn't contain 'top' property"),
item["right"] ?: error("rect at index $index doesn't contain 'right' property"),
item["bottom"] ?: error("rect at index $index doesn't contain 'bottom' property")
)
}
private fun getAvailableMemory(): ActivityManager.MemoryInfo {
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
return ActivityManager.MemoryInfo().also { memoryInfo ->
activityManager.getMemoryInfo(memoryInfo)
}
}
private fun areAnimationsEnabled(): Boolean {
return try {
// Check both animator duration scale and transition animation scale
// TRANSITION_ANIMATION_SCALE controls "remove animations" setting
val animatorScale = Settings.Global.getFloat(
contentResolver,
Settings.Global.ANIMATOR_DURATION_SCALE,
1.0f
)
val transitionScale = Settings.Global.getFloat(
contentResolver,
Settings.Global.TRANSITION_ANIMATION_SCALE,
1.0f
)
val windowScale = Settings.Global.getFloat(
contentResolver,
Settings.Global.WINDOW_ANIMATION_SCALE,
1.0f
)
// If any of these are 0, animations are effectively disabled
animatorScale > 0.0f && transitionScale > 0.0f && windowScale > 0.0f
} catch (e: Settings.SettingNotFoundException) {
true
}
}
}