|
| 1 | +package com.close.hook.ads.data.repository |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.database.ContentObserver |
| 5 | +import android.net.Uri |
| 6 | +import android.os.Handler |
| 7 | +import android.os.Looper |
| 8 | +import android.util.Log |
| 9 | +import com.close.hook.ads.data.model.RuleMatch |
| 10 | +import com.close.hook.ads.data.model.Url |
| 11 | +import com.close.hook.ads.data.RuleSnapshot |
| 12 | +import com.close.hook.ads.provider.UrlContentProvider |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean |
| 14 | + |
| 15 | +object RuleRepository { |
| 16 | + |
| 17 | + private const val LOG_PREFIX = "[RuleRepository]" |
| 18 | + private const val MIN_REFRESH_INTERVAL_MS = 5_000L |
| 19 | + |
| 20 | + private val contentUri: Uri = Uri.Builder() |
| 21 | + .scheme("content") |
| 22 | + .authority(UrlContentProvider.AUTHORITY) |
| 23 | + .appendPath(UrlContentProvider.URL_TABLE_NAME) |
| 24 | + .build() |
| 25 | + |
| 26 | + @Volatile private var appContext: Context? = null |
| 27 | + @Volatile private var snapshot: RuleSnapshot = RuleSnapshot.EMPTY |
| 28 | + @Volatile private var lastRefreshAt: Long = 0L |
| 29 | + |
| 30 | + private val initialized = AtomicBoolean(false) |
| 31 | + private val dirty = AtomicBoolean(true) |
| 32 | + private val refreshLock = Any() |
| 33 | + |
| 34 | + private val observer by lazy { |
| 35 | + object : ContentObserver( |
| 36 | + null |
| 37 | + ) { |
| 38 | + override fun onChange(selfChange: Boolean) { |
| 39 | + dirty.set(true) |
| 40 | + } |
| 41 | + |
| 42 | + override fun onChange(selfChange: Boolean, uri: Uri?) { |
| 43 | + dirty.set(true) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fun init(context: Context) { |
| 49 | + appContext = context |
| 50 | + } |
| 51 | + |
| 52 | + fun shouldBlock(requestValue: String, host: String?): RuleMatch { |
| 53 | + ensureFreshSnapshot(force = false) |
| 54 | + return snapshot.match(requestValue = requestValue, host = host) |
| 55 | + } |
| 56 | + |
| 57 | + private fun ensureFreshSnapshot(force: Boolean) { |
| 58 | + val rawContext = appContext ?: return |
| 59 | + |
| 60 | + val now = System.currentTimeMillis() |
| 61 | + val shouldRefresh = force || dirty.get() || (now - lastRefreshAt >= MIN_REFRESH_INTERVAL_MS) |
| 62 | + if (!shouldRefresh) return |
| 63 | + |
| 64 | + synchronized(refreshLock) { |
| 65 | + val freshNow = System.currentTimeMillis() |
| 66 | + val stillNeedRefresh = force || dirty.get() || (freshNow - lastRefreshAt >= MIN_REFRESH_INTERVAL_MS) |
| 67 | + if (!stillNeedRefresh) return |
| 68 | + |
| 69 | + if (initialized.compareAndSet(false, true)) { |
| 70 | + try { |
| 71 | + val safeContext = rawContext.applicationContext ?: rawContext |
| 72 | + safeContext.contentResolver.registerContentObserver(contentUri, true, observer) |
| 73 | + } catch (e: Throwable) { |
| 74 | + Log.w(LOG_PREFIX, "Failed to register observer: ${e.message}") |
| 75 | + initialized.set(false) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + runCatching { |
| 80 | + val safeContext = rawContext.applicationContext ?: rawContext |
| 81 | + val rules = loadAllRules(safeContext) |
| 82 | + snapshot = RuleSnapshot.fromUrls(rules) |
| 83 | + dirty.set(false) |
| 84 | + }.onFailure { error -> |
| 85 | + Log.w(LOG_PREFIX, "Failed to refresh rule snapshot: ${error.message}") |
| 86 | + } |
| 87 | + lastRefreshAt = freshNow |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private fun loadAllRules(context: Context): List<Url> { |
| 92 | + val result = ArrayList<Url>() |
| 93 | + context.contentResolver.query( |
| 94 | + contentUri, |
| 95 | + arrayOf(Url.URL_TYPE, Url.URL_ADDRESS), |
| 96 | + null, null, null |
| 97 | + )?.use { cursor -> |
| 98 | + val typeIndex = cursor.getColumnIndex(Url.URL_TYPE) |
| 99 | + val urlIndex = cursor.getColumnIndex(Url.URL_ADDRESS) |
| 100 | + if (typeIndex == -1 || urlIndex == -1) return emptyList() |
| 101 | + while (cursor.moveToNext()) { |
| 102 | + val type = cursor.getString(typeIndex).orEmpty() |
| 103 | + val url = cursor.getString(urlIndex).orEmpty() |
| 104 | + result += Url(type = type, url = url) |
| 105 | + } |
| 106 | + } |
| 107 | + return result |
| 108 | + } |
| 109 | +} |
0 commit comments