|
Android example source code file (WikiActivityHelper.java)
The WikiActivityHelper.java Android example source code
/*
* Copyright (C) 2008 Google Inc.
*
* 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 com.google.android.wikinotes;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import com.google.android.wikinotes.db.WikiNote;
/**
* A helper class that implements operations required by Activities in the
* system. Notably, this includes launching the Activities for edit and
* delete.
*/
public class WikiActivityHelper {
public static final int ACTIVITY_EDIT = 1;
public static final int ACTIVITY_DELETE = 2;
public static final int ACTIVITY_LIST = 3;
public static final int ACTIVITY_SEARCH = 4;
private Activity mContext;
/**
* Preferred constructor.
*
* @param context
* the Activity to be used for things like calling
* startActivity
*/
public WikiActivityHelper(Activity context) {
mContext = context;
}
/**
* @see #WikiActivityHelper(Activity)
*/
// intentionally private; tell IDEs not to warn us about it
@SuppressWarnings("unused")
private WikiActivityHelper() {
}
/**
* If a list of notes is requested, fire the WikiNotes list Content URI
* and let the WikiNotesList activity handle it.
*/
public void listNotes() {
Intent i = new Intent(Intent.ACTION_VIEW,
WikiNote.Notes.ALL_NOTES_URI);
mContext.startActivity(i);
}
/**
* If requested, go back to the start page wikinote by requesting an
* intent with the default start page URI
*/
public void goHome() {
Uri startPageURL = Uri
.withAppendedPath(WikiNote.Notes.ALL_NOTES_URI, mContext
.getResources().getString(R.string.start_page));
Intent startPage = new Intent(Intent.ACTION_VIEW, startPageURL);
mContext.startActivity(startPage);
}
/**
* Create an intent to start the WikiNoteEditor using the current title
* and body information (if any).
*/
public void editNote(String mNoteName, Cursor cursor) {
// This intent could use the android.intent.action.EDIT for a wiki
// note
// to invoke, but instead I wanted to demonstrate the mechanism for
// invoking
// an intent on a known class within the same application directly.
// Note
// also that the title and body of the note to edit are passed in
// using
// the extras bundle.
Intent i = new Intent(mContext, WikiNoteEditor.class);
i.putExtra(WikiNote.Notes.TITLE, mNoteName);
String body;
if (cursor != null) {
body = cursor.getString(cursor
.getColumnIndexOrThrow(WikiNote.Notes.BODY));
} else {
body = "";
}
i.putExtra(WikiNote.Notes.BODY, body);
mContext.startActivityForResult(i, ACTIVITY_EDIT);
}
/**
* If requested, delete the current note. The user is prompted to confirm
* this operation with a dialog, and if they choose to go ahead with the
* deletion, the current activity is finish()ed once the data has been
* removed, so that android naturally backtracks to the previous activity.
*/
public void deleteNote(final Cursor cursor) {
new AlertDialog.Builder(mContext)
.setTitle(
mContext.getResources()
.getString(R.string.delete_title))
.setMessage(R.string.delete_message)
.setPositiveButton(R.string.yes_button, new OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Uri noteUri = ContentUris
.withAppendedId(WikiNote.Notes.ALL_NOTES_URI, cursor
.getInt(0));
mContext.getContentResolver().delete(noteUri, null, null);
mContext.setResult(Activity.RESULT_OK);
mContext.finish();
}
}).setNegativeButton(R.string.no_button, null).show();
}
/**
* Equivalent to showOutcomeDialog(titleId, msg, false).
*
* @see #showOutcomeDialog(int, String, boolean)
* @param titleId
* the resource ID of the title of the dialog window
* @param msg
* the message to display
*/
private void showOutcomeDialog(int titleId, String msg) {
showOutcomeDialog(titleId, msg, false);
}
/**
* Creates and displays a Dialog with the indicated title and body. If
* kill is true, the Dialog calls finish() on the hosting Activity and
* restarts it with an identical Intent. This effectively restarts or
* refreshes the Activity, so that it can reload whatever data it was
* displaying.
*
* @param titleId
* the resource ID of the title of the dialog window
* @param msg
* the message to display
*/
private void showOutcomeDialog(int titleId, String msg, final boolean kill) {
new AlertDialog.Builder(mContext).setCancelable(false)
.setTitle(mContext.getResources().getString(titleId))
.setMessage(msg)
.setPositiveButton(R.string.export_dismiss_button,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
if (kill) {
// restart the Activity w/ same
// Intent
Intent intent = mContext
.getIntent();
mContext.finish();
mContext.startActivity(intent);
}
}
}).create().show();
}
/**
* Exports notes to the SD card. Displays Dialogs as appropriate (using
* the Activity provided in the constructor as a Context) to inform the
* user as to what is going on.
*/
public void exportNotes() {
// first see if an SD card is even present; abort if not
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
showOutcomeDialog(R.string.export_failure_title, mContext
.getResources().getString(R.string.export_failure_missing_sd));
return;
}
// do a query for all records
Cursor c = mContext.managedQuery(WikiNote.Notes.ALL_NOTES_URI,
WikiNote.WIKI_EXPORT_PROJECTION,
null, null,
WikiNote.Notes.DEFAULT_SORT_ORDER);
// iterate over all Notes, building up an XML StringBuffer along the
// way
boolean dataAvailable = c.moveToFirst();
StringBuffer sb = new StringBuffer();
String title, body, created, modified;
int cnt = 0;
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
Other Android examples (source code examples)Here is a short list of links related to this Android WikiActivityHelper.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.