Skip to content

Commit

Permalink
Recover on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Oct 23, 2024
1 parent 0844528 commit 40af9c3
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 40 deletions.
12 changes: 3 additions & 9 deletions app/schemas/com.koalasat.pokey.database.AppDatabase/5.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "233b09174bbfc8635a2635b94e69c5c8",
"identityHash": "304dc2730bab233084f32790628da8ae",
"entities": [
{
"tableName": "notification",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `eventId` TEXT NOT NULL, `kind` INTEGER NOT NULL, `time` INTEGER NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `eventId` TEXT NOT NULL, `time` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
Expand All @@ -20,12 +20,6 @@
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "kind",
"columnName": "kind",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "time",
"columnName": "time",
Expand Down Expand Up @@ -56,7 +50,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '233b09174bbfc8635a2635b94e69c5c8')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '304dc2730bab233084f32790628da8ae')"
]
}
}
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Expand Down Expand Up @@ -39,6 +40,20 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".service.BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>

</manifest>
30 changes: 28 additions & 2 deletions app/src/main/java/com/koalasat/pokey/Pokey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package com.koalasat.pokey

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.koalasat.pokey.database.AppDatabase
import com.koalasat.pokey.models.EncryptedStorage.preferences
import com.koalasat.pokey.models.PrefKeys
Expand All @@ -24,6 +28,8 @@ class Pokey : Application() {
super.onCreate()
instance = this

updateIsEnabled(isForegroundServiceEnabled(this))

RelayPool.register(Client)
}

Expand All @@ -39,22 +45,42 @@ class Pokey : Application() {
NotificationsService::class.java,
),
)
NotificationsService.setRunningState(true)
saveForegroundServicePreference(this, true)
}

fun stopService() {
val intent = Intent(applicationContext, NotificationsService::class.java)
applicationContext.stopService(intent)
NotificationsService.setRunningState(false)
saveForegroundServicePreference(this, false)
}

