Converted classmap to kotlin

This commit is contained in:
Viktor De Pasquale 2019-05-13 15:34:53 +02:00
parent 92789c3113
commit eb169cb133
8 changed files with 36 additions and 37 deletions

View File

@ -1,31 +0,0 @@
package com.topjohnwu.magisk;
import com.topjohnwu.magisk.model.download.DownloadModuleService;
import com.topjohnwu.magisk.model.receiver.GeneralReceiver;
import com.topjohnwu.magisk.model.update.UpdateCheckService;
import com.topjohnwu.magisk.ui.MainActivity;
import com.topjohnwu.magisk.ui.SplashActivity;
import com.topjohnwu.magisk.ui.flash.FlashActivity;
import com.topjohnwu.magisk.ui.surequest.SuRequestActivity;
import java.util.HashMap;
import java.util.Map;
public class ClassMap {
private static Map<Class, Class> classMap = new HashMap<>();
static {
classMap.put(App.class, a.e.class);
classMap.put(MainActivity.class, a.b.class);
classMap.put(SplashActivity.class, a.c.class);
classMap.put(FlashActivity.class, a.f.class);
classMap.put(UpdateCheckService.class, a.g.class);
classMap.put(GeneralReceiver.class, a.h.class);
classMap.put(DownloadModuleService.class, a.j.class);
classMap.put(SuRequestActivity.class, a.m.class);
}
public static <T> Class<T> get(Class c) {
return classMap.get(c);
}
}

View File

@ -0,0 +1,27 @@
package com.topjohnwu.magisk
import com.topjohnwu.magisk.model.download.DownloadModuleService
import com.topjohnwu.magisk.model.receiver.GeneralReceiver
import com.topjohnwu.magisk.model.update.UpdateCheckService
import com.topjohnwu.magisk.ui.MainActivity
import com.topjohnwu.magisk.ui.SplashActivity
import com.topjohnwu.magisk.ui.flash.FlashActivity
import com.topjohnwu.magisk.ui.surequest.SuRequestActivity
object ClassMap {
private val map = mapOf(
App::class.java to a.e::class.java,
MainActivity::class.java to a.b::class.java,
SplashActivity::class.java to a.c::class.java,
FlashActivity::class.java to a.f::class.java,
UpdateCheckService::class.java to a.g::class.java,
GeneralReceiver::class.java to a.h::class.java,
DownloadModuleService::class.java to a.j::class.java,
SuRequestActivity::class.java to a.m::class.java
)
@JvmStatic
operator fun get(c: Class<*>): Class<*>? {
return map.getOrElse(c) { null } //as? Class<T>
}
}

View File

@ -41,7 +41,7 @@ open class GeneralReceiver : BroadcastReceiver() {
} }
when (action) { when (action) {
SuRequestActivity.REQUEST -> { SuRequestActivity.REQUEST -> {
val i = Intent(context, ClassMap.get<Any>(SuRequestActivity::class.java)) val i = Intent(context, ClassMap[SuRequestActivity::class.java])
.setAction(action) .setAction(action)
.putExtra("socket", intent.getStringExtra("socket")) .putExtra("socket", intent.getStringExtra("socket"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

View File

@ -48,7 +48,7 @@ open class MainActivity : MagiskActivity<MainViewModel, ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
if (!SplashActivity.DONE) { if (!SplashActivity.DONE) {
startActivity(Intent(this, ClassMap.get<Any>(SplashActivity::class.java))) startActivity(Intent(this, ClassMap[SplashActivity::class.java]))
finish() finish()
} }

View File

@ -66,7 +66,7 @@ open class SplashActivity : AppCompatActivity() {
//UpdateRepos().exec() //UpdateRepos().exec()
} }
val intent = Intent(this, ClassMap.get<Any>(MainActivity::class.java)) val intent = Intent(this, ClassMap[MainActivity::class.java])
intent.putExtra(Const.Key.OPEN_SECTION, getIntent().getStringExtra(Const.Key.OPEN_SECTION)) intent.putExtra(Const.Key.OPEN_SECTION, getIntent().getStringExtra(Const.Key.OPEN_SECTION))
DONE = true DONE = true
startActivity(intent) startActivity(intent)

View File

@ -28,7 +28,7 @@ class ModulesFragment : MagiskFragment<ModuleViewModel, FragmentModulesBinding>(
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) { if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) {
// Get the URI of the selected file // Get the URI of the selected file
val intent = Intent(activity, ClassMap.get<Any>(FlashActivity::class.java)) val intent = Intent(activity, ClassMap[FlashActivity::class.java])
intent.setData(data.data).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP) intent.setData(data.data).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP)
startActivity(intent) startActivity(intent)
} }

View File

@ -82,7 +82,7 @@ class ReposFragment : MagiskFragment<ModuleViewModel, FragmentReposBinding>(),
fun download(install: Boolean) { fun download(install: Boolean) {
context.runWithExternalRW { context.runWithExternalRW {
val intent = Intent(activity, ClassMap.get<Any>(DownloadModuleService::class.java)) val intent = Intent(activity, ClassMap[DownloadModuleService::class.java])
.putExtra("repo", item).putExtra("install", install) .putExtra("repo", item).putExtra("install", install)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent) //hmm, service starts itself in foreground, this seems unnecessary context.startForegroundService(intent) //hmm, service starts itself in foreground, this seems unnecessary

View File

@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import androidx.annotation.WorkerThread; import androidx.annotation.WorkerThread;
import androidx.work.Constraints; import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.ListenableWorker;
import androidx.work.NetworkType; import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest; import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager; import androidx.work.WorkManager;
@ -110,13 +111,15 @@ public class Utils {
return BuildConfig.VERSION_NAME.contains("-"); return BuildConfig.VERSION_NAME.contains("-");
} }
@SuppressWarnings("unchecked")
public static void scheduleUpdateCheck() { public static void scheduleUpdateCheck() {
if (Config.get(Config.Key.CHECK_UPDATES)) { if (Config.get(Config.Key.CHECK_UPDATES)) {
Constraints constraints = new Constraints.Builder() Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build(); .build();
Class<? extends ListenableWorker> service = (Class<? extends ListenableWorker>) ClassMap.get(UpdateCheckService.class);
PeriodicWorkRequest request = new PeriodicWorkRequest PeriodicWorkRequest request = new PeriodicWorkRequest
.Builder(ClassMap.get(UpdateCheckService.class), 12, TimeUnit.HOURS) .Builder(service, 12, TimeUnit.HOURS)
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
WorkManager.getInstance().enqueueUniquePeriodicWork( WorkManager.getInstance().enqueueUniquePeriodicWork(