Skip to content

Commit

Permalink
fix data backup
Browse files Browse the repository at this point in the history
  • Loading branch information
WrBug committed Sep 12, 2024
1 parent 350a42f commit 0ee2026
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 85 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.yhaolpz:FloatWindow:1.0.9'
implementation project(':basecommon')
api project(':basecommon')
kapt "com.android.databinding:compiler:3.1.4"
implementation 'com.elvishew:xlog:1.6.1'
implementation 'org.jetbrains.anko:anko-commons:0.10.8'
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@ package com.wrbug.developerhelper.ui.widget.appsettingview

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.net.Uri
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ScrollView
import androidx.appcompat.app.AlertDialog
import com.wrbug.developerhelper.BuildConfig
import com.wrbug.developerhelper.R
import com.wrbug.developerhelper.basecommon.BaseActivity
import com.wrbug.developerhelper.basecommon.requestStoragePermission
import com.wrbug.developerhelper.basecommon.showToast
import com.wrbug.developerhelper.commonutil.AppManagerUtils
import com.wrbug.developerhelper.commonutil.entity.ApkInfo
import com.wrbug.developerhelper.mmkv.ConfigKv
import com.wrbug.developerhelper.mmkv.manager.MMKVManager
import android.content.Intent
import android.net.Uri
import androidx.appcompat.widget.AppCompatButton
import com.wrbug.developerhelper.BuildConfig
import com.wrbug.developerhelper.basecommon.BaseActivity
import com.wrbug.developerhelper.commonutil.shell.ShellManager
import com.wrbug.developerhelper.commonutil.zip
import com.wrbug.developerhelper.util.BackupUtils
import com.wrbug.developerhelper.commonutil.toUri
import com.wrbug.developerhelper.commonwidget.util.setOnDoubleCheckClickListener
import com.wrbug.developerhelper.commonutil.zip
import com.wrbug.developerhelper.commonwidget.util.setOnRootCheckClickListener
import com.wrbug.developerhelper.commonwidget.util.visible
import com.wrbug.developerhelper.databinding.ViewAppSettingBinding
import com.wrbug.developerhelper.mmkv.ConfigKv
import com.wrbug.developerhelper.mmkv.manager.MMKVManager
import com.wrbug.developerhelper.util.BackupUtils
import gdut.bsx.share2.Share2
import gdut.bsx.share2.ShareContentType
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.io.File

