Last active
December 20, 2022 21:33
-
-
Save SurajBahadur/671521c379502495d9ef0f6f1dc21724 to your computer and use it in GitHub Desktop.
Single and multiple runtime permissions in kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.wififinder | |
import android.Manifest | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import android.net.ConnectivityManager | |
import android.net.wifi.WifiManager | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.View | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.ContextCompat | |
import com.example.wififinder.databinding.ActivityMainBinding | |
import com.example.wififinder.utis.showSnackbar | |
import com.google.android.material.snackbar.Snackbar | |
class MainActivity : AppCompatActivity() { | |
private lateinit var layout: View | |
private lateinit var binding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
layout = binding.root | |
setContentView(layout) | |
checkPermissionGiven() | |
} | |
private fun checkPermissionGiven() { | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
== PackageManager.PERMISSION_GRANTED | |
) { | |
//permission granted | |
getCurrentSsid(this) | |
} else requestLocationPermission() | |
} | |
private fun requestLocationPermission() { | |
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) { | |
layout.showSnackbar( | |
R.string.location_access_required, | |
Snackbar.LENGTH_INDEFINITE, | |
R.string.ok | |
) { | |
launchMultiPermission() | |
} | |
} else { | |
// You can directly ask for the permission. | |
launchMultiPermission() | |
} | |
} | |
/** | |
* For multi permission | |
*/ | |
private fun launchMultiPermission() { | |
requestMultiplePermissions.launch( | |
arrayOf( | |
Manifest.permission.ACCESS_FINE_LOCATION, | |
Manifest.permission.ACCESS_WIFI_STATE | |
) | |
) | |
} | |
/** | |
* For single permission | |
*/ | |
private fun launchSinglePermission() { | |
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION) | |
} | |
private val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> | |
permissions.entries.forEach { | |
Log.e("DEBUG", "${it.key} = ${it.value}") | |
} | |
} | |
private val requestPermissionLauncher = | |
registerForActivityResult( | |
ActivityResultContracts.RequestPermission() | |
) { isGranted: Boolean -> | |
if (isGranted) { | |
// Permission has been granted. Start camera preview Activity. | |
getCurrentSsid(this) | |
} else { | |
// Permission request was denied. | |
layout.showSnackbar( | |
R.string.location_permission_denied, | |
Snackbar.LENGTH_SHORT, | |
R.string.ok | |
) | |
} | |
} | |
private fun getCurrentSsid(context: Context): String { | |
var ssid = "NA" | |
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) | |
if (networkInfo!!.isConnected) { | |
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager | |
val connectionInfo = wifiManager.connectionInfo | |
if (connectionInfo != null && connectionInfo.ssid.isNotEmpty()) { | |
ssid = connectionInfo.ssid | |
} | |
} | |
return ssid | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.wififinder.utis | |
/** | |
* Created by Suraj Bahadur on 11-06-2021. | |
*/ | |
import android.view.View | |
import com.google.android.material.snackbar.Snackbar | |
fun View.showSnackbar(msgId: Int, length: Int, actionMessageId: Int) { | |
showSnackbar(msgId, length, actionMessageId) {} | |
} | |
fun View.showSnackbar( | |
msgId: Int, | |
length: Int, | |
actionMessageId: Int, | |
action: (View) -> Unit | |
) { | |
showSnackbar(context.getString(msgId), length, context.getString(actionMessageId), action) | |
} | |
fun View.showSnackbar( | |
msg: String, | |
length: Int, | |
actionMessage: CharSequence?, | |
action: (View) -> Unit | |
) { | |
val snackbar = Snackbar.make(this, msg, length) | |
if (actionMessage != null) { | |
snackbar.setAction(actionMessage) { | |
action(this) | |
}.show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment