mirror of
https://github.com/He4eT/yamf_launcher.git
synced 2026-05-05 01:47:24 +00:00
Changed location to use network location instead where available and improved requesting location permission.
This commit is contained in:
parent
8c2b1168c4
commit
9196b63541
10 changed files with 90 additions and 61 deletions
|
|
@ -951,9 +951,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
|
|||
@SuppressLint("NotifyDataSetChanged")
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (!permissionUtils.hasContactsPermission(this@MainActivity, Manifest.permission.READ_CONTACTS)) {
|
||||
if (!permissionUtils.hasPermission(this@MainActivity, Manifest.permission.READ_CONTACTS)) {
|
||||
sharedPreferenceManager.setContactsEnabled(false)
|
||||
}
|
||||
if (!permissionUtils.hasPermission(this@MainActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
||||
sharedPreferenceManager.setWeatherGPS(false)
|
||||
}
|
||||
if (returnAllowed) {
|
||||
backToHome(0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
|
|||
contactPref = findPreference("contactsEnabled")
|
||||
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()
|
||||
return@OnPreferenceChangeListener false
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,20 @@
|
|||
package eu.ottop.yamlauncher.settings
|
||||
|
||||
import android.Manifest
|
||||
import android.os.Bundle
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.SwitchPreference
|
||||
import eu.ottop.yamlauncher.R
|
||||
import eu.ottop.yamlauncher.utils.PermissionUtils
|
||||
import eu.ottop.yamlauncher.utils.UIUtils
|
||||
|
||||
class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
|
||||
|
||||
private lateinit var sharedPreferenceManager: SharedPreferenceManager
|
||||
private val permissionUtils = PermissionUtils()
|
||||
|
||||
private var gpsLocationPref: SwitchPreference? = null
|
||||
private var manualLocationPref: Preference? = null
|
||||
private var leftSwipePref: Preference? = null
|
||||
private var rightSwipePref: Preference? = null
|
||||
|
|
@ -26,20 +30,24 @@ class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
|
|||
clockApp = findPreference("clockSwipeApp")
|
||||
dateApp = findPreference("dateSwipeApp")
|
||||
|
||||
val gpsLocationPref = findPreference<SwitchPreference?>("gpsLocation")
|
||||
gpsLocationPref = findPreference("gpsLocation")
|
||||
manualLocationPref = findPreference("manualLocation")
|
||||
leftSwipePref = findPreference("leftSwipeApp")
|
||||
rightSwipePref = findPreference("rightSwipeApp")
|
||||
|
||||
// Only enable manual location when gps location is disabled
|
||||
if (gpsLocationPref != null && manualLocationPref != null) {
|
||||
manualLocationPref?.isEnabled = !gpsLocationPref.isChecked
|
||||
manualLocationPref?.isEnabled = (gpsLocationPref?.isChecked == false)
|
||||
|
||||
gpsLocationPref.onPreferenceChangeListener =
|
||||
gpsLocationPref?.onPreferenceChangeListener =
|
||||
Preference.OnPreferenceChangeListener { _, newValue ->
|
||||
val isGpsEnabled = newValue as Boolean
|
||||
manualLocationPref?.isEnabled = !isGpsEnabled
|
||||
true
|
||||
if (newValue as Boolean && !permissionUtils.hasPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
||||
(requireActivity() as SettingsActivity).requestLocationPermission()
|
||||
return@OnPreferenceChangeListener false
|
||||
} else {
|
||||
manualLocationPref?.isEnabled = !newValue
|
||||
return@OnPreferenceChangeListener true
|
||||
}
|
||||
}
|
||||
|
||||
manualLocationPref?.onPreferenceClickListener =
|
||||
|
|
@ -86,4 +94,9 @@ class HomeSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
|
|||
override fun getTitle(): String {
|
||||
return getString(R.string.home_settings_title)
|
||||
}
|
||||
|
||||
fun setLocationPreference(isEnabled: Boolean) {
|
||||
manualLocationPref?.isEnabled = !isEnabled
|
||||
gpsLocationPref?.isChecked = isEnabled
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {
|
||||
try {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
this@SettingsActivity,
|
||||
arrayOf(Manifest.permission.READ_CONTACTS),
|
||||
1
|
||||
)
|
||||
|
|
@ -216,8 +226,20 @@ class SettingsActivity : AppCompatActivity() {
|
|||
grantResults: IntArray
|
||||
) {
|
||||
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) {
|
||||
val fragment = supportFragmentManager.findFragmentById(R.id.settingsLayout) as AppMenuSettingsFragment
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
fragment.setContactPreference(true)
|
||||
} else {
|
||||
|
|
@ -229,9 +251,12 @@ class SettingsActivity : AppCompatActivity() {
|
|||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (!permissionUtils.hasContactsPermission(this@SettingsActivity, Manifest.permission.READ_CONTACTS)) {
|
||||
if (!permissionUtils.hasPermission(this@SettingsActivity, Manifest.permission.READ_CONTACTS)) {
|
||||
sharedPreferenceManager.setContactsEnabled(false)
|
||||
}
|
||||
if (!permissionUtils.hasPermission(this@SettingsActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
||||
sharedPreferenceManager.setWeatherGPS(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -129,6 +129,12 @@ class SharedPreferenceManager (private val context: Context) {
|
|||
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?) {
|
||||
val editor = preferences.edit()
|
||||
editor.putString("location", location)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import androidx.core.content.ContextCompat
|
|||
|
||||
class PermissionUtils {
|
||||
|
||||
fun hasContactsPermission(context: Context, permission: String): Boolean {
|
||||
fun hasPermission(context: Context, permission: String): Boolean {
|
||||
return ContextCompat.checkSelfPermission(
|
||||
context,
|
||||
permission
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import android.content.Context
|
|||
import android.content.pm.PackageManager
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import eu.ottop.yamlauncher.MainActivity
|
||||
import eu.ottop.yamlauncher.R
|
||||
|
|
@ -23,38 +22,47 @@ class WeatherSystem(private val context: Context) {
|
|||
private val stringUtils = StringUtils()
|
||||
|
||||
suspend fun setGpsLocation(activity: MainActivity) {
|
||||
|
||||
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(
|
||||
activity,
|
||||
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
|
||||
1
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
locationManager.getCurrentLocation(
|
||||
LocationManager.GPS_PROVIDER, // Only GPS provider functions on my phone with CalyxOS, so that's what you get.
|
||||
null,
|
||||
ContextCompat.getMainExecutor(context)
|
||||
)
|
||||
{ location: Location? ->
|
||||
if (location != null) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val latitude = location.latitude
|
||||
val longitude = location.longitude
|
||||
sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", context.getString(R.string.latest_location))
|
||||
activity.updateWeatherText()
|
||||
}
|
||||
|
||||
try {
|
||||
val provider = if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
||||
LocationManager.NETWORK_PROVIDER
|
||||
} else {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
activity.updateWeatherText()
|
||||
LocationManager.GPS_PROVIDER
|
||||
}
|
||||
|
||||
locationManager.getCurrentLocation(
|
||||
provider,
|
||||
null,
|
||||
ContextCompat.getMainExecutor(context)
|
||||
)
|
||||
|
||||
{ location: Location? ->
|
||||
if (location != null) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val latitude = location.latitude
|
||||
val longitude = location.longitude
|
||||
sharedPreferenceManager.setWeatherLocation(
|
||||
"latitude=${latitude}&longitude=${longitude}",
|
||||
context.getString(R.string.latest_location)
|
||||
)
|
||||
activity.updateWeatherText()
|
||||
}
|
||||
|
||||
} else {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
activity.updateWeatherText()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(_: Exception) {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Run within Dispatchers.IO from the outside (doesn't seem to refresh properly otherwise)
|
||||
|
|
|
|||
|
|
@ -217,14 +217,4 @@
|
|||
<item>celsius</item>
|
||||
<item>fahrenheit</item>
|
||||
</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>
|
||||
|
|
@ -1,8 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
|
||||
<color name="settings_bg">#FF1B1B1B</color>
|
||||
|
||||
</resources>
|
||||
|
|
@ -30,18 +30,6 @@
|
|||
<style name="TransparentActionBar" parent="Widget.AppCompat.ActionBar">
|
||||
<!-- Transparent background for ActionBar -->
|
||||
<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 name="Theme.YamLauncher" parent="Base.Theme.YamLauncher" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue