Skip to content

Commit

Permalink
Repair crash on Android 14 #2067
Browse files Browse the repository at this point in the history
  • Loading branch information
klassm committed Oct 14, 2023
1 parent e49603b commit b084c77
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
21 changes: 19 additions & 2 deletions app/src/main/java/li/klass/fhem/activities/AndFHEMMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.content.Intent.ACTION_SEARCH
import android.content.Intent.ACTION_VIEW
import android.content.IntentFilter
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
Expand Down Expand Up @@ -211,7 +212,15 @@ open class AndFHEMMainActivity : AppCompatActivity() {
}

broadcastReceiver = Receiver()
registerReceiver(broadcastReceiver, broadcastReceiver!!.intentFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerReceiver(
broadcastReceiver,
broadcastReceiver!!.intentFilter,
RECEIVER_NOT_EXPORTED
)
} else {
registerReceiver(broadcastReceiver, broadcastReceiver!!.intentFilter)
}

initDrawerLayout()

Expand Down Expand Up @@ -374,7 +383,15 @@ open class AndFHEMMainActivity : AppCompatActivity() {
saveInstanceStateCalled = false

if (broadcastReceiver != null) {
registerReceiver(broadcastReceiver, broadcastReceiver!!.intentFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerReceiver(
broadcastReceiver,
broadcastReceiver!!.intentFilter,
RECEIVER_NOT_EXPORTED
)
} else {
registerReceiver(broadcastReceiver, broadcastReceiver!!.intentFilter)
}
}

if (availableConnectionDataAdapter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ class AndroidControlsProviderService : ControlsProviderService() {
}

}
registerReceiver(broadcastReceiver, IntentFilter(Actions.DEVICES_UPDATED))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerReceiver(
broadcastReceiver,
IntentFilter(Actions.DEVICES_UPDATED),
RECEIVER_NOT_EXPORTED
)
} else {
registerReceiver(broadcastReceiver, IntentFilter(Actions.DEVICES_UPDATED))
}
}

override fun onDestroy() {
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/java/li/klass/fhem/fragments/core/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ package li.klass.fhem.fragments.core

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.RECEIVER_NOT_EXPORTED
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import li.klass.fhem.R
import li.klass.fhem.activities.core.Updateable
import li.klass.fhem.constants.Actions
Expand Down Expand Up @@ -245,7 +255,11 @@ abstract class BaseFragment : Fragment(), Updateable, Serializable, SwipeRefresh
}

fun attach() {
activity.registerReceiver(this, intentFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.registerReceiver(this, intentFilter, RECEIVER_NOT_EXPORTED)
} else {
activity.registerReceiver(this, intentFilter)
}
}

fun detach() {
Expand All @@ -254,7 +268,6 @@ abstract class BaseFragment : Fragment(), Updateable, Serializable, SwipeRefresh
} catch (e: IllegalArgumentException) {
Log.e(UIBroadcastReceiver::class.java.name, "error while detaching", e)
}

}
}

Expand Down

0 comments on commit b084c77

Please sign in to comment.