Skip to content

Commit

Permalink
Fixed: The WiFi hotspot no longer starts automatically after granting…
Browse files Browse the repository at this point in the history
… notification permission in Android 13 and above.

* We implemented the `registerForActivityResult` method to handle notification permission, ensuring the hotspot starts seamlessly upon permission grant.
* Improved the permission-asking scenario. Before it ask once for notification permission and at the second time it shows the custom dialog that shows why we need this permission. But we can ask permission twice in our application so we have improved the asking of permission. If the user denies this permission then we show the custom dialog with message why we need this permission.
  • Loading branch information
MohitMaliDeveloper committed Mar 1, 2024
1 parent 98671f1 commit c0dbb81
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()

Check warning on line 127 in core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt#L127

Added line #L127 was not covered by tests
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(),
POST_NOTIFICATIONS

Check warning on line 131 in core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt#L130-L131

Added lines #L130 - L131 were not covered by tests
)
) {
alertDialogShower.show(
KiwixDialog.NotificationPermissionDialog,
requireActivity()::navigateToAppSettings

Check warning on line 136 in core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt#L134-L136

Added lines #L134 - L136 were not covered by tests
)
}
}
}

Check warning on line 140 in core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt#L140

Added line #L140 was not covered by tests

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)

Check warning on line 195 in core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt#L195

Added line #L195 was not covered by tests
}
} 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 c0dbb81

Please sign in to comment.