Commit df78fd2d authored by Viktor De Pasquale's avatar Viktor De Pasquale

Fixed setting custom channels and switching between official ones being broken

parent dabe6267
package com.topjohnwu.magisk
import androidx.appcompat.app.AppCompatDelegate
import com.chibatching.kotpref.ContextProvider
import com.chibatching.kotpref.KotprefModel
import com.topjohnwu.magisk.KConfig.UpdateChannel.*
import com.topjohnwu.magisk.KConfig.UpdateChannel.STABLE
import com.topjohnwu.magisk.utils.get
object KConfig : KotprefModel() {
object KConfig : KotprefModel(get<ContextProvider>()) {
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"
)
private var internalUpdateChannel by intPref(STABLE.id, "updateChannel")
var useCustomTabs by booleanPref(true, "useCustomTabs")
@JvmStatic
var customUpdateChannel by stringPref("", "custom_channel")
@JvmStatic
var updateChannel: UpdateChannel
get() = valueOf(internalUpdateChannel)
get() = UpdateChannel.byId(internalUpdateChannel)
set(value) {
internalUpdateChannel = value.toString()
internalUpdateChannel = value.id
}
val isStable get() = !(isCanary || isBeta)
val isCanary get() = updateChannel == CANARY || updateChannel == CANARY_DEBUG
val isBeta get() = updateChannel == BETA
internal const val DEFAULT_CHANNEL = "topjohnwu/magisk_files"
enum class UpdateChannel(val id: Int) {
enum class UpdateChannel {
STABLE, BETA, CANARY, CANARY_DEBUG
STABLE(Config.Value.STABLE_CHANNEL),
BETA(Config.Value.BETA_CHANNEL),
CANARY(Config.Value.CANARY_CHANNEL),
CANARY_DEBUG(Config.Value.CANARY_DEBUG_CHANNEL),
CUSTOM(Config.Value.CUSTOM_CHANNEL);
companion object {
fun byId(id: Int) = when (id) {
Config.Value.STABLE_CHANNEL -> STABLE
Config.Value.BETA_CHANNEL -> BETA
Config.Value.CUSTOM_CHANNEL -> CUSTOM
Config.Value.CANARY_CHANNEL -> CANARY
Config.Value.CANARY_DEBUG_CHANNEL -> CANARY_DEBUG
else -> STABLE
}
}
}
}
\ No newline at end of file
package com.topjohnwu.magisk.data.network
import com.topjohnwu.magisk.Constants
import com.topjohnwu.magisk.KConfig
import com.topjohnwu.magisk.model.entity.MagiskConfig
import io.reactivex.Single
import okhttp3.ResponseBody
......@@ -26,6 +27,9 @@ interface GithubRawApiServices {
@GET("$MAGISK_FILES/master/canary_builds/canary.json")
fun fetchCanaryDebugConfig(): Single<MagiskConfig>
@GET
fun fetchCustomConfig(@Url url: String): Single<MagiskConfig>
@GET("$MAGISK_FILES/{$REVISION}/snet.apk")
@Streaming
fun fetchSafetynet(@Path(REVISION) revision: String = Constants.SNET_REVISION): Single<ResponseBody>
......@@ -67,7 +71,7 @@ interface GithubRawApiServices {
private const val FILE = "file"
private const val MAGISK_FILES = "topjohnwu/magisk_files"
private const val MAGISK_FILES = KConfig.DEFAULT_CHANNEL
private const val MAGISK_MASTER = "topjohnwu/Magisk/master"
private const val MAGISK_MODULES = "Magisk-Modules-Repo"
}
......
......@@ -24,12 +24,6 @@ class MagiskRepository(
private val packageManager: PackageManager
) {
private val config = apiRaw.fetchConfig()
private val configBeta = apiRaw.fetchBetaConfig()
private val configCanary = apiRaw.fetchCanaryConfig()
private val configCanaryDebug = apiRaw.fetchCanaryDebugConfig()
fun fetchMagisk() = fetchConfig()
.flatMap { apiRaw.fetchFile(it.magisk.link) }
.map { it.writeToFile(context, FILE_MAGISK_ZIP) }
......@@ -52,10 +46,11 @@ class MagiskRepository(
fun fetchConfig() = when (KConfig.updateChannel) {
KConfig.UpdateChannel.STABLE -> config
KConfig.UpdateChannel.BETA -> configBeta
KConfig.UpdateChannel.CANARY -> configCanary
KConfig.UpdateChannel.CANARY_DEBUG -> configCanaryDebug
KConfig.UpdateChannel.STABLE -> apiRaw.fetchConfig()
KConfig.UpdateChannel.BETA -> apiRaw.fetchBetaConfig()
KConfig.UpdateChannel.CANARY -> apiRaw.fetchCanaryConfig()
KConfig.UpdateChannel.CANARY_DEBUG -> apiRaw.fetchCanaryDebugConfig()
KConfig.UpdateChannel.CUSTOM -> apiRaw.fetchCustomConfig(KConfig.customUpdateChannel)
}
......
......@@ -2,6 +2,7 @@ package com.topjohnwu.magisk.di
import android.content.Context
import androidx.preference.PreferenceManager
import com.chibatching.kotpref.ContextProvider
import com.skoumal.teanity.rxbus.RxBus
import com.topjohnwu.magisk.App
import org.koin.dsl.module
......@@ -15,4 +16,11 @@ val applicationModule = module {
factory(Protected) { get<App>().protectedContext }
single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) }
single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) }
single { createContextProvider(get(Protected)) as ContextProvider }
}
private fun createContextProvider(context: Context) = object : ContextProvider {
override fun getApplicationContext(): Context {
return context
}
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ import androidx.core.view.isVisible
import androidx.preference.*
import androidx.recyclerview.widget.RecyclerView
import com.topjohnwu.magisk.App
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.KConfig
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.ModuleRepository
import com.topjohnwu.magisk.data.repository.SettingRepository
......@@ -64,4 +66,19 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat(),
else
view.setPadding(0, view.paddingTop, view.paddingRight, view.paddingBottom)
}
protected fun setCustomUpdateChannel(userRepo: String) {
KConfig.customUpdateChannel = userRepo
}
protected fun getChannelCompat(channel: Int): KConfig.UpdateChannel {
return when (channel) {
Config.Value.DEFAULT_CHANNEL -> KConfig.UpdateChannel.STABLE
Config.Value.BETA_CHANNEL -> KConfig.UpdateChannel.BETA
Config.Value.CANARY_CHANNEL -> KConfig.UpdateChannel.CANARY
Config.Value.CANARY_DEBUG_CHANNEL -> KConfig.UpdateChannel.CANARY_DEBUG
Config.Value.CUSTOM_CHANNEL -> KConfig.UpdateChannel.CUSTOM
else -> KConfig.updateChannel
}
}
}
......@@ -12,6 +12,8 @@ import android.widget.Toast;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.KConfig;
import com.topjohnwu.magisk.KConfig.UpdateChannel;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.ui.base.BasePreferenceFragment;
import com.topjohnwu.magisk.utils.DownloadApp;
......@@ -130,21 +132,23 @@ public final class SettingsFragment extends BasePreferenceFragment {
SwitchPreferenceCompat fingerprint = (SwitchPreferenceCompat) findPreference(Config.Key.SU_FINGERPRINT);
updateChannel.setOnPreferenceChangeListener((p, o) -> {
int prev = Config.get(Config.Key.UPDATE_CHANNEL);
int channel = Integer.parseInt((String) o);
if (channel == Config.Value.CUSTOM_CHANNEL) {
final UpdateChannel previousUpdateChannel = KConfig.getUpdateChannel();
final UpdateChannel updateChannel = getChannelCompat(channel);
KConfig.setUpdateChannel(updateChannel);
if (updateChannel == UpdateChannel.CUSTOM) {
View v = LayoutInflater.from(requireActivity()).inflate(R.layout.custom_channel_dialog, null);
EditText url = v.findViewById(R.id.custom_url);
url.setText(getPrefs().getString(Config.Key.CUSTOM_CHANNEL, ""));
url.setText(KConfig.getCustomUpdateChannel());
new AlertDialog.Builder(requireActivity())
.setTitle(R.string.settings_update_custom)
.setView(v)
.setPositiveButton(R.string.ok, (d, i) ->
Config.set(Config.Key.CUSTOM_CHANNEL, url.getText().toString()))
.setNegativeButton(R.string.close, (d, i) ->
Config.set(Config.Key.UPDATE_CHANNEL, prev))
.setOnCancelListener(d ->
Config.set(Config.Key.UPDATE_CHANNEL, prev))
.setPositiveButton(R.string.ok, (d, i) -> setCustomUpdateChannel(url.getText().toString()))
.setNegativeButton(R.string.close, (d, i) -> KConfig.setUpdateChannel(previousUpdateChannel))
.setOnCancelListener(d -> KConfig.setUpdateChannel(previousUpdateChannel))
.show();
}
return true;
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="15dp"
android:paddingTop="5dp"
android:paddingEnd="15dp"
android:paddingBottom="5dp">
android:padding="@dimen/margin_generic">
<TextView
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:labelFor="@id/custom_url"
android:text="@string/settings_update_custom_msg"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:textColorPrimary" />
android:hint="@string/settings_update_custom_msg"
app:hintEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/custom_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri" />
</com.google.android.material.textfield.TextInputLayout>
<EditText
android:id="@+id/custom_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri" />
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment