Skip to content

Commit

Permalink
Restores action on incoming call
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsos committed Nov 6, 2020
1 parent 0f9c905 commit f02a0b5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 32 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<uses-sdk tools:overrideLibrary="timber.log" />

Expand Down Expand Up @@ -97,8 +98,6 @@
android:value="com.kelsos.mbrc.ui.navigation.main.MainActivity" />
</activity>

<activity android:name=".ui.navigation.library.search.SearchResultsActivity" />

<activity
android:name=".ui.activities.SplashActivity"
android:noHistory="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RemoteService : Service(), ForegroundHooks {
super.onCreate()
scope = Toothpick.openScope(application)
Toothpick.inject(this, scope)
this.registerReceiver(receiver, receiver.filter())
this.registerReceiver(receiver, receiver.filter(this))
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kelsos.mbrc.ui.preferences
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import androidx.core.app.ActivityCompat
Expand All @@ -12,8 +13,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.kelsos.mbrc.BuildConfig
import com.kelsos.mbrc.R
import com.kelsos.mbrc.constants.UserInputEventType
import com.kelsos.mbrc.events.MessageEvent
import com.kelsos.mbrc.controller.RemoteService
import com.kelsos.mbrc.events.bus.RxBus
import com.kelsos.mbrc.logging.FileLoggingTree
import com.kelsos.mbrc.ui.connection_manager.ConnectionManagerActivity
Expand Down Expand Up @@ -74,17 +74,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
Timber.d(e, "failed")
}

val showNotification =
findPreference<CheckBoxPreference>(resources.getString(R.string.settings_key_notification_control))

showNotification?.setOnPreferenceChangeListener { _, newValue ->
val value = newValue as Boolean
if (!value) {
bus!!.post(MessageEvent(UserInputEventType.CancelNotification))
}
true
}

val mLicense = findPreference<Preference>(resources.getString(R.string.settings_key_license))
mLicense?.setOnPreferenceClickListener {
showLicenseDialog()
Expand All @@ -97,11 +86,7 @@ class SettingsFragment : PreferenceFragmentCompat() {


private fun requestPhoneStatePermission() {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.READ_PHONE_STATE),
REQUEST_CODE
)
requestPermissions(arrayOf(Manifest.permission.READ_PHONE_STATE), REQUEST_CODE)
}

