Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ android {
}
}

dependencies {
implementation("com.xzakota.hyper.notification:focus-api:1.4")
implementation("androidx.core:core:1.18.0")
}

tasks.named("preBuild") { dependsOn(prepareEcardWidgetResources) }

flutter {
Expand Down
14 changes: 13 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<!-- Focus SDK calls are guarded at API 27; older devices use plain notifications. -->
<uses-sdk tools:overrideLibrary="com.xzakota.hyper.notification.focus"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.POST_PROMOTED_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
Expand Down Expand Up @@ -51,6 +56,13 @@
<data android:scheme="techpie" android:host="ecard" android:path="/pay"/>
</intent-filter>
</activity>
<receiver android:name=".island.IslandFrameReceiver" android:exported="false"/>
<receiver android:name=".island.IslandRestoreReceiver" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
<!-- DNS-only tunnel for the eCard bind-code flow (see EcardBindVpnService). -->
<service
android:name=".ecardbind.EcardBindVpnService"
Expand Down
22 changes: 22 additions & 0 deletions android/app/src/main/kotlin/club/geekpie/techpie/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import android.content.ContentValues
import android.content.Intent
import android.content.pm.PackageManager
import android.net.VpnService
import android.os.Build
import club.geekpie.techpie.island.FocusSequenceStore
import club.geekpie.techpie.island.IslandBridge
import club.geekpie.techpie.island.IslandCapabilityProbe
import club.geekpie.techpie.island.IslandNotificationFactory
import android.os.Handler
import android.os.Looper
import android.provider.CalendarContract
Expand All @@ -25,13 +30,18 @@ class MainActivity : FlutterActivity() {
private val mainHandler = Handler(Looper.getMainLooper())
private var ecardWidgets: EcardWidgetBridge? = null
private var ecardFeedback: EcardFeedback? = null
private var islandBridge: IslandBridge? = null

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
ecardWidgets = EcardWidgetBridge(this, flutterEngine.dartExecutor.binaryMessenger).also {
it.capture(intent, notifyFlutter = false)
}
ecardFeedback = EcardFeedback(this, flutterEngine.dartExecutor.binaryMessenger)
islandBridge = IslandBridge(
this, flutterEngine.dartExecutor.binaryMessenger,
IslandCapabilityProbe(this), IslandNotificationFactory(this, FocusSequenceStore(this)),
) { requestNotificationPermission() }

MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
Expand Down Expand Up @@ -82,9 +92,20 @@ class MainActivity : FlutterActivity() {
ecardWidgets = null
ecardFeedback?.dispose()
ecardFeedback = null
islandBridge?.dispose()
islandBridge = null
super.cleanUpFlutterEngine(flutterEngine)
}

private fun requestNotificationPermission(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return true
val granted = ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED
if (!granted) ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS), REQUEST_ISLAND_NOTIFICATION)
return granted
}

private fun handleStartEcardBindHijack(
call: MethodCall,
result: MethodChannel.Result,
Expand Down Expand Up @@ -333,6 +354,7 @@ class MainActivity : FlutterActivity() {
private companion object {
const val CALENDAR_IMPORTER_CHANNEL = "techpie/calendar_importer"
const val ECARD_BIND_CHANNEL = "techpie/ecard_bind"
const val REQUEST_ISLAND_NOTIFICATION = 48292
const val REQUEST_CALENDAR_PERMISSIONS = 48291
const val REQUEST_VPN_CONSENT = 0x0ECB
const val VPN_STATE_ATTEMPTS = 15
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package club.geekpie.techpie.island

import android.content.Context
import androidx.core.content.edit

/** Island update sequences must be monotonically increasing or the system drops the update. */
internal object FocusSequence {
fun next(last: Long, nowMs: Long): Long = maxOf(last + 1L, nowMs)
}

/**
* Per-notification island sequence numbers, following the wall clock.
*
* Frames refresh at most once a minute, so persisting every tick would be
* wasted I/O; persist only when the in-memory value has advanced by more than
* [PERSIST_STEP_MS] past what is on disk (guards against clock rollback).
*/
class FocusSequenceStore(context: Context) {
private val prefs = context.applicationContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
private val cache = mutableMapOf<Int, Long>()
private val persisted = mutableMapOf<Int, Long>()

@Synchronized
fun next(notifyId: Int, nowMs: Long = System.currentTimeMillis()): Long {
val key = "seq_$notifyId"
val last = cache[notifyId] ?: prefs.getLong(key, 0L)
val seq = FocusSequence.next(last, nowMs)
cache[notifyId] = seq
val lastWritten = persisted[notifyId] ?: prefs.getLong(key, 0L)
if (seq - lastWritten >= PERSIST_STEP_MS) {
persisted[notifyId] = seq
prefs.edit { putLong(key, seq) }
}
return seq
}

private companion object {
const val PREFS_NAME = "island_focus_seq"
const val PERSIST_STEP_MS = 30_000L
}
}
Loading
Loading