fields: refactor more

pull/653/merge
Varun Patil 2023-08-21 03:12:41 -07:00
parent 56a1ec6cf6
commit 23b0f4b102
4 changed files with 81 additions and 55 deletions

View File

@ -185,7 +185,7 @@ import java.net.URLDecoder
@JavascriptInterface
fun configSetLocalFolders(json: String?) {
if (json == null) return;
query.configSetLocalFolders(json)
query.localFolders = JSONArray(json)
}
@Throws(Exception::class)
@ -210,7 +210,7 @@ import java.net.URLDecoder
} else if (path.matches(API.SHARE_LOCAL)) {
return makeResponse(dlService!!.shareLocal(parts[4].toLong()))
} else if (path.matches(API.CONFIG_LOCAL_FOLDES)) {
return makeResponse(query.getLocalFoldersConfig())
return makeResponse(query.localFolders)
} else {
throw Exception("Not Found")
}

View File

@ -1,5 +1,7 @@
package gallery.memories.mapper
import androidx.exifinterface.media.ExifInterface
class Fields {
object Day {
const val DAYID = Photo.DAYID
@ -27,4 +29,30 @@ class Fields {
object Perm {
const val DELETE = "D"
}
object EXIF {
val MAP = mapOf(
ExifInterface.TAG_APERTURE_VALUE to "Aperture",
ExifInterface.TAG_FOCAL_LENGTH to "FocalLength",
ExifInterface.TAG_F_NUMBER to "FNumber",
ExifInterface.TAG_SHUTTER_SPEED_VALUE to "ShutterSpeed",
ExifInterface.TAG_EXPOSURE_TIME to "ExposureTime",
ExifInterface.TAG_ISO_SPEED to "ISO",
ExifInterface.TAG_DATETIME_ORIGINAL to "DateTimeOriginal",
ExifInterface.TAG_OFFSET_TIME_ORIGINAL to "OffsetTimeOriginal",
ExifInterface.TAG_GPS_LATITUDE to "GPSLatitude",
ExifInterface.TAG_GPS_LONGITUDE to "GPSLongitude",
ExifInterface.TAG_GPS_ALTITUDE to "GPSAltitude",
ExifInterface.TAG_MAKE to "Make",
ExifInterface.TAG_MODEL to "Model",
ExifInterface.TAG_ORIENTATION to "Orientation",
ExifInterface.TAG_IMAGE_DESCRIPTION to "Description"
)
}
object Bucket {
const val ID = "id"
const val NAME = "name"
const val ENABLED = "enabled"
}
}

View File

@ -0,0 +1,24 @@
package gallery.memories.service
import android.content.Context
import gallery.memories.R
class ConfigService(private val mCtx: Context) {
companion object {
private var mEnabledBuckets: Set<String>? = null
}
var enabledBucketIds: Set<String>
get() {
if (mEnabledBuckets != null) return mEnabledBuckets!!
mEnabledBuckets = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0)
.getStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), null) ?: setOf()
return mEnabledBuckets!!
}
set(value) {
mEnabledBuckets = value
mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit()
.putStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), value)
.apply()
}
}

View File

