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

View File

@ -1,5 +1,7 @@
package gallery.memories.mapper package gallery.memories.mapper
import androidx.exifinterface.media.ExifInterface
class Fields { class Fields {
object Day { object Day {
const val DAYID = Photo.DAYID const val DAYID = Photo.DAYID
@ -27,4 +29,30 @@ class Fields {
object Perm { object Perm {
const val DELETE = "D" 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) { @UnstableApi class TimelineQuery(private val mCtx: MainActivity) {
private val mDbService = DbService(mCtx).initialize() private val mDbService = DbService(mCtx).initialize()
private val mConfigService = ConfigService(mCtx)
private val TAG = TimelineQuery::class.java.simpleName private val TAG = TimelineQuery::class.java.simpleName
// Photo deletion events // Photo deletion events
@ -33,9 +34,6 @@ import java.util.concurrent.CountDownLatch
var deleteIntentLauncher: ActivityResultLauncher<IntentSenderRequest> var deleteIntentLauncher: ActivityResultLauncher<IntentSenderRequest>
var deleteCallback: ((ActivityResult?) -> Unit)? = null var deleteCallback: ((ActivityResult?) -> Unit)? = null
// Caches
var mEnabledBuckets: Set<String>? = null
// Observers // Observers
var imageObserver: ContentObserver? = null var imageObserver: ContentObserver? = null
var videoObserver: ContentObserver? = null var videoObserver: ContentObserver? = null
@ -110,7 +108,7 @@ import java.util.concurrent.CountDownLatch
@Throws(JSONException::class) @Throws(JSONException::class)
fun getByDayId(dayId: Long): JSONArray { fun getByDayId(dayId: Long): JSONArray {
// Get the photos for the day from DB // 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() val fileIds = dbPhotos.map { it.localId }.toMutableList()
if (fileIds.isEmpty()) return JSONArray() if (fileIds.isEmpty()) return JSONArray()
@ -131,7 +129,7 @@ import java.util.concurrent.CountDownLatch
@Throws(JSONException::class) @Throws(JSONException::class)
fun getDays(): JSONArray { 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) @Throws(Exception::class)
@ -155,23 +153,11 @@ import java.util.concurrent.CountDownLatch
try { try {
val exif = ExifInterface(image.dataPath) val exif = ExifInterface(image.dataPath)
obj.put(Fields.Photo.EXIF, JSONObject() obj.put(Fields.Photo.EXIF, JSONObject().apply {
.put("Aperture", exif.getAttribute(ExifInterface.TAG_APERTURE_VALUE)) Fields.EXIF.MAP.forEach { (key, field) ->
.put("FocalLength", exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH)) put(field, exif.getAttribute(key))
.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))
)
} catch (e: IOException) { } catch (e: IOException) {
Log.w(TAG, "Error reading EXIF data for $id") 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") Log.v(TAG, "Inserted file to local DB: $fileId / $baseName")
} }
fun getEnabledBucketIds(): Set<String> { /** This is in timeline query because it calls the database service */
if (mEnabledBuckets != null) return mEnabledBuckets!! var localFolders: JSONArray
mEnabledBuckets = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0) get() {
.getStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), null) ?: setOf() return mDbService.getBuckets().map {
return mEnabledBuckets!! JSONObject()
} .put(Fields.Bucket.ID, it.key)
.put(Fields.Bucket.NAME, it.value)
fun getLocalFoldersConfig(): JSONArray { .put(Fields.Bucket.ENABLED, mConfigService.enabledBucketIds.contains(it.key))
val enabledSet = getEnabledBucketIds() }.let { JSONArray(it) }
}
return mDbService.getBuckets().map { set(value) {
JSONObject() val enabledSet = mutableSetOf<String>()
.put("id", it.key) for (i in 0 until value.length()) {
.put("name", it.value) val obj = value.getJSONObject(i)
.put("enabled", enabledSet.contains(it.key)) if (obj.getBoolean(Fields.Bucket.ENABLED)) {
}.let { JSONArray(it) } enabledSet.add(obj.getString(Fields.Bucket.ID))
} }
}
fun configSetLocalFolders(json: String) { mConfigService.enabledBucketIds = enabledSet
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())
}
} }
mEnabledBuckets = enabledSet
mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit()
.putStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), enabledSet)
.apply()
}
} }