Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
48e2d6a8
Commit
48e2d6a8
authored
Dec 14, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify several hacks
parent
b4120cdd
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
64 additions
and
88 deletions
+64
-88
App.kt
app/src/main/java/com/topjohnwu/magisk/core/App.kt
+19
-22
Hacks.kt
app/src/main/java/com/topjohnwu/magisk/core/Hacks.kt
+28
-52
BaseActivity.kt
.../main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt
+1
-1
MagiskInstaller.kt
...n/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt
+1
-1
Locales.kt
app/src/main/java/com/topjohnwu/magisk/core/utils/Locales.kt
+10
-7
XAndroid.kt
app/src/main/java/com/topjohnwu/magisk/ktx/XAndroid.kt
+2
-2
MarkwonImagePlugin.kt
...ain/java/com/topjohnwu/magisk/utils/MarkwonImagePlugin.kt
+3
-3
No files found.
app/src/main/java/com/topjohnwu/magisk/core/App.kt
View file @
48e2d6a8
...
...
@@ -7,12 +7,8 @@ import android.content.Context
import
android.content.res.Configuration
import
android.os.Bundle
import
com.topjohnwu.magisk.DynAPK
import
com.topjohnwu.magisk.core.utils.DispatcherExecutor
import
com.topjohnwu.magisk.core.utils.RootRegistry
import
com.topjohnwu.magisk.core.utils.ShellInit
import
com.topjohnwu.magisk.core.utils.updateConfig
import
com.topjohnwu.magisk.core.utils.*
import
com.topjohnwu.magisk.di.ServiceLocator
import
com.topjohnwu.magisk.ktx.unwrap
import
com.topjohnwu.superuser.Shell
import
com.topjohnwu.superuser.internal.UiThreadHandler
import
com.topjohnwu.superuser.ipc.RootService
...
...
@@ -40,28 +36,34 @@ open class App() : Application() {
}
}
override
fun
attachBaseContext
(
base
:
Context
)
{
override
fun
attachBaseContext
(
context
:
Context
)
{
Shell
.
setDefaultBuilder
(
Shell
.
Builder
.
create
()
.
setFlags
(
Shell
.
FLAG_MOUNT_MASTER
)
.
setInitializers
(
ShellInit
::
class
.
java
)
.
setTimeout
(
2
))
Shell
.
EXECUTOR
=
DispatcherExecutor
(
Dispatchers
.
IO
)
//
Some context magic
//
Get the actual ContextImpl
val
app
:
Application
val
impl
:
Context
if
(
base
is
Application
)
{
app
=
base
impl
=
base
.
baseContext
val
base
:
Context
if
(
context
is
Application
)
{
app
=
context
base
=
context
.
baseContext
}
else
{
app
=
this
impl
=
base
base
=
context
}
val
wrapped
=
impl
.
wrap
(
)
super
.
attachBaseContext
(
wrapped
)
super
.
attachBaseContext
(
base
)
ServiceLocator
.
context
=
base
ServiceLocator
.
context
=
wrapped
AssetHack
.
init
(
impl
)
refreshLocale
()
AppApkPath
=
if
(
isRunningAsStub
)
{
DynAPK
.
current
(
base
).
path
}
else
{
base
.
packageResourcePath
}
base
.
resources
.
patch
()
app
.
registerActivityLifecycleCallbacks
(
ForegroundTracker
)
}
...
...
@@ -74,14 +76,9 @@ open class App() : Application() {
)
}
// This is required as some platforms expect ContextImpl
override
fun
getBaseContext
():
Context
{
return
super
.
getBaseContext
().
unwrap
()
}
override
fun
onConfigurationChanged
(
newConfig
:
Configuration
)
{
if
(
resources
.
configuration
.
diff
(
newConfig
)
!=
0
)
{
resources
.
update
Config
(
newConfig
)
resources
.
set
Config
(
newConfig
)
}
if
(!
isRunningAsStub
)
super
.
onConfigurationChanged
(
newConfig
)
...
...
app/src/main/java/com/topjohnwu/magisk/core/Hacks.kt
View file @
48e2d6a8
...
...
@@ -13,70 +13,46 @@ import android.content.res.Resources
import
android.util.DisplayMetrics
import
com.topjohnwu.magisk.DynAPK
import
com.topjohnwu.magisk.R
import
com.topjohnwu.magisk.core.utils.
refresh
Locale
import
com.topjohnwu.magisk.
core.utils.updateConfig
import
com.topjohnwu.magisk.core.utils.
sync
Locale
import
com.topjohnwu.magisk.
di.AppContext
fun
AssetManager
.
addAssetPath
(
path
:
String
)
{
DynAPK
.
addAssetPath
(
this
,
path
)
}
lateinit
var
AppApkPath
:
String
fun
Context
.
wrap
(
inject
:
Boolean
=
false
):
Context
=
if
(
inject
)
ReInjectedContext
(
this
)
else
InjectedContext
(
this
)
fun
AssetManager
.
addAssetPath
(
path
:
String
)
=
DynAPK
.
addAssetPath
(
this
,
path
)
fun
Class
<*>.
cmp
(
pkg
:
String
)
=
ComponentName
(
pkg
,
Info
.
stub
?.
classToComponent
?.
get
(
name
)
?:
name
)
inline
fun
<
reified
T
>
Activity
.
redirect
()
=
Intent
(
intent
)
.
setComponent
(
T
::
class
.
java
.
cmp
(
packageName
))
.
setFlags
(
0
)
inline
fun
<
reified
T
>
Context
.
intent
()
=
Intent
().
setComponent
(
T
::
class
.
java
.
cmp
(
packageName
))
fun
Context
.
wrap
():
Context
=
if
(
this
is
PatchedContext
)
this
else
PatchedContext
(
this
)
private
open
class
InjectedContext
(
base
:
Context
)
:
ContextWrapper
(
base
)
{
open
val
res
:
Resources
get
()
=
AssetHack
.
resource
override
fun
getAssets
():
AssetManager
=
res
.
assets
override
fun
getResources
()
=
res
private
class
PatchedContext
(
base
:
Context
)
:
ContextWrapper
(
base
)
{
init
{
base
.
resources
.
patch
()
}
override
fun
getClassLoader
()
=
javaClass
.
classLoader
!!
override
fun
createConfigurationContext
(
config
:
Configuration
):
Context
{
return
super
.
createConfigurationContext
(
config
).
wrap
(
true
)
}
override
fun
createConfigurationContext
(
config
:
Configuration
)
=
super
.
createConfigurationContext
(
config
).
wrap
()
}
private
class
ReInjectedContext
(
base
:
Context
)
:
InjectedContext
(
base
)
{
override
val
res
by
lazy
{
base
.
resources
.
patch
()
}
private
fun
Resources
.
patch
():
Resources
{
updateConfig
()
if
(
isRunningAsStub
)
assets
.
addAssetPath
(
AssetHack
.
apk
)
return
this
}
fun
Resources
.
patch
():
Resources
{
syncLocale
()
if
(
isRunningAsStub
)
assets
.
addAssetPath
(
AppApkPath
)
return
this
}
object
AssetHack
{
fun
createNewResources
():
Resources
{
val
asset
=
AssetManager
::
class
.
java
.
newInstance
()
asset
.
addAssetPath
(
AppApkPath
)
val
config
=
Configuration
(
AppContext
.
resources
.
configuration
)
val
metrics
=
DisplayMetrics
()
metrics
.
setTo
(
AppContext
.
resources
.
displayMetrics
)
return
Resources
(
asset
,
metrics
,
config
)
}
lateinit
var
resource
:
Resources
lateinit
var
apk
:
String
fun
Class
<*>.
cmp
(
pkg
:
String
)
=
ComponentName
(
pkg
,
Info
.
stub
?.
classToComponent
?.
get
(
name
)
?:
name
)
fun
init
(
context
:
Context
)
{
resource
=
context
.
resources
refreshLocale
()
if
(
isRunningAsStub
)
{
apk
=
DynAPK
.
current
(
context
).
path
resource
.
assets
.
addAssetPath
(
apk
)
}
else
{
apk
=
context
.
packageResourcePath
}
}
inline
fun
<
reified
T
>
Activity
.
redirect
()
=
Intent
(
intent
)
.
setComponent
(
T
::
class
.
java
.
cmp
(
packageName
))
.
setFlags
(
0
)
fun
newResource
():
Resources
{
val
asset
=
AssetManager
::
class
.
java
.
newInstance
()
asset
.
addAssetPath
(
apk
)
val
config
=
Configuration
(
resource
.
configuration
)
val
metrics
=
DisplayMetrics
()
metrics
.
setTo
(
resource
.
displayMetrics
)
return
Resources
(
asset
,
metrics
,
config
)
}
}
inline
fun
<
reified
T
>
Context
.
intent
()
=
Intent
().
setComponent
(
T
::
class
.
java
.
cmp
(
packageName
))
// Keep a reference to these resources to prevent it from
// being removed when running "remove unused resources"
...
...
app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt
View file @
48e2d6a8
...
...
@@ -36,7 +36,7 @@ abstract class BaseActivity : AppCompatActivity() {
}
override
fun
attachBaseContext
(
base
:
Context
)
{
super
.
attachBaseContext
(
base
.
wrap
(
true
))
super
.
attachBaseContext
(
base
.
wrap
())
}
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
...
...
app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt
View file @
48e2d6a8
...
...
@@ -422,7 +422,7 @@ abstract class MagiskInstallImpl protected constructor(
protected
fun
fixEnv
()
=
extractFiles
()
&&
"fix_env $installDir"
.
sh
().
isSuccess
protected
fun
uninstall
()
=
"run_uninstaller $
{AssetHack.apk}
"
.
sh
().
isSuccess
protected
fun
uninstall
()
=
"run_uninstaller $
AppApkPath
"
.
sh
().
isSuccess
@WorkerThread
protected
abstract
suspend
fun
operations
():
Boolean
...
...
app/src/main/java/com/topjohnwu/magisk/core/utils/Locales.kt
View file @
48e2d6a8
...
...
@@ -6,8 +6,9 @@ import android.annotation.SuppressLint
import
android.content.res.Configuration
import
android.content.res.Resources
import
com.topjohnwu.magisk.R
import
com.topjohnwu.magisk.core.AssetHack
import
com.topjohnwu.magisk.core.Config
import
com.topjohnwu.magisk.core.createNewResources
import
com.topjohnwu.magisk.di.AppContext
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.withContext
import
java.util.*
...
...
@@ -25,7 +26,7 @@ withContext(Dispatchers.Default) {
val
compareId
=
R
.
string
.
app_changelog
// Create a completely new resource to prevent cross talk over active configs
val
res
=
AssetHack
.
newResource
()
val
res
=
createNewResources
()
val
locales
=
ArrayList
<
String
>().
apply
{
// Add default locale
...
...
@@ -40,13 +41,13 @@ withContext(Dispatchers.Default) {
}.
map
{
Locale
.
forLanguageTag
(
it
)
}.
distinctBy
{
res
.
update
Locale
(
it
)
res
.
set
Locale
(
it
)
res
.
getString
(
compareId
)
}.
sortedWith
{
a
,
b
->
a
.
getDisplayName
(
a
).
compareTo
(
b
.
getDisplayName
(
b
),
true
)
}
res
.
update
Locale
(
defaultLocale
)
res
.
set
Locale
(
defaultLocale
)
val
defName
=
res
.
getString
(
R
.
string
.
system_default
)
val
names
=
ArrayList
<
String
>(
locales
.
size
+
1
)
...
...
@@ -63,12 +64,14 @@ withContext(Dispatchers.Default) {
(
names
.
toTypedArray
()
to
values
.
toTypedArray
()).
also
{
cachedLocales
=
it
}
}
fun
Resources
.
updateConfig
(
config
:
Configuration
=
c
onfiguration
)
{
fun
Resources
.
setConfig
(
config
:
C
onfiguration
)
{
config
.
setLocale
(
currentLocale
)
updateConfiguration
(
config
,
displayMetrics
)
}
fun
Resources
.
updateLocale
(
locale
:
Locale
)
{
fun
Resources
.
syncLocale
()
=
setConfig
(
configuration
)
fun
Resources
.
setLocale
(
locale
:
Locale
)
{
configuration
.
setLocale
(
locale
)
updateConfiguration
(
configuration
,
displayMetrics
)
}
...
...
@@ -80,5 +83,5 @@ fun refreshLocale() {
else
->
Locale
.
forLanguageTag
(
localeConfig
)
}
Locale
.
setDefault
(
currentLocale
)
A
ssetHack
.
resource
.
updateConfig
()
A
ppContext
.
resources
.
syncLocale
()
}
app/src/main/java/com/topjohnwu/magisk/ktx/XAndroid.kt
View file @
48e2d6a8
...
...
@@ -41,10 +41,10 @@ import androidx.lifecycle.lifecycleScope
import
androidx.transition.AutoTransition
import
androidx.transition.TransitionManager
import
com.topjohnwu.magisk.R
import
com.topjohnwu.magisk.core.AssetHack
import
com.topjohnwu.magisk.core.Const
import
com.topjohnwu.magisk.core.base.BaseActivity
import
com.topjohnwu.magisk.core.utils.currentLocale
import
com.topjohnwu.magisk.di.AppContext
import
com.topjohnwu.magisk.utils.DynamicClassLoader
import
com.topjohnwu.magisk.utils.Utils
import
com.topjohnwu.superuser.Shell
...
...
@@ -342,7 +342,7 @@ var TextView.precomputedText: CharSequence
}
fun
Int
.
dpInPx
():
Int
{
val
scale
=
A
ssetHack
.
resource
.
displayMetrics
.
density
val
scale
=
A
ppContext
.
resources
.
displayMetrics
.
density
return
(
this
*
scale
+
0.5
).
toInt
()
}
...
...
app/src/main/java/com/topjohnwu/magisk/utils/MarkwonImagePlugin.kt
View file @
48e2d6a8
...
...
@@ -15,7 +15,7 @@ import android.widget.TextView
import
androidx.annotation.WorkerThread
import
com.caverock.androidsvg.SVG
import
com.caverock.androidsvg.SVGParseException
import
com.topjohnwu.magisk.
core.AssetHack
import
com.topjohnwu.magisk.
di.AppContext
import
com.topjohnwu.superuser.internal.WaitRunnable
import
io.noties.markwon.AbstractMarkwonPlugin
import
io.noties.markwon.MarkwonSpansFactory
...
...
@@ -216,7 +216,7 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
return
PictureDrawable
(
picture
)
}
val
density
:
Float
=
A
ssetHack
.
resource
.
displayMetrics
.
density
val
density
:
Float
=
A
ppContext
.
resources
.
displayMetrics
.
density
val
width
=
(
w
*
density
+
.
5f
).
toInt
()
val
height
=
(
h
*
density
+
.
5f
).
toInt
()
...
...
@@ -226,7 +226,7 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
canvas
.
scale
(
density
,
density
)
svg
.
renderToCanvas
(
canvas
)
return
BitmapDrawable
(
A
ssetHack
.
resource
,
bitmap
)
return
BitmapDrawable
(
A
ppContext
.
resources
,
bitmap
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment