Skip to content

Commit

Permalink
fixed handling for notification permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
kl3jvi committed Aug 8, 2023
1 parent 11d6812 commit 6ac3a7f
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 149 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.kl3jvi.sysinfo"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
Expand Down Expand Up @@ -42,11 +42,11 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_19
targetCompatibility JavaVersion.VERSION_19
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '19'
jvmTarget = '17'
}
buildFeatures {
viewBinding true
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
Expand Down
63 changes: 52 additions & 11 deletions app/src/main/java/com/kl3jvi/sysinfo/SysInfoApplication.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.kl3jvi.sysinfo

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Color
import android.os.Build
import android.util.Log
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import com.getkeepsafe.relinker.ReLinker
import com.kl3jvi.sysinfo.data.provider.CpuDataProvider
import com.kl3jvi.sysinfo.di.allModules
import com.kl3jvi.sysinfo.utils.Settings
import com.kl3jvi.sysinfo.utils.thenCatching
import com.kl3jvi.sysinfo.workers.SystemMonitorWorker
import com.microsoft.appcenter.AppCenter
Expand All @@ -20,13 +25,16 @@ import java.util.concurrent.TimeUnit

class SysInfoApplication : Application(), KoinComponent {
private val cpuDataProvider: CpuDataProvider by inject()
private val notificationManager: NotificationManager by inject()
private val settings: Settings by inject()

override fun onCreate() {
super.onCreate()
startKoin()
initNativeCpuInfo()
addSystemMonitor()
addMSApp()
createNotificationChannel()
}

private fun addMSApp() {
Expand All @@ -39,10 +47,14 @@ class SysInfoApplication : Application(), KoinComponent {
}

private fun addSystemMonitor() {
val workRequest = PeriodicWorkRequestBuilder<SystemMonitorWorker>(15, TimeUnit.MINUTES)
.build()

WorkManager.getInstance(this).enqueue(workRequest)
val shouldMonitor = settings.systemMonitoringState
if (shouldMonitor) {
val workRequest = PeriodicWorkRequestBuilder<SystemMonitorWorker>(
15,
TimeUnit.MINUTES
).build()
WorkManager.getInstance(this).enqueue(workRequest)
}
}

private fun startKoin() = startKoin {
Expand All @@ -51,17 +63,46 @@ class SysInfoApplication : Application(), KoinComponent {
}

private fun initNativeCpuInfo() {
ReLinker.loadLibrary(this, LIB_NAME)
.thenCatching {
cpuDataProvider.initLibrary()
}.onSuccess {
Log.i("Initialised CpuInfo", "successfully")
}.onFailure {
Log.e("Failed cpu-info", "initialisation", it)
ReLinker.loadLibrary(this, LIB_NAME).thenCatching {
cpuDataProvider.initLibrary()
}.onSuccess {
Log.i("Initialised CpuInfo", "successfully")
}.onFailure {
Log.e("Failed cpu-info", "initialisation", it)
}
}

private fun createNotificationChannel() {
// Create the NotificationChannel, but only for API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = CHANNEL_NAME // Define these strings in your res/values/strings.xml
val descriptionText = CHANNEL_DESC
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(
CHANNEL_ID,
name,
importance
).apply {
description = descriptionText
enableLights(true)
lightColor = Color.RED
enableVibration(true)
vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
setBypassDnd(true)
setShowBadge(true)
}

// Register the channel with the system
notificationManager.createNotificationChannel(channel)
}
}

companion object {
private const val CHANNEL_ID = "SystemMonitorChannel"
private const val CHANNEL_NAME = "System Monitor"
private const val CHANNEL_DESC = "Notifications for system monitoring"

const val LIB_NAME = "cpuinfo-libs"
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/kl3jvi/sysinfo/data/model/CPULoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enum class CPULoad {
Low,
Medium,
High
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/kl3jvi/sysinfo/data/model/RamLoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enum class RamLoad {
Low,
Medium,
High
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,3 @@ class CpuDataProvider(
private const val CPU_INFO_DIRECTORY = "/sys/devices/system/cpu/"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,3 @@ class RamDataProvider(
.distinctUntilChanged()
.flowOn(Dispatchers.IO)
}


40 changes: 29 additions & 11 deletions app/src/main/java/com/kl3jvi/sysinfo/utils/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.kl3jvi.sysinfo.utils

import android.content.SharedPreferences
import android.util.Log
import androidx.annotation.StringRes
import androidx.core.content.edit
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat

/**
* Sets the callback to be invoked when this preference is changed by the user (but before
* the internal state has been updated). Allows the type of the preference to be specified.
* If the new value doesn't match the preference type the listener isn't called.
*
* @param onPreferenceChangeListener The callback to be invoked
*/
inline fun <reified T> Preference.setOnPreferenceChangeListener(
crossinline onPreferenceChangeListener: (Preference, T) -> Boolean
) {
Expand All @@ -19,9 +15,31 @@ inline fun <reified T> Preference.setOnPreferenceChangeListener(
}
}

/**
* Find a preference with the corresponding key and throw if it does not exist.
* @param preferenceId Resource ID from preference_keys
*/
fun <T : Preference> PreferenceFragmentCompat.requirePreference(@StringRes preferenceId: Int) =
requireNotNull(findPreference<T>(getPreferenceKey(preferenceId)))

fun <T : Preference> PreferenceFragmentCompat.configurePreference(
@StringRes preferenceId: Int,
preferences: SharedPreferences,
clickListener: Preference.OnPreferenceClickListener? = null,
block: T.() -> Unit = {}
): T {
val preference = requirePreference<T>(preferenceId)
preference.block()

preference.setOnPreferenceChangeListener<Any> { _, newValue ->
val key = getPreferenceKey(preferenceId)
preferences.edit {
Log.e("Changed Val", newValue.toString())
when (newValue) {
is Boolean -> putBoolean(key, newValue)
is Int -> putLong(key, newValue.toLong() * 1000)
is String -> putString(key, newValue)
}
}
true
}

preference.onPreferenceClickListener = clickListener
return preference
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/kl3jvi/sysinfo/utils/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList

fun <T> Flow<T>.ifChanged() = ifChanged { it }

Expand Down Expand Up @@ -113,3 +116,11 @@ fun <T, R> Flow<T>.ifAnyChanged(transform: (T) -> Array<R>): Flow<T> {
}
}
}

suspend fun <T> Flow<T>.scanAndLimit(limit: Int): List<T> {
return scan(emptyList<T>()) { accumulator, value ->
(accumulator + value).takeLast(limit)
}.take(limit)
.toList()
.flatten()
}
15 changes: 13 additions & 2 deletions app/src/main/java/com/kl3jvi/sysinfo/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.SharedPreferences
import com.example.sysinfo.R
import com.kl3jvi.sysinfo.data.local.PreferencesHolder
import com.kl3jvi.sysinfo.data.local.booleanPreference
import com.kl3jvi.sysinfo.data.local.intPreference
import com.kl3jvi.sysinfo.data.local.longPreference

Expand All @@ -22,15 +23,25 @@ class Settings(
default = CPU_REFRESH_RATE_DEFAULT
)

val ramWarningThreshold by intPreference(
val ramThresholdPercentage by longPreference(
appContext.getPreferenceKey(R.string.cpu_frequency_pref),
default = RAM_WARNING_FREQUENCY_DEFAULT
)

val monitoringPeriod by longPreference(
appContext.getPreferenceKey(R.string.monitoring_period),
default = 60_000L // 1minute
)

val systemMonitoringState by booleanPreference(
appContext.getPreferenceKey(R.string.enable_monitoring_pref),
default = false
)

companion object {
const val RAM_REFRESH_RATE_DEFAULT = 5000L
const val CPU_REFRESH_RATE_DEFAULT = 2000L

const val RAM_WARNING_FREQUENCY_DEFAULT = 40
const val RAM_WARNING_FREQUENCY_DEFAULT = 40L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kl3jvi.sysinfo.view.activities

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.navigation.NavController
import androidx.navigation.Navigation.findNavController
import androidx.navigation.ui.NavigationUI.setupActionBarWithNavController
Expand All @@ -15,6 +16,7 @@ class MainActivity : AppCompatActivity(), KoinComponent {
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down
Loading

0 comments on commit 6ac3a7f

Please sign in to comment.