Skip to content

Commit

Permalink
show rationale for permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Nov 11, 2024
1 parent 3f7519c commit 5d431f3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import java.util.*


class WifiDialogFragment : DialogFragment() {
private var mDialog: AlertDialog? = null
private lateinit var mDialog: AlertDialog
private var wifiManager: WifiManager? = null
private val wifiList = ArrayList<String>()
private var mContext: Context? = null
private var SSID: String? = null
private var mView: View? = null
private lateinit var mView: View
private var firstScan = true
private var progressBar: ProgressBar? = null
override fun onAttach(context: Context) {
super.onAttach(context)
val inflater = requireActivity().layoutInflater
mView = inflater.inflate(R.layout.dialog_listview, null)
progressBar = mView!!.findViewById(R.id.progressBar)
progressBar = mView.findViewById(R.id.progressBar)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
Expand All @@ -40,36 +40,36 @@ class WifiDialogFragment : DialogFragment() {
mDialog = DialogUtils.createAlertDialog(context, mView, R.drawable.dialog_icon)
.setTitle("Choose a network: ")
.setNegativeButton("Cancel") { dialog: DialogInterface, _: Int -> dialog.dismiss() }.create()
mDialog!!.window!!.setBackgroundDrawableResource(android.R.color.transparent)
return mDialog!!
mDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
return mDialog
}

private fun setAdapter() {
val listView = mView!!.findViewById<ListView>(R.id.listView)
val arrayAdapter = ArrayAdapter(mContext!!, R.layout.simple_list_item, wifiList)
val listView = mView.findViewById<ListView>(R.id.listView)
val arrayAdapter = ArrayAdapter(requireContext(), R.layout.simple_list_item, wifiList)
listView.adapter = arrayAdapter
listView.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, _: View?, position: Int, _: Long ->
SSID = wifiList[position]
if (targetFragment != null) {
val intent = Intent()
intent.putExtra(WIFI_SSID_KEY, SSID!!.trim { it <= ' ' })
targetFragment!!.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
intent.putExtra(WIFI_SSID_KEY, SSID?.trim { it <= ' ' })
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
wifiList.clear()
dismiss()
}
}
}

private fun setupWifi() {
wifiManager = mContext!!.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager = mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (wifiManager == null) return
wifiManager!!.isWifiEnabled = true
wifiManager?.isWifiEnabled = true
val wifiScanReceiver = wifiBroadcastReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
mContext!!.registerReceiver(wifiScanReceiver, intentFilter)
val success = wifiManager!!.startScan()
if (!success) {
mContext?.registerReceiver(wifiScanReceiver, intentFilter)
val success = wifiManager?.startScan()
if (success == false) {
scanFailure()
}
}
Expand All @@ -88,10 +88,10 @@ class WifiDialogFragment : DialogFragment() {
}
}

private fun getSSIDs(results: List<ScanResult>) {
private fun getSSIDs(results: List<ScanResult>?) {
wifiList.clear()
// converts Object list to array
val `object`: Array<Any> = results.toTypedArray()
val `object`: Array<Any>? = results?.toTypedArray()
val temp = `object`.contentToString()
val resultArray = temp.split(",".toRegex()).toTypedArray()

Expand All @@ -109,24 +109,24 @@ class WifiDialogFragment : DialogFragment() {
private fun addToList(ssid: String) {
if (ssid.trim { it <= ' ' }.isNotEmpty()) {
wifiList.add(ssid)
progressBar!!.visibility = View.INVISIBLE
progressBar?.visibility = View.INVISIBLE
}
}

private fun scanSuccess() {
val results = wifiManager!!.scanResults
val results = wifiManager?.scanResults
getSSIDs(results)
setAdapter()
}

private fun scanFailure() {
// handle failure: new scan did not succeed
val results = wifiManager!!.scanResults
val results = wifiManager?.scanResults
getSSIDs(results)
if (results.size >= 1 && firstScan) {
if (results?.isNotEmpty() == true && firstScan) {
Toast.makeText(context, "Scan unsuccessful. These are old results", Toast.LENGTH_LONG).show()
setAdapter()
} else if (results.size < 1 && firstScan) {
} else if (results?.isEmpty() == true && firstScan) {
ifResultListEmpty()
}
firstScan = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package io.treehouses.remote.ui.network.bottomsheetdialogs

import android.Manifest
import android.app.Activity
import android.content.Context.LOCATION_SERVICE
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.fragment.app.viewModels
import io.treehouses.remote.Constants
import io.treehouses.remote.bases.BaseBottomSheetDialog
Expand Down Expand Up @@ -56,6 +64,54 @@ open class WifiBottomSheet : BaseBottomSheetDialog() {
bind.checkBoxEnterprise.setOnCheckedChangeListener { _, isChecked ->
viewModel.hiddenOrEnterprise(isChecked)
}
bind.btnWifiSearch.setOnClickListener { openWifiDialog(this@WifiBottomSheet, context) }
bind.btnWifiSearch.setOnClickListener {
val locationManager = context?.getSystemService(LOCATION_SERVICE) as LocationManager

if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
showLocationPermissionRationale()
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
}
} else {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
context?.startActivity(intent)
} else {
openWifiDialog(this@WifiBottomSheet, context)
}
}
}
}

private fun showLocationPermissionRationale() {
AlertDialog.Builder(requireContext())
.setTitle("Location Permission Needed")
.setMessage("This app requires location access to enable WiFi search functionality. Please grant location permission to continue.")
.setPositiveButton("OK") { _, _ ->
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
Toast.makeText(context, "Location permission is required to enable WiFi search", Toast.LENGTH_SHORT).show()
}
.show()
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
bind.btnWifiSearch.performClick()
} else {
Toast.makeText(context, "Location permission is required to enable WiFi search", Toast.LENGTH_SHORT).show()
}
}
}

companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1001
}
}

0 comments on commit 5d431f3

Please sign in to comment.