This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- storage adapter using external storage - controller/model adapter for android - base64 platform independent - picture selector - some refactoring
- Loading branch information
Showing
70 changed files
with
617 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
android/src/main/java/de/x4fyr/paiman/app/adapter/AndroidBase64Encoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.x4fyr.paiman.app.adapter | ||
|
||
import android.util.Base64 | ||
|
||
/** | ||
* Encoder using [android.util.Base64] | ||
*/ | ||
class AndroidBase64Encoder : Base64Encoder() { | ||
override fun encodeToString(byteArray: ByteArray): String = Base64.encodeToString(byteArray, Base64.DEFAULT) | ||
} |
113 changes: 108 additions & 5 deletions
113
android/src/main/java/de/x4fyr/paiman/app/service/AndroidPictureSelectorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,114 @@ | ||
package de.x4fyr.paiman.app.service | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.CursorLoader | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Parcelable | ||
import android.provider.MediaStore | ||
import android.util.Log | ||
import de.x4fyr.paiman.app.WebViewWrapperActivity | ||
import de.x4fyr.paiman.app.services.PictureSelectorService | ||
import java.io.File | ||
import java.io.InputStream | ||
|
||
class AndroidPictureSelectorService: PictureSelectorService { | ||
/** Open system dependent dialog to get a jpeg picture */ | ||
override fun pickPicture(onReturn: (InputStream?) -> Unit) { | ||
TODO("not implemented") | ||
/** | ||
* Taken an modified from https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2 | ||
*/ | ||
class AndroidPictureSelectorService(private var owningActivity: WebViewWrapperActivity) : PictureSelectorService { | ||
|
||
companion object { | ||
private const val TAG = "ImagePicker" | ||
private const val TEMP_IMAGE_NAME = "tempImage" | ||
private const val PICK_IMAGE_ID = 234 //the number doesn't matter | ||
} | ||
|
||
/** Action on return of the picker */ | ||
private var onReturn: ((stream: InputStream?) -> Unit)? = null | ||
|
||
|
||
init { | ||
owningActivity.addActivityResultHandler(this) { requestCode, resultCode, intent -> | ||
if (requestCode == PICK_IMAGE_ID) { | ||
val url = getUrlFromResult(owningActivity, resultCode, intent) | ||
val stream = getInputStreamFromUrl(url) | ||
onReturn?.invoke(stream) | ||
} | ||
} | ||
} | ||
|
||
override fun pickPicture(onReturn: (stream: InputStream?) -> Unit) { | ||
owningActivity.startActivityForResult(getPickImageIntent(), PICK_IMAGE_ID) | ||
this.onReturn = onReturn | ||
} | ||
|
||
|
||
private fun getPickImageIntent(): Intent? { | ||
var chooserIntent: Intent? = null | ||
|
||
var intentList: MutableList<Intent> = ArrayList() | ||
|
||
val pickIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) | ||
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) | ||
takePhotoIntent.putExtra("return-data", true) | ||
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(owningActivity))) | ||
intentList = addIntentsToList(owningActivity, intentList, pickIntent) | ||
intentList = addIntentsToList(owningActivity, intentList, takePhotoIntent) | ||
|
||
if (intentList.size > 0) { | ||
chooserIntent = Intent.createChooser(intentList.removeAt(intentList.size - 1), "Pick Image") | ||
chooserIntent!!.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toTypedArray<Parcelable>()) | ||
} | ||
|
||
return chooserIntent | ||
} | ||
} | ||
|
||
private fun addIntentsToList(context: Context, list: MutableList<Intent>, intent: Intent): MutableList<Intent> { | ||
val resInfo = context.packageManager.queryIntentActivities(intent, 0) | ||
for (resolveInfo in resInfo) { | ||
val packageName = resolveInfo.activityInfo.packageName | ||
val targetedIntent = Intent(intent) | ||
targetedIntent.`package` = packageName | ||
list.add(targetedIntent) | ||
Log.d(TAG, "Intent: " + intent.action + " package: " + packageName) | ||
} | ||
return list | ||
} | ||
|
||
private fun getTempFile(context: Context): File { | ||
val imageFile = File(context.externalCacheDir ?: context.cacheDir, TEMP_IMAGE_NAME) | ||
imageFile.parentFile.mkdirs() | ||
return imageFile | ||
} | ||
|
||
private fun getUrlFromResult(context: Context, resultCode: Int, imageReturnedIntent: Intent?): String { | ||
val imageFile: File = getTempFile(context) | ||
var selectedImage: String? = null | ||
if (resultCode == Activity.RESULT_OK) { | ||
val intentDataString = imageReturnedIntent?.data?.toString() | ||
if (intentDataString == null || intentDataString.contains(imageFile.toString())) { | ||
/** CAMERA **/ | ||
selectedImage = imageFile.path | ||
} else { | ||
/** ALBUM **/ | ||
selectedImage = if (imageReturnedIntent.data.toString().contains("content:/")) { | ||
val projection: Array<String> = Array(1) { MediaStore.Images.Media.DATA } | ||
val cursor = CursorLoader(context, imageReturnedIntent.data, projection, null, null, | ||
null).loadInBackground() | ||
val idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) | ||
cursor.moveToFirst() | ||
cursor.getString(idx) | ||
} else { | ||
imageReturnedIntent.data.path | ||
} | ||
} | ||
} | ||
return selectedImage ?: "" | ||
} | ||
|
||
|
||
private fun getInputStreamFromUrl(url: String): InputStream? = File(url) | ||
.takeIf { it.exists() && it.canRead() } | ||
?.inputStream()?.takeIf { it.available() > 0 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
android/src/main/java/de/x4fyr/paiman/app/ui/controller.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package de.x4fyr.paiman.app.ui | ||
|
||
import android.webkit.JavascriptInterface | ||
import de.x4fyr.paiman.app.services.PictureSelectorService | ||
import de.x4fyr.paiman.app.services.WebViewService | ||
import de.x4fyr.paiman.app.ui.views.entry.EntryController | ||
import de.x4fyr.paiman.app.ui.views.overview.OverviewController | ||
import de.x4fyr.paiman.app.ui.views.overview.OverviewModel | ||
import de.x4fyr.paiman.app.ui.views.paintingDetail.PaintingDetailController | ||
import de.x4fyr.paiman.app.ui.views.paintingDetail.PaintingDetailFactory | ||
import de.x4fyr.paiman.app.ui.views.paintingDetail.PaintingDetailModel | ||
|
||
class PaintingDetailControllerAndroidAdapter( | ||
webViewService: WebViewService, | ||
model: PaintingDetailModel, | ||
returnController: Controller, | ||
pictureSelectorService: PictureSelectorService | ||
) : PaintingDetailController( webViewService, model, returnController, pictureSelectorService ) { | ||
|
||
/** Callback: return to [returnController] */ | ||
@JavascriptInterface | ||
override fun back() = super.back() | ||
|
||
/** Callback: add tag */ | ||
@JavascriptInterface | ||
override fun addTag(tag: String) = super.addTag(tag) | ||
|
||
/** Callback: add wip */ | ||
@JavascriptInterface | ||
override fun addWIP() = super.addWIP() | ||
|
||
/** Callback: add ref */ | ||
@JavascriptInterface | ||
override fun addRef() = super.addRef() | ||
|
||
/** Callback: finishing */ | ||
@JavascriptInterface | ||
override fun finishing(year: Int, month: Int) = super.finishing(year, month) | ||
} | ||
|
||
/** Adapter for [OverviewController] to make functions available as javascript interface */ | ||
class OverviewControllerAndroidAdapter(private val webViewService: WebViewService, | ||
private val model: OverviewModel, | ||
private val paintingDetailFactory: PaintingDetailFactory, | ||
private val pictureSelectorService: PictureSelectorService) | ||
: OverviewController(webViewService, model, paintingDetailFactory, pictureSelectorService) { | ||
|
||
/** Callback: Refresh previews */ | ||
@JavascriptInterface | ||
override fun refresh() = super.refresh() | ||
|
||
/** Callback: Open detail view of given painting by [id] */ | ||
@JavascriptInterface | ||
override fun openPainting(id: String) = super.openPainting(id) | ||
|
||
@JavascriptInterface | ||
override fun addPainting(title: String?) = super.addPainting(title) | ||
|
||
@JavascriptInterface | ||
override fun selectImage() = super.selectImage() | ||
} | ||
|
||
/** Adapter for [EntryController] to make functions available as javascript interface */ | ||
class EntryControllerAndroidAdapter(webViewService: WebViewService, mainViewController: OverviewController): EntryController(webViewService, mainViewController) { | ||
|
||
@JavascriptInterface | ||
override fun openNext() = super.openNext() | ||
} |
15 changes: 0 additions & 15 deletions
15
android/src/main/java/de/x4fyr/paiman/app/ui/controller/EntryViewControllerAndroidAdapter.kt
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
android/src/main/java/de/x4fyr/paiman/app/ui/controller/MainViewControllerAndroidAdapter.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package de.x4fyr.paiman.app.ui.model | ||
|
||
import android.webkit.JavascriptInterface | ||
import de.x4fyr.paiman.app.adapter.Base64Encoder | ||
import de.x4fyr.paiman.app.ui.views.overview.OverviewModel | ||
import de.x4fyr.paiman.app.ui.views.paintingDetail.PaintingDetailModel | ||
import de.x4fyr.paiman.lib.services.PaintingService | ||
import de.x4fyr.paiman.lib.services.QueryService | ||
|
||
class OverviewModelAndroidAdapter( | ||
paintingService: PaintingService, | ||
queryService: QueryService, | ||
base64Encoder: Base64Encoder | ||
) : OverviewModel(paintingService, queryService, base64Encoder) { | ||
@JavascriptInterface | ||
override fun getPreviews(): String = super.getPreviews() | ||
} | ||
|
||
class PaintingDetailModelAndroidAdapter( | ||
paintingService: PaintingService, | ||
id: String, | ||
base64Encoder: Base64Encoder | ||
) : PaintingDetailModel(paintingService, id, base64Encoder) { | ||
@JavascriptInterface | ||
override fun getHolder(): String = super.getHolder() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.