use of com.nolanlawson.keepscore.serialization.GamesBackupSummary in project KeepScore by nolanlawson.
the class MainActivity method showLoadBackupDialog.
private void showLoadBackupDialog() {
if (!SdcardHelper.isAvailable()) {
ToastHelper.showLong(this, R.string.toast_no_sdcard);
return;
}
final List<String> backups = SdcardHelper.list(Location.Backups);
if (backups.isEmpty()) {
ToastHelper.showShort(this, R.string.toast_no_backups);
return;
}
final ProgressDialog progressDialog = showProgressDialog(R.string.text_loading_generic, backups.size());
// show progress dialog to avoid jankiness
new AsyncTask<Void, Void, List<GamesBackupSummary>>() {
@Override
protected List<GamesBackupSummary> doInBackground(Void... params) {
List<GamesBackupSummary> summaries = new ArrayList<GamesBackupSummary>();
// fetch the summaries only, so that we don't have to read the entire XML file for each one
for (String backup : backups) {
File file = SdcardHelper.getFile(backup, Location.Backups);
Uri uri = Uri.fromFile(file);
Format format = file.getName().endsWith(".gz") ? Format.GZIP : Format.XML;
GamesBackupSummary summary = GamesBackupSerializer.readGamesBackupSummary(uri, format, getContentResolver());
summaries.add(summary);
publishProgress((Void) null);
}
// show most recent ones first
Collections.sort(summaries, new Comparator<GamesBackupSummary>() {
public int compare(GamesBackupSummary lhs, GamesBackupSummary rhs) {
return Long.valueOf(rhs.getDateSaved()).compareTo(lhs.getDateSaved());
}
});
return summaries;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progressDialog.incrementProgressBy(1);
}
@Override
protected void onPostExecute(List<GamesBackupSummary> result) {
super.onPostExecute(result);
progressDialog.dismiss();
showLoadBackupDialogFinished(result);
}
}.execute((Void) null);
}
use of com.nolanlawson.keepscore.serialization.GamesBackupSummary in project KeepScore by nolanlawson.
the class MainActivity method showLoadBackupDialogFinished.
private void showLoadBackupDialogFinished(List<GamesBackupSummary> summaries) {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final GamesBackupSummaryAdapter adapter = new GamesBackupSummaryAdapter(MainActivity.this, displayMetrics, summaries);
new AlertDialog.Builder(MainActivity.this).setCancelable(true).setTitle(R.string.title_choose_backup).setNegativeButton(android.R.string.cancel, null).setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
GamesBackupSummary summary = adapter.getItem(which);
Uri uri = Uri.fromFile(SdcardHelper.getFile(summary.getFilename(), Location.Backups));
Format format = summary.getFilename().endsWith(".gz") ? Format.GZIP : Format.XML;
loadBackup(summary, uri, format);
}
}).show();
}
use of com.nolanlawson.keepscore.serialization.GamesBackupSummary in project KeepScore by nolanlawson.
the class GamesBackupSummaryAdapter method getView.
@Override
public View getView(int position, View view, ViewGroup parent) {
// view wrapper optimization per Romain Guy
final Context context = parent.getContext();
ViewWrapper viewWrapper;
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(LAYOUT_RES_ID, parent, false);
viewWrapper = new ViewWrapper(view);
view.setTag(viewWrapper);
// set the min width to match the max number of digits on the numGames
viewWrapper.getNumGamesTextView().setMinWidth(gameCountMinWidth);
} else {
viewWrapper = (ViewWrapper) view.getTag();
}
GamesBackupSummary summary = getItem(position);
viewWrapper.getAutoOrManualTextView().setText(summary.isAutomatic() ? R.string.text_backup_automatic : R.string.text_backup_manual);
viewWrapper.getDateTextView().setText(dateFormat.format(new Date(summary.getDateSaved())));
viewWrapper.getFilenameTextView().setText(summary.getFilename());
viewWrapper.getNumGamesTextView().setText(Integer.toString(summary.getGameCount()));
return view;
}
use of com.nolanlawson.keepscore.serialization.GamesBackupSummary in project KeepScore by nolanlawson.
the class MainActivity method loadBackupFileFromShare.
/**
* if the user opened up a games-20xxxxxxxxx.xml.gz file from a file browser, open it here.
* @param intent
*/
private void loadBackupFileFromShare(final Intent intent) {
if (intent == null || intent.getData() == null) {
// no intent data, so abort
return;
}
// this functionality is not supported in Eclair
if (VersionHelper.getVersionSdkIntCompat() < VersionHelper.VERSION_FROYO) {
return;
}
if (uriIntentsConfirmedByUser.contains(intent.getDataString())) {
// user already dismissed or confirmed the dialog; no need to show it again
return;
}
log.i("Received intent: %s", intent);
log.i("Received data: %s", intent.getData());
GamesBackupSummary summary;
try {
summary = GamesBackupSerializer.readGamesBackupSummary(intent.getData(), Format.XML, getContentResolver());
} catch (Exception e) {
log.e(e, "Unexpected error loading %s", intent.getData());
ToastHelper.showLong(this, R.string.toast_error_with_backup);
return;
}
final GamesBackupSummary finalSummary = summary;
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
GamesBackupSummaryAdapter adapter = new GamesBackupSummaryAdapter(this, displayMetrics, new ArrayList<GamesBackupSummary>(Collections.singleton(finalSummary)));
DialogInterface.OnClickListener onOk = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
uriIntentsConfirmedByUser.add(intent.getDataString());
loadBackup(finalSummary, intent.getData(), Format.XML);
}
};
DialogInterface.OnClickListener onCancel = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
uriIntentsConfirmedByUser.add(intent.getDataString());
}
};
new AlertDialog.Builder(this).setCancelable(true).setTitle(R.string.title_choose_backup).setNegativeButton(android.R.string.cancel, onCancel).setPositiveButton(android.R.string.ok, onOk).setAdapter(adapter, onOk).show();
}
Aggregations