@ -26,6 +26,7 @@ import java.util.concurrent.CountDownLatch
@UnstableApi class TimelineQuery(private val mCtx: MainActivity) {
private val mDbService = DbService(mCtx).initialize()
private val mConfigService = ConfigService(mCtx)
private val TAG = TimelineQuery::class.java.simpleName
// Photo deletion events
@ -33,9 +34,6 @@ import java.util.concurrent.CountDownLatch
var deleteIntentLauncher: ActivityResultLauncher<IntentSenderRequest>
var deleteCallback: ((ActivityResult?) -> Unit)? = null
// Caches
var mEnabledBuckets: Set<String>? = null
// Observers
var imageObserver: ContentObserver? = null
var videoObserver: ContentObserver? = null
@ -110,7 +108,7 @@ import java.util.concurrent.CountDownLatch
@Throws(JSONException::class)
fun getByDayId(dayId: Long): JSONArray {
// Get the photos for the day from DB
val dbPhotos = mDbService.getPhotosByDay(dayId, getEnabledBucketIds())
val dbPhotos = mDbService.getPhotosByDay(dayId, mConfigService.enabledBucketIds)
val fileIds = dbPhotos.map { it.localId }.toMutableList()
if (fileIds.isEmpty()) return JSONArray()
@ -131,7 +129,7 @@ import java.util.concurrent.CountDownLatch
@Throws(JSONException::class)
fun getDays(): JSONArray {
return mDbService.getDays(getEnabledBucketIds()).map { day -> day.json }.let { JSONArray(it) }
return mDbService.getDays(mConfigService.enabledBucketIds).map { day -> day.json }.let { JSONArray(it) }
}
@Throws(Exception::class)
@ -155,23 +153,11 @@ import java.util.concurrent.CountDownLatch
try {
val exif = ExifInterface(image.dataPath)
obj.put(Fields.Photo.EXIF, JSONObject()
.put("Aperture", exif.getAttribute(ExifInterface.TAG_APERTURE_VALUE))
.put("FocalLength", exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH))
.put("FNumber", exif.getAttribute(ExifInterface.TAG_F_NUMBER))
.put("ShutterSpeed", exif.getAttribute(ExifInterface.TAG_SHUTTER_SPEED_VALUE))
.put("ExposureTime", exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME))
.put("ISO", exif.getAttribute(ExifInterface.TAG_ISO_SPEED))
.put("DateTimeOriginal", exif.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL))
.put("OffsetTimeOriginal", exif.getAttribute(ExifInterface.TAG_OFFSET_TIME_ORIGINAL))
.put("GPSLatitude", exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE))
.put("GPSLongitude", exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE))
.put("GPSAltitude", exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE))
.put("Make", exif.getAttribute(ExifInterface.TAG_MAKE))
.put("Model", exif.getAttribute(ExifInterface.TAG_MODEL))
.put("Orientation", exif.getAttribute(ExifInterface.TAG_ORIENTATION))
.put("Description", exif.getAttribute(ExifInterface.TAG_IMAGE_DESCRIPTION))
)
obj.put(Fields.Photo.EXIF, JSONObject().apply {
Fields.EXIF.MAP.forEach { (key, field) ->
put(field, exif.getAttribute(key))
}
})
} catch (e: IOException) {
Log.w(TAG, "Error reading EXIF data for $id")
}
@ -300,36 +286,24 @@ import java.util.concurrent.CountDownLatch
Log.v(TAG, "Inserted file to local DB: $fileId / $baseName")
}
fun getEnabledBucketIds(): Set<String> {
if (mEnabledBuckets != null) return mEnabledBuckets!!
mEnabledBuckets = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0)
.getStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), null) ?: setOf()
return mEnabledBuckets!!
}
fun getLocalFoldersConfig(): JSONArray {
val enabledSet = getEnabledBucketIds()
return mDbService.getBuckets().map {
JSONObject()
.put("id", it.key)
.put("name", it.value)
.put("enabled", enabledSet.contains(it.key))
}.let { JSONArray(it) }
}
fun configSetLocalFolders(json: String) {
val enabledSet = mutableSetOf<String>()
val array = JSONArray(json)
for (i in 0 until array.length()) {
val obj = array.getJSONObject(i)
if (obj.getBoolean("enabled")) {
enabledSet.add(obj.getLong("id").toString())
}
/** This is in timeline query because it calls the database service */
var localFolders: JSONArray
get() {
return mDbService.getBuckets().map {
JSONObject()
.put(Fields.Bucket.ID, it.key)
.put(Fields.Bucket.NAME, it.value)
.put(Fields.Bucket.ENABLED, mConfigService.enabledBucketIds.contains(it.key))
}.let { JSONArray(it) }
}
set(value) {
val enabledSet = mutableSetOf<String>()
for (i in 0 until value.length()) {
val obj = value.getJSONObject(i)
if (obj.getBoolean(Fields.Bucket.ENABLED)) {
enabledSet.add(obj.getString(Fields.Bucket.ID))
}
}
mConfigService.enabledBucketIds = enabledSet
}
mEnabledBuckets = enabledSet
mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit()
.putStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), enabledSet)
.apply()
}
}