Search in sources :

Example 1 with GamesBackupSummary

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);
}
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 GamesBackupSummary

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();
}
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)

Example 3 with GamesBackupSummary

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;
}
Also used : Context(android.content.Context) GamesBackupSummary(com.nolanlawson.keepscore.serialization.GamesBackupSummary) LayoutInflater(android.view.LayoutInflater) Date(java.util.Date)

Example 4 with GamesBackupSummary

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();
}
Also used : GamesBackupSummaryAdapter(com.nolanlawson.keepscore.data.GamesBackupSummaryAdapter) DialogInterface(android.content.DialogInterface) GamesBackupSummary(com.nolanlawson.keepscore.serialization.GamesBackupSummary) OnClickListener(android.view.View.OnClickListener) DisplayMetrics(android.util.DisplayMetrics)

Aggregations

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