can.yrt.onebusaway.backup.Backup.java Source code

Java tutorial

Introduction

Here is the source code for can.yrt.onebusaway.backup.Backup.java

Source

/*
 * Copyright (C) 2012 Paul Watts (paulcwatts@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package can.yrt.onebusaway.backup;

import can.yrt.oba.provider.ObaContract;
import can.yrt.oba.provider.ObaProvider;

import org.apache.commons.io.FileUtils;

import android.annotation.TargetApi;
import android.content.ContentProviderClient;
import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * Big note, that this is currently fairly unsafe.
 * There's no thread safety at all to make sure the database file
 * isn't being written on another thread while we are copying the file.
 * If that becomes an issue, I would rather just dump everything
 * into a CSV file, since we know that reading/writing to the
 * ContentProvider interface is going to be threadsafe.
 *
 * @author paulw
 */
public final class Backup {

    private static final String BACKUP_TYPE = "OBABackups";

    private static final String BACKUP_NAME = "db.backup";

    public static boolean isBackupEnabled() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    private static File getDB(Context context) {
        return ObaProvider.getDatabasePath(context);
    }

    @TargetApi(8)
    private static File getBackup(Context context) {
        if (!isBackupEnabled()) {
            throw new RuntimeException("Backup isn't enabled!");
        }
        File backupDir = Environment.getExternalStoragePublicDirectory(BACKUP_TYPE);
        return new File(backupDir, BACKUP_NAME);
    }

    /**
     * Performs a backup to the SD card.
     *
     * @param context
     */
    public static String backup(Context context) throws IOException {
        // We need two things:
        // 1. The path to the database;
        // 2. The path on the SD card to the backup file.
        File backupPath = getBackup(context);
        FileUtils.copyFile(getDB(context), backupPath);
        return backupPath.getAbsolutePath();
    }

    /**
     * Performs a restore from the SD card.
     *
     * @param context
     */
    public static void restore(Context context) throws IOException {
        File dbPath = getDB(context);
        File backupPath = getBackup(context);

        // At least here we can decide that the database is closed.
        ContentProviderClient client = null;
        try {
            client = context.getContentResolver().acquireContentProviderClient(ObaContract.AUTHORITY);
            ObaProvider provider = (ObaProvider) client.getLocalContentProvider();
            provider.closeDB();

            FileUtils.copyFile(backupPath, dbPath);

        } finally {
            if (client != null) {
                client.release();
            }
        }
    }

    public static boolean isRestoreAvailable(Context context) {
        return getBackup(context).exists();
    }
}