Skip to content

Commit

Permalink
Pre [0.1.4] Update main preference
Browse files Browse the repository at this point in the history
Changed
- Check/require location permission when turn on the follow location
  instead of click the preview & set
- Check the location permission when open the main preference, turn off
  follow location if permission not grated.
  • Loading branch information
lucka-me committed Mar 6, 2019
1 parent 903b29c commit fb77298
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.content.ContextCompat
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import org.jetbrains.anko.defaultSharedPreferences

class PreferenceMainActivity : AppCompatActivity() {
Expand All @@ -28,6 +29,17 @@ class PreferenceMainActivity : AppCompatActivity() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference_main, rootKey)

if (
ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.ACCESS_FINE_LOCATION
)
!= PackageManager.PERMISSION_GRANTED
) {
requireContext().defaultSharedPreferences.edit()
.putBoolean(getString(R.string.pref_live_wallpaper_follow_location), false)
.apply()
}

val prefLiveRadius = findPreference<EditTextPreference>(getString(R.string.pref_live_wallpaper_radius))
prefLiveRadius.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
Expand All @@ -36,47 +48,53 @@ class PreferenceMainActivity : AppCompatActivity() {
String.format(getString(R.string.pref_live_wallpaper_radius_summary), preference.text)
}

findPreference<Preference>(getString(R.string.pref_live_wallpaper_set)).onPreferenceClickListener =
object : Preference.OnPreferenceClickListener {
override fun onPreferenceClick(preference: Preference?): Boolean {
if (preference == null) return false
if (
ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.ACCESS_FINE_LOCATION
)
== PackageManager.PERMISSION_GRANTED
) {
startActivity(
Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
.putExtra(
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
ComponentName(requireContext(), WallmapperLiveService::class.java)
)
)
} else {
if (
ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(), Manifest.permission.ACCESS_FINE_LOCATION
)
) {
DialogKit.showDialog(
requireContext(),
R.string.dialog_title_request_permission,
R.string.request_permission_fine_location,
cancelable = false
)
} else {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
DefaultValue.Request.RequestPermissionFineLocation.code
)
}
}

return true
findPreference<Preference>(getString(R.string.pref_live_wallpaper_set))
.onPreferenceClickListener = Preference.OnPreferenceClickListener {
startActivity(
Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
.putExtra(
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
ComponentName(requireContext(), WallmapperLiveService::class.java)
)
)
true
}

findPreference<SwitchPreference>(getString(R.string.pref_live_wallpaper_follow_location))
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
if (
ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.ACCESS_FINE_LOCATION
)
== PackageManager.PERMISSION_GRANTED
) {
true
} else {
if (
ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(), Manifest.permission.ACCESS_FINE_LOCATION
)
) {
DialogKit.showDialog(
requireContext(),
R.string.dialog_title_request_permission,
R.string.request_permission_fine_location,
cancelable = false
)
} else {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
DefaultValue.Request.RequestPermissionFineLocation.code
)
}
false
}
} else {
true
}
}
}

override fun onResume() {
Expand Down Expand Up @@ -150,4 +168,15 @@ class PreferenceMainActivity : AppCompatActivity() {
}
return super.onOptionsItemSelected(item)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
DefaultValue.Request.RequestPermissionFineLocation.code -> {
defaultSharedPreferences.edit()
.putBoolean(getString(R.string.pref_live_wallpaper_follow_location), true)
.apply()
}
}
}
}
4 changes: 2 additions & 2 deletions wallmapper/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Tue Mar 05 22:01:54 CST 2019
VERSION_CODE=495
#Tue Mar 05 22:36:37 CST 2019
VERSION_CODE=497

0 comments on commit fb77298

Please sign in to comment.