Skip to content

Commit

Permalink
update share and save executors
Browse files Browse the repository at this point in the history
  • Loading branch information
jclark118 committed Aug 11, 2023
1 parent bec1c02 commit 5a15a7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
25 changes: 22 additions & 3 deletions mapcache/src/main/java/mil/nga/mapcache/load/ShareCopyExecutor.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package mil.nga.mapcache.load

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.FragmentActivity
import androidx.core.content.FileProvider
import mil.nga.geopackage.GeoPackageConstants
import mil.nga.geopackage.io.GeoPackageIOUtils
import mil.nga.mapcache.BuildConfig
import mil.nga.mapcache.R
import java.io.File
import java.io.IOException
Expand All @@ -18,11 +22,13 @@ import java.util.concurrent.Executors
/**
* Copy an internal database to a shareable location and share
*/
class ShareCopyExecutor(val activity : FragmentActivity) {
class ShareCopyExecutor(val activity : Activity, val shareIntent : Intent) {

private val alertDialog: AlertDialog
private val myExecutor: ExecutorService = Executors.newSingleThreadExecutor()
private var geoPackageName : String = ""
private val AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider"


init {
val builder = AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
Expand Down Expand Up @@ -62,13 +68,26 @@ class ShareCopyExecutor(val activity : FragmentActivity) {
try {
GeoPackageIOUtils.copyFile(gpkgFile, cacheFile)
} catch (e: IOException) {

//TODO better handle exceptions
alertDialog.dismiss()
} catch (e: InterruptedException){
Thread.currentThread().interrupt()
}
handler.post {
alertDialog.dismiss()
// Create the content Uri and add intent permissions
val databaseUri = FileProvider.getUriForFile(activity, AUTHORITY, cacheFile)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
launchShareIntent(shareIntent, databaseUri)
}
}
}

private fun launchShareIntent(shareIntent: Intent, databaseUri: Uri) {
shareIntent.putExtra(Intent.EXTRA_STREAM, databaseUri)
activity.startActivityForResult(
Intent.createChooser(shareIntent, "Share"),
ShareTask.ACTIVITY_SHARE_FILE
)
}
}
19 changes: 9 additions & 10 deletions mapcache/src/main/java/mil/nga/mapcache/load/ShareTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ private void shareDatabaseOption() {
// Launch the share copy task

//TODO: change to ShareCopyExecutor
ShareCopyTask shareCopyTask = new ShareCopyTask(shareIntent);
shareCopyTask.execute(geoPackageFile, geoPackageName);
ShareCopyExecutor shareCopyExecutor = new ShareCopyExecutor(activity, shareIntent);
shareCopyExecutor.shareGeoPackage(getDatabaseCacheDirectory(), geoPackageFile, geoPackageName);
// ShareCopyTask shareCopyTask = new ShareCopyTask(shareIntent);
// shareCopyTask.execute(geoPackageFile, geoPackageName);
}
} catch (Exception e) {
GeoPackageUtils.showMessage(activity, "Error sharing GeoPackage", e.getMessage());
Expand All @@ -117,11 +119,11 @@ private void saveDatabaseOption(){
shareIntent.setAction(Intent.ACTION_SEND);

// Launch the save to disk task
//TODO: Switch to SaveToDiskExecutor
// SaveToDiskExecutor diskExecutor = new SaveToDiskExecutor(activity);
// diskExecutor.saveToDisk(getDatabaseCacheDirectory(), geoPackageFile, geoPackageName);
SaveToDiskTask saveTask = new SaveToDiskTask(shareIntent);
saveTask.execute(geoPackageFile, geoPackageName);
//TODO: Switch to SaveToDiskExecutor
SaveToDiskExecutor diskExecutor = new SaveToDiskExecutor(activity);
diskExecutor.saveToDisk(getDatabaseCacheDirectory(), geoPackageFile, geoPackageName);
// SaveToDiskTask saveTask = new SaveToDiskTask(shareIntent);
// saveTask.execute(geoPackageFile, geoPackageName);
} catch (Exception e) {
GeoPackageUtils.showMessage(activity, "Error saving to file", e.getMessage());
}
Expand Down Expand Up @@ -197,10 +199,7 @@ private File getDatabaseCacheDirectory() {
* @param databaseUri
*/
private void launchShareIntent(Intent shareIntent, Uri databaseUri) {
// Add the Uri
shareIntent.putExtra(Intent.EXTRA_STREAM, databaseUri);

// Start the share activity for result to delete the cache when done
activity.startActivityForResult(Intent.createChooser(shareIntent, "Share"), ACTIVITY_SHARE_FILE);
}

Expand Down

0 comments on commit 5a15a7c

Please sign in to comment.