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

network: show rationale for permissions (fixes #2089) #2156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -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,21 @@
package io.treehouses.remote.ui.network.bottomsheetdialogs

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
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.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import io.treehouses.remote.R
import android.view.ViewGroup
import android.widget.Toast
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 +66,45 @@ 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) {
showLocationPermissionRationale()
} 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(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
.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()
}.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
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<item name="android:tint">@color/daynight_textColor</item>
</style>

<style name="CustomAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<style name="CustomAlertDialogTheme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:windowBackground">@color/card_background</item>
<item name="android:textColor">@color/daynight_textColor</item>
<item name="colorAccent">@color/colorAccent</item>
Expand Down
Loading