Skip to content

Commit 2f44e3e

Browse files
implement rewriting orientations in RotationControl
1 parent c2464da commit 2f44e3e

File tree

6 files changed

+94
-39
lines changed

6 files changed

+94
-39
lines changed

RotationControl/src/main/java/com/programminghoch10/RotationControl/ROTATION_MODE.kt

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,120 @@ package com.programminghoch10.RotationControl
33
import android.app.Application
44
import android.content.pm.ActivityInfo
55

6-
val context = Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null) as Application
6+
val context = run {
7+
val applicationContext = Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null) as Application?
8+
if (applicationContext?.packageName == BuildConfig.APPLICATION_ID) return@run applicationContext.resources
9+
return@run null
10+
}
11+
12+
private fun getString(id: Int): String {
13+
if (context == null) return ""
14+
return context.getString(id)
15+
}
716

817
enum class ROTATION_MODE(val key: String, val value: Int, val title: String, val summary: String) {
918
// yoinked from https://developer.android.com/reference/android/R.attr.html#screenOrientation
1019

1120
SCREEN_ORIENTATION_UNSET(
1221
"UNSET",
1322
-2,
14-
context.getString(R.string.screen_orientation_unset_title),
15-
context.getString(R.string.screen_orientation_unset_summary),
23+
getString(R.string.screen_orientation_unset_title),
24+
getString(R.string.screen_orientation_unset_summary),
1625
),
1726
SCREEN_ORIENTATION_UNSPECIFIED(
1827
"UNSPECIFIED",
1928
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,
20-
context.getString(R.string.screen_orientation_unspecified_title),
21-
context.getString(R.string.screen_orientation_unspecified_summary),
29+
getString(R.string.screen_orientation_unspecified_title),
30+
getString(R.string.screen_orientation_unspecified_summary),
2231
),
2332
SCREEN_ORIENTATION_LANDSCAPE(
2433
"LANDSCAPE",
2534
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
26-
context.getString(R.string.screen_orientation_landscape_title),
27-
context.getString(R.string.screen_orientation_landscape_summary),
35+
getString(R.string.screen_orientation_landscape_title),
36+
getString(R.string.screen_orientation_landscape_summary),
2837
),
2938
SCREEN_ORIENTATION_PORTRAIT(
3039
"PORTRAIT",
3140
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
32-
context.getString(R.string.screen_orientation_portrait_title),
33-
context.getString(R.string.screen_orientation_portrait_summary),
41+
getString(R.string.screen_orientation_portrait_title),
42+
getString(R.string.screen_orientation_portrait_summary),
3443
),
3544
SCREEN_ORIENTATION_USER(
3645
"USER",
3746
ActivityInfo.SCREEN_ORIENTATION_USER,
38-
context.getString(R.string.screen_orientation_user_title),
39-
context.getString(R.string.screen_orientation_user_summary),
47+
getString(R.string.screen_orientation_user_title),
48+
getString(R.string.screen_orientation_user_summary),
4049
),
4150
SCREEN_ORIENTATION_BEHIND(
4251
"BEHIND",
4352
ActivityInfo.SCREEN_ORIENTATION_BEHIND,
44-
context.getString(R.string.screen_orientation_behind_title),
45-
context.getString(R.string.screen_orientation_behind_summary),
53+
getString(R.string.screen_orientation_behind_title),
54+
getString(R.string.screen_orientation_behind_summary),
4655
),
4756
SCREEN_ORIENTATION_SENSOR(
4857
"SENSOR",
4958
ActivityInfo.SCREEN_ORIENTATION_SENSOR,
50-
context.getString(R.string.screen_orientation_sensor_title),
51-
context.getString(R.string.screen_orientation_sensor_summary),
59+
getString(R.string.screen_orientation_sensor_title),
60+
getString(R.string.screen_orientation_sensor_summary),
5261
),
5362
SCREEN_ORIENTATION_NOSENSOR(
5463
"NOSENSOR",
5564
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,
56-
context.getString(R.string.screen_orientation_nosensor_title),
57-
context.getString(R.string.screen_orientation_nosensor_summary),
65+
getString(R.string.screen_orientation_nosensor_title),
66+
getString(R.string.screen_orientation_nosensor_summary),
5867
),
5968
SCREEN_ORIENTATION_SENSOR_LANDSCAPE(
6069
"SENSOR_LANDSCAPE",
6170
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
62-
context.getString(R.string.screen_orientation_sensor_landscape_title),
63-
context.getString(R.string.screen_orientation_sensor_landscape_summary),
71+
getString(R.string.screen_orientation_sensor_landscape_title),
72+
getString(R.string.screen_orientation_sensor_landscape_summary),
6473
),
6574
SCREEN_ORIENTATION_SENSOR_PORTRAIT(
6675
"SENSOR_PORTRAIT",
6776
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
68-
context.getString(R.string.screen_orientation_sensor_portrait_title),
69-
context.getString(R.string.screen_orientation_sensor_portrait_summary),
77+
getString(R.string.screen_orientation_sensor_portrait_title),
78+
getString(R.string.screen_orientation_sensor_portrait_summary),
7079
),
7180
SCREEN_ORIENTATION_REVERSE_LANDSCAPE(
7281
"REVERSE_LANDSCAPE",
7382
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
74-
context.getString(R.string.screen_orientation_reverse_landscape_title),
75-
context.getString(R.string.screen_orientation_reverse_landscape_summary),
83+
getString(R.string.screen_orientation_reverse_landscape_title),
84+
getString(R.string.screen_orientation_reverse_landscape_summary),
7685
),
7786
SCREEN_ORIENTATION_REVERSE_PORTRAIT(
7887
"REVERSE_PORTRAIT",
7988
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
80-
context.getString(R.string.screen_orientation_reverse_portrait_title),
81-
context.getString(R.string.screen_orientation_reverse_portrait_summary),
89+
getString(R.string.screen_orientation_reverse_portrait_title),
90+
getString(R.string.screen_orientation_reverse_portrait_summary),
8291
),
8392
SCREEN_ORIENTATION_FULL_SENSOR(
8493
"FULL_SENSOR",
8594
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR,
86-
context.getString(R.string.screen_orientation_full_sensor_title),
87-
context.getString(R.string.screen_orientation_full_sensor_summary),
95+
getString(R.string.screen_orientation_full_sensor_title),
96+
getString(R.string.screen_orientation_full_sensor_summary),
8897
),
8998
SCREEN_ORIENTATION_USER_LANDSCAPE(
9099
"USER_LANDSCAPE",
91100
ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE,
92-
context.getString(R.string.screen_orientation_user_landscape),
93-
context.getString(R.string.screen_orientation_user_landscape_summary),
101+
getString(R.string.screen_orientation_user_landscape),
102+
getString(R.string.screen_orientation_user_landscape_summary),
94103
),
95104
SCREEN_ORIENTATION_USER_PORTRAIT(
96105
"USER_PORTRAIT",
97106
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT,
98-
context.getString(R.string.screen_orientation_user_portrait_title),
99-
context.getString(R.string.screen_orientation_user_portrait_summary),
107+
getString(R.string.screen_orientation_user_portrait_title),
108+
getString(R.string.screen_orientation_user_portrait_summary),
100109
),
101110
SCREEN_ORIENTATION_FULL_USER(
102111
"FULL_USER",
103112
ActivityInfo.SCREEN_ORIENTATION_FULL_USER,
104-
context.getString(R.string.screen_orientation_full_user_title),
105-
context.getString(R.string.screen_orientation_full_user_summary),
113+
getString(R.string.screen_orientation_full_user_title),
114+
getString(R.string.screen_orientation_full_user_summary),
106115
),
107116
SCREEN_ORIENTATION_LOCKED(
108117
"LOCKED",
109118
ActivityInfo.SCREEN_ORIENTATION_LOCKED,
110-
context.getString(R.string.screen_orientation_locked_title),
111-
context.getString(R.string.screen_orientation_locked_summary),
119+
getString(R.string.screen_orientation_locked_title),
120+
getString(R.string.screen_orientation_locked_summary),
112121
),
113122
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.programminghoch10.RotationControl
2+
3+
val rewriteLockedOrientation = mapOf(
4+
ROTATION_MODE.SCREEN_ORIENTATION_LANDSCAPE to ROTATION_MODE.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
5+
ROTATION_MODE.SCREEN_ORIENTATION_PORTRAIT to ROTATION_MODE.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
6+
ROTATION_MODE.SCREEN_ORIENTATION_REVERSE_LANDSCAPE to ROTATION_MODE.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
7+
ROTATION_MODE.SCREEN_ORIENTATION_REVERSE_PORTRAIT to ROTATION_MODE.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
8+
)
9+
10+
val rewriteSensorOrientation = mapOf(
11+
ROTATION_MODE.SCREEN_ORIENTATION_SENSOR to ROTATION_MODE.SCREEN_ORIENTATION_FULL_SENSOR,
12+
ROTATION_MODE.SCREEN_ORIENTATION_USER to ROTATION_MODE.SCREEN_ORIENTATION_FULL_USER,
13+
)
14+

RotationControl/src/main/java/com/programminghoch10/RotationControl/SettingsActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class SettingsActivity : FragmentActivity() {
3131

3232
@SuppressLint("WorldReadableFiles")
3333
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
34-
setPreferencesFromResource(R.xml.root_preferences, rootKey)
3534
preferenceManager.sharedPreferencesName = SHARED_PREFERENCES_NAME
3635
preferenceManager.sharedPreferencesMode = MODE_WORLD_READABLE
36+
setPreferencesFromResource(R.xml.root_preferences, rootKey)
3737
val preferenceCategory = findPreference<PreferenceCategory>("category_rotation_mode")!!
3838
val context = requireContext()
3939

@@ -49,6 +49,8 @@ class SettingsActivity : FragmentActivity() {
4949
true
5050
}
5151
preferenceCategory.addPreference(preference)
52+
if (rotationMode in rewriteLockedOrientation.keys) preference.dependency = "rewrite_locked_orientations"
53+
if (rotationMode in rewriteSensorOrientation.keys) preference.dependency = "rewrite_sensor_orientations"
5254
}
5355
}
5456
}