companion object {
private val _isEnabled = MutableLiveData(false)
val isEnabled: LiveData<Boolean> get() = _isEnabled

@Volatile
private var instance: Pokey? = null

fun getInstance(): Pokey =
instance ?: synchronized(this) {
instance ?: Pokey().also { instance = it }
}

fun updateIsEnabled(value: Boolean) {
_isEnabled.value = value
}

private fun saveForegroundServicePreference(context: Context, value: Boolean) {
val sharedPreferences: SharedPreferences = context.getSharedPreferences("PokeyPreferences", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putBoolean("foreground_service_enabled", value)
editor.apply()
updateIsEnabled(value)
}

fun isForegroundServiceEnabled(context: Context): Boolean {
val sharedPreferences: SharedPreferences = context.getSharedPreferences("PokeyPreferences", Context.MODE_PRIVATE)
return sharedPreferences.getBoolean("foreground_service_enabled", false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ data class NotificationEntity(
@PrimaryKey(autoGenerate = true)
val id: Int,
val eventId: String,
val kind: Int,
val time: Long,
)
46 changes: 46 additions & 0 deletions app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.koalasat.pokey.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.koalasat.pokey.Pokey

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (!Pokey.isForegroundServiceEnabled(context)) return

if (intent.action == Intent.ACTION_PACKAGE_REPLACED && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
if (intent.dataString?.contains("com.greenart7c3.nostrsigner") == true) {
Log.d("BootReceiver", "Starting ConnectivityService ACTION_PACKAGE_REPLACED")
context.startForegroundService(
Intent(
context,
NotificationsService::class.java,
),
)
}
}

if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
Log.d("BootReceiver", "Starting ConnectivityService ACTION_MY_PACKAGE_REPLACED")
context.startForegroundService(
Intent(
context,
NotificationsService::class.java,
),
)
}

if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
Log.d("BootReceiver", "Starting ConnectivityService ACTION_BOOT_COMPLETED")
context.startForegroundService(
Intent(
context,
NotificationsService::class.java,
),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
Expand Down Expand Up @@ -270,7 +271,7 @@ class NotificationsService : Service() {

if (!event.hasVerifiedSignature()) return@launch

dao.insertNotification(NotificationEntity(0, event.id, event.kind, event.createdAt))
dao.insertNotification(NotificationEntity(0, event.id, event.createdAt))

var title = getString(R.string.unknown)
var text = ""
Expand Down Expand Up @@ -313,13 +314,4 @@ class NotificationsService : Service() {
}
return hexKey
}

companion object {
private val _isActive = MutableLiveData(false)
val isActive: LiveData<Boolean> get() = _isActive

fun setRunningState(running: Boolean) {
_isActive.value = running
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/koalasat/pokey/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.koalasat.pokey.Pokey
import com.koalasat.pokey.R
import com.koalasat.pokey.databinding.FragmentHomeBinding
import com.koalasat.pokey.service.NotificationsService
Expand Down Expand Up @@ -58,7 +59,7 @@ class HomeFragment : Fragment() {
}
}

NotificationsService.isActive.observe(viewLifecycleOwner) {
Pokey.isEnabled.observe(viewLifecycleOwner) {
if (it) {
val typedValue = TypedValue()
requireContext().theme.resolveAttribute(android.R.attr.colorButtonNormal, typedValue, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HomeViewModel : ViewModel() {
val npubInput: LiveData<String> get() = _npubInput

private val _serviceStart = MutableLiveData<Boolean>().apply {
value = NotificationsService.isActive.value
value = Pokey.isEnabled.value
}
val serviceStart: LiveData<Boolean> get() = _serviceStart

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.koalasat.pokey.R
import com.koalasat.pokey.service.NotificationsService
import com.vitorpamplona.ammolite.relays.Relay

class RelayListAdapter(private val items: List<Relay>) : RecyclerView.Adapter<RelayListAdapter.ViewHolder>() {
Expand All @@ -17,7 +16,7 @@ class RelayListAdapter(private val items: List<Relay>) : RecyclerView.Adapter<Re
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_relay_list, parent, false)
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_relay_item, parent, false)
return ViewHolder(view)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.koalasat.pokey.Pokey
import com.koalasat.pokey.R
import com.koalasat.pokey.databinding.FragmentRelaysBinding
import com.koalasat.pokey.service.NotificationsService
Expand Down Expand Up @@ -39,7 +40,7 @@ class RelaysFragment : Fragment() {
textView.text = it
}

NotificationsService.isActive.observe(viewLifecycleOwner) {
Pokey.isEnabled.observe(viewLifecycleOwner) {
textView.text = if (it) {
getString(R.string.relays)
} else {
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_relays_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="100"
android:viewportHeight="100"
android:width="800dp"
android:height="800dp">
<group
android:scaleX="1.597919"
android:scaleY="1.597919"
android:translateX="-31.30763"
android:translateY="-31.43249">
<path
android:pathData="M73.9 27c-3.9 0 -7.1 3.2 -7.1 7.1 0 0.6 0.1 1.1 0.2 1.6l-6.7 4.4c-2.1 -2.4 -5.1 -4 -8.6 -4 -2.6 0 -5 0.9 -6.9 2.4l-11 -7.6C34.5 28 33 24.8 30.2 23.5 26.9 22 23 23.4 21.5 26.6c-1.5 3.2 -0.1 7.2 3.1 8.7 1.7 0.8 3.6 0.8 5.3 0.1l11.3 7.9c-0.5 1.2 -0.8 2.6 -0.8 4 0 0.8 0.1 1.5 0.2 2.3l-6.9 2.5c-0.9 -1 -2.1 -1.8 -3.6 -2.2 -3.9 -1.1 -8 1.2 -9.1 5.2 -1.1 3.9 1.2 8 5.2 9.1 3.9 1.1 8 -1.2 9.1 -5.2 0.1 -0.4 0.2 -0.8 0.2 -1.2L43.4 55c2.1 2.2 5 3.6 8.2 3.6 0.3 0 0.6 0 0.9 0l1.1 5.6c-2.1 1.7 -3.3 4.4 -3 7.2 0.4 4.6 4.5 7.9 9.1 7.5 4.6 -0.4 7.9 -4.5 7.5 -9.1 -0.4 -4.1 -3.7 -7.2 -7.7 -7.5l-1.2 -5.9c2.8 -2 4.7 -5.4 4.7 -9.1 0 -0.6 -0.1 -1.2 -0.1 -1.7l7.9 -5.2c1 0.5 2.1 0.8 3.2 0.8 3.9 0 7.1 -3.2 7.1 -7.1C81 30.2 77.8 27 73.9 27ZM51.7 54.6c-4 0 -7.2 -3.2 -7.2 -7.2 0 -4 3.2 -7.2 7.2 -7.2 4 0 7.2 3.2 7.2 7.2 0 3.9 -3.2 7.2 -7.2 7.2z"
android:fillColor="#000000" />
<path
android:pathData="M55 47.4A3.2 3.2 0 0 1 48.6 47.4A3.2 3.2 0 0 1 55 47.4Z"
android:fillColor="#000000" />
</group>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/fragment_relay_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginBottom="16dp"/>
</LinearLayout>
12 changes: 0 additions & 12 deletions app/src/main/res/layout/fragment_relay_list.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_relays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
android:id="@+id/relays"
android:layout_width="405dp"
android:layout_height="638dp"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/bottom_nav_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<item
android:id="@+id/navigation_relays"
android:icon="@drawable/ic_dashboard_black_24dp"
android:icon="@drawable/ic_relays_black_24dp"
android:title="@string/title_relays" />

<item
Expand Down

0 comments on commit 40af9c3

Please sign in to comment.