fix bug with battery stat not updating

This commit is contained in:
Talmid of Levi 2023-12-18 14:18:19 -05:00
parent 0183efa97a
commit 180c2b8cf6
7 changed files with 69 additions and 81 deletions

5
.idea/gradle.xml generated
View File

@ -4,16 +4,15 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="jbr-17" />
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="resolveExternalAnnotations" value="false" />
</GradleProjectSettings>
</option>
</component>

Binary file not shown.

Binary file not shown.

View File

@ -43,6 +43,9 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:showOnLockScreen="true"
android:theme="@style/Theme.AppCompat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -6,25 +6,82 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.AudioManager
import android.media.MediaRouter
import android.os.IBinder
import android.os.SystemClock
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
class FlipScreenService : Service() {
override fun onBind(intent: Intent): IBinder? {
TODO("Return the communication channel to the service.")
return null
}
override fun onCreate() {
super.onCreate()
registerReceivers()
}
private fun registerReceivers() {
val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(contxt: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_POWER_CONNECTED,
Intent.ACTION_POWER_DISCONNECTED,
Intent.ACTION_BATTERY_CHANGED -> {
LocalBroadcastManager.getInstance(contxt)
.sendBroadcast(Intent(CatFlipScreenApplication.BROADCAST_BATTERY_CHANGED))
}
AudioManager.RINGER_MODE_CHANGED_ACTION -> {
LocalBroadcastManager.getInstance(contxt)
.sendBroadcast(Intent(CatFlipScreenApplication.BROADCAST_AUDIO_MODE_CHANGED))
}
}
}
}
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_POWER_CONNECTED))
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_POWER_DISCONNECTED))
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
this.registerReceiver(broadCastReceiver, IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION))
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
showNotification()
showPresentationScreen()
return super.onStartCommand(intent, flags, startId)
val input = intent!!.getStringExtra("inputExtra")
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
)
val notification: Notification = NotificationCompat.Builder(this, CatFlipScreenApplication.CHANNEL_ID)
.setContentTitle("Auto Start Service")
.setContentText(input)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentIntent(pendingIntent)
.build()
val mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
CatFlipScreenApplication.CHANNEL_ID,
CatFlipScreenApplication.CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
mNotificationManager.createNotificationChannel(channel)
NotificationCompat.Builder(this, CatFlipScreenApplication.CHANNEL_ID)
startForeground(1, notification)
return START_NOT_STICKY
}
override fun onTaskRemoved(rootIntent: Intent?) {
@ -57,33 +114,4 @@ class FlipScreenService : Service() {
)
}
}
private fun showNotification() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
CatFlipScreenApplication.CHANNEL_ID,
CatFlipScreenApplication.CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH
)
channel.description = "Cat Flip Screen Service"
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE)
}
val builder: Notification.Builder = Notification.Builder(
this,
CatFlipScreenApplication.CHANNEL_ID
)
val notification = builder
.setContentTitle("Cat Flip Screen")
.setContentText("For making the external display work.")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.build()
startForeground(1, notification)
}
}

View File

@ -1,14 +1,9 @@
package net.mezimmah.catflipscreen
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.media.AudioManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceFragmentCompat
@ -24,32 +19,8 @@ class MainActivity : AppCompatActivity() {
.commit()
}
startForegroundService(Intent(this, FlipScreenService::class.java))
registerReceivers()
}
private fun registerReceivers() {
val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(contxt: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_POWER_CONNECTED,
Intent.ACTION_POWER_DISCONNECTED,
Intent.ACTION_BATTERY_CHANGED -> {
LocalBroadcastManager.getInstance(contxt)
.sendBroadcast(Intent(CatFlipScreenApplication.BROADCAST_BATTERY_CHANGED))
}
AudioManager.RINGER_MODE_CHANGED_ACTION -> {
LocalBroadcastManager.getInstance(contxt)
.sendBroadcast(Intent(CatFlipScreenApplication.BROADCAST_AUDIO_MODE_CHANGED))
}
}
}
}
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_POWER_CONNECTED))
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_POWER_DISCONNECTED))
this.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
this.registerReceiver(broadCastReceiver, IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION))
}
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
@ -58,8 +29,6 @@ class MainActivity : AppCompatActivity() {
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?,
key: String?) {
// if (key == "enable_display") {
// }
}
override fun onResume() {

View File

@ -18,21 +18,10 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/time_header">
<Preference
app:selectable="false"
app:enabled="true"
app:title="Cat Flip Screen"
app:summary="This app makes the external screen to show the clock, batter, sound profile and connectivity status." />
<SwitchPreferenceCompat
app:key="show_time"
app:defaultValue="true"
app:enabled="false"
app:title="@string/show_time_title" />
<SwitchPreferenceCompat
app:dependency="show_time"
app:key="attachment"
app:isPreferenceVisible="false"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/show_time_title" />
</PreferenceCategory>
</PreferenceScreen>