Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Commit

Permalink
android: add GDrive asynchronous cache layer
Browse files Browse the repository at this point in the history
  • Loading branch information
bvolkmer committed Oct 21, 2017
1 parent 7f91da3 commit 58bdac9
Show file tree
Hide file tree
Showing 18 changed files with 721 additions and 179 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dependencies {
kapt 'com.google.dagger:dagger-compiler:2.11'
provided 'org.glassfish:javax.annotation:10.0-b28'
kapt 'com.google.dagger:dagger-android-processor:2.11'
compile 'com.jakewharton:disklrucache:2.0.2'
}

apply plugin: 'com.google.gms.google-services'
54 changes: 32 additions & 22 deletions android/src/main/java/de/x4fyr/paiman/app/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import android.util.Log
import android.util.LruCache
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import android.widget.TextView
import de.x4fyr.paiman.R
import de.x4fyr.paiman.lib.adapter.AndroidGoogleDriveStorageAdapter
import de.x4fyr.paiman.lib.adapter.StorageAdapter
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.getStackTraceString
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject

/** An dialog Fragment that has an apply action.
Expand Down Expand Up @@ -59,26 +61,6 @@ abstract class BaseActivity: AppCompatActivity() {
}
}

override fun onStart() {
storageAdapter.connect(this)
super.onStart()
}

override fun onResume() {
storageAdapter.connect(this)
super.onResume()
}

override fun onPause() {
storageAdapter.disconnect()
super.onPause()
}

override fun onStop() {
storageAdapter.disconnect()
super.onStop()
}

}

/** Safely get a valid stream from an android URL */
Expand Down Expand Up @@ -122,7 +104,8 @@ fun Activity.errorDialog(msg: Int, throwable: Throwable) {
try {
val view = layoutInflater.inflate(R.layout.dialog_error, null, false)
view.findViewById<TextView>(R.id.message).text = getString(msg)
view.findViewById<EditText>(R.id.exception).setText(throwable.message!!)
view.findViewById<TextView>(
R.id.exception).text = throwable.message!! + '\n' + throwable.getStackTraceString()
AlertDialog.Builder(this@errorDialog)
.setView(view)
.create()
Expand All @@ -141,4 +124,31 @@ class BitmapCache<K>(maxKbSize: Int): LruCache<K, Bitmap>(maxKbSize) {
override fun sizeOf(key: K, value: Bitmap): Int {
return value.byteCount/1024
}
}

/** Create [StorageAdapter.StorageException.General] with logging the error */
fun StorageAdapter.error(msg: String): StorageAdapter.StorageException.General {
Log.e(this::class.simpleName, msg)
return StorageAdapter.StorageException.General(msg)
}

/** Create [StorageAdapter.StorageException.General] including `cause` with logging the error */
fun StorageAdapter.error(msg: String, cause: Throwable): StorageAdapter.StorageException.General {
Log.e(this::class.simpleName, msg, cause)
return StorageAdapter.StorageException.General(msg, cause)
}

/** Writes an InputStream to an OutputStream */
fun InputStream.writeTo(outputStream: OutputStream) {
this.use { inputStream ->
outputStream.use { outputStream ->
val buffer = ByteArray(1024)
var bytesRead = inputStream.read(buffer)
while (bytesRead != -1) {
outputStream.write(buffer)
bytesRead = inputStream.read(buffer)
}
outputStream.flush()
}
}
}
4 changes: 3 additions & 1 deletion android/src/main/java/de/x4fyr/paiman/dagger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dagger.android.ContributesAndroidInjector
import dagger.android.DaggerApplication
import dagger.android.support.AndroidSupportInjectionModule
import de.x4fyr.paiman.lib.adapter.AndroidGoogleDriveStorageAdapter
import de.x4fyr.paiman.lib.adapter.CachedGoogleDriveStorageAdapter
import de.x4fyr.paiman.lib.provider.AndroidServiceProvider
import de.x4fyr.paiman.lib.provider.ServiceProvider
import de.x4fyr.paiman.lib.services.PaintingService
Expand Down Expand Up @@ -85,10 +86,11 @@ class ServicesModule {
storageAdapter
}


@Provides
@Singleton
fun provideAndroidServiceProvider(context: android.app.Application,
storageAdapter: AndroidGoogleDriveStorageAdapter): AndroidServiceProvider = try {
storageAdapter: CachedGoogleDriveStorageAdapter): AndroidServiceProvider = try {
provider
} catch (e: UninitializedPropertyAccessException) {
provider = AndroidServiceProvider(context, storageAdapter)
Expand Down
Loading

0 comments on commit 58bdac9

Please sign in to comment.