Less broken contacts functionality

This commit is contained in:
ottoptj 2024-09-08 06:15:55 +03:00
commit 1ac6814b58
7 changed files with 87 additions and 30 deletions

View file

@ -57,6 +57,7 @@ import eu.ottop.yamlauncher.utils.AppMenuEdgeFactory
import eu.ottop.yamlauncher.utils.AppMenuLinearLayoutManager import eu.ottop.yamlauncher.utils.AppMenuLinearLayoutManager
import eu.ottop.yamlauncher.utils.AppUtils import eu.ottop.yamlauncher.utils.AppUtils
import eu.ottop.yamlauncher.utils.GestureUtils import eu.ottop.yamlauncher.utils.GestureUtils
import eu.ottop.yamlauncher.utils.PermissionUtils
import eu.ottop.yamlauncher.utils.StringUtils import eu.ottop.yamlauncher.utils.StringUtils
import eu.ottop.yamlauncher.utils.UIUtils import eu.ottop.yamlauncher.utils.UIUtils
import eu.ottop.yamlauncher.utils.WeatherSystem import eu.ottop.yamlauncher.utils.WeatherSystem
@ -74,6 +75,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private lateinit var weatherSystem: WeatherSystem private lateinit var weatherSystem: WeatherSystem
private lateinit var appUtils: AppUtils private lateinit var appUtils: AppUtils
private val stringUtils = StringUtils() private val stringUtils = StringUtils()
private val permissionUtils = PermissionUtils()
private lateinit var uiUtils: UIUtils private lateinit var uiUtils: UIUtils
private lateinit var gestureUtils: GestureUtils private lateinit var gestureUtils: GestureUtils
@ -489,9 +491,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
"contactsEnabled" -> { "contactsEnabled" -> {
if (sharedPreferenceManager.areContactsEnabled()) {
checkContactsPermission()
}
uiUtils.setContactsVisibility(searchSwitcher, binding.searchLayout, binding.searchReplacement) uiUtils.setContactsVisibility(searchSwitcher, binding.searchLayout, binding.searchReplacement)
} }
@ -692,9 +691,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
setupAppRecycler(newApps) setupAppRecycler(newApps)
setupSearch() setupSearch()
if (sharedPreferenceManager.areContactsEnabled()) { setupContactRecycler()
setupContactRecycler()
}
} }
} }
@ -737,10 +735,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
private fun getContacts(filterString: String): MutableList<Pair<String, Int>> { private fun getContacts(filterString: String): MutableList<Pair<String, Int>> {
if (!checkContactsPermission()) {
return mutableListOf()
}
val contacts = mutableListOf<Pair<String, Int>>() val contacts = mutableListOf<Pair<String, Int>>()
val contentResolver: ContentResolver = contentResolver val contentResolver: ContentResolver = contentResolver
@ -775,26 +769,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
return contacts return contacts
} }
private fun checkContactsPermission(): Boolean {
try {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_CONTACTS
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.READ_CONTACTS),
1
)
return false
}
return true
} catch(_: Exception) {
return false
}
}
private suspend fun updateContacts(filterString: String) { private suspend fun updateContacts(filterString: String) {
val contacts = getContacts(filterString) val contacts = getContacts(filterString)
@ -923,6 +898,9 @@ 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)) {
sharedPreferenceManager.setContactsEnabled(false)
}
if (returnAllowed) { if (returnAllowed) {
backToHome(0) backToHome(0)
} }

View file

@ -1,16 +1,40 @@
package eu.ottop.yamlauncher.settings package eu.ottop.yamlauncher.settings
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import eu.ottop.yamlauncher.R import eu.ottop.yamlauncher.R
import eu.ottop.yamlauncher.utils.AppUtils
import eu.ottop.yamlauncher.utils.PermissionUtils
class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider { class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
private val permissionUtils = PermissionUtils()
private var contactPref: SwitchPreference? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.app_menu_preferences, rootKey) setPreferencesFromResource(R.xml.app_menu_preferences, rootKey)
contactPref = findPreference("contactsEnabled")
contactPref?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean && !permissionUtils.hasContactsPermission(requireContext(), Manifest.permission.READ_CONTACTS)) {
(requireActivity() as SettingsActivity).requestContactsPermission()
return@OnPreferenceChangeListener false
} else {
return@OnPreferenceChangeListener true
}
}
} }
override fun getTitle(): String { override fun getTitle(): String {
return "App Menu Settings" return "App Menu Settings"
} }
fun setContactPreference(isEnabled: Boolean) {
contactPref?.isChecked = isEnabled
}
} }

View file

@ -1,19 +1,22 @@
package eu.ottop.yamlauncher.settings package eu.ottop.yamlauncher.settings
import android.Manifest
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import eu.ottop.yamlauncher.R import eu.ottop.yamlauncher.R
import eu.ottop.yamlauncher.databinding.ActivitySettingsBinding import eu.ottop.yamlauncher.databinding.ActivitySettingsBinding
import org.json.JSONObject import org.json.JSONObject
import java.io.IOException
class SettingsActivity : AppCompatActivity() { class SettingsActivity : AppCompatActivity() {
@ -179,4 +182,31 @@ class SettingsActivity : AppCompatActivity() {
} }
} }
fun requestContactsPermission() {
try {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.READ_CONTACTS),
1
)
} catch(_: Exception) {}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
val fragment = supportFragmentManager.findFragmentById(R.id.settingsLayout) as AppMenuSettingsFragment
if (requestCode == 1) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fragment.setContactPreference(true)
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
fragment.setContactPreference(false)
}
}
}
} }

View file

@ -6,6 +6,7 @@ import android.provider.Settings
import android.widget.Toast import android.widget.Toast
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import eu.ottop.yamlauncher.R import eu.ottop.yamlauncher.R
import eu.ottop.yamlauncher.utils.UIUtils import eu.ottop.yamlauncher.utils.UIUtils

View file

@ -208,6 +208,12 @@ class SharedPreferenceManager (private val context: Context) {
return preferences.getBoolean("contactsEnabled", false) return preferences.getBoolean("contactsEnabled", false)
} }
fun setContactsEnabled(isEnabled: Boolean) {
val editor = preferences.edit()
editor.putBoolean("contactsEnabled", isEnabled)
editor.apply()
}
// Hidden Apps // Hidden Apps
fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) { fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) {
val editor = preferences.edit() val editor = preferences.edit()

View file

@ -1,11 +1,14 @@
package eu.ottop.yamlauncher.utils package eu.ottop.yamlauncher.utils
import android.Manifest
import android.content.Context import android.content.Context
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherActivityInfo
import android.content.pm.LauncherApps import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.os.UserHandle import android.os.UserHandle
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat
import eu.ottop.yamlauncher.settings.SharedPreferenceManager import eu.ottop.yamlauncher.settings.SharedPreferenceManager
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext

View file

@ -0,0 +1,15 @@
package eu.ottop.yamlauncher.utils
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
class PermissionUtils {
fun hasContactsPermission(context: Context, permission: String): Boolean {
return ContextCompat.checkSelfPermission(
context,
permission
) == PackageManager.PERMISSION_GRANTED
}
}