Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
128 lines (109 sloc) 3.65 KB
/*
This file is part of LogMyLife, an application for logging events.
Copyright (C) 2011 Luke Meyer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program (see LICENSE file).
If not, see http://www.gnu.org/licenses/
*/
package net.sosiouxme.logmylife.activity;
import net.sosiouxme.logmylife.R;
import net.sosiouxme.logmylife.domain.DbAdapter;
import net.sosiouxme.logmylife.domain.ExportHelper;
import net.sosiouxme.logmylife.domain.ImportHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class Data extends Activity implements OnClickListener {
private static final int DIALOG_IMPORT_OK = 0;
private DbAdapter mDba;
private TextView mResultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_data);
mDba = new DbAdapter(this);
findViewById(R.id.export_button).setOnClickListener(this);
findViewById(R.id.import_button).setOnClickListener(this);
mResultText = (TextView) findViewById(R.id.result); //TODO: persist contents
setResult(RESULT_OK);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDba.close();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.export_button:
new Exporter().execute();
break;
case R.id.import_button:
showDialog(DIALOG_IMPORT_OK);
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_IMPORT_OK:
return new AlertDialog.Builder(this)
.setMessage(R.string.import_warning_text)
.setNegativeButton(R.string.import_dialog_cancel, null)
.setPositiveButton(R.string.import_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new Importer().execute();
}
})
.create();
}
return super.onCreateDialog(id);
}
private class Exporter extends ExportHelper {
public Exporter() {
super(Data.this, mDba.getDbPath());
}
@Override
protected void onPostExecute(String errMsg) {
super.onPostExecute(errMsg);
String result = (errMsg == null)
? str(R.string.export_success) + EXPORT_FILE
: str(R.string.export_fail) + errMsg;
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
mResultText.setText(result);
}
}
private class Importer extends ImportHelper {
public Importer() {
super(Data.this, mDba.getDbPath());
}
@Override
protected void onPostExecute(String errMsg) {
super.onPostExecute(errMsg);
String result = (errMsg == null)
? str(R.string.import_success)
: str(R.string.import_fail) + errMsg;
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
if(errMsg == null) { // succeeded - go back to Main w/ new DB
setResult(RESULT_FIRST_USER); // message: get a new DB handle
Data.this.finish();
} else {
mResultText.setText(result);
}
}
}
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.