Skip to content

Instantly share code, notes, and snippets.

@meet30997
Last active August 23, 2022 12:33
Show Gist options
  • Save meet30997/d49ea41837decbb7940ae1de86407518 to your computer and use it in GitHub Desktop.
Save meet30997/d49ea41837decbb7940ae1de86407518 to your computer and use it in GitHub Desktop.
Check Flight Mode And Show Dialog Easily
class FlightModeManager(private val activity: AppCompatActivity) {
private var broadcastReceiver: BroadcastReceiver? = null
init {
initBroadcastReceiver()
// If Flight mode is off then we save false so we can show the dialog next time
// when user turn on the flight mode
if (!isFlightModeOn()) {
setFlightWarningShown(false)
}
// Registered broadcast receiver on create Event and Destroy Event of Activity
activity.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_CREATE -> registerFlightModeBroadCastReceiver()
Lifecycle.Event.ON_DESTROY -> unregisterBroadCastReceiver()
else -> {}
}
}
})
}
/**A broadcast receiver who receives the event when flight mode is on/off **/
private fun initBroadcastReceiver() {
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// If Flight mode is off then we save false so we can show the dialog next time
// when user turn on the flight mode
if (!isFlightModeOn()) {
setFlightWarningShown(false)
}
}
}
}
/**Register broadcast for flightMode**/
private fun registerFlightModeBroadCastReceiver() {
val intentFilter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
broadcastReceiver?.let { activity.registerReceiver(it, intentFilter) }
"BroadCast Registered For Flight Mode".debugLog()
}
/**UnRegister broadcast for flight mode**/
private fun unregisterBroadCastReceiver() {
broadcastReceiver?.let {
activity.unregisterReceiver(it)
broadcastReceiver = null
}
"BroadCast UnRegistered For Flight Mode".debugLog()
}
/**Check if Flight Mode is enable or not**/
fun checkFlightMode() {
// If Int is 1 then flight mode is enabled so we show the dialog to user
// If dialog already shows to user then we won't show the dialog
// Dialog only re-appears only when user turn off and on the flight mode again
if (isFlightModeOn() && !isFlightModeAlreadyShown()) {
setFlightWarningShown(true)
AlertView(activity as FragmentActivity).showAlert(
msg = activity.getString(R.string.explore_offline_alert_msg),
title = activity.getString(R.string.explore_offline_alert),
positiveButton = activity.getString(R.string.dialog_cancel),
negativeButton = activity.getString(R.string.profile_settings),
positiveButtonListener = {},
negativeButtonListener = {
// Open Phone's Flight Mode Settings
try {
activity.startActivity(
Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
)
} catch (
e: Exception
) {
// If matching activity not found then we open the default settings
activity.startActivity(
Intent(Settings.ACTION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
)
}
}
)
}
}
/**Set isShown for popup**/
private fun setFlightWarningShown(isShown: Boolean) {
PrefManager.preferences.edit().putBoolean(isFlightModeWarningShows, isShown).apply()
}
/**Check if dialog is already shown to the user or not**/
private fun isFlightModeAlreadyShown() = PrefManager.preferences.getBoolean(
isFlightModeWarningShows, false
)
/**Query the system setting to check the flight mode is on or off**/
private fun isFlightModeOn() = Settings.System.getInt(
activity.contentResolver,
Settings.Global.AIRPLANE_MODE_ON,
0
) == 1
/**Shared key to store the boolean**/
companion object {
private const val isFlightModeWarningShows = "isFlightModeWarningShows"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment