Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
cliambrown committed Dec 5, 2021
2 parents b1bd45b + b41a199 commit 0c69c04
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 30 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>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.widget.RemoteViews
import com.cliambrown.easynoise.helpers.*
import android.app.PendingIntent
import android.content.ComponentName
import android.os.Build

/**
* Implementation of App Widget functionality.
Expand Down Expand Up @@ -49,7 +48,7 @@ class EasyNoiseWidget : AppWidgetProvider() {
if (context == null) {
return
}
Util.startPlayerService(context, TOGGLE_PLAY)
PlayerService.start(context, TOGGLE_PLAY)
}

fun setPlaying(context: Context?, isPlaying: Boolean) {
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/cliambrown/easynoise/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cliambrown.easynoise

import android.Manifest
import android.animation.AnimatorListenerAdapter
import android.content.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Expand All @@ -17,7 +16,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 @@ -11,7 +11,6 @@ import android.os.Build
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
22 changes: 12 additions & 10 deletions app/src/main/java/com/cliambrown/easynoise/OutsidePauseReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,46 @@ class OutsidePauseReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

val action = intent.action
var playerAction: String? = null

when (action) {
val playerAction = when (intent.action) {
PHONE_STATE -> {
// Phone call start/stop
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
val states = arrayOf("IDLE", "RINGING", "OFFHOOK")
if (!states.contains(state)) return
if (state == "IDLE") {
playerAction = CALL_ENDED
CALL_ENDED
} else {
playerAction = CALL_STARTED
CALL_STARTED
}
}
AudioManager.ACTION_AUDIO_BECOMING_NOISY -> {
// Headphones unplugged / disconnected
playerAction = AUDIO_BECOMING_NOISY
AUDIO_BECOMING_NOISY
}
HEADSET_PLUG -> {
// Wired headset monitoring
val state = intent.getIntExtra("state", 0)
if (state > 0) playerAction = HEADPHONES_CONNECTED
if (state > 0) HEADPHONES_CONNECTED
else null
}
HEADSET_STATE_CHANGED -> {
// Bluetooth monitoring
val state = intent.getIntExtra("android.bluetooth.headset.extra.STATE", 0)
if (state == 2) playerAction = HEADPHONES_CONNECTED
if (state == 2) HEADPHONES_CONNECTED
else null
}
CONNECTION_STATE_CHANGED -> {
// Bluetooth, works for Ice Cream Sandwich
val state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 0)
if (state == 2) playerAction = HEADPHONES_CONNECTED
if (state == 2) HEADPHONES_CONNECTED
else null
}
else -> null
}

if (playerAction != null) {
PlayerService.start(context, playerAction)
}

}
}
30 changes: 19 additions & 11 deletions app/src/main/java/com/cliambrown/easynoise/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.cliambrown.easynoise
import android.app.Activity
import android.app.Service
import android.content.Context
import android.content.ComponentName
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
Expand All @@ -12,6 +13,7 @@ import android.media.SoundPool
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.TileService
import com.cliambrown.easynoise.helpers.*
import android.widget.Toast
import kotlin.math.roundToInt
Expand All @@ -38,6 +40,16 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {

var outsidePauseReceiver: OutsidePauseReceiver? = null

companion object {
fun start(context: Context, action: String): Boolean {
Intent(context, PlayerService::class.java).setAction(action).run {
if (Build.VERSION.SDK_INT < 26) context.startService(this)
else context.startForegroundService(this)
}
return true
}
}

inner class LocalBinder : Binder() {
// Return this instance of LocalService so clients can call public methods
fun getService(): PlayerService = this@PlayerService
Expand Down Expand Up @@ -156,7 +168,7 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
fun 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 Down Expand Up @@ -228,6 +240,12 @@ 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() {
Expand Down Expand Up @@ -274,14 +292,4 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener {
}
if (tempIsPlaying) play(false)
}

companion object {
fun start(context: Context, action: String): Boolean {
Intent(context, PlayerService::class.java).setAction(action).run {
if (Build.VERSION.SDK_INT < 26) context.startService(this)
else context.startForegroundService(this)
}
return true
}
}
}
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) {
PlayerService.start(applicationContext, PLAY)
} else {
PlayerService.start(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()
}
}
1 change: 0 additions & 1 deletion app/src/main/res/menu/main_toolbar.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_info"
android:icon="@drawable/ic_baseline_info_24"
Expand Down
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 0c69c04

Please sign in to comment.