Skip to content

Commit

Permalink
Merge pull request #25 from lovoo/develop
Browse files Browse the repository at this point in the history
1.3.0 to master
  • Loading branch information
mario222k authored Nov 24, 2020
2 parents 0335625 + 9282053 commit f3d2cdb
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object AppStart {
intent.addCategory(Intent.CATEGORY_HOME)
val pm = InstrumentationRegistry.getInstrumentation().context.packageManager
val resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
return resolveInfo.activityInfo.packageName
return resolveInfo?.activityInfo?.packageName ?: ""
}

fun launchApp(device: UiDevice) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
android:name="androidx.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ buildscript {
ext.orchestrator_version = "1.2.0"
ext.androidX_test_version = "1.2.0"

ext.target_version = 29
ext.target_version = 30
ext.min_version = 18

def version = System.getenv("VERSION")
def code = System.getenv("VERSION_CODE")
ext.version_name = version == null ? "1.1.0" : version
ext.version_name = version == null ? "1.3.0" : version
ext.version_code = code == null ? 10 : code.toInteger()

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:3.6.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.jetbrains.dokka:dokka-gradle-plugin:0.9.18'
classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.27.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
Expand All @@ -38,6 +39,7 @@ import com.lovoo.android.pickcore.destination.PrivateDirectory
import com.lovoo.android.pickcore.destination.PublicDirectory
import com.lovoo.android.pickcore.loader.CameraLoader
import com.lovoo.android.pickcore.permission.Permission
import com.lovoo.android.pickcore.util.isMinimumR

