package com.example.CatchApiDemo;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.catchnotes.api.CatchAPI;
import com.catchnotes.api.CatchAccount;
import com.catchnotes.api.CatchNote;
public static final String APP_NAME = "CatchApiDemo";
protected static final int DIALOG_SIGN_IN = 0;
protected static final int DIALOG_COMPOSE_NOTE = 1;
protected String mAccessToken;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button signInButton = (Button) findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new OnClickListener() {
showDialog(DIALOG_SIGN_IN);
}
});
final Button fetchButton = (Button) findViewById(R.id.fetch_button);
fetchButton.setOnClickListener(new OnClickListener() {
if (mAccessToken == null) {
Toast.makeText(Dashboard.this, "Not signed in!", Toast.LENGTH_SHORT).show();
} else {
new FetchNotesTask(getApplicationContext()).execute();
}
}
});
final Button createButton = (Button) findViewById(R.id.create_button);
createButton.setOnClickListener(new OnClickListener() {
if (mAccessToken == null) {
Toast.makeText(Dashboard.this, "Not signed in!", Toast.LENGTH_SHORT).show();
} else {
showDialog(DIALOG_COMPOSE_NOTE);
}
}
});
}
@Override
Dialog dialog;
AlertDialog.Builder builder;
switch (id) {
case DIALOG_SIGN_IN:
{
final View inflated = LayoutInflater.from(this).inflate(R.layout.sign_in, null);
builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.sign_in_dialog_title)
.setCancelable(true)
.setView(inflated)
.setPositiveButton(getString(R.string.sign_in), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
EditText userEdit = (EditText) inflated.findViewById(R.id.username_edittext);
EditText passEdit = (EditText) inflated.findViewById(R.id.password_edittext);
new SignInTask(getApplicationContext())
.execute(userEdit.getText().toString(), passEdit.getText().toString());
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
}
case DIALOG_COMPOSE_NOTE:
{
final View inflated = LayoutInflater.from(this).inflate(R.layout.new_note, null);
builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.new_note_dialog_title)
.setCancelable(true)
.setView(inflated)
.setPositiveButton(getString(R.string.add_note), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
EditText composer = (EditText) inflated.findViewById(R.id.compose_edittext);
new CreateNoteTask(getApplicationContext()).execute(composer.getText().toString());
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
}
default:
dialog = null;
break;
}
return dialog;
}
private class SignInTask extends AsyncTask<String, Void, String> {
private Context mContext;
private CatchAccount mAccount;
mContext = context;
mAccount = new CatchAccount();
}
@Override
String username = params[0];
String password = params[1];
CatchAPI api = new CatchAPI(APP_NAME, mContext);
int result = api.signIn(username, password, mAccount);
if (result == CatchAPI.RESULT_OK) {
Log.d(APP_NAME, "Signed in.");
Log.d(APP_NAME, "Account ID: "+mAccount.id);
Log.d(APP_NAME, "Account Name: "+mAccount.username);
Log.d(APP_NAME, "Account Email: "+mAccount.email);
return mAccount.auth_token;
} else {
Log.d(APP_NAME, "Couldn't sign in. Error code: "+result);
return null;
}
}
@Override
Dashboard.this.mAccessToken = accessToken;
if (accessToken == null) {
Toast.makeText(Dashboard.this, "Sign-in Failed!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Dashboard.this, "User "+mAccount.username+" signed in.",
Toast.LENGTH_SHORT).show();
}
}
}
private Context mContext;
private int mResult;
mContext = context;
}
return DateUtils.formatDateTime(mContext, timestamp,
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE |
DateUtils.FORMAT_SHOW_TIME);
}
@Override
CatchAPI api = new CatchAPI(APP_NAME, mContext);
api.setAccessToken(Dashboard.this.mAccessToken);
ArrayList<CatchNote> notes = new ArrayList<CatchNote>();
mResult = api.getNotes(notes);
if (mResult == CatchAPI.RESULT_OK) {
Log.d(APP_NAME, "Displaying "+notes.size()+" notes...");
for (CatchNote note : notes) {
Log.d(APP_NAME, "----"+
" Note ID: "+note.id+
" Created: "+formatDate(note.creationTime)+
" Modified: "+formatDate(note.modificationTime)+
" ----");
Log.d(APP_NAME, note.summary.toString());
}
Log.d(APP_NAME, "Displayed "+notes.size()+" notes.");
} else {
Log.d(APP_NAME, "Couldn't fetch notes. Error code: "+mResult);
return null;
}
return null;
}
@Override
if (mResult == CatchAPI.RESULT_OK) {
Toast.makeText(Dashboard.this, "Notes Fetched", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Dashboard.this, "Note Fetch Failed!",
Toast.LENGTH_SHORT).show();
}
}
}
private class CreateNoteTask extends AsyncTask<String, Void, String> {
private Context mContext;
mContext = context;
}
@Override
String text = params[0];
CatchAPI api = new CatchAPI(APP_NAME, mContext);
api.setAccessToken(Dashboard.this.mAccessToken);
CatchNote note = new CatchNote();
long timestamp = System.currentTimeMillis();
note.creationTime = timestamp;
note.modificationTime = timestamp;
note.text = text;
int result = api.addNote(note);
if (result == CatchAPI.RESULT_OK) {
Log.d(APP_NAME, "Created note ID: "+note.id);
return note.id;
} else {
Log.d(APP_NAME, "Couldn't add note. Error code: "+result);
return null;
}
}
@Override
if (noteId != null) {
Toast.makeText(Dashboard.this, "Note Added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Dashboard.this, "Note Creation Failed!",
Toast.LENGTH_SHORT).show();
}
}
}
}