Skip to content

Commit

Permalink
Add TileService and update tile
Browse files Browse the repository at this point in the history
  • Loading branch information
cliambrown committed Dec 5, 2021
2 parents 48ff3b9 + 953be16 commit 926c891
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 41 deletions.
Binary file not shown.
17 changes: 15 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
android:supportsRtl="true"
android:theme="@style/Theme.EasyNoise">

<service
android:name=".QSTileService"
android:enabled="true"
android:exported="true"
android:label="@string/app_name"
android:icon="@drawable/notification_icon"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
<meta-data android:name="android.service.quicksettings.ACTIVE_TILE"
android:value="true" />
</service>

<receiver
android:name=".OutsidePauseReceiver"
android:enabled="true"
Expand All @@ -27,7 +41,6 @@
<action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED" />
</intent-filter>
</receiver>

<receiver
android:name=".EasyNoiseWidget"
android:exported="true">
Expand Down Expand Up @@ -80,10 +93,10 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
6 changes: 0 additions & 6 deletions app/src/main/java/com/cliambrown/easynoise/EasyNoiseWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import android.widget.RemoteViews
import com.cliambrown.easynoise.helpers.*
import android.app.PendingIntent
import android.content.ComponentName
import android.os.Build
import android.util.Log

/**
* Implementation of App Widget functionality.
Expand All @@ -37,7 +35,6 @@ class EasyNoiseWidget : AppWidgetProvider() {
}

override fun onReceive(context: Context?, intent: Intent?) {
Log.i("clb-info", "EasyNoiseWidget onReceive")
val action = intent?.action
when (action) {
TOGGLE_PLAY -> togglePlay(context)
Expand All @@ -48,15 +45,13 @@ class EasyNoiseWidget : AppWidgetProvider() {
}

fun togglePlay(context: Context?) {
Log.i("clb-info", "EasyNoiseWidget togglePlay")
if (context == null) {
return
}
Util.startPlayerService(context, TOGGLE_PLAY)
}

fun setPlaying(context: Context?, isPlaying: Boolean) {
Log.i("clb-info", "EasyNoiseWidget setPlaying")
if (context == null) {
return
}
Expand All @@ -75,7 +70,6 @@ internal fun updateAppWidget(
appWidgetId: Int,
_isPlaying: Boolean? = null
) {
Log.i("clb-info", "EasyNoiseWidget updateAppWidget")
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.easy_noise_widget)

Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/cliambrown/easynoise/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.Menu
import android.view.animation.Animation
import androidx.constraintlayout.widget.ConstraintLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package com.cliambrown.easynoise
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.cliambrown.easynoise.helpers.*

class NotificationReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
Log.i("clb-info", "NotificationReceiver onReceive")
val action = intent.action
val actions = arrayOf(PLAY, PAUSE, TOGGLE_PLAY, DISMISS, VOLUME_UP, VOLUME_DOWN)
if (!actions.contains(action) || action == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.util.Log
import android.view.View
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.cliambrown.easynoise.helpers.*

class NotificationUtils(base: Context?) : ContextWrapper(base) {
Expand Down
37 changes: 10 additions & 27 deletions app/src/main/java/com/cliambrown/easynoise/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package com.cliambrown.easynoise

import android.app.Activity
import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.SoundPool
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import android.service.quicksettings.TileService
import com.cliambrown.easynoise.helpers.*
import android.widget.Toast
import kotlin.math.roundToInt
Expand Down Expand Up @@ -43,7 +45,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun registerClient(activity: Activity) {
Log.i("clb-info", "PlayerService registerClient")
mActivity = activity as Callbacks
}

Expand All @@ -57,7 +58,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

override fun onCreate() {
Log.i("clb-info", "PlayerService onCreate")
wasPlaying = getPrefs().getBoolean("wasPlaying", false)
outsidePauseReceiver = OutsidePauseReceiver()
val filter = IntentFilter()
Expand All @@ -71,15 +71,12 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i("clb-info", "PlayerService onStartCommand")

if (intent == null) {
return START_NOT_STICKY
}
Log.i("clb-info", "PlayerService test1")

val action = intent.action
Log.i("clb-info", "action = " + action)
when (action) {
PLAY -> play()
PAUSE -> pause()
Expand All @@ -100,20 +97,15 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
if (isPlaying) pause(false)
}
HEADPHONES_CONNECTED -> {
Log.i("clb-info", "PlayerService test2")
audioIsNoisy = false
Log.i("clb-info", "wasPlaying = " + wasPlaying.toString())
Log.i("clb-info", "onPhoneCall = " + onPhoneCall.toString())
if (wasPlaying && !onPhoneCall) play(false)
}
}
Log.i("clb-info", "PlayerService test3")

return START_NOT_STICKY
}

fun createNotification(isPlaying: Boolean) {
Log.i("clb-info", "PlayerService createNotification")
if (notificationUtils == null) {
notificationUtils = NotificationUtils(this)
}
Expand All @@ -122,7 +114,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun updateWidget(toIsPlaying: Boolean) {
Log.i("clb-info", "PlayerService updateWidget")
val newIntent = Intent(this, EasyNoiseWidget::class.java)
if (toIsPlaying) {
newIntent.setAction(SET_PLAYING)
Expand All @@ -133,7 +124,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

override fun onDestroy() {
Log.i("clb-info", "PlayerService onDestroy")
pause()
unregisterReceiver(outsidePauseReceiver)
if (streamID != null && streamID!! > 0) {
Expand All @@ -147,7 +137,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun initSoundPool() {
Log.i("clb-info", "PlayerService initSoundPool")
if (isLoading) return
isLoading = true
if (soundPool == null) {
Expand All @@ -166,10 +155,9 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun loadNoise() {
Log.i("clb-info", "PlayerService loadNoise")
val noise = getPrefs().getString("noise", "fuzz")
currentNoise = noise
var resource: Int = when (noise) {
val resource: Int = when (noise) {
resources.getString(R.string.fuzz) -> R.raw.fuzz
resources.getString(R.string.gray) -> R.raw.grey_noise
resources.getString(R.string.gray_2) -> R.raw.grey_noise_2
Expand All @@ -185,7 +173,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

override fun onLoadComplete(pSoundPool: SoundPool, pSampleID: Int, status: Int) {
Log.i("clb-info", "PlayerService onLoadComplete")
streamLoaded = (soundID > 0)
isLoading = false
if (streamLoaded) {
Expand All @@ -199,19 +186,16 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun playLoaded() {
Log.i("clb-info", "PlayerService playLoaded")
val floatVol = updateVolume()
streamID = soundPool?.play(soundID, floatVol, floatVol, 1, -1, 1.0F)
isPlaying = true
}

fun getIsPlaying(): Boolean {
Log.i("clb-info", "PlayerService getIsPlaying")
return isPlaying
}

fun togglePlay() {
Log.i("clb-info", "PlayerService togglePlay")
if (!isPlaying && !isLoading) {
play()
} else {
Expand All @@ -220,7 +204,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun play(doUpdatePref: Boolean = true) {
Log.i("clb-info", "PlayerService play")
lastAction = "play"
if (streamLoaded) {
playLoaded()
Expand All @@ -231,15 +214,13 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun pause(doUpdatePref: Boolean = true) {
Log.i("clb-info", "PlayerService pause")
lastAction = "pause"
soundPool?.autoPause()
isPlaying = false
onPlayChanged(false, doUpdatePref)
}

fun onPlayChanged(toPlaying: Boolean, doUpdatePref: Boolean) {
Log.i("clb-info", "PlayerService onPlayChanged")
mActivity?.updateClient(if (toPlaying) PLAY else PAUSE)
updateWidget(toPlaying)
createNotification(toPlaying)
Expand All @@ -248,10 +229,15 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
wasPlaying = toPlaying
getPrefs().edit().putBoolean("wasPlaying", toPlaying).apply()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
TileService.requestListeningState(
this,
ComponentName(this, QSTileService::class.java.getName())
)
}
}

fun dismiss() {
Log.i("clb-info", "PlayerService dismiss")
mActivity?.updateClient(DISMISS)
pause()
stopForeground(true)
Expand All @@ -260,15 +246,13 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {

@JvmName("getPrefs1")
fun getPrefs(): SharedPreferences {
Log.i("clb-info", "PlayerService getPrefs")
if (prefs == null) {
prefs = getSharedPreferences(applicationContext.packageName, 0)
}
return prefs!!
}

fun updateVolume(adjustBy: Int? = null): Float {
Log.i("clb-info", "PlayerService updateVolume")
var volume = getPrefs().getInt("volume", 50).toDouble()
val maxVolume = 100.0
if (adjustBy != null) {
Expand All @@ -286,7 +270,6 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}

fun noiseChanged() {
Log.i("clb-info", "PlayerService noiseChanged")
val newNoise = getPrefs().getString("noise", "fuzz")
if (newNoise.equals(currentNoise)) return
val tempIsPlaying = isPlaying
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/cliambrown/easynoise/QSTileService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cliambrown.easynoise

import android.graphics.drawable.Icon
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.annotation.RequiresApi
import com.cliambrown.easynoise.helpers.*

@RequiresApi(Build.VERSION_CODES.N)
class QSTileService : TileService() {

override fun onTileAdded() {
super.onTileAdded()
updateTile()
}

override fun onClick() {
super.onClick()
if(qsTile.state == Tile.STATE_INACTIVE) {
Util.startPlayerService(applicationContext, PLAY)
} else {
Util.startPlayerService(applicationContext, PAUSE)
}
}

override fun onStartListening() {
super.onStartListening()
updateTile()
}

fun updateTile() {
val isPlaying = getSharedPreferences(applicationContext.packageName, 0)
.getBoolean("isPlaying", false)
val resource = if (isPlaying) R.drawable.notification_icon else R.drawable.paused_notification_icon
qsTile.setIcon(Icon.createWithResource(this, resource))
qsTile.state = if (isPlaying) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
qsTile.updateTile()
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 926c891

Please sign in to comment.