Search in sources :

Example 1 with RBackup

use of com.cpjd.roblu.models.RBackup in project Roblu by wdavies973.

the class IO method convertBackupFile.

/**
 * Converts a backup file (external) into an RBackup object instance
 * @param toCopy the Uri of the external file the user selected
 * @return RBackup object instance
 */
public RBackup convertBackupFile(Uri toCopy) {
    File file = new File(context.getCacheDir(), PREFIX + File.separator + "tempBackup.backup");
    if (file.mkdirs())
        Log.d("RBS", "Successfully created backup parent dirs");
    if (file.exists()) {
        if (!file.delete())
            Log.d("RBS", "Failed to delete old cached backup file.");
    }
    try {
        InputStream is = context.getContentResolver().openInputStream(toCopy);
        FileOutputStream out = new FileOutputStream(file);
        if (is != null) {
            IOUtils.copy(is, out);
            RBackup backup = (RBackup) deserializeObject(file);
            is.close();
            out.flush();
            out.close();
            return backup;
        }
        return null;
    } catch (Exception e) {
        Log.d("RBS", "Exception: " + e.getMessage());
        return null;
    } finally {
        if (file.delete())
            Log.d("RBS", "Cached backup file successfully deleted.");
    }
}
Also used : RBackup(com.cpjd.roblu.models.RBackup) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException)

Example 2 with RBackup

use of com.cpjd.roblu.models.RBackup in project Roblu by wdavies973.

the class EventCreateMethodPicker method onActivityResult.

/**
 * Receives result data from child activities
 * @param requestCode the request code of the child activities
 * @param resultCode the result code of the child activity
 * @param data any result data returned from the activity
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    /*
         * The user selected a backup file, let's attempt to import it here
         */
    if (requestCode == Constants.FILE_CHOOSER) {
        // this means the user didn't select a file, no point in returning an error message
        if (data == null)
            return;
        try {
            IO io = new IO(getApplicationContext());
            RBackup backup = io.convertBackupFile(data.getData());
            if (!backup.getFileVersion().equals(IO.PREFIX)) {
                Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Invalid backup file. Backup was created with an older version of Roblu.", true, rui.getPrimaryColor());
                return;
            }
            /*
                 * Create the event, we're not gonna use an AsyncTask because the user is just
                 * watching the event import anyway, and it will only freeze the UI for a couple hundred
                 * milliseconds.
                 */
            REvent event = backup.getEvent();
            event.setCloudEnabled(false);
            event.setID(io.getNewEventID());
            io.saveEvent(event);
            io.saveForm(event.getID(), backup.getForm());
            if (backup.getTeams() != null) {
                for (RTeam team : backup.getTeams()) {
                    for (RTab tab : team.getTabs()) {
                        for (RMetric metric : tab.getMetrics()) {
                            if (metric instanceof RGallery && ((RGallery) metric).getImages() != null) {
                                // Add images to the current gallery
                                for (int i = 0; i < ((RGallery) metric).getImages().size(); i++) {
                                    ((RGallery) metric).getPictureIDs().add(io.savePicture(event.getID(), ((RGallery) metric).getImages().get(i)));
                                }
                                ((RGallery) metric).setImages(null);
                            }
                        }
                    }
                    io.saveTeam(event.getID(), team);
                }
            }
            Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Successfully imported event from backup", false, rui.getPrimaryColor());
            Intent intent = new Intent();
            intent.putExtra("eventID", event.getID());
            setResult(Constants.NEW_EVENT_CREATED, intent);
            finish();
        } catch (Exception e) {
            Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Invalid backup file", true, rui.getPrimaryColor());
        }
    } else /*
         * The user created an event manually with EventEditor, we actually don't need to do anything but auto-finish our class
         * with a result code letting the TeamsView class now to refresh the event list
         */
    if (resultCode == Constants.NEW_EVENT_CREATED) {
        Bundle b = data.getExtras();
        Intent intent = new Intent();
        if (b != null)
            intent.putExtras(b);
        setResult(Constants.NEW_EVENT_CREATED, intent);
        finish();
    }
}
Also used : RBackup(com.cpjd.roblu.models.RBackup) RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) IO(com.cpjd.roblu.io.IO) Bundle(android.os.Bundle) REvent(com.cpjd.roblu.models.REvent) Intent(android.content.Intent) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Aggregations

RBackup (com.cpjd.roblu.models.RBackup)2 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 IO (com.cpjd.roblu.io.IO)1 REvent (com.cpjd.roblu.models.REvent)1 RTab (com.cpjd.roblu.models.RTab)1 RTeam (com.cpjd.roblu.models.RTeam)1 RGallery (com.cpjd.roblu.models.metrics.RGallery)1 RMetric (com.cpjd.roblu.models.metrics.RMetric)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1