Skip to content

Commit

Permalink
Android: Fix PermissionsHandler activity detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sepalani committed Sep 22, 2024
1 parent d92e2ef commit d3aeff9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.color.MaterialColors
import com.google.android.material.datepicker.CalendarConstraints
Expand Down Expand Up @@ -59,6 +60,9 @@ class SettingsAdapter(
val settings: Settings?
get() = fragmentView.settings

val fragmentActivity: FragmentActivity
get() = fragmentView.fragmentActivity

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SettingViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class SwitchSettingViewHolder(

if (setting.setting === BooleanSetting.MAIN_EMULATE_WII_SPEAK && isChecked) {
if (!PermissionsHandler.hasRecordAudioPermission(itemView.context)) {
val settingsActivity = itemView.context as Activity
PermissionsHandler.requestRecordAudioPermission(settingsActivity)
val currentActivity = adapter.fragmentActivity as Activity
PermissionsHandler.requestRecordAudioPermission(currentActivity)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ class ActivityTracker : ActivityLifecycleCallbacks {
var currentActivity : Activity? = null
private set

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
currentActivity = activity
}
override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}

override fun onActivityStarted(activity: Activity) {}

override fun onActivityResumed(activity: Activity) {
currentActivity = activity
resumedActivities.add(activity)
if (!backgroundExecutionAllowed && !resumedActivities.isEmpty()) {
backgroundExecutionAllowed = true
Expand All @@ -25,6 +24,9 @@ class ActivityTracker : ActivityLifecycleCallbacks {
}

override fun onActivityPaused(activity: Activity) {
if (currentActivity === activity) {
currentActivity = null
}
resumedActivities.remove(activity)
if (backgroundExecutionAllowed && resumedActivities.isEmpty()) {
backgroundExecutionAllowed = false
Expand All @@ -36,11 +38,7 @@ class ActivityTracker : ActivityLifecycleCallbacks {

override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {}

override fun onActivityDestroyed(activity: Activity) {
if (currentActivity === activity) {
currentActivity = null
}
}
override fun onActivityDestroyed(activity: Activity) {}

companion object {
@JvmStatic
Expand Down

0 comments on commit d3aeff9

Please sign in to comment.