Changed location to use network location instead where available and improved requesting location permission.

This commit is contained in:
ottoptj 2024-10-11 06:16:23 +03:00
commit 9196b63541
10 changed files with 90 additions and 61 deletions

View file

@ -951,9 +951,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (!permissionUtils.hasContactsPermission(this@MainActivity, Manifest.permission.READ_CONTACTS)) { if (!permissionUtils.hasPermission(this@MainActivity, Manifest.permission.READ_CONTACTS)) {
sharedPreferenceManager.setContactsEnabled(false) sharedPreferenceManager.setContactsEnabled(false)
} }
if (!permissionUtils.hasPermission(this@MainActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
sharedPreferenceManager.setWeatherGPS(false)
}
if (returnAllowed) { if (returnAllowed) {
backToHome(0) backToHome(0)
} }

View file

@ -17,7 +17,7 @@ class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
contactPref = findPreference("contactsEnabled") contactPref = findPreference("contactsEnabled")
contactPref?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> contactPref?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean && !permissionUtils.hasContactsPermission(requireContext(), Manifest.permission.READ_CONTACTS)) { if (newValue as Boolean && !permissionUtils.hasPermission(requireContext(), Manifest.permission.READ_CONTACTS)) {
(requireActivity() as SettingsActivity).requestContactsPermission() (requireActivity() as SettingsActivity).requestContactsPermission()
return@OnPreferenceChangeListener false return@OnPreferenceChangeListener false
} else { } else {

View file

@ -1,16 +1,20 @@
package eu.ottop.yamlauncher.settings package eu.ottop.yamlauncher.settings
import android.Manifest
import android.os.Bundle import android.os.Bundle
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
import eu.ottop.yamlauncher.R import eu.ottop.yamlauncher.R
import eu.ottop.yamlauncher.utils.PermissionUtils
import eu.ottop.yamlauncher.utils.UIUtils import eu.ottop.yamlauncher.utils.UIUtils
class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider { class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
private lateinit var sharedPreferenceManager: SharedPreferenceManager private lateinit var sharedPreferenceManager: SharedPreferenceManager
private val permissionUtils = PermissionUtils()
private var gpsLocationPref: SwitchPreference? = null
private var manualLocationPref: Preference? = null private var manualLocationPref: Preference? = null
private var leftSwipePref: Preference? = null private var leftSwipePref: Preference? = null
private var rightSwipePref: Preference? = null private var rightSwipePref: Preference? = null
@ -26,20 +30,24 @@ class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
clockApp = findPreference("clockSwipeApp") clockApp = findPreference("clockSwipeApp")
dateApp = findPreference("dateSwipeApp") dateApp = findPreference("dateSwipeApp")
val gpsLocationPref = findPreference<SwitchPreference?>("gpsLocation") gpsLocationPref = findPreference("gpsLocation")
manualLocationPref = findPreference("manualLocation") manualLocationPref = findPreference("manualLocation")
leftSwipePref = findPreference("leftSwipeApp") leftSwipePref = findPreference("leftSwipeApp")
rightSwipePref = findPreference("rightSwipeApp") rightSwipePref = findPreference("rightSwipeApp")
// Only enable manual location when gps location is disabled // Only enable manual location when gps location is disabled
if (gpsLocationPref != null && manualLocationPref != null) { if (gpsLocationPref != null && manualLocationPref != null) {
manualLocationPref?.isEnabled = !gpsLocationPref.isChecked manualLocationPref?.isEnabled = (gpsLocationPref?.isChecked == false)
gpsLocationPref.onPreferenceChangeListener = gpsLocationPref?.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue -> Preference.OnPreferenceChangeListener { _, newValue ->
val isGpsEnabled = newValue as Boolean if (newValue as Boolean && !permissionUtils.hasPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
manualLocationPref?.isEnabled = !isGpsEnabled (requireActivity() as SettingsActivity).requestLocationPermission()
true return@OnPreferenceChangeListener false
} else {
manualLocationPref?.isEnabled = !newValue
return@OnPreferenceChangeListener true
}
} }
manualLocationPref?.onPreferenceClickListener = manualLocationPref?.onPreferenceClickListener =
@ -86,4 +94,9 @@ class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
override fun getTitle(): String { override fun getTitle(): String {
return getString(R.string.home_settings_title) return getString(R.string.home_settings_title)
} }
fun setLocationPreference(isEnabled: Boolean) {
manualLocationPref?.isEnabled = !isEnabled
gpsLocationPref?.isChecked = isEnabled
}
} }

View file

@ -189,10 +189,20 @@ class SettingsActivity : AppCompatActivity() {
} }
} }
fun requestLocationPermission() {
try {
ActivityCompat.requestPermissions(
this@SettingsActivity,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
0
)
} catch(_: Exception) {}
}
fun requestContactsPermission() { fun requestContactsPermission() {
try { try {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this, this@SettingsActivity,
arrayOf(Manifest.permission.READ_CONTACTS), arrayOf(Manifest.permission.READ_CONTACTS),
1 1
) )
@ -216,8 +226,20 @@ class SettingsActivity : AppCompatActivity() {
grantResults: IntArray grantResults: IntArray
) { ) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults)
val fragment = supportFragmentManager.findFragmentById(R.id.settingsLayout) as AppMenuSettingsFragment
if (requestCode == 0) {
val fragment = supportFragmentManager.findFragmentById(R.id.settingsLayout) as HomeSettingsFragment
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fragment.setLocationPreference(true)
} else {
Toast.makeText(this, getString(R.string.permission_denied), Toast.LENGTH_SHORT).show()
fragment.setLocationPreference(false)
}
}
if (requestCode == 1) { if (requestCode == 1) {
val fragment = supportFragmentManager.findFragmentById(R.id.settingsLayout) as AppMenuSettingsFragment
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fragment.setContactPreference(true) fragment.setContactPreference(true)
} else { } else {
@ -229,9 +251,12 @@ class SettingsActivity : AppCompatActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (!permissionUtils.hasContactsPermission(this@SettingsActivity, Manifest.permission.READ_CONTACTS)) { if (!permissionUtils.hasPermission(this@SettingsActivity, Manifest.permission.READ_CONTACTS)) {
sharedPreferenceManager.setContactsEnabled(false) sharedPreferenceManager.setContactsEnabled(false)
} }
if (!permissionUtils.hasPermission(this@SettingsActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
sharedPreferenceManager.setWeatherGPS(false)
}
} }
} }

