Skip to content

Commit

Permalink
remove viewmodel ref from shareTask
Browse files Browse the repository at this point in the history
  • Loading branch information
jclark118 committed Jul 31, 2023
1 parent c4f8ebd commit c9339bd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import org.jetbrains.annotations.NotNull;
import org.locationtech.proj4j.units.Units;

import java.io.File;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.text.DecimalFormat;
Expand Down Expand Up @@ -1076,7 +1077,10 @@ public void onRenameGP(String oldName, String newName) {
public void onShareGP(String gpName) {
// Set the geopackage name before we ask permissions and get routed back through MainActivity
// to exportGeoPackageToExternal()
File databaseFile = geoPackageViewModel.getDatabaseFile(gpName);
shareTask.setFileExternal(geoPackageViewModel.isExternal(gpName));
shareTask.setGeoPackageName(gpName);
shareTask.setGeoPackageFile(databaseFile);
getImportPermissions(MainActivity.MANAGER_PERMISSIONS_REQUEST_ACCESS_EXPORT_DATABASE);
}

Expand Down Expand Up @@ -1900,8 +1904,10 @@ public void importGeopackageFromFile() {
* Save a GeoPackage to external disk (after we've been given permission)
*/
public void exportGeoPackageToExternal() {
if (shareTask != null && shareTask.getGeoPackageName() != null) {
shareTask.askToSaveOrShare(shareTask.getGeoPackageName());

if (shareTask != null && shareTask.getGeoPackageName() != null &&
shareTask.getGeoPackageFile() != null) {
shareTask.askToSaveOrShare();
}
}

Expand Down
135 changes: 75 additions & 60 deletions mapcache/src/main/java/mil/nga/mapcache/load/ShareTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -51,53 +55,48 @@ public class ShareTask {
private Activity activity;

/**
* Need access to the viewModel to retrieve database files
* Name should be saved to the task
*/
private GeoPackageViewModel geoPackageViewModel;
private String geoPackageName;

/**
* Name should be saved to the task
* GeoPackage file for sharing
*/
private String geoPackageName;
private File geoPackageFile;

/**
* Is the saved geoPackage an external file
*/
private boolean isFileExternal = false;

public ShareTask(FragmentActivity activity) {
this.activity = activity;
geoPackageViewModel = ViewModelProviders.of(activity).get(GeoPackageViewModel.class);
}


/**
* Share database with external apps via intent
*
* @param database GeoPackage name
*/
public void shareDatabaseOption(final String database) {

private void shareDatabaseOption() {
try {
// Get the database file
File databaseFile = geoPackageViewModel.getDatabaseFile(database);

// Create the share intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("*/*");

// If external database, no permission is needed
if (geoPackageViewModel.isExternal(database)) {
if (isFileExternal) {
// Create the Uri and share
Uri databaseUri = FileProvider.getUriForFile(activity,
AUTHORITY,
databaseFile);
Uri databaseUri = FileProvider.getUriForFile(activity, AUTHORITY, geoPackageFile);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
launchShareIntent(shareIntent, databaseUri);
}
// If internal database, file must be copied to cache for permission
else {
// Launch the share copy task
ShareCopyTask shareCopyTask = new ShareCopyTask(shareIntent);
shareCopyTask.execute(databaseFile, database);
shareCopyTask.execute(geoPackageFile, geoPackageName);
}

} catch (Exception e) {
GeoPackageUtils.showMessage(activity, "Error sharing GeoPackage", e.getMessage());
}
Expand All @@ -107,21 +106,16 @@ public void shareDatabaseOption(final String database) {

/**
* Save the given database to the downloads directory
* @param database GeoPackage name
*/
private void saveDatabaseOption(final String database){
private void saveDatabaseOption(){
try {
// Get the database file
File databaseFile = geoPackageViewModel.getDatabaseFile(database);

// Create the share intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);

// Launch the save to disk task
SaveToDiskTask saveTask = new SaveToDiskTask(shareIntent);
saveTask.execute(databaseFile, database);

saveTask.execute(geoPackageFile, geoPackageName);
} catch (Exception e) {
GeoPackageUtils.showMessage(activity, "Error saving to file", e.getMessage());
}
Expand All @@ -133,41 +127,48 @@ private void saveDatabaseOption(final String database){
* Shows a popup to ask if the user wants to save to disk or share with external apps
* @return constant representing either share or save
*/
public void askToSaveOrShare(String database){
// Create Alert window with basic input text layout
LayoutInflater inflater = LayoutInflater.from(activity);
View alertView = inflater.inflate(R.layout.share_file_popup, null);
ViewAnimation.setScaleAnimatiom(alertView, 200);
// title
TextView titleText = (TextView) alertView.findViewById(R.id.alert_title);
titleText.setText("Share GeoPackage");

// Initial dialog asking for create or import
AlertDialog.Builder dialog = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
.setView(alertView);
final AlertDialog alertDialog = dialog.create();

// Click listener for "Share"
alertView.findViewById(R.id.share_menu_share_card)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareDatabaseOption(database);
alertDialog.dismiss();
}
});
public void askToSaveOrShare(){
try {
if(geoPackageFile == null || geoPackageName == null){
throw new Exception("GeoPackage could not be found");
}
// Create Alert window with basic input text layout
LayoutInflater inflater = LayoutInflater.from(activity);
View alertView = inflater.inflate(R.layout.share_file_popup, null);
ViewAnimation.setScaleAnimatiom(alertView, 200);
// title
TextView titleText = (TextView) alertView.findViewById(R.id.alert_title);
titleText.setText("Share GeoPackage");

// Initial dialog asking for create or import
AlertDialog.Builder dialog = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
.setView(alertView);
final AlertDialog alertDialog = dialog.create();

// Click listener for "Share"
alertView.findViewById(R.id.share_menu_share_card)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareDatabaseOption();
alertDialog.dismiss();
}
});

// Click listener for "Save"
alertView.findViewById(R.id.share_menu_save_card)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveDatabaseOption(database);
alertDialog.dismiss();
}
});
// Click listener for "Save"
alertView.findViewById(R.id.share_menu_save_card)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveDatabaseOption();
alertDialog.dismiss();
}
});

alertDialog.show();
alertDialog.show();
} catch (Exception e) {
GeoPackageUtils.showMessage(activity, "Error sharing", e.getMessage());
}
}


Expand All @@ -190,13 +191,11 @@ 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 Expand Up @@ -431,6 +430,22 @@ public String getGeoPackageName() {
public void setGeoPackageName(String geoPackageName) {
this.geoPackageName = geoPackageName;
}

public void setGeoPackageFile(File geoPackageFile){
this.geoPackageFile = geoPackageFile;
}

public File getGeoPackageFile(){
return this.geoPackageFile;
}

public boolean isFileExternal() {
return isFileExternal;
}

public void setFileExternal(boolean fileExternal) {
isFileExternal = fileExternal;
}
}


Expand Down

0 comments on commit c9339bd

Please sign in to comment.