Skip to content

Commit 8de65a4

Browse files
almost done with kotlin migration
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 547a509 commit 8de65a4

4 files changed

Lines changed: 254 additions & 241 deletions

File tree

app/src/main/java/com/fox2code/mmm/MainActivity.kt

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import com.fox2code.mmm.repo.RepoModule
5151
import com.fox2code.mmm.settings.SettingsActivity
5252
import com.fox2code.mmm.utils.ExternalHelper
5353
import com.fox2code.mmm.utils.RuntimeUtils
54+
import com.fox2code.mmm.utils.SyncManager
5455
import com.fox2code.mmm.utils.io.net.Http.Companion.cleanDnsCache
5556
import com.fox2code.mmm.utils.io.net.Http.Companion.hasWebView
5657
import com.fox2code.mmm.utils.realm.ReposList
@@ -388,17 +389,20 @@ class MainActivity : FoxActivity(), OnRefreshListener, SearchView.OnQueryTextLis
388389
if (BuildConfig.DEBUG) Timber.i("Check Update")
389390
// update repos
390391
if (hasWebView()) {
391-
RepoManager.getINSTANCE()!!.update { value: Double ->
392-
runOnUiThread(if (max == 0) Runnable {
393-
progressIndicator.setProgressCompat(
394-
(value * PRECISION).toInt(), true
395-
)
396-
} else Runnable {
397-
progressIndicator.setProgressCompat(
398-
(value * PRECISION * 0.75f).toInt(), true
399-
)
400-
})
392+
val updateListener: SyncManager.UpdateListener = object : SyncManager.UpdateListener {
393+
override fun update(value: Double) {
394+
runOnUiThread(if (max == 0) Runnable {
395+
progressIndicator.setProgressCompat(
396+
(value * PRECISION).toInt(), true
397+
)
398+
} else Runnable {
399+
progressIndicator.setProgressCompat(
400+
(value * PRECISION * 0.75f).toInt(), true
401+
)
402+
})
403+
}
401404
}
405+
RepoManager.getINSTANCE()!!.update(updateListener)
402406
}
403407
// various notifications
404408
NotificationType.NEED_CAPTCHA_ANDROIDACY.autoAdd(moduleViewListBuilder)
@@ -606,13 +610,16 @@ class MainActivity : FoxActivity(), OnRefreshListener, SearchView.OnQueryTextLis
606610
progressIndicator!!.max = PRECISION
607611
}
608612
if (BuildConfig.DEBUG) Timber.i("Check Update")
609-
RepoManager.getINSTANCE()!!.update { value: Double ->
610-
runOnUiThread {
611-
progressIndicator!!.setProgressCompat(
612-
(value * PRECISION).toInt(), true
613-
)
613+
val updateListener: SyncManager.UpdateListener = object : SyncManager.UpdateListener {
614+
override fun update(value: Double) {
615+
runOnUiThread {
616+
progressIndicator!!.setProgressCompat(
617+
(value * PRECISION).toInt(), true
618+
)
619+
}
614620
}
615621
}
622+
RepoManager.getINSTANCE()!!.update(updateListener)
616623
runOnUiThread {
617624
progressIndicator!!.setProgressCompat(PRECISION, true)
618625
progressIndicator!!.visibility = View.GONE
@@ -647,17 +654,20 @@ class MainActivity : FoxActivity(), OnRefreshListener, SearchView.OnQueryTextLis
647654
Thread({
648655
cleanDnsCache() // Allow DNS reload from network
649656
val max = instance!!.getUpdatableModuleCount()
650-
RepoManager.getINSTANCE()!!.update { value: Double ->
651-
runOnUiThread(if (max == 0) Runnable {
652-
progressIndicator!!.setProgressCompat(
653-
(value * PRECISION).toInt(), true
654-
)
655-
} else Runnable {
656-
progressIndicator!!.setProgressCompat(
657-
(value * PRECISION * 0.75f).toInt(), true
658-
)
659-
})
657+
val updateListener: SyncManager.UpdateListener = object : SyncManager.UpdateListener {
658+
override fun update(value: Double) {
659+
runOnUiThread(if (max == 0) Runnable {
660+
progressIndicator!!.setProgressCompat(
661+
(value * PRECISION).toInt(), true
662+
)
663+
} else Runnable {
664+
progressIndicator!!.setProgressCompat(
665+
(value * PRECISION * 0.75f).toInt(), true
666+
)
667+
})
668+
}
660669
}
670+
RepoManager.getINSTANCE()!!.update(updateListener)
661671
NotificationType.NEED_CAPTCHA_ANDROIDACY.autoAdd(moduleViewListBuilder)
662672
if (!NotificationType.NO_INTERNET.shouldRemove()) {
663673
moduleViewListBuilderOnline.addNotification(NotificationType.NO_INTERNET)
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
/*
22
* Copyright (c) 2023 to present Androidacy and contributors. Names, logos, icons, and the Androidacy name are all trademarks of Androidacy and may not be used without license. See LICENSE for more information.
33
*/
4+
package com.fox2code.mmm.utils
45

5-
package com.fox2code.mmm.utils;
6+
import android.app.AlarmManager
7+
import android.app.PendingIntent
8+
import android.content.Context
9+
import android.content.Intent
10+
import com.fox2code.mmm.MainActivity
11+
import java.util.concurrent.ThreadLocalRandom
12+
import kotlin.system.exitProcess
613

7-
import android.app.AlarmManager;
8-
import android.app.PendingIntent;
9-
import android.content.Context;
10-
import android.content.Intent;
11-
12-
import com.fox2code.mmm.MainActivity;
13-
14-
import java.util.concurrent.ThreadLocalRandom;
15-
16-
public enum ProcessHelper {
14+
enum class ProcessHelper {
1715
;
18-
private static final int sPendingIntentId = ThreadLocalRandom.current().nextInt(100, 1000000 + 1);
1916

20-
public static void restartApplicationProcess(Context context) {
21-
Intent mStartActivity = new Intent(context, MainActivity.class);
22-
mStartActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
23-
PendingIntent mPendingIntent = PendingIntent.getActivity(context, sPendingIntentId,
24-
mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
25-
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
26-
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
27-
System.exit(0); // Exit app process
17+
companion object {
18+
private val sPendingIntentId = ThreadLocalRandom.current().nextInt(100, 1000000 + 1)
19+
@JvmStatic
20+
fun restartApplicationProcess(context: Context) {
21+
val mStartActivity = Intent(context, MainActivity::class.java)
22+
mStartActivity.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
23+
val mPendingIntent = PendingIntent.getActivity(
24+
context, sPendingIntentId,
25+
mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
26+
)
27+
val mgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
28+
mgr[AlarmManager.RTC, System.currentTimeMillis() + 100] = mPendingIntent
29+
exitProcess(0) // Exit app process
30+
}
2831
}
29-
}
32+
}
Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,79 @@
11
/*
22
* Copyright (c) 2023 to present Androidacy and contributors. Names, logos, icons, and the Androidacy name are all trademarks of Androidacy and may not be used without license. See LICENSE for more information.
33
*/
4-
5-
package com.fox2code.mmm.utils;
6-
7-
import androidx.annotation.NonNull;
8-
import androidx.annotation.Nullable;
4+
package com.fox2code.mmm.utils
95

106
/**
117
* Manager that want both to be thread safe and not to worry about thread safety
12-
* {@link #scan()} and {@link #update(UpdateListener)} can be called from multiple
13-
* thread at the same time, {@link #scanInternal(UpdateListener)} will only be
8+
* [.scan] and [.update] can be called from multiple
9+
* thread at the same time, [.scanInternal] will only be
1410
* called from one thread at a time only.
1511
*/
16-
public abstract class SyncManager {
17-
private static final UpdateListener NO_OP = value -> {
18-
};
19-
protected final Object syncLock = new Object();
20-
private boolean syncing;
21-
private long lastSync;
22-
23-
public final void scanAsync() {
24-
if (!this.syncing) {
25-
new Thread(this::scan, "Scan Thread").start();
12+
abstract class SyncManager {
13+
protected val syncLock = Any()
14+
private var syncing = false
15+
private var lastSync: Long = 0
16+
fun scanAsync() {
17+
if (!syncing) {
18+
Thread({ this.scan() }, "Scan Thread").start()
2619
}
2720
}
2821

29-
public final void scan() {
30-
this.update(null);
22+
fun scan() {
23+
update(null)
3124
}
3225

3326
// MultiThread friendly method
34-
public final void update(@Nullable UpdateListener updateListener) {
35-
if (updateListener == null) updateListener = NO_OP;
36-
if (!this.syncing) {
27+
@Suppress("NAME_SHADOWING")
28+
fun update(updateListener: UpdateListener?) {
29+
var updateListener = updateListener
30+
if (updateListener == null) updateListener = NO_OP
31+
if (!syncing) {
3732
// Do scan
38-
synchronized (this.syncLock) {
39-
if (System.currentTimeMillis() < this.lastSync + 50L)
40-
return; // Skip sync if it was synced too recently
41-
this.syncing = true;
33+
synchronized(syncLock) {
34+
if (System.currentTimeMillis() < lastSync + 50L) return // Skip sync if it was synced too recently
35+
syncing = true
4236
try {
43-
this.scanInternal(updateListener);
37+
scanInternal(updateListener)
4438
} finally {
45-
this.lastSync = System.currentTimeMillis();
46-
this.syncing = false;
39+
lastSync = System.currentTimeMillis()
40+
syncing = false
4741
}
4842
}
4943
} else {
5044
// Wait for current scan
51-
synchronized (this.syncLock) {
52-
Thread.yield();
53-
}
45+
synchronized(syncLock) { Thread.yield() }
5446
}
5547
}
5648

5749
// Pause execution until the scan is completed if one is currently running
58-
public final void afterScan() {
59-
if (this.syncing) synchronized (this.syncLock) {
60-
Thread.yield();
61-
}
50+
fun afterScan() {
51+
if (syncing) synchronized(syncLock) { Thread.yield() }
6252
}
6353

64-
public final void runAfterScan(Runnable runnable) {
65-
synchronized (this.syncLock) {
66-
runnable.run();
67-
}
54+
fun runAfterScan(runnable: Runnable) {
55+
synchronized(syncLock) { runnable.run() }
6856
}
6957

70-
public final void afterUpdate() {
71-
if (this.syncing) synchronized (this.syncLock) {
72-
Thread.yield();
73-
}
58+
fun afterUpdate() {
59+
if (syncing) synchronized(syncLock) { Thread.yield() }
7460
}
7561

76-
public final void runAfterUpdate(Runnable runnable) {
77-
synchronized (this.syncLock) {
78-
runnable.run();
79-
}
62+
fun runAfterUpdate(runnable: Runnable) {
63+
synchronized(syncLock) { runnable.run() }
8064
}
8165

8266
// This method can't be called twice at the same time.
83-
protected abstract void scanInternal(@NonNull UpdateListener updateListener);
67+
protected abstract fun scanInternal(updateListener: UpdateListener)
68+
interface UpdateListener {
69+
fun update(value: Double)
70+
}
8471

85-
public interface UpdateListener {
86-
void update(double value);
72+
companion object {
73+
private val NO_OP: UpdateListener = object : UpdateListener {
74+
override fun update(value: Double) {
75+
76+
}
77+
}
8778
}
88-
}
79+
}

0 commit comments

Comments
 (0)