diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ae388c2..0897082 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,16 +4,15 @@ diff --git a/CatFlipScreen.apk b/CatFlipScreen.apk index e1e3914..768e4d7 100644 Binary files a/CatFlipScreen.apk and b/CatFlipScreen.apk differ diff --git a/app/release/app-release.apk b/app/release/app-release.apk index e1e3914..768e4d7 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5cc194b..a80e801 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -43,6 +43,9 @@ diff --git a/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt b/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt index ff762a9..5708d4e 100644 --- a/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt +++ b/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt @@ -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) - } } \ No newline at end of file diff --git a/app/src/main/java/net/mezimmah/catflipscreen/MainActivity.kt b/app/src/main/java/net/mezimmah/catflipscreen/MainActivity.kt index f5eba06..24c2e59 100644 --- a/app/src/main/java/net/mezimmah/catflipscreen/MainActivity.kt +++ b/app/src/main/java/net/mezimmah/catflipscreen/MainActivity.kt @@ -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() { diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 7e46819..927a8c8 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -18,21 +18,10 @@ - + - - - - - \ No newline at end of file