Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/2 #6

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.library) apply false
// alias(libs.plugins.secrets.gradle.plugin) apply false
}

Expand Down
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "ny.photomap.data"
compileSdk = 35

defaultConfig {
minSdk = 29

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.exifinterface)
testImplementation(libs.junit)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.robolectric)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
Empty file added data/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions data/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ny.photomap.data

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ny.photomap.data.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions data/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
54 changes: 54 additions & 0 deletions data/src/main/java/ny/photomap/data/PhotoDataSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ny.photomap.data

import android.content.ContentResolver
import android.content.ContentUris
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore

class PhotoDataSource(private val contentResolver: ContentResolver) {

private val projection = arrayOf(
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATE_TAKEN
)

private val order =
"${MediaStore.Images.Media.DATE_TAKEN}, ${MediaStore.Images.Media.DATE_ADDED}, ${MediaStore.Images.Media.DATE_MODIFIED} DESC"

fun getAllPhotoUriList(): List<Uri> = getPhotoUriList(null, null)
fun getDateRangePhotoUriList(startMilliSecond: Long, endMilliSecond: Long): List<Uri> =
getPhotoUriList(
"${MediaStore.Images.Media.DATE_TAKEN} BETWEEN ? AND ?",
arrayOf(startMilliSecond.toString(), endMilliSecond.toString())
)

private fun getPhotoUriList(selection: String?, selectionArgs: Array<String>?): List<Uri> {
val photoUriList = mutableListOf<Uri>()
try {
val query: Cursor? = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
order,
null
)

query?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)

while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
val uri =
ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
photoUriList.add(uri)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return photoUriList
}

}
83 changes: 83 additions & 0 deletions data/src/main/java/ny/photomap/data/PhotoRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ny.photomap.data

import android.content.ContentResolver
import android.net.Uri
import androidx.exifinterface.media.ExifInterface
import ny.photomap.model.PhotoInfo

class PhotoRepository(val contentResolver: ContentResolver, val dataSource: PhotoDataSource) {

fun getAllPhotoInfoList(): List<PhotoInfo> {
return dataSource.getAllPhotoUriList().mapNotNull {
convertPhotoInfo(it)
}
}

fun getDateRangePhotoInfoList(startMilliSecond: Long, endMilliSecond: Long): List<PhotoInfo> {
return dataSource.getDateRangePhotoUriList(
startMilliSecond = startMilliSecond,
endMilliSecond = endMilliSecond
).mapNotNull {
convertPhotoInfo(it)
}
}

fun getLocationPhotoInfoList(
targetLatitude: Double,
targetLongitude: Double,
surroundRange: Double,
): List<PhotoInfo> {
return dataSource.getAllPhotoUriList().mapNotNull {
convertPhotoInfo(
uri = it,
targetLatitude = targetLatitude,
targetLongitude = targetLongitude,
surroundRange = surroundRange
)
}
}

fun convertPhotoInfo(uri: Uri): PhotoInfo? = getExifInterface(uri)?.let {
it.latLong?.let { (latitude, longitude) ->
PhotoInfo(
uri = uri,
latitude = latitude,
longitude = longitude,
generationTime = it.generationTime
)
}
}

fun convertPhotoInfo(
uri : Uri,
targetLatitude: Double,
targetLongitude: Double,
surroundRange: Double,
): PhotoInfo? =
getExifInterface(uri)?.let {
it.latLong?.let { (latitude, longitude) ->
// todo 근처 위치 판단 로직
if (targetLatitude in latitude - surroundRange..latitude + surroundRange
&& targetLongitude in longitude - surroundRange..longitude + surroundRange
) {
PhotoInfo(
uri = uri,
latitude = latitude,
longitude = longitude,
generationTime = it.generationTime
)
} else null
}
}


fun getExifInterface(uri: Uri): ExifInterface? =
contentResolver.openInputStream(uri)?.use { inputStream ->
ExifInterface(inputStream)
}

val ExifInterface.generationTime: String?
get() = getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)
?: getAttribute(ExifInterface.TAG_DATETIME)
?: getAttribute(ExifInterface.TAG_DATETIME_DIGITIZED)
}
10 changes: 10 additions & 0 deletions data/src/main/java/ny/photomap/model/PhotoInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ny.photomap.model

import android.net.Uri

data class PhotoInfo(
val uri: Uri,
val latitude: Double,
val longitude: Double,
val generationTime: String?,
)
17 changes: 17 additions & 0 deletions data/src/test/java/ny/photomap/data/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ny.photomap.data

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
Loading
Loading