Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: The WiFi hotspot no longer starts automatically after granting notification permission in Android 13 and above. #3729

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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.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 @@
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 @@
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 @@
else -> startHotspotManuallyDialog()
}
}

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