Back to project page WineDB.
The source code is released under:
MIT License
If you think the Android project WineDB listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.selesse.android.winedb.database; //from w w w. j a v a2 s. c o m import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Environment; import android.text.InputType; import android.widget.EditText; import android.widget.Toast; import com.selesse.android.winedb.R; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; /** * Class used to import and export file backups of wine databases. Closely linked to {@link WineDatabaseHandler}. */ public class FileDatabaseBackup { private Context context; private final String defaultBackupName = "winedb.bak"; public FileDatabaseBackup(Context context) { this.context = context; } public void startExportDatabase() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle(R.string.export_dialog_title); alert.setMessage(R.string.export_dialog_message); // Set an EditText view to get user input, disable auto correct on it final EditText input = new EditText(context); input.setText(Environment.getExternalStorageDirectory().getPath() + "/" + defaultBackupName); input.setInputType(~(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT)); alert.setView(input); if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { showErrorToast(context.getString(R.string.sd_card_not_mounted)); } if (!Environment.getExternalStorageDirectory().canWrite()) { showErrorToast(context.getString(R.string.export_dialog_no_write)); return; } alert.setPositiveButton(R.string.export, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); final File exportLocation = new File(value); if (exportLocation.exists()) { createConfirmExportDialog(exportLocation); } else { exportWineDatabase(exportLocation); } } }).setNegativeButton(R.string.cancel, null); alert.show(); } private void createConfirmExportDialog(final File exportLocation) { AlertDialog.Builder confirm = new AlertDialog.Builder(context); DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { exportWineDatabase(exportLocation); } } }; confirm.setTitle(R.string.confirm); confirm.setMessage(R.string.export_overwrite_file); confirm.setPositiveButton(R.string.yes, dialogListener).setNegativeButton(R.string.no, dialogListener).show(); } private void exportWineDatabase(File exportLocation) { try { WineDatabaseHandler handler = WineDatabaseHandler.getInstance(context); handler.exportDatabase(exportLocation.getPath()); Toast.makeText(context, R.string.export_success, Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { showErrorToast(context.getString(R.string.export_dialog_no_database)); } catch (IOException e) { showErrorToast(context.getString(R.string.export_error)); } } public void startImportDatabase() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle(R.string.import_dialog_title); alert.setMessage(R.string.import_dialog_message); // Set an EditText view to get user input final EditText input = new EditText(context); input.setText(Environment.getExternalStorageDirectory().getPath() + "/" + defaultBackupName); input.setInputType(~(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT)); alert.setView(input); alert.setPositiveButton(R.string.imports, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); File importLocation = new File(value); if (importLocation.exists()) { importWineDatabase(importLocation); } else { showErrorToast(context.getString(R.string.import_file_not_found)); } } }).setNegativeButton(R.string.cancel, null); alert.show(); } private void importWineDatabase(File importLocation) { try { WineDatabaseHandler handler = WineDatabaseHandler.getInstance(context); handler.importDatabase(importLocation.getPath()); handler.refresh(); } catch (FileNotFoundException e) { showErrorToast(context.getString(R.string.import_error)); } catch (IOException e) { showErrorToast(context.getString(R.string.import_error)); } } private void showErrorToast(String errorString) { Toast.makeText(context, errorString, Toast.LENGTH_LONG).show(); } }