class AppSettingView : ScrollView {
Expand Down Expand Up @@ -93,7 +87,7 @@ class AppSettingView : ScrollView {
apkInfo?.apply {
showNotice(
context.getString(R.string.confirm_delete_app_data)
) { _, _ ->
) {
if (AppManagerUtils.clearAppData(applicationInfo.packageName)) {
activityFinish()
showToast(context.getString(R.string.clear_complete))
Expand All @@ -108,7 +102,7 @@ class AppSettingView : ScrollView {
return
}
apkInfo?.apply {
showNotice(context.getString(R.string.confirm_stop_app)) { _, _ ->
showNotice(context.getString(R.string.confirm_stop_app)) {
if (AppManagerUtils.forceStopApp(applicationInfo.packageName)) {
activityFinish()
}
Expand All @@ -121,11 +115,12 @@ class AppSettingView : ScrollView {
return
}
apkInfo?.apply {
showNotice(context.getString(R.string.confirm_restart_app),
DialogInterface.OnClickListener { _, _ ->
AppManagerUtils.restartApp(context, applicationInfo.packageName)
activityFinish()
})
showNotice(
context.getString(R.string.confirm_restart_app)
) {
AppManagerUtils.restartApp(context, applicationInfo.packageName)
activityFinish()
}
}
}

Expand All @@ -134,8 +129,7 @@ class AppSettingView : ScrollView {
return
}
apkInfo?.apply {
val backupAppData =
BackupUtils.backupAppData(applicationInfo.packageName, applicationInfo.dataDir)
val backupAppData = BackupUtils.backupAppData(applicationInfo.packageName)
if (backupAppData == null) {
showToast(context.getString(R.string.backup_failed))
return
Expand All @@ -149,68 +143,58 @@ class AppSettingView : ScrollView {
}

private fun showShareDataNotice(backupAppData: File) {
showNotice(context.getString(R.string.backup_success_and_share_msg),
DialogInterface.OnClickListener { _, _ ->
(context as BaseActivity).requestPermission(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
object : BaseActivity.PermissionCallback() {
override fun granted() {
val zipFile = File(
context.externalCacheDir, "${apkInfo?.getAppName() ?: ""}-data.zip"
)
backupAppData.zip(zipFile)
val uri = zipFile.toUri(context)
if (uri == null) {
showToast(context.getString(R.string.share_failed))
return
}
activityFinish()
Share2.Builder(context as Activity)
.setContentType(ShareContentType.FILE).setShareFileUri(uri)
.setOnActivityResult(10).build().shareBySystem()
showNotice(context.getString(R.string.backup_success_and_share_msg)) {
(context as BaseActivity).requestPermission(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
object : BaseActivity.PermissionCallback() {
override fun granted() {
val zipFile = File(
context.externalCacheDir, "${apkInfo?.getAppName() ?: ""}-data.zip"
)
backupAppData.zip(zipFile)
val uri = zipFile.toUri(context)
if (uri == null) {
showToast(context.getString(R.string.share_failed))
return
}
activityFinish()
Share2.Builder(context as Activity).setContentType(ShareContentType.FILE)
.setShareFileUri(uri).setOnActivityResult(10).build().shareBySystem()
}

})
})

})
}
}

private fun doBackupApk() {
if (checkRoot().not()) {
return
}
apkInfo?.apply {
val uri = BackupUtils.backupApk(
applicationInfo.packageName,
applicationInfo.publicSourceDir,
"${getAppName()}_${packageInfo.versionName}.apk"
)
if (uri == null) {
showToast(context.getString(R.string.backup_failed))
return
}
if (context !is BaseActivity) {
showToast(context.getString(R.string.backup_success_msg))
return
context.requestStoragePermission {
apkInfo?.apply {
val uri = BackupUtils.backupApk(
applicationInfo.packageName,
applicationInfo.publicSourceDir,
"${getAppName()}_${packageInfo.versionName}.apk"
)
if (uri == null) {
showToast(context.getString(R.string.backup_failed))
return@apply
}
if (context !is BaseActivity) {
showToast(context.getString(R.string.backup_success_msg))
return@apply
}
showShareApkDialog(uri)
}
showShareApkDialog(uri)

}
}

private fun showShareApkDialog(uri: Uri) {
showNotice(context.getString(R.string.backup_success_and_share_msg)) { _, _ ->
(context as BaseActivity).requestPermission(arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
), object : BaseActivity.PermissionCallback() {
override fun granted() {
activityFinish()
Share2.Builder(context as Activity).setContentType(ShareContentType.FILE)
.setShareFileUri(uri).setOnActivityResult(10).build().shareBySystem()
}

})

showNotice(context.getString(R.string.backup_success_and_share_msg)) {
activityFinish()
Share2.Builder(context as Activity).setContentType(ShareContentType.FILE)
.setShareFileUri(uri).setOnActivityResult(10).build().shareBySystem()
}
}

Expand All @@ -228,9 +212,11 @@ class AppSettingView : ScrollView {
}
}

private fun showNotice(msg: String, listener: DialogInterface.OnClickListener) {
private fun showNotice(msg: String, listener: () -> Unit) {
AlertDialog.Builder(context).setTitle(R.string.notice).setMessage(msg)
.setNegativeButton(R.string.cancel, null).setPositiveButton(R.string.ok, listener)
.create().show()
.setNegativeButton(R.string.cancel, null).setPositiveButton(
R.string.ok
) { _, _ -> listener() }.create().show()
}

}
13 changes: 9 additions & 4 deletions app/src/main/java/com/wrbug/developerhelper/util/BackupUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.wrbug.developerhelper.util
import android.net.Uri
import android.os.Environment
import com.wrbug.developerhelper.basecommon.BaseApp
import com.wrbug.developerhelper.commonutil.Constant
import com.wrbug.developerhelper.commonutil.shell.ShellManager
import com.wrbug.developerhelper.commonutil.toUri
import java.io.File

object BackupUtils {
private val backupDir: File by lazy {
val file = File(Environment.getExternalStorageDirectory(), "com.wrbug.developerHelper/backup")
val file =
File(Environment.getExternalStorageDirectory(), "com.wrbug.developerHelper/backup")
if (file.exists().not()) {
file.mkdirs()
}
Expand All @@ -24,9 +26,12 @@ object BackupUtils {
return null
}

fun backupAppData(packageName: String, dataDir: String): File? {
val backupDataDir =
File(backupDir, "datas/$packageName/${System.currentTimeMillis().format("yyyy-MM-dd-HH_mm_ss")}")
fun backupAppData(packageName: String): File? {
val backupDataDir = File(
backupDir,
"datas/$packageName/${System.currentTimeMillis().format("yyyy-MM-dd-HH_mm_ss")}"
)
val dataDir = Constant.getDataDir(packageName)
if (ShellManager.cpFile(dataDir, backupDataDir.absolutePath)) {
return backupDataDir
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
*/
package com.wrbug.developerhelper.basecommon

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.annotation.IdRes
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
Expand Down Expand Up @@ -56,12 +63,54 @@ fun AppCompatActivity.startActivityForResult(intent: Intent, callback: ActivityR
}

fun AppCompatActivity.startActivityForResultOk(
intent: Intent,
action: Intent?.() -> Unit
intent: Intent, action: Intent?.() -> Unit
) {
ActResultRequest(this).startForResult(intent, object : ActivityResultCallback() {
override fun onActivityResultOk(data: Intent?) {
action(data)
}
})
}


fun Context.requestStoragePermission(callback: () -> Unit) {
if (this !is BaseActivity) {
return
}
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> {
if (!Environment.isExternalStorageManager()) {
AlertDialog.Builder(this).setTitle(R.string.notice)
.setMessage("该功能需要读写内部存储权限,点击前往设置")
.setNegativeButton(R.string.cancel, null).setPositiveButton(
R.string.ok
) { _, _ ->
try {
val intent =
Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse(String.format("package:%s", packageName))
startActivity(intent)
} catch (e: Exception) {
val intent = Intent()
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
startActivity(intent)
}
}.create().show()
} else {
callback()
}
}

else -> {
requestPermission(arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
), object : BaseActivity.PermissionCallback() {
override fun granted() {
callback()
}
})
}
}
}
4 changes: 3 additions & 1 deletion basecommon/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">baseCommon</string>
<string name="notice">提示</string>
<string name="ok">确定</string>
<string name="cancel">取消</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ object Constant {
private const val DATA_MIRROR_DIR = "/data_mirror/data_ce/null/0"
private const val DATA_DIR = "/data/data"

fun getDataDir(packageName: String): String {
return "$dataDir/$packageName"
}

val dataDir by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
DATA_MIRROR_DIR
Expand Down

0 comments on commit 0ee2026

Please sign in to comment.