RotationControl/src/main/java/com/programminghoch10/RotationControl/XposedHook.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ class XposedHook : IXposedHookLoadPackage {
2121
Int::class.java,
2222
object : XC_MethodHook() {
2323
override fun beforeHookedMethod(param: MethodHookParam) {
24+
val originalRotationMode = ROTATION_MODE.entries.find { it.value == param.args[0] } ?: ROTATION_MODE.SCREEN_ORIENTATION_UNSET
2425
val sharedPreferences: SharedPreferences = XSharedPreferences(BuildConfig.APPLICATION_ID, SHARED_PREFERENCES_NAME)
25-
val selectedRotationMode = ROTATION_MODE.entries.find { sharedPreferences.getBoolean(it.key, false) } ?: ROTATION_MODE_DEFAULT
26+
var selectedRotationMode = ROTATION_MODE.entries.find { sharedPreferences.getBoolean(it.key, false) } ?: ROTATION_MODE_DEFAULT
27+
if (selectedRotationMode == ROTATION_MODE.SCREEN_ORIENTATION_UNSET) selectedRotationMode = originalRotationMode
28+
if (sharedPreferences.getBoolean("rewrite_locked_orientations", false)) {
29+
selectedRotationMode = rewriteLockedOrientation.get(selectedRotationMode) ?: selectedRotationMode
30+
}
31+
if (sharedPreferences.getBoolean("rewrite_sensor_orientations", false)) {
32+
selectedRotationMode = rewriteSensorOrientation.get(selectedRotationMode) ?: selectedRotationMode
33+
}
2634
if (selectedRotationMode == ROTATION_MODE.SCREEN_ORIENTATION_UNSET) return
2735
param.args[0] = selectedRotationMode.value
2836
}

RotationControl/src/main/res/values/strings.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<string name="app_name">RotationControl</string>
33
<string name="description">Force rotation for selected packages</string>
44
<string name="title_activity_settings">RotationControl Configuration</string>
5-
<string name="category_rotation_mode">Rotation Mode</string>
5+
<string name="category_rotation_mode">Forced Rotation Mode</string>
66
<string name="screen_orientation_unset_title">UNSET</string>
7-
<string name="screen_orientation_unset_summary">Do not override screen orientation. You might as well disable the module instead of using this option.</string>
7+
<string name="screen_orientation_unset_summary">Do not override screen orientation. Use this option if you only want to use the rewrite functionality above.</string>
88
<string name="screen_orientation_unspecified_title">UNSPECIFIED</string>
99
<string name="screen_orientation_unspecified_summary">No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user\'s preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device.</string>
1010
<string name="screen_orientation_landscape_title">LANDSCAPE</string>
@@ -37,4 +37,9 @@
3737
<string name="screen_orientation_full_user_summary">Respect the user\'s sensor-based rotation preference, but if sensor-based rotation is enabled then allow the screen to rotate in all 4 possible directions regardless of what the device will normally do (for example some devices won\'t normally use 180 degree rotation).</string>
3838
<string name="screen_orientation_locked_title">LOCKED</string>
3939
<string name="screen_orientation_locked_summary">Screen is locked to its current rotation, whatever that is.</string>
40+
<string name="rewrite_locked_orientations_title">Rewrite to sensor orientation</string>
41+
<string name="rewrite_locked_orientations_summary">Rewrite locked orientations to sensor orientations.</string>
42+
<string name="rewrite_sensor_orientation_title">Force full sensor rotation</string>
43+
<string name="rewrite_sensor_orientations_summary">Rewrite sensor orientations to full sensor orientations.</string>
44+
<string name="category_rewrite">Rewrite Options</string>
4045
</resources>

RotationControl/src/main/res/xml/root_preferences.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
22

3+
<PreferenceCategory
4+
app:title="@string/category_rewrite">
5+
<SwitchPreference
6+
app:key="rewrite_locked_orientations"
7+
app:title="@string/rewrite_locked_orientations_title"
8+
app:summary="@string/rewrite_locked_orientations_summary"
9+
app:defaultValue="false"
10+
app:disableDependentsState="true" />
11+
12+
<SwitchPreference
13+
app:key="rewrite_sensor_orientations"
14+
app:title="@string/rewrite_sensor_orientation_title"
15+
app:summary="@string/rewrite_sensor_orientations_summary"
16+
app:defaultValue="false"
17+
app:disableDependentsState="true" />
18+
</PreferenceCategory>
19+
320
<PreferenceCategory
421
app:title="@string/category_rotation_mode"
522
app:key="category_rotation_mode" />

0 commit comments

Comments
 (0)