Search in sources :

Example 1 with Format

use of com.nolanlawson.keepscore.helper.SdcardHelper.Format 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);
}
Also used : GamesBackupSummary(com.nolanlawson.keepscore.serialization.GamesBackupSummary) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) Comparator(java.util.Comparator) Format(com.nolanlawson.keepscore.helper.SdcardHelper.Format) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with Format

use of com.nolanlawson.keepscore.helper.SdcardHelper.Format 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();
}
Also used : AlertDialog(android.app.AlertDialog) GamesBackupSummaryAdapter(com.nolanlawson.keepscore.data.GamesBackupSummaryAdapter) Format(com.nolanlawson.keepscore.helper.SdcardHelper.Format) DialogInterface(android.content.DialogInterface) GamesBackupSummary(com.nolanlawson.keepscore.serialization.GamesBackupSummary) OnClickListener(android.view.View.OnClickListener) DisplayMetrics(android.util.DisplayMetrics) Uri(android.net.Uri)

Aggregations

Uri (android.net.Uri)2 Format (com.nolanlawson.keepscore.helper.SdcardHelper.Format)2 GamesBackupSummary (com.nolanlawson.keepscore.serialization.GamesBackupSummary)2 AlertDialog (android.app.AlertDialog)1 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 DisplayMetrics (android.util.DisplayMetrics)1 OnClickListener (android.view.View.OnClickListener)1 GamesBackupSummaryAdapter (com.nolanlawson.keepscore.data.GamesBackupSummaryAdapter)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1