Skip to content

Commit

Permalink
Merge pull request #3729 from kiwix/Fix#3728
Browse files Browse the repository at this point in the history
Fixed: The WiFi hotspot no longer starts automatically after granting notification permission in Android 13 and above.
  • Loading branch information
kelson42 authored Mar 5, 2024
2 parents f483ff1 + b5c99ca commit a1eae97
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
Expand All @@ -47,7 +48,6 @@ import org.kiwix.kiwixmobile.core.databinding.ActivityZimHostBinding
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isCustomApp
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.requestNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.navigateToAppSettings
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
Expand Down Expand Up @@ -120,6 +120,25 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
as ArrayList<String>
}

private val notificationPermissionListener = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
startStopServer()
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(),
POST_NOTIFICATIONS
)
) {
alertDialogShower.show(
KiwixDialog.NotificationPermissionDialog,
requireActivity()::navigateToAppSettings
)
}
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -173,29 +192,14 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
if (requireActivity().hasNotificationPermission(sharedPreferenceUtil)) {
startStopServer()
} else {
requestNotificationPermission()
notificationPermissionListener.launch(POST_NOTIFICATIONS)
}
} else {
startStopServer()
}
}
}

private fun requestNotificationPermission() {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(),
POST_NOTIFICATIONS
)
) {
requireActivity().requestNotificationPermission()
} else {
alertDialogShower.show(
KiwixDialog.NotificationPermissionDialog,
requireActivity()::navigateToAppSettings
)
}
}

private fun startStopServer() {
when {
ServerUtils.isServerStarted -> stopServer()
Expand All @@ -206,6 +210,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
else -> startHotspotManuallyDialog()
}
}

else -> toast(R.string.no_books_selected_toast_message, Toast.LENGTH_SHORT)
}
}
Expand Down

0 comments on commit a1eae97

Please sign in to comment.