private fun hasPhonePermission(): Boolean {
Expand Down Expand Up @@ -143,6 +128,31 @@ class SettingsFragment : PreferenceFragmentCompat() {
this.bus = bus
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (
requestCode == REQUEST_CODE &&
grantResults.isNotEmpty() &&
grantResults.first() == PackageManager.PERMISSION_GRANTED
) {
requireActivity().run {
Timber.v("Restarting service")
stopService(Intent(this, RemoteService::class.java))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(Intent(this, RemoteService::class.java))
} else {
startService(Intent(this, RemoteService::class.java))
}
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

}

companion object {

private const val REQUEST_CODE = 15
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.kelsos.mbrc.utilities

import android.Manifest.permission.READ_PHONE_STATE
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.content.pm.PackageManager
import android.telephony.TelephonyManager
import com.kelsos.mbrc.constants.Protocol
import com.kelsos.mbrc.constants.ProtocolEventType
import com.kelsos.mbrc.constants.UserInputEventType
import com.kelsos.mbrc.controller.RemoteService
import com.kelsos.mbrc.data.UserAction
import com.kelsos.mbrc.events.MessageEvent
import com.kelsos.mbrc.events.bus.RxBus
import timber.log.Timber
import javax.inject.Inject

class RemoteBroadcastReceiver
Expand All @@ -26,19 +29,34 @@ constructor(
* intent fired by the ReplyHandler or the PHONE_STATE intent fired by the
* Android operating system.
*/
fun filter(): IntentFilter {
val filter = IntentFilter()
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
filter.addAction(RemoteViewIntentBuilder.REMOTE_PLAY_PRESSED)
filter.addAction(RemoteViewIntentBuilder.REMOTE_NEXT_PRESSED)
filter.addAction(RemoteViewIntentBuilder.REMOTE_CLOSE_PRESSED)
filter.addAction(RemoteViewIntentBuilder.REMOTE_PREVIOUS_PRESSED)
filter.addAction(RemoteViewIntentBuilder.CANCELLED_NOTIFICATION)
return filter
fun filter(context: Context): IntentFilter {
val hasPermission =
context.checkSelfPermission(READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
val handleCallAction = settingsManager.getCallAction() != SettingsManager.NONE

return IntentFilter().apply {
if (hasPermission && handleCallAction) {
addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
addAction(RemoteViewIntentBuilder.REMOTE_PLAY_PRESSED)
addAction(RemoteViewIntentBuilder.REMOTE_NEXT_PRESSED)
addAction(RemoteViewIntentBuilder.REMOTE_CLOSE_PRESSED)
addAction(RemoteViewIntentBuilder.REMOTE_PREVIOUS_PRESSED)
addAction(RemoteViewIntentBuilder.CANCELLED_NOTIFICATION)
}
}
}

override fun onReceive(context: Context, intent: Intent) {
Timber.v("Incoming %s", intent)
when (intent.action) {
TelephonyManager.ACTION_PHONE_STATE_CHANGED -> {
Timber.v("Incoming")
val bundle = intent.extras ?: return
val state = bundle.getString(TelephonyManager.EXTRA_STATE)
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state!!, ignoreCase = true)) {
handleRinging()
}
}
RemoteViewIntentBuilder.REMOTE_PLAY_PRESSED -> {
postAction(UserAction(Protocol.PlayerPlayPause, true))
}
Expand All @@ -57,6 +75,14 @@ constructor(
}
}

private fun handleRinging() {
when (settingsManager.getCallAction()) {
SettingsManager.PAUSE -> postAction(UserAction(Protocol.PlayerPause, true))
SettingsManager.STOP -> postAction(UserAction(Protocol.PlayerStop, true))
SettingsManager.REDUCE -> bus.post(MessageEvent(ProtocolEventType.ReduceVolume))
}
}

private fun postAction(data: UserAction) {
bus.post(MessageEvent(ProtocolEventType.UserAction, data))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.kelsos.mbrc.utilities

import android.app.Application
import android.content.SharedPreferences
import androidx.annotation.StringDef
import com.kelsos.mbrc.R
import com.kelsos.mbrc.logging.FileLoggingTree
import com.kelsos.mbrc.utilities.RemoteUtils.getVersionCode
import com.kelsos.mbrc.utilities.SettingsManager.Companion.NONE
import timber.log.Timber
import java.util.*
import javax.inject.Inject
Expand Down Expand Up @@ -36,6 +38,10 @@ constructor(
return preferences.getBoolean(getKey(R.string.settings_key_debug_logging), false)
}

@SettingsManager.CallAction
override fun getCallAction(): String = preferences.getString(
getKey(R.string.settings_key_incoming_call_action), NONE) ?: NONE

override fun isPluginUpdateCheckEnabled(): Boolean {
return preferences.getBoolean(getKey(R.string.settings_key_plugin_check), false)
}
Expand Down Expand Up @@ -79,7 +85,21 @@ constructor(
}

interface SettingsManager {

@CallAction fun getCallAction(): String

@StringDef(NONE,
PAUSE,
STOP,
REDUCE)
@Retention(AnnotationRetention.SOURCE)
annotation class CallAction

companion object {
const val NONE = "none"
const val PAUSE = "pause"
const val STOP = "stop"
const val REDUCE = "reduce"
}
suspend fun shouldDisplayOnlyAlbumArtists(): Boolean
fun setShouldDisplayOnlyAlbumArtist(onlyAlbumArtist: Boolean)
fun shouldShowChangeLog(): Boolean
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/application_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

<androidx.preference.PreferenceCategory android:title="@string/settings_miscellaneous">

<androidx.preference.ListPreference
android:defaultValue="@string/incoming_action_default_value"
android:entries="@array/incoming_action_options"
android:entryValues="@array/incoming_action_options_values"
android:key="@string/settings_key_incoming_call_action"
android:summary="@string/settings_miscellaneous_incoming_call_description"
android:title="@string/settings_miscellaneous_incoming_call_action"/>

<androidx.preference.CheckBoxPreference
android:defaultValue="false"
android:key="@string/settings_key_plugin_check"
Expand Down
1 change: 1 addition & 0 deletions changelog/src/main/res/layout/changelog_dialog__layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/changelog"
android:layout_width="match_parent"
android:layout_marginTop="8dp"
tools:listitem="@layout/changelog_dialog__header"
tools:itemCount="5"
android:layout_height="match_parent"/>
Expand Down

0 comments on commit f02a0b5

Please sign in to comment.