From b018124226036f6b9da540ed1b0641b4d350fdc9 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Mon, 6 May 2019 19:03:28 +0200 Subject: [PATCH] Added (ported back) features from initial design [retrofit,moshi,kotpref] Marked most of the old classes using Networking as deprecated to clearly visualise their future removal --- app/build.gradle | 19 +++ .../java/com/topjohnwu/magisk/Constants.kt | 20 +++ .../main/java/com/topjohnwu/magisk/KConfig.kt | 36 ++++ .../magisk/data/database/AppDatabase.kt | 19 +++ .../topjohnwu/magisk/data/database/LogDao.kt | 34 ++++ .../magisk/data/database/MagiskDB.java | 22 ++- .../magisk/data/database/PolicyDao.kt | 67 ++++++++ .../data/database/RepoDatabaseHelper.java | 13 +- .../magisk/data/database/RepositoryDao.kt | 17 ++ .../magisk/data/database/SettingsDao.kt | 21 +++ .../magisk/data/database/StringsDao.kt | 22 +++ .../magisk/data/database/base/BaseDao.kt | 15 ++ .../data/database/base/DatabaseDefinition.kt | 33 ++++ .../magisk/data/database/base/MagiskQuery.kt | 5 + .../data/database/base/MagiskQueryBuilder.kt | 157 ++++++++++++++++++ .../magisk/data/network/GithubApiServices.kt | 22 +++ .../data/network/GithubRawApiServices.kt | 75 +++++++++ .../magisk/data/network/GithubServices.kt | 21 +++ .../magisk/data/repository/LogRepository.kt | 45 +++++ .../data/repository/MagiskRepository.kt | 78 +++++++++ .../data/repository/ModuleRepository.kt | 67 ++++++++ .../topjohnwu/magisk/di/NetworkingModule.kt | 61 ++++++- .../magisk/model/entity/GithubRepo.kt | 12 ++ .../magisk/model/entity/MagiskConfig.kt | 32 ++++ .../magisk/model/entity/MagiskLog.kt | 46 +++++ .../magisk/model/entity/MagiskModule.kt | 81 +++++++++ .../magisk/model/entity/MagiskPolicy.kt | 74 +++++++++ .../topjohnwu/magisk/model/entity/Version.kt | 3 + .../com/topjohnwu/magisk/model/zip/Zip.kt | 75 +++++++++ .../topjohnwu/magisk/tasks/CheckUpdates.java | 5 +- .../topjohnwu/magisk/tasks/UpdateRepos.java | 1 + .../com/topjohnwu/magisk/utils/Event.java | 43 +++-- .../com/topjohnwu/magisk/utils/XAndroid.kt | 21 +++ .../java/com/topjohnwu/magisk/utils/XJava.kt | 38 +++++ .../com/topjohnwu/magisk/utils/XNetwork.kt | 54 ++++++ .../java/com/topjohnwu/magisk/utils/XSU.kt | 19 +++ .../com/topjohnwu/magisk/utils/XString.kt | 5 +- .../java/com/topjohnwu/magisk/utils/XView.kt | 14 ++ .../java/com/topjohnwu/net/Networking.java | 1 + 39 files changed, 1374 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/Constants.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/KConfig.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/AppDatabase.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/LogDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/RepositoryDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/SettingsDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/StringsDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/base/BaseDao.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/base/DatabaseDefinition.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/base/MagiskQuery.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/database/base/MagiskQueryBuilder.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/network/GithubApiServices.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/network/GithubRawApiServices.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/network/GithubServices.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/repository/LogRepository.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/GithubRepo.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskConfig.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskLog.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskModule.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/MagiskPolicy.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/entity/Version.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/zip/Zip.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/XJava.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/XSU.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/XView.kt diff --git a/app/build.gradle b/app/build.gradle index 68be34143..9cb129bae 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,6 +47,10 @@ android { } } +androidExtensions { + experimental = true +} + dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation project(':net') @@ -72,8 +76,23 @@ dependencies { implementation "org.koin:koin-android:${koin}" implementation "org.koin:koin-androidx-viewmodel:${koin}" + def vRetrofit = "2.5.0" + def vOkHttp = "3.12.0" + def vMoshi = "1.8.0" + implementation "com.squareup.retrofit2:retrofit:${vRetrofit}" + implementation "com.squareup.retrofit2:converter-moshi:${vRetrofit}" + implementation "com.squareup.retrofit2:adapter-rxjava2:${vRetrofit}" + implementation "com.squareup.okhttp3:okhttp:${vOkHttp}" + implementation "com.squareup.okhttp3:logging-interceptor:${vOkHttp}" + implementation "com.squareup.moshi:moshi:${vMoshi}" + implementation "com.squareup.moshi:moshi-kotlin:${vMoshi}" + + def vKotpref = "2.8.0" + implementation "com.chibatching.kotpref:kotpref:${vKotpref}" + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.browser:browser:1.0.0' implementation 'androidx.preference:preference:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04' implementation 'androidx.cardview:cardview:1.0.0' diff --git a/app/src/main/java/com/topjohnwu/magisk/Constants.kt b/app/src/main/java/com/topjohnwu/magisk/Constants.kt new file mode 100644 index 000000000..c23f3422a --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/Constants.kt @@ -0,0 +1,20 @@ +package com.topjohnwu.magisk + +import android.os.Process + +object Constants { + + // Paths + val MAGISK_PATH = "/sbin/.magisk/img" + val MAGISK_LOG = "/cache/magisk.log" + + val USER_ID = Process.myUid() / 100000 + + const val SNET_REVISION = "b66b1a914978e5f4c4bbfd74a59f4ad371bac107" + const val BOOTCTL_REVISION = "9c5dfc1b8245c0b5b524901ef0ff0f8335757b77" + + const val GITHUB_URL = "https://github.com/" + const val GITHUB_API_URL = "https://api.github.com/" + const val GITHUB_RAW_API_URL = "https://raw.githubusercontent.com/" + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/KConfig.kt b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt new file mode 100644 index 000000000..be3ecde27 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt @@ -0,0 +1,36 @@ +package com.topjohnwu.magisk + +import androidx.appcompat.app.AppCompatDelegate +import com.chibatching.kotpref.KotprefModel +import com.topjohnwu.magisk.KConfig.UpdateChannel.* + +object KConfig : KotprefModel() { + override val kotprefName: String = "${context.packageName}_preferences" + + var darkMode by intPref(default = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, key = "darkMode") + var magiskChecksum by stringPref("", "magiskChecksum") + var forceEncrypt by booleanPref(false, "forceEncryption") + var keepVerity by booleanPref(false, "keepVerity") + var bootFormat by stringPref("img", "bootFormat") + var suLogTimeout by longPref(0, "suLogTimeout") + private var internalUpdateChannel by stringPref( + KConfig.UpdateChannel.STABLE.toString(), + "updateChannel" + ) + var useCustomTabs by booleanPref(true, "useCustomTabs") + + var updateChannel: UpdateChannel + get() = valueOf(internalUpdateChannel) + set(value) { + internalUpdateChannel = value.toString() + } + + val isStable get() = !(isCanary || isBeta) + val isCanary get() = updateChannel == CANARY || updateChannel == CANARY_DEBUG + val isBeta get() = updateChannel == BETA + + + enum class UpdateChannel { + STABLE, BETA, CANARY, CANARY_DEBUG + } +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/AppDatabase.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/AppDatabase.kt new file mode 100644 index 000000000..e09f238bb --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/AppDatabase.kt @@ -0,0 +1,19 @@ +package com.topjohnwu.magisk.data.database + +import androidx.room.Database +import androidx.room.RoomDatabase +import com.topjohnwu.magisk.model.entity.Repository + +@Database( + version = 1, + entities = [Repository::class] +) +abstract class AppDatabase : RoomDatabase() { + + companion object { + const val NAME = "database" + } + + abstract fun repoDao(): RepositoryDao + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/LogDao.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/LogDao.kt new file mode 100644 index 000000000..1bb589d2a --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/LogDao.kt @@ -0,0 +1,34 @@ +package com.topjohnwu.magisk.data.database + +import com.topjohnwu.magisk.Config +import com.topjohnwu.magisk.data.database.base.* +import com.topjohnwu.magisk.model.entity.MagiskLog +import com.topjohnwu.magisk.model.entity.toLog +import com.topjohnwu.magisk.model.entity.toMap +import java.util.concurrent.TimeUnit + +class LogDao : BaseDao() { + + override val table = DatabaseDefinition.Table.LOG + + fun deleteOutdated( + suTimeout: Long = Config.suLogTimeout * TimeUnit.DAYS.toMillis(1) + ) = query { + condition { + lessThan("time", suTimeout.toString()) + } + }.ignoreElement() + + fun deleteAll() = query {}.ignoreElement() + + fun fetchAll() = query { + condition { + equals("uid", uid.toString()) + } + }.map { it.first().toPolicy(context.packageManager) } + .doOnError { + if (it is PackageManager.NameNotFoundException) { + delete(uid).subscribe() + } + } + + fun update(policy: MagiskPolicy) = query { + + values(policy.toMap()) + }.ignoreElement() + + fun fetchAll() = query { + condition { equals("key", key) } + }.map { it.first().values.first().toIntOrNull() ?: -1 } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/StringsDao.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/StringsDao.kt new file mode 100644 index 000000000..916c71d31 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/StringsDao.kt @@ -0,0 +1,22 @@ +package com.topjohnwu.magisk.data.database + +import com.topjohnwu.magisk.data.database.base.* + +class StringsDao : BaseDao() { + + override val table = DatabaseDefinition.Table.STRINGS + + fun delete(key: String) = query { + condition { equals("key", key) } + }.ignoreElement() + + fun put(key: String, value: String) = query { + values(key to value) + }.ignoreElement() + + fun fetch(key: String, default: String = "") = query