-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some refactors and refine operation records
- Loading branch information
Showing
22 changed files
with
406 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/cn/tinyhai/ban_uninstall/receiver/PackageChangeReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cn.tinyhai.ban_uninstall.receiver | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Handler | ||
import androidx.core.content.ContextCompat | ||
import cn.tinyhai.ban_uninstall.BuildConfig | ||
import cn.tinyhai.ban_uninstall.auth.server.AuthService | ||
import cn.tinyhai.ban_uninstall.configs.Configs | ||
import cn.tinyhai.ban_uninstall.transact.server.TransactService | ||
import cn.tinyhai.ban_uninstall.utils.XPLogUtils | ||
|
||
@SuppressLint("PrivateApi") | ||
class PackageChangeReceiver : BroadcastReceiver() { | ||
|
||
private val intentFilter = IntentFilter().apply { | ||
addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED) | ||
addAction(Intent.ACTION_PACKAGE_REPLACED) | ||
addDataScheme("package") | ||
} | ||
|
||
private val registerReceiverForAllUsers by lazy { | ||
try { | ||
Context::class.java.getDeclaredMethod( | ||
"registerReceiverForAllUsers", | ||
BroadcastReceiver::class.java, | ||
IntentFilter::class.java, | ||
String::class.java, | ||
Handler::class.java | ||
) | ||
} catch (e: NoSuchMethodException) { | ||
null | ||
} | ||
} | ||
|
||
private val getSendingUserId by lazy { | ||
BroadcastReceiver::class.java.getDeclaredMethod("getSendingUserId") | ||
.also { it.isAccessible = true } | ||
} | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
if (!intentFilter.hasAction(intent.action)) { | ||
return | ||
} | ||
|
||
val sendingUserId = getSendingUserId.invoke(this) as Int | ||
val uri = intent.data | ||
val packageName = uri?.encodedSchemeSpecificPart | ||
when (intent.action) { | ||
Intent.ACTION_PACKAGE_REPLACED -> { | ||
XPLogUtils.log("pkg replace uri = $uri, userId = $sendingUserId") | ||
if (packageName == BuildConfig.APPLICATION_ID && sendingUserId == 0) { | ||
AuthService.preventAll() | ||
} | ||
} | ||
|
||
Intent.ACTION_PACKAGE_FULLY_REMOVED -> { | ||
XPLogUtils.log("pkg uninstall uri = $uri, userId = $sendingUserId") | ||
packageName?.let { | ||
TransactService.onPkgUninstall(packageName, sendingUserId) | ||
} | ||
if (packageName == BuildConfig.APPLICATION_ID && sendingUserId == 0) { | ||
XPLogUtils.log("self package removed") | ||
Configs.onSelfRemoved() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun register(context: Context) { | ||
XPLogUtils.log("register PackageChangeReceiver") | ||
registerReceiverForAllUsers?.let { | ||
it.invoke( | ||
context, | ||
this, | ||
intentFilter, | ||
null, | ||
null | ||
) | ||
Unit | ||
} ?: ContextCompat.registerReceiver( | ||
context, this, intentFilter, | ||
ContextCompat.RECEIVER_NOT_EXPORTED | ||
) | ||
} | ||
} |
Oops, something went wrong.