Skip to content

Commit

Permalink
Update [0.1.4] - New feature: Random style for live wallpaper
Browse files Browse the repository at this point in the history
Added
- Change style randomly for live wallpaper
- New class [LiveWallpaper] in [DefaultValue]

Changed
- Detect if selectedIndex is changed in onResume() for [MainActivity]
  and [MapStyleManagerActivity]
- Sort out string resource of zh
- Minor text changed in About page

Fixed
- App may crash in some extreme situations like push back very quickly
  after launch, by adding a task list for [MapKit]
  • Loading branch information
lucka-me committed Mar 11, 2019
1 parent fb77298 commit 1bad459
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 45 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

<h1 align=center>Changelog</h1>

```markdown
## [0.1.4] - 2019-03-11
- 0.1.3(492) -> 0.1.4(523)
- New feature: Random style for live wallpaper

### Added
- Change style randomly for live wallpaper
- Following location for live wallpaper can be disabled now

### Changed
- App will request location permission when turn on the Follow Location instead
of open the Preview & Set page
- Minor text changed

### Fixed
- App may crash in some extreme situations like push back very quickly after
launch
```

```markdown
## [0.1.3] - 2019-03-03
- 0.1.2(476) -> 0.1.3(492)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align=center><img src="./Resource/Banner.svg"></img></h1>

<p align=center>
<a href="./CHANGELOG.md"><img alt="Version" src="https://img.shields.io/badge/version-0.1.3-yellow.svg"/></a>
<a href="./CHANGELOG.md"><img alt="Version" src="https://img.shields.io/badge/version-0.1.4-yellow.svg"/></a>
<a href="https://lucka.moe"><img alt="Author" src="https://img.shields.io/badge/author-Lucka-2578B5.svg"/></a>
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MPL_2.0-000000.svg"/></a><br>
<a href="https://www.android.com/versions/marshmallow-6-0/"><img alt="Minmum SDK 23" src="https://img.shields.io/badge/min_SDK-23-78C257.svg"/></a>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion wallmapper/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":492,"versionName":"0.1.3","enabled":true,"outputFile":"wallmapper-release.apk","fullName":"release","baseName":"release"},"path":"wallmapper-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":523,"versionName":"0.1.4","enabled":true,"outputFile":"wallmapper-release.apk","fullName":"release","baseName":"release"},"path":"wallmapper-release.apk","properties":{}}]
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class DefaultValue {
}
}

class LiveWallpaper {
companion object {
const val RANDOM_STYLE_INTERVAL: Int = 60
const val RADIUS: Float = 1F
}
}

enum class Request(val code: Int) {
RequestPermissionWriteExternalStorage(1001),
RequestPermissionFineLocation(1002),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
mapKit.onResume()
deactivateButtons()
mapKit.setStyle {
fabSnapshot.show()
activateButtons()
}
}

override fun onPause() {
Expand Down
67 changes: 53 additions & 14 deletions wallmapper/src/main/java/labs/lucka/wallmapper/MapKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import org.jetbrains.anko.defaultSharedPreferences
import java.util.*
import kotlin.collections.ArrayList
import kotlin.random.Random

class MapKit(private val context: Context) {

private lateinit var mapView: MapView
private lateinit var map: MapboxMap
private var mapInitialized: Boolean = false
private val preInitializationTaskList: ArrayList<() -> Unit> = arrayListOf()
private var selectedStyleIndex: MapStyleIndex = MapStyleIndex("", "", "")
private val snapshotKit: SnapshotKit
var snapshot: Bitmap? = null

Expand Down Expand Up @@ -53,14 +59,24 @@ class MapKit(private val context: Context) {
.tilt(tilt.toDouble())
.bearing(bearing.toDouble())
.build()
mapInitialized = true
preInitializationTaskList.forEach { task -> task() }
preInitializationTaskList.clear()
onMapReady(map)
setStyle()

}
}

fun setStyle(callback: () -> Unit = { }) {
val selectedStyleIndex = getSelectedStyleIndex(context)
if (!mapInitialized) {
preInitializationTaskList.add { setStyle(callback) }
return
}
val newSelectedStyleIndex: MapStyleIndex = getSelectedStyleIndex(context)
if (newSelectedStyleIndex == selectedStyleIndex) {
callback()
return
}
selectedStyleIndex = newSelectedStyleIndex
val styleBuilder: Style.Builder =
when (selectedStyleIndex.type) {

Expand All @@ -80,6 +96,11 @@ class MapKit(private val context: Context) {

fun takeSnapshot(width: Int, height: Int, onSnapshotReady: (Bitmap) -> Unit, onError: (String?) -> Unit) {

if (!mapInitialized) {
preInitializationTaskList.add { takeSnapshot(width, height, onSnapshotReady, onError) }
return
}

map.getStyle { style: Style ->
handleLabels(context, style)
snapshotKit.takeSnapshotJson(
Expand All @@ -102,14 +123,16 @@ class MapKit(private val context: Context) {
fun onResume () { mapView.onResume () }

fun onPause() {
val position = map.cameraPosition
context.defaultSharedPreferences.edit()
.putFloat(context.getString(R.string.pref_map_last_position_latitude), position.target.latitude.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_longitude), position.target.longitude.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_zoom), position.zoom.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_tilt), position.tilt.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_bearing), position.bearing.toFloat())
.apply()
if (mapInitialized) {
val position = map.cameraPosition
context.defaultSharedPreferences.edit()
.putFloat(context.getString(R.string.pref_map_last_position_latitude), position.target.latitude.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_longitude), position.target.longitude.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_zoom), position.zoom.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_tilt), position.tilt.toFloat())
.putFloat(context.getString(R.string.pref_map_last_position_bearing), position.bearing.toFloat())
.apply()
}
mapView.onPause()
snapshotKit.onPause()
}
Expand Down Expand Up @@ -196,7 +219,8 @@ class MapKit(private val context: Context) {
return
}
if (
context.defaultSharedPreferences.getInt(context.getString(R.string.pref_data_version), DefaultValue.DATA_VERSION)
context.defaultSharedPreferences
.getInt(context.getString(R.string.pref_data_version), DefaultValue.DATA_VERSION)
== DataKit.CURRENT_DATA_VERSION
) return

Expand Down Expand Up @@ -232,12 +256,27 @@ class MapKit(private val context: Context) {
}

fun getSelectedStyleIndex(context: Context): MapStyleIndex {
val selectedStyleIndex: Int = context.defaultSharedPreferences.getInt(
val selectedStyleIndex =
context.defaultSharedPreferences.getInt(
context.getString(R.string.pref_style_manager_selected_index), 0
)
MapKit.checkAndUpdateStyleList(context)
val mapStyleIndexList = DataKit.loadStyleIndexList(context)
return mapStyleIndexList[if (selectedStyleIndex >= mapStyleIndexList.size) 0 else selectedStyleIndex]
}

fun getRandomStyleIndex(context: Context): MapStyleIndex {
val selectedStyleIndex = context.defaultSharedPreferences.getInt(
context.getString(R.string.pref_style_manager_selected_index), 0
)
MapKit.checkAndUpdateStyleList(context)
val mapStyleIndexList = DataKit.loadStyleIndexList(context)
return mapStyleIndexList[if (selectedStyleIndex >= mapStyleIndexList.size) 0 else selectedStyleIndex]
var randomIndex = Random(Date().time).nextInt(0, mapStyleIndexList.size - 2)
if (randomIndex >= selectedStyleIndex) randomIndex++
context.defaultSharedPreferences.edit()
.putInt(context.getString(R.string.pref_style_manager_selected_index), randomIndex)
.apply()
return mapStyleIndexList[randomIndex]
}

fun handleLabels(context: Context, style: Style) {
Expand Down
12 changes: 12 additions & 0 deletions wallmapper/src/main/java/labs/lucka/wallmapper/MapStyleIndex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ data class MapStyleIndex(
ONLINE, LOCAL, CUSTOMIZED, MAPBOX, LUCKA
}

override fun equals(other: Any?): Boolean {
return if (other is MapStyleIndex) {
name == other.name
&& author == other.author
&& path == other.path
&& type == other.type
&& imagePath == other.imagePath
} else {
false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class MapStyleManagerActivity: AppCompatActivity() {
object : MapStyleManagerRecyclerViewAdapter.Listener {

override fun onSelectedIndexChanged(position: Int) {
resultIntent.putExtra(getString(R.string.activity_result_should_reset_style), true)
setResult(Activity.RESULT_OK, resultIntent)
lastSelectedIndex = position
}

override fun onSwipeToDelete(position: Int) {
Expand Down Expand Up @@ -103,6 +102,8 @@ class MapStyleManagerActivity: AppCompatActivity() {
}
private lateinit var snapshotKit: SnapshotKit
private var resultIntent: Intent = Intent()
private var initSelectedIndex: Int = 0
private var lastSelectedIndex: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -117,11 +118,15 @@ class MapStyleManagerActivity: AppCompatActivity() {
recyclerViewMapStyleList.layoutManager = LinearLayoutManager(this)
recyclerViewMapStyleList.adapter = recyclerViewAdapter
recyclerViewAdapter.attachItemTouchHelperTo(recyclerViewMapStyleList)

initSelectedIndex = defaultSharedPreferences.getInt(getString(R.string.pref_style_manager_selected_index), 0)
lastSelectedIndex = initSelectedIndex
}

override fun onResume() {
super.onResume()
resultIntent = Intent()
recyclerViewAdapter.updateSelectedIndex()
}

override fun onPause() {
Expand All @@ -130,6 +135,16 @@ class MapStyleManagerActivity: AppCompatActivity() {
super.onPause()
}

override fun onBackPressed() {
if (lastSelectedIndex != initSelectedIndex) {
defaultSharedPreferences
.edit().putInt(getString(R.string.pref_style_manager_selected_index), lastSelectedIndex).apply()
//resultIntent.putExtra(getString(R.string.activity_result_should_reset_style), true)
//setResult(Activity.RESULT_OK, resultIntent)
}
super.onBackPressed()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_style_manager, menu)
return super.onCreateOptionsMenu(menu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class MapStyleManagerRecyclerViewAdapter(
if (selectedIndex != position) {
val oldIndex = selectedIndex
selectedIndex = position
context.defaultSharedPreferences
.edit().putInt(context.getString(R.string.pref_style_manager_selected_index), selectedIndex).apply()
adapterListener.onSelectedIndexChanged(selectedIndex)
notifyItemChanged(oldIndex)
notifyItemChanged(selectedIndex)
Expand Down Expand Up @@ -145,16 +143,20 @@ class MapStyleManagerRecyclerViewAdapter(
}

fun updateSelectedIndex() {
selectedIndex = context.defaultSharedPreferences.getInt(
var newSelectedIndex = context.defaultSharedPreferences.getInt(
context.getString(R.string.pref_style_manager_selected_index), 0
)
if (selectedIndex >= mapStyleIndexList.size) {
selectedIndex = 0
if (newSelectedIndex == selectedIndex) return
if (newSelectedIndex >= mapStyleIndexList.size) {
newSelectedIndex = 0
context.defaultSharedPreferences.edit()
.putInt(context.getString(R.string.pref_style_manager_selected_index), selectedIndex)
.putInt(context.getString(R.string.pref_style_manager_selected_index), newSelectedIndex)
.apply()
}
notifyItemChanged(selectedIndex)
notifyItemChanged(newSelectedIndex)
selectedIndex = newSelectedIndex
adapterListener.onSelectedIndexChanged(selectedIndex)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ class PreferenceMainActivity : AppCompatActivity() {
setPreferencesFromResource(R.xml.preference_main, rootKey)

if (
ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.ACCESS_FINE_LOCATION
)
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()
}

// Set input type
val prefLiveRandomInterval =
findPreference<EditTextPreference>(getString(R.string.pref_live_wallpaper_random_style_interval))
prefLiveRandomInterval.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER
}
prefLiveRandomInterval.summaryProvider = Preference.SummaryProvider { preference: EditTextPreference ->
String.format(getString(R.string.pref_live_wallpaper_random_style_interval_summary), preference.text)
}
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 Down
Loading

0 comments on commit 1bad459

Please sign in to comment.