summaryrefslogtreecommitdiff
path: root/samples/NotePad/src/com/example/android/notepad/NotesList.java
diff options
context:
space:
mode:
authorJoe Malin <jmalin@google.com>2010-08-09 17:18:13 -0700
committerJoe Malin <jmalin@google.com>2010-08-09 17:18:13 -0700
commit70b1869f383179f87d4db7ff237c80595458fd2d (patch)
treeafa52d3a1dd3a2210215c4374f11f26ea3a2dd20 /samples/NotePad/src/com/example/android/notepad/NotesList.java
parent4124e0a1f07e4e54c37b0cfbb1b7438806ff02a6 (diff)
Revert "Revised Note Pad sample, new test app for Note Pad"
This reverts commit 4124e0a1f07e4e54c37b0cfbb1b7438806ff02a6.
Diffstat (limited to 'samples/NotePad/src/com/example/android/notepad/NotesList.java')
-rw-r--r--samples/NotePad/src/com/example/android/notepad/NotesList.java430
1 files changed, 94 insertions, 336 deletions
diff --git a/samples/NotePad/src/com/example/android/notepad/NotesList.java b/samples/NotePad/src/com/example/android/notepad/NotesList.java
index 63d480ee8..bbcb936a3 100644
--- a/samples/NotePad/src/com/example/android/notepad/NotesList.java
+++ b/samples/NotePad/src/com/example/android/notepad/NotesList.java
@@ -16,9 +16,14 @@
package com.example.android.notepad;
+import com.example.android.notepad.NotePad.Notes;
+
import android.app.ListActivity;
+import android.content.ClipboardManager;
+import android.content.ClippedData;
import android.content.ComponentName;
import android.content.ContentUris;
+import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
@@ -33,464 +38,217 @@ import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
-import com.example.android.notepad.NotePad;
-
/**
* Displays a list of notes. Will display notes from the {@link Uri}
- * provided in the incoming Intent if there is one, otherwise it defaults to displaying the
- * contents of the {@link NotePadProvider}.
- *
- * NOTE: Notice that the provider operations in this Activity are taking place on the UI thread.
- * This is not a good practice. It is only done here to make the code more readable. A real
- * application should use the {@link android.content.AsyncQueryHandler} or
- * {@link android.os.AsyncTask} object to perform operations asynchronously on a separate thread.
+ * provided in the intent if there is one, otherwise defaults to displaying the
+ * contents of the {@link NotePadProvider}
*/
public class NotesList extends ListActivity {
-
- // For logging and debugging
private static final String TAG = "NotesList";
// Menu item ids
public static final int MENU_ITEM_DELETE = Menu.FIRST;
- public static final int MENU_ITEM_INSERT = Menu.FIRST + 1;
-
+ public static final int MENU_ITEM_COPY = Menu.FIRST + 1;
+ public static final int MENU_ITEM_INSERT = Menu.FIRST + 2;
+ public static final int MENU_ITEM_PASTE = Menu.FIRST + 3;
/**
- * The columns needed by the cursor adapter
+ * The columns we are interested in from the database
*/
private static final String[] PROJECTION = new String[] {
- NotePad.Notes._ID, // 0
- NotePad.Notes.COLUMN_NAME_TITLE, // 1
+ Notes._ID, // 0
+ Notes.TITLE, // 1
};
- /**
- * onCreate is called when Android starts this Activity from scratch.
- */
+ /** The index of the title column */
+ private static final int COLUMN_INDEX_TITLE = 1;
+
+ private MenuItem mPasteItem;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- // The user does not need to hold down the key to use menu shortcuts.
setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
- /* If no data is given in the Intent that started this Activity, then this Activity
- * was started when the intent filter matched a MAIN action. We should use the default
- * provider URI.
- */
- // Gets the intent that started this Activity.
+ // If no data was given in the intent (because we were started
+ // as a MAIN activity), then use our default content provider.
Intent intent = getIntent();
-
- // If there is no data associated with the Intent, sets the data to the default URI, which
- // accesses a list of notes.
if (intent.getData() == null) {
- intent.setData(NotePad.Notes.CONTENT_URI);
+ intent.setData(Notes.CONTENT_URI);
}
- /*
- * Sets the callback for context menu activation for the ListView. The listener is set
- * to be this Activity. The effect is that context menus are enabled for items in the
- * ListView, and the context menu is handled by a method in NotesList.
- */
-
+ // Inform the list we provide context menus for items
getListView().setOnCreateContextMenuListener(this);
-
- /* Performs a managed query. The Activity handles closing and requerying the cursor
- * when needed.
- *
- * Please see the introductory note about performing provider operations on the UI thread.
- */
- Cursor cursor = managedQuery(
- getIntent().getData(), // Use the default content URI for the provider.
- PROJECTION, // Return the note ID and title for each note.
- null, // No where clause, return all records.
- null, // No where clause, therefore no where column values.
- NotePad.Notes.DEFAULT_SORT_ORDER // Use the default sort order.
- );
-
- /*
- * The following two arrays create a "map" between columns in the cursor and view IDs
- * for items in the ListView. Each element in the dataColumns array represents
- * a column name; each element in the viewID array represents the ID of a View.
- * The SimpleCursorAdapter maps them in ascending order to determine where each column
- * value will appear in the ListView.
- */
-
- // The names of the cursor columns to display in the view, initialized to the title column
- String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE } ;
-
- // The view IDs that will display the cursor columns, initialized to the TextView in
- // noteslist_item.xml
- int[] viewIDs = { android.R.id.text1 };
-
- // Creates the backing adapter for the ListView.
- SimpleCursorAdapter adapter
- = new SimpleCursorAdapter(
- this, // The Context for the ListView
- R.layout.noteslist_item, // Points to the XML for a list item
- cursor, // The cursor to get items from
- dataColumns,
- viewIDs
- );
-
- // Sets the ListView's adapter to be the cursor adapter that was just created.
+
+ // Perform a managed query. The Activity will handle closing and requerying the cursor
+ // when needed.
+ Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
+ Notes.DEFAULT_SORT_ORDER);
+
+ // Used to map notes entries from the database to views
+ SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
+ new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });
setListAdapter(adapter);
}
- /**
- * Called when the user clicks the device's Menu button the first time for
- * this Activity. Android passes in a Menu object that is populated with items.
- *
- * Sets up a menu that provides the Insert option plus a list of alternative actions for
- * this Activity. Other applications that want to handle notes can "register" themselves in
- * Android by providing an intent filter that includes the category ALTERNATIVE and the
- * mimeTYpe NotePad.Notes.CONTENT_TYPE. If they do this, the code in onCreateOptionsMenu()
- * will add the Activity that contains the intent filter to its list of options. In effect,
- * the menu will offer the user other applications that can handle notes.
- * @param menu A Menu object, to which menu items should be added.
- * @return True, always. The menu should be displayed.
- */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
- // Adds an Insert menu item
- MenuItem menuItem = menu.add(
- Menu.NONE, // No menu group.
- MENU_ITEM_INSERT, // Unique ID for this item.
- Menu.NONE, // No order within the group.
- R.string.menu_insert // Displayed text for the menu item.
- );
-
- // Sets keyboard shortcuts for the menu item, either "3" or "a";
- menuItem.setShortcut('3', 'a');
+ // This is our one standard application action -- inserting a
+ // new note into the list.
+ menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)
+ .setShortcut('3', 'a')
+ .setIcon(android.R.drawable.ic_menu_add);
- // Sets the icon for the menu item
- menuItem.setIcon(android.R.drawable.ic_menu_add);
+ // If there is currently data in the clipboard, we can paste it
+ // as a new note.
+ mPasteItem = menu.add(0, MENU_ITEM_PASTE, 0, R.string.menu_paste)
+ .setShortcut('4', 'p');
- // Generates any additional actions that can be performed on the
+ // Generate any additional actions that can be performed on the
// overall list. In a normal install, there are no additional
// actions found here, but this allows other applications to extend
// our menu with their own actions.
-
- /* Creates a new Intent with the same incoming data and no defined action.
- * It also sets its category to ALTERNATIVE. This prepares the Intent as a place
- * to group alternative options in the menu.
- */
Intent intent = new Intent(null, getIntent().getData());
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
+ menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
+ new ComponentName(this, NotesList.class), null, intent, 0, null);
- /* Creates a ComponentName from the current Context and this Activity object's
- * class object.
- */
- ComponentName component = new ComponentName(this, NotesList.class);
-
- /*
- * Adds any other activities that want to be alternatives for this view. In effect,
- * any application can add itself as an alternative on the options menu.
- */
- menu.addIntentOptions(
- Menu.CATEGORY_ALTERNATIVE, // Add the options to the Alternatives group
- Menu.NONE, // Do not use a unique ID
- Menu.NONE, // No need to order the options
- component, // The ComponentName of the Activity making the request.
- // This Activity is excluded from the list of alternatives.
- null, // No specific items are listed first.
- intent, // The Intent to resolve to, in effect, an Intent listing
- // the alternatives
- Menu.NONE, // no flags are needed
- null // Since no specifics were used, so a menu item array is
- // not needed.
- );
-
- // Returns true so that the menu is displayed.
return true;
}
- /**
- * Called when the user clicks the device's Menu button any time after the first time for
- * this Activity. This modifies the standard options menu that was set up previously.
- * See onCreateOptionsMenu().
- * @param menu The existing Menu object for this Activity.
- * @return True, always. The menu should be displayed.
- */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
- // Does the displayed list of notes have any items in it?
- final boolean haveItems = getListAdapter().getCount() > 0;
+ // The paste menu item is enabled if there is data on the clipboard.
+ ClipboardManager clipboard = (ClipboardManager)
+ getSystemService(Context.CLIPBOARD_SERVICE);
+ if (clipboard.hasPrimaryClip()) {
+ mPasteItem.setEnabled(true);
+ } else {
+ mPasteItem.setEnabled(false);
+ }
- /*
- * If there are notes in the list, assumes that one of them is selected.
- * Generates the menu options that can be performed on the current selection,
- * which includes options from this application plus actions from other
- * applications that "registered" themselves using an intent filter for the
- * NotePad.Notes.CONTENT_TYPE mimeType.
- */
+ final boolean haveItems = getListAdapter().getCount() > 0;
- // Modifies the current menu
+ // If there are any notes in the list (which implies that one of
+ // them is selected), then we need to generate the actions that
+ // can be performed on the current selection. This will be a combination
+ // of our own specific actions along with any extensions that can be
+ // found.
if (haveItems) {
-
- /*
- * Appends the selected item's row ID (note ID) to the the URI in the incoming Intent's
- * data area. The incoming Intent at this point could only be GET_CONTENT, which means
- * that the URI is for a single note, so this statement constructs a URI that will
- * select a single note from the provider.
- */
+ // This is the selected item.
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
- /*
- * Builds the menu. The EDIT action is always the first action in the list, so
- * a specifics array is required, but only needs one element.
- *
- */
-
- // Creates an array of "specifics", Intents that must appear before all other options
+ // Build menu... always starts with the EDIT action...
Intent[] specifics = new Intent[1];
-
- // Sets the Intent of the specifics array to be EDIT on the note ID URI
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
-
- // Creates an array of menu items. The result of mapping specifics to Intents is put
- // into this array
MenuItem[] items = new MenuItem[1];
- // Creates an Intent with no defined action and the note ID URI as data
+ // ... is followed by whatever other actions are available...
Intent intent = new Intent(null, uri);
-
- /* Adds the category ALTERNATIVE to the Intent, with the note ID URI as its
- * data. This prepares the Intent as a place to group alternative options in the
- * menu.
- */
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
+ menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,
+ items);
- /*
- * Add alternatives to the menu
- */
- menu.addIntentOptions(
- Menu.CATEGORY_ALTERNATIVE, // Add the Intents as options in the alternatives group.
- Menu.NONE, // A unique item ID is not required.
- Menu.NONE, // The alternatives don't need to be in order.
- null, // The caller's name is not excluded from the group.
- specifics, // These specific options must appear first.
- intent, // These Intent objects map to the options in specifics.
- Menu.NONE, // No flags are required.
- items // The menu items generated from the specifics-to-
- // Intents mapping
- );
-
- // If the Edit menu item exists, adds shortcuts for it.
+ // Give a shortcut to the edit action.
if (items[0] != null) {
-
- // Sets the Edit menu item shortcut to numeric "1", letter "e"
items[0].setShortcut('1', 'e');
}
} else {
- // If the list is empty, removes any existing alternative actions from the menu
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}
- // Displays the menu
return true;
}
- /**
- * This method is called when the user selects an option from the menu, but no item
- * in the list is selected. If the option was INSERT, then a new Intent is sent out with action
- * ACTION_INSERT. The data from the incoming Intent is put into the new Intent. In effect,
- * this triggers the NoteEditor activity in the NotePad application.
- *
- * If the item was not INSERT, then most likely it was an alternative option from another
- * application. The parent method is called to process the item.
- * @param item The menu item that was selected by the user
- * @return True, if the INSERT menu item was selected; otherwise, the result of calling
- * the parent method.
- */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
-
- // Gets the ID of the selected item
switch (item.getItemId()) {
-
- // If the menu item is Insert
case MENU_ITEM_INSERT:
-
- /*
- * Launches a new Activity using an Intent. The intent filter for the Activity
- * has to have action ACTION_INSERT. No category is set, so DEFAULT is assumed.
- * In effect, this starts the NoteEditor Activity in NotePad.
- */
+ // Launch activity to insert a new item
startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));
-
- // Returns true, so that no more processing is done.
+ return true;
+ case MENU_ITEM_PASTE:
+ // Launch activity to insert a new item
+ startActivity(new Intent(Intent.ACTION_PASTE, getIntent().getData()));
return true;
}
-
- // If the menu item selected is not Insert, then this calls the regular processing to
- // handle the item.
return super.onOptionsItemSelected(item);
}
- /**
- * This method is called when the user context-clicks a note in the list. NotesList registers
- * itself as the handler for context menus in its ListView (this is done in onCreate()).
- *
- * The only available option is DELETE.
- *
- * Context-click is equivalent to long-press.
- *
- * @param menu A ContexMenu object to which items should be added.
- * @param view The View for which the context menu is being constructed.
- * @param menuInfo Data associated with view.
- * @throws ClassCastException
- */
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info;
-
- // Tries to get the position of the item in the ListView that was long-pressed.
try {
- // Casts the incoming data object into the type for AdapterView objects.
- info = (AdapterView.AdapterContextMenuInfo) menuInfo;
-
+ info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
-
- // If the menu object can't be cast, logs an error.
Log.e(TAG, "bad menuInfo", e);
return;
}
- /*
- * Gets the data associated with the item at the selected position. getItem() returns
- * whatever the backing adapter of the ListView has associated with the item. In NotesList,
- * the adapter associated all of the data for a note with its list item. As a result,
- * getItem() returns that data as a Cursor.
- */
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
-
- // If the cursor is empty, then for some reason the adapter can't get the data from the
- // provider, so returns null to the caller.
if (cursor == null) {
// For some reason the requested item isn't available, do nothing
return;
}
- // Finds the index of the title column in the Cursor
- int titleIndex = cursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_TITLE);
+ // Setup the menu header
+ menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));
- // Sets the menu header to be the title of the selected note.
- menu.setHeaderTitle(cursor.getString(titleIndex));
+ // Add a menu item to copy the note
+ menu.add(0, MENU_ITEM_COPY, 0, R.string.menu_copy);
- // Adds a menu item to the context menu
- menu.add(
- Menu.NONE, // No grouping is needed for this menu
- MENU_ITEM_DELETE, // A unique ID for this menu item.
- Menu.NONE, // No ordering is necessary in this menu
- R.string.menu_delete // The resource ID for the string to display for this item.
- );
+ // Add a menu item to delete the note
+ menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);
}
-
- /**
- * This method is called when the user selects an item from the context menu
- * (see onCreateContextMenu()). The only menu item that is actually handled is DELETE.
- * Anything else is an alternative option, for which default handling should be done.
- *
- * @param item The selected menu item
- * @return True if the menu item was DELETE, and no default processing is need, otherwise false,
- * which triggers the default handling of the item.
- * @throws ClassCastException
- */
+
@Override
public boolean onContextItemSelected(MenuItem item) {
-
- // The data from the menu item.
AdapterView.AdapterContextMenuInfo info;
-
- /*
- * Gets the extra info from the menu item. When an note in the Notes list is long-pressed, a
- * context menu appears. The menu items for the menu automatically get the data
- * associated with the note that was long-pressed. The data comes from the provider that
- * backs the list.
- *
- * The note's data is passed to the context menu creation routine in a ContextMenuInfo
- * object.
- *
- * When one of the context menu items is clicked, the same data is passed, along with the
- * note ID, to onContextItemSelected() via the item parameter.
- */
-
try {
- // Casts the data object in the item into the type for AdapterView objects.
- info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
-
+ info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
-
- // If the object can't be cast, logs an error
Log.e(TAG, "bad menuInfo", e);
-
- // Triggers default processing of the menu item.
return false;
}
- /*
- * Gets the menu item's ID and compares it to known actions. The only action that is
- * implemented is DELETE (it is set in onCreateContextMenu()). The switch statement is
- * used to facilitate adding other actions in the future.
- */
switch (item.getItemId()) {
case MENU_ITEM_DELETE: {
- // Appends the selected note's ID to the URI sent with the incoming Intent.
+ // Delete the note that the context menu is for
Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
-
- // Deletes the note from the provider by passing in a URI in note ID format.
- // Please see the introductory note about performing provider operations on the
- // UI thread.
- getContentResolver().delete(
- noteUri, // The URI of the provider
- null, // No where clause is needed, since only a single note ID is being
- // passed in.
- null // No where clause is used, so no where arguments are needed.
- );
-
- // Returns to the caller and skips further processing.
+ getContentResolver().delete(noteUri, null, null);
return true;
}
+//BEGIN_INCLUDE(copy)
+ case MENU_ITEM_COPY: {
+ // Copy the note that the context menu is for on to the clipboard
+ ClipboardManager clipboard = (ClipboardManager)
+ getSystemService(Context.CLIPBOARD_SERVICE);
+ Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
+ clipboard.setPrimaryClip(new ClippedData(null, null, new ClippedData.Item(
+ noteUri)));
+ return true;
+ }
+//END_INCLUDE(copy)
}
-
- // For menu items other than DELETE, returns to the caller for further processing.
return false;
}
- /**
- * This method is called when the user clicks a note in the displayed list.
- *
- * This method handles incoming actions of either PICK (get data from the provider) or
- * GET_CONTENT (get or create data). If the incoming action is EDIT, this method sends a
- * new Intent to start NoteEditor.
- * @param l The ListView that contains the clicked item
- * @param v The View of the individual item
- * @param position The position of v in the displayed list
- * @param id The row ID of the clicked item
- */
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
-
- // Constructs a new URI from the incoming URI and the row ID
Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
-
- // Gets the action from the incoming Intent
+
String action = getIntent().getAction();
-
- // Handles requests for note data
if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
-
- // Sets the result to return to the component that called this Activity. The
- // result contains the new URI
+ // The caller is waiting for us to return a note selected by
+ // the user. The have clicked on one, so return it now.
setResult(RESULT_OK, new Intent().setData(uri));
} else {
-
- // Sends out an Intent to start an Activity that can handle ACTION_EDIT. The
- // Intent's data is the note ID URI. The effect is to call NoteEdit.
+ // Launch activity to view/edit the currently selected item
startActivity(new Intent(Intent.ACTION_EDIT, uri));
}
}