View file

@ -129,6 +129,12 @@ class SharedPreferenceManager (private val context: Context) {
return preferences.getBoolean("gpsLocation", false) return preferences.getBoolean("gpsLocation", false)
} }
fun setWeatherGPS(isEnabled: Boolean) {
val editor = preferences.edit()
editor.putBoolean("gpsLocation", isEnabled)
editor.apply()
}
fun setWeatherLocation(location: String, region: String?) { fun setWeatherLocation(location: String, region: String?) {
val editor = preferences.edit() val editor = preferences.edit()
editor.putString("location", location) editor.putString("location", location)

View file

@ -6,7 +6,7 @@ import androidx.core.content.ContextCompat
class PermissionUtils { class PermissionUtils {
fun hasContactsPermission(context: Context, permission: String): Boolean { fun hasPermission(context: Context, permission: String): Boolean {
return ContextCompat.checkSelfPermission( return ContextCompat.checkSelfPermission(
context, context,
permission permission

View file

@ -5,7 +5,6 @@ import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.location.Location import android.location.Location
import android.location.LocationManager import android.location.LocationManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import eu.ottop.yamlauncher.MainActivity import eu.ottop.yamlauncher.MainActivity
import eu.ottop.yamlauncher.R import eu.ottop.yamlauncher.R
@ -23,28 +22,35 @@ class WeatherSystem(private val context: Context) {
private val stringUtils = StringUtils() private val stringUtils = StringUtils()
suspend fun setGpsLocation(activity: MainActivity) { suspend fun setGpsLocation(activity: MainActivity) {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
1
)
return return
} }
try {
val provider = if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
LocationManager.NETWORK_PROVIDER
} else {
LocationManager.GPS_PROVIDER
}
locationManager.getCurrentLocation( locationManager.getCurrentLocation(
LocationManager.GPS_PROVIDER, // Only GPS provider functions on my phone with CalyxOS, so that's what you get. provider,
null, null,
ContextCompat.getMainExecutor(context) ContextCompat.getMainExecutor(context)
) )
{ location: Location? -> { location: Location? ->
if (location != null) { if (location != null) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val latitude = location.latitude val latitude = location.latitude
val longitude = location.longitude val longitude = location.longitude
sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", context.getString(R.string.latest_location)) sharedPreferenceManager.setWeatherLocation(
"latitude=${latitude}&longitude=${longitude}",
context.getString(R.string.latest_location)
)
activity.updateWeatherText() activity.updateWeatherText()
} }
@ -54,7 +60,9 @@ class WeatherSystem(private val context: Context) {
} }
} }
} }
} catch(_: Exception) {
return
}
} }
// Run within Dispatchers.IO from the outside (doesn't seem to refresh properly otherwise) // Run within Dispatchers.IO from the outside (doesn't seem to refresh properly otherwise)

View file

@ -217,14 +217,4 @@
<item>celsius</item> <item>celsius</item>
<item>fahrenheit</item> <item>fahrenheit</item>
</string-array> </string-array>
<!-- Reply Preference -->
<string-array name="reply_entries">
<item>Reply</item>
<item>Reply to all</item>
</string-array>
<string-array name="reply_values">
<item>reply</item>
<item>reply_all</item>
</string-array>
</resources> </resources>

View file

@ -1,8 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="settings_bg">#FF1B1B1B</color> <color name="settings_bg">#FF1B1B1B</color>
</resources> </resources>

View file

@ -30,18 +30,6 @@
<style name="TransparentActionBar" parent="Widget.AppCompat.ActionBar"> <style name="TransparentActionBar" parent="Widget.AppCompat.ActionBar">
<!-- Transparent background for ActionBar --> <!-- Transparent background for ActionBar -->
<item name="background">@android:color/transparent</item> <item name="background">@android:color/transparent</item>
</style>
<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
<item name="android:textSize">25sp</item>
<item name="android:editTextColor">#f3f3f3</item>
<item name="android:cursorVisible">true</item>
</style>
<style name="AppTextAppearance" parent="Widget.AppCompat.SearchView">
<item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault</item>
</style> </style>
<style name="Theme.YamLauncher" parent="Base.Theme.YamLauncher" /> <style name="Theme.YamLauncher" parent="Base.Theme.YamLauncher" />