/**
* Ready to use solution to handle Android Camera capture.
Expand Down Expand Up @@ -120,6 +122,15 @@ class PickPicCaptureFragment : DialogFragment() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE) {
val preferences = requireContext().getSharedPreferences(PREF_CATEGORY, Context.MODE_PRIVATE)
if (isMinimumR()) {
grantResults.forEach {
preferences.edit().putBoolean(PREF_KEY_PERMISSION, it != PackageManager.PERMISSION_DENIED).apply()
}
} else {
preferences.edit().putBoolean(PREF_KEY_PERMISSION, false).apply()
}

val deniedPermissions = Permission.getDeniedPermissions(requireActivity(), Permission.cameraPermissions.plus(Permission.galleryPermissions))
if (deniedPermissions.isEmpty()) {
startCamera()
Expand All @@ -138,7 +149,6 @@ class PickPicCaptureFragment : DialogFragment() {
if (!isFirstTime && deniedList.firstOrNull { it.second } != null) {
Permission.openSettings(requireActivity())
} else {
preferences.edit().putBoolean(PREF_KEY_PERMISSION, false).apply()
val permissions = Array(deniedList.size) { i -> deniedList[i].first }
Permission.requestPermissions(this, PERMISSION_REQUEST_CODE, permissions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.lovoo.android.pickcore.contract.CameraDestination
import com.lovoo.android.pickcore.destination.PrivateDirectory
import com.lovoo.android.pickcore.destination.moveToPublicDirectory
import com.lovoo.android.pickcore.loader.CameraLoader
import com.lovoo.android.pickcore.util.isMinimumQ
import io.reactivex.Single
import java.io.File

Expand All @@ -55,7 +56,7 @@ class CaptureResultWorker(

override fun createWork(): Single<Result> {
return Single.create<Result> { emitter ->
val file = if (isPublic) inputFile else inputFile.moveToPublicDirectory().file
val file = if (isPublic || isMinimumQ()) inputFile else inputFile.moveToPublicDirectory().file

if (file == null) {
context.sendBroadcast(Intent(INTENT_ACTION_ON_RESULT))
Expand Down
6 changes: 6 additions & 0 deletions pickcore/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:remove="android:maxSdkVersion" />

<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.Fragment
import com.lovoo.android.pickcore.contract.CameraDestination
import com.lovoo.android.pickcore.contract.getUri
import com.lovoo.android.pickcore.util.aboveQ
import com.lovoo.android.pickcore.util.isMinimumQ
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand Down Expand Up @@ -133,8 +133,8 @@ object CameraLoader {

val shouldScale = scale < 0.9f
val shouldRotate = degree % 360 != 0
if (!shouldScale && !shouldRotate) {
updateMediaScanner(context, arrayOf(filePath), listener)
if (!shouldScale && !shouldRotate && !isMinimumQ()) {
updateMediaScanner(context, arrayOf(filePath), null, listener)
return
}

Expand All @@ -159,31 +159,35 @@ object CameraLoader {
val appName = context.getString(context.applicationInfo.labelRes)

// Calculate new relative path for Q and before Q
val newFilePath = if (aboveQ()) {
val tempPath = filePath.replace(".jpg", "-2.jpg")
val newFilePath = if (isMinimumQ()) {
val tempPath = filePath // .replace(".jpg", "-2.jpg")
val lastItem = tempPath.split("/").last()
tempPath.replaceAfterLast("/", "$appName/$lastItem")
} else {
filePath.replace(".jpg", "-2.jpg")
filePath // .replace(".jpg", "-2.jpg")
}

// Create the new file with specified path
val file = File(newFilePath)

var fileUri: Uri? = null
var fos: OutputStream? = null
try {
// Separate logic for Q and before Q
if (aboveQ()) {
if (isMinimumQ()) {
// Prepare file values for insertion
val values = ContentValues().apply {
val now = System.currentTimeMillis()
put(MediaStore.Images.Media.DISPLAY_NAME, file.name)
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.DATE_ADDED, now)
put(MediaStore.MediaColumns.DATE_TAKEN, now)
put(MediaStore.Images.Media.RELATIVE_PATH, "${Environment.DIRECTORY_PICTURES}/$appName")
}

// Prepare OutputStream for insertion
val uri: Uri? = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
uri?.let { fos = context.contentResolver.openOutputStream(it) }
fileUri = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
fileUri?.let { fos = context.contentResolver.openOutputStream(it) }
} else {
// Create new FileOutputStream
fos = FileOutputStream(file)
Expand All @@ -197,24 +201,21 @@ object CameraLoader {
fos?.close()

// Signal new image was added
updateMediaScanner(context, arrayOf(filePath, file.absolutePath), listener)
updateMediaScanner(context, arrayOf(file.absolutePath), fileUri, listener)
}
} catch (e: IOException) {
e.printStackTrace()
}
}

// Notify listeners a new image was added
private fun updateMediaScanner(context: Context, files: Array<String>, listener: MediaScannerConnection.OnScanCompletedListener) {
private fun updateMediaScanner(context: Context, files: Array<String>, fileUri: Uri?, listener: MediaScannerConnection.OnScanCompletedListener) {
MediaScannerConnection.scanFile(context, files, null) { path, uri ->
listener.onScanCompleted(path, uri)
val curUri = uri ?: fileUri ?: Uri.parse(path)
listener.onScanCompleted(path, curUri)
context.sendBroadcast(Intent(INTENT_INVALIDATE_GALLERY))
if (!aboveQ()) {
context.sendBroadcast(Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri
?: Uri.parse(path)
)
)
if (!isMinimumQ()) {
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, curUri))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import android.provider.MediaStore
import androidx.loader.content.CursorLoader
import com.lovoo.android.pickcore.Constants
import com.lovoo.android.pickcore.model.GalleryLib
import com.lovoo.android.pickcore.util.aboveQ
import com.lovoo.android.pickcore.util.isMinimumQ

/**
* A [CursorLoader] implementation that fetch album information from external [MediaStore.Files]
Expand All @@ -44,10 +44,10 @@ class GalleryLoader(
projection,
selection,
selectArguments,
"${MediaStore.Images.Media.DATE_TAKEN} DESC"
"${MediaStore.Images.Media.DISPLAY_NAME} ASC"
) {

private val columns = if (aboveQ()) arrayOf(
private val columns = if (isMinimumQ()) arrayOf(
MediaStore.Files.FileColumns._ID,
COLUMN_NAME_ID,
COLUMN_NAME_DISPLAY_NAME,
Expand All @@ -65,7 +65,7 @@ class GalleryLoader(
return try {
val galleries = super.loadInBackground()
val allEntry = MatrixCursor(columns)
if (aboveQ()) {
if (isMinimumQ()) {
loadCursorPostQ(galleries, allEntry)
} else {
loadCursorPreQ(galleries, allEntry)
Expand Down Expand Up @@ -159,7 +159,7 @@ class GalleryLoader(
private const val COLUMN_NAME_COUNT = "count"

private val query = MediaStore.Files.getContentUri("external")
private val projection = if (aboveQ()) arrayOf(
private val projection = if (isMinimumQ()) arrayOf(
MediaStore.Files.FileColumns._ID,
COLUMN_NAME_ID,
COLUMN_NAME_DISPLAY_NAME,
Expand All @@ -172,7 +172,7 @@ class GalleryLoader(
MediaStore.MediaColumns.DATA,
"COUNT(*) AS $COLUMN_NAME_COUNT"
)
private val group = if (aboveQ()) "" else ") GROUP BY ($COLUMN_NAME_ID"
private val group = if (isMinimumQ()) "" else ") GROUP BY ($COLUMN_NAME_ID"
private val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE}=? AND ${MediaStore.MediaColumns.SIZE}>0$group"
private val selectArguments = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString())

Expand All @@ -190,7 +190,7 @@ class GalleryLoader(
* @param cursor the [Cursor]
* @return the [GalleryLib] object with the data from the [Cursor]
*/
fun convert(cursor: Cursor) = if (aboveQ()) GalleryLib(
fun convert(cursor: Cursor) = if (isMinimumQ()) GalleryLib(
cursor.getString(cursor.getColumnIndex(COLUMN_NAME_ID)),
getUri(cursor)?.toString(),
cursor.getString(cursor.getColumnIndex(COLUMN_NAME_DISPLAY_NAME)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PictureLoader(
projection,
getSelection(gallery),
getSelectionArgs(gallery),
"${MediaStore.Images.Media.DATE_TAKEN} DESC"
"${MediaStore.Images.Media.DATE_ADDED} DESC"
) {

private val cameraEngine: CameraEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ package com.lovoo.android.pickcore.util

import android.os.Build

fun aboveQ() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
fun isMinimumQ() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

fun isMinimumR() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R


0 comments on commit f3d2cdb

Please sign in to comment.