Skip to content

Commit

Permalink
#108: Impl. ZipConfigStrategy to have common logic how to load/create…
Browse files Browse the repository at this point in the history
… zipConfig
  • Loading branch information
k3b committed Oct 6, 2019
1 parent e4a8a29 commit e0f5b81
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
}
case R.id.cmd_backup:
BackupActivity.showActivity(" menu " + menuItem.getTitle(),
this, null, null, null,
this, null, null,
getAsMergedQuery(),
mFilterValue.getPath(),
BackupActivity.REQUEST_BACKUP_ID);
return true;
case R.id.action_details:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.util.Log;

import java.io.File;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Date;

Expand All @@ -37,7 +36,6 @@
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.database.QueryParameter;
import de.k3b.io.FileUtils;
import de.k3b.io.IItemSaver;
import de.k3b.io.IProgessListener;
import de.k3b.io.StringUtils;
Expand Down Expand Up @@ -74,23 +72,6 @@ public class Backup2ZipService implements IProgessListener, ZipLog {
// used to translate ZipLog.traceMessage() to become IProgessListener
private int lastZipItemNumber = 0;

public static IZipConfig loadZipConfig(Uri uri, Context context) {
if ((uri != null) && ZipConfigRepository.isZipConfig(uri.toString())) {
InputStream inputsteam = null;
try {
inputsteam = context.getContentResolver().openInputStream(uri);
return new ZipConfigRepository(null).load(inputsteam, uri);
} catch (Exception ex) {
// file not found or no permission
Log.w(LibZipGlobal.LOG_TAG, mDebugPrefix + context.getClass().getSimpleName()
+ "-loadZipConfig(" + uri + ") failed " + ex.getClass().getSimpleName(), ex);
} finally {
FileUtils.close(inputsteam, uri);
}
}
return null;
}

public Backup2ZipService(Context context, IZipConfig zipConfig, ZipStorage zipStorage,
Date backupDate) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -93,93 +91,58 @@
*
* API
* * uri = intent.getData() load file via file-uri
* * else intent.Extra[STATE_ZIP_CONFIG]
* * else intent.Extra[EXTRA_STATE_ZIP_CONFIG]
*/
public class BackupActivity extends ActivityWithAutoCloseDialogs implements Common {
private static final int REQUEST_ID_PICK_ZIP_OUT_DIR = 1234;

public static final int REQUEST_BACKUP_ID = 99289;
public static final int REQUEST_ID_PICK_EXIF = 99293;

private static final String EXTRA_STATE_ZIP_CONFIG = BackupProgressActivity.EXTRA_STATE_ZIP_CONFIG;
/**
* shows progress dialog while backup is running
*/
private static final int REQUEST_ID_BACKUP_PROGRESS = 99294;
private static final String DCIM_ROOT = (null == OsUtils.getDefaultPhotoRoot())
? "--"
: OsUtils.getDefaultPhotoRoot().getAbsolutePath();

private static final String STATE_ZIP_CONFIG = "zip_config";
private static String mDebugPrefix = "BackupActivity: ";

private Gui gui = null;
private SelectedFiles mSelectedFiles;

/**
* Shows Activity to edit and execute backup/copy to zip file
*
* Data priority
* uri = *.zip.apm.cfg
* else uri = *.album
* else pk-s of selectedFiles
* else filter+query
*/
public static void showActivity(String debugContext, Activity context,
Uri uri, SelectedFiles selectedFiles,
SelectedFiles selectedFiles,
IGalleryFilter filter, QueryParameter query,
String selectedRelDir, int requestCode) {
final Intent intent = new Intent().setClass(context,
BackupActivity.class);

intent.setAction(Intent.ACTION_EDIT);

IZipConfig config = Backup2ZipService.loadZipConfig(uri, context);
File selectedRelDirFile = FileUtils.createFile(selectedRelDir);
final String zipName = (selectedRelDirFile == null) ? null : selectedRelDirFile.getName();
if (null == config) {
config = getPreviousZipConfig(null, zipName);
}
if (null == config) {
config = new ZipConfigDto(null);
QueryParameter mergedQuery = getQuery(debugContext, context,
uri, selectedFiles, filter, query);
if (mergedQuery != null) {
config.setFilter(mergedQuery.toReParseableString(null));
}
int requestCode) {
final Intent intent = new Intent()
.setClass(context, BackupActivity.class)
.setAction(Intent.ACTION_EDIT);

if (selectedRelDirFile != null) {
config.setZipRelPath(selectedRelDirFile.getPath());
config.setZipName(zipName);
}
}
SetConfigToIntent(debugContext, context, selectedFiles, filter, query, intent);
IntentUtil.startActivity(debugContext, context, requestCode, intent);
}

private static void SetConfigToIntent(String debugContext, Activity context, SelectedFiles selectedFiles, IGalleryFilter filter, QueryParameter query, Intent intent) {
IZipConfig config = ZipConfigStrategy.getOrCreate(debugContext, context, selectedFiles,
null, filter, query);

if (config != null) {
intent.putExtra(STATE_ZIP_CONFIG, (Serializable) config);
intent.putExtra(EXTRA_STATE_ZIP_CONFIG, (Serializable) config);
}
if (LibZipGlobal.debugEnabled) {
Log.d(LibZipGlobal.LOG_TAG, mDebugPrefix + context.getClass().getSimpleName()
+ " > BackupActivity.showActivity " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

IntentUtil.startActivity(debugContext, context, requestCode, intent);
}


private static IZipConfig getPreviousZipConfig(BackupActivity context, String zipName) {
if (zipName != null) {
File repositoryFile = ZipConfigRepository.getZipConfigFile(zipName);
if ((repositoryFile != null) && (repositoryFile.exists())) {
try {
IZipConfig repo = new ZipConfigRepository(null).load(new FileInputStream(repositoryFile), repositoryFile);
if (repo != null) {
if (context != null) {
showStatistics(context, repo, zipName);
}
// ZipConfigDto is serializable and ZipConfigRepository is not.
return new ZipConfigDto(repo);
}
} catch (IOException ignore) {
}
}
IZipConfig config = ZipConfigRepository.getZipConfigOrNull(zipName);
if (config != null) {
showStatistics(context, config, zipName);
}
return null;
return config;
}

private static void showStatistics(BackupActivity context, IZipConfig config, String messagePrefix) {
Expand All @@ -206,35 +169,6 @@ private void showCurrentStatistics() {
showStatistics(this, gui, null);
}

/**
*
* Data priority
*
* uri = *.album
* else pk-s of selectedFiles
* else filter+query
*
* @return query or null if not found
*/
private static QueryParameter getQuery(String debugContext, Activity context,
Uri uri, SelectedFiles selectedFiles,
IGalleryFilter filter, QueryParameter query) {
QueryParameter mergedQuery = AndroidAlbumUtils.getQueryFromUri(debugContext, context, null, uri, null);

if (mergedQuery == null) {
final int idCount = (selectedFiles == null) ? 0 : selectedFiles.size();
if ((query != null) || (filter != null) || (idCount > 0)) {

if (idCount > 0) {
mergedQuery = FotoSql.setWhereSelectionPks(new QueryParameter(), selectedFiles.toIdString());
} else {
mergedQuery = AndroidAlbumUtils.getAsMergedNewQuery(query, filter);
}
}
}
return mergedQuery;
}

private void cmdShowDetails() {
final QueryParameter asMergedQuery
= Backup2ZipService.getEffectiveQueryParameter(this.gui);
Expand All @@ -256,7 +190,7 @@ private void cmdShowDetails() {
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
saveGuiToData();
savedInstanceState.putSerializable(STATE_ZIP_CONFIG, mZipConfigData);
savedInstanceState.putSerializable(EXTRA_STATE_ZIP_CONFIG, mZipConfigData);
super.onSaveInstanceState(savedInstanceState);
}

Expand All @@ -274,24 +208,22 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
defineGui();

Intent intent = getIntent();

mSelectedFiles = getSelectedFiles("onCreate ", intent, false);

if (savedInstanceState != null) {
mZipConfigData.loadFrom((IZipConfig) savedInstanceState.getSerializable(STATE_ZIP_CONFIG));
mZipConfigData.loadFrom((IZipConfig) savedInstanceState.getSerializable(EXTRA_STATE_ZIP_CONFIG));
} else {
Intent intent = getIntent();

Uri uri = intent.getData();
IZipConfig config = Backup2ZipService.loadZipConfig(uri, this);
if (config != null) {
mZipConfigData.loadFrom(config);
showStatistics(this, config, null);
if (uri != null) {
mZipConfigData.loadFrom(ZipConfigStrategy.getOrCreate("onCreate", this, null,
uri, null, null));
} else {
mZipConfigData.loadFrom((IZipConfig) intent.getSerializableExtra(STATE_ZIP_CONFIG));
mZipConfigData.loadFrom((IZipConfig) intent.getSerializableExtra(EXTRA_STATE_ZIP_CONFIG));
}
}
loadGuiFromData();
gui.updateHistory();
showCurrentStatistics();
}

/**
Expand Down Expand Up @@ -694,6 +626,9 @@ private Gui() {
editZipRelPath,
editZipDir,
editFilter) {

private final String historyFilter = getEditIdPrefix(BackupActivity.this) + 4;

@Override
protected boolean onHistoryPick(EditorHandler editorHandler, EditText editText, String text) {
boolean chagend = ((text != null) && (editText != null)
Expand Down Expand Up @@ -722,6 +657,19 @@ protected boolean onHistoryPick(EditorHandler editorHandler, EditText editText,
return result;
}

@Override
protected String formatMenuItemText(String historyId, String itemText) {
if (historyFilter.compareTo(historyId) == 0) {
QueryParameter query = QueryParameter.parse(itemText);
String details = formatter.format(TagSql.parseQueryEx(query, true)).toString();
if (details.length() > 0) {
return details
.replace("\n", " ")
.replace(DCIM_ROOT, "");
}
}
return super.formatMenuItemText(historyId, itemText);
}
}.setIncludeEmpty(true);

}
Expand All @@ -739,6 +687,8 @@ private void updateHistory(QueryParameter query) {
List<String> paths = new ArrayList<String>();
List<String> filenames = new ArrayList<String>();

paths.add(DCIM_ROOT);

String minFolder = FotoSql.getMinFolder(getApplicationContext(), query, true);
updateHistory(FileUtils.getDir(minFolder), filenames, paths, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class BackupProgressActivity extends LocalizedActivity {
*/
public static final boolean USE_DOCUMENT_PROVIDER = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);

private static final String EXTRA_STATE_ZIP_CONFIG = "zip_config";
protected static final String EXTRA_STATE_ZIP_CONFIG = "zip_config";
private static String mDebugPrefix = "BuProgressActivity: ";

// != null while async backup is running
Expand Down
Loading

0 comments on commit e0f5b81

Please sign in to comment.