• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.notepad;
18 
19 import android.app.ListActivity;
20 import android.content.ComponentName;
21 import android.content.ContentUris;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.ContextMenu;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.ContextMenu.ContextMenuInfo;
33 import android.widget.AdapterView;
34 import android.widget.ListView;
35 import android.widget.SimpleCursorAdapter;
36 
37 import com.example.android.notepad.NotePad.NoteColumns;
38 
39 /**
40  * Displays a list of notes. Will display notes from the {@link Uri}
41  * provided in the intent if there is one, otherwise defaults to displaying the
42  * contents of the {@link NoteProvider}
43  */
44 public class NotesList extends ListActivity {
45     private static final String TAG = "NotesList";
46 
47     /**
48      * The columns we are interested in from the database
49      */
50     private static final String[] PROJECTION = new String[] {
51         NoteColumns._ID, // 0
52         NoteColumns.TITLE, // 1
53     };
54 
55     /** The index of the title column */
56     private static final int COLUMN_INDEX_TITLE = 1;
57 
58     @Override
onCreate(Bundle savedInstanceState)59     protected void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61 
62         setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
63 
64         // If no data was given in the intent (because we were started
65         // as a MAIN activity), then use our default content provider.
66         Intent intent = getIntent();
67         if (intent.getData() == null) {
68             intent.setData(NoteColumns.CONTENT_URI);
69         }
70 
71         // Inform the list we provide context menus for items
72         getListView().setOnCreateContextMenuListener(this);
73 
74         // Perform a managed query. The Activity will handle closing and requerying the cursor
75         // when needed.
76         Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
77                                         NoteColumns.DEFAULT_SORT_ORDER);
78 
79         // Used to map notes entries from the database to views
80         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
81                 new String[] { NoteColumns.TITLE }, new int[] { android.R.id.text1 });
82         setListAdapter(adapter);
83     }
84 
85     @Override
onCreateOptionsMenu(Menu menu)86     public boolean onCreateOptionsMenu(Menu menu) {
87         // Inflate menu from XML resource
88         MenuInflater inflater = getMenuInflater();
89         inflater.inflate(R.menu.list_options_menu, menu);
90 
91         // Generate any additional actions that can be performed on the
92         // overall list.  In a normal install, there are no additional
93         // actions found here, but this allows other applications to extend
94         // our menu with their own actions.
95         Intent intent = new Intent(null, getIntent().getData());
96         intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
97         menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
98                 new ComponentName(this, NotesList.class), null, intent, 0, null);
99 
100         return super.onCreateOptionsMenu(menu);
101     }
102 
103     @Override
onOptionsItemSelected(MenuItem item)104     public boolean onOptionsItemSelected(MenuItem item) {
105         switch (item.getItemId()) {
106         case R.id.menu_add:
107             // Launch activity to insert a new item
108             startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));
109             return true;
110         default:
111             return super.onOptionsItemSelected(item);
112         }
113     }
114 
115     @Override
onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)116     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
117         AdapterView.AdapterContextMenuInfo info;
118         try {
119              info = (AdapterView.AdapterContextMenuInfo) menuInfo;
120         } catch (ClassCastException e) {
121             Log.e(TAG, "bad menuInfo", e);
122             return;
123         }
124 
125         Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
126         if (cursor == null) {
127             // For some reason the requested item isn't available, do nothing
128             return;
129         }
130 
131         // Inflate menu from XML resource
132         MenuInflater inflater = getMenuInflater();
133         inflater.inflate(R.menu.list_context_menu, menu);
134 
135         // Set the context menu header
136         menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));
137 
138         // Append to the
139         // menu items for any other activities that can do stuff with it
140         // as well.  This does a query on the system for any activities that
141         // implement the ALTERNATIVE_ACTION for our data, adding a menu item
142         // for each one that is found.
143         Intent intent = new Intent(null, Uri.withAppendedPath(getIntent().getData(),
144                                         Integer.toString((int) info.id) ));
145         intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
146         menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
147                 new ComponentName(this, NotesList.class), null, intent, 0, null);
148     }
149 
150     @Override
onContextItemSelected(MenuItem item)151     public boolean onContextItemSelected(MenuItem item) {
152         AdapterView.AdapterContextMenuInfo info;
153         try {
154              info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
155         } catch (ClassCastException e) {
156             Log.e(TAG, "bad menuInfo", e);
157             return false;
158         }
159 
160         Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
161 
162         switch (item.getItemId()) {
163         case R.id.context_open:
164             // Launch activity to view/edit the currently selected item
165             startActivity(new Intent(Intent.ACTION_EDIT, noteUri));
166             return true;
167         case R.id.context_delete:
168             // Delete the note that the context menu is for
169             getContentResolver().delete(noteUri, null, null);
170             return true;
171         default:
172             return super.onContextItemSelected(item);
173         }
174     }
175 
176     @Override
onListItemClick(ListView l, View v, int position, long id)177     protected void onListItemClick(ListView l, View v, int position, long id) {
178         Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), id);
179 
180         String action = getIntent().getAction();
181         if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
182             // The caller is waiting for us to return a note selected by
183             // the user.  The have clicked on one, so return it now.
184             setResult(RESULT_OK, new Intent().setData(noteUri));
185         } else {
186             // Launch activity to view/edit the currently selected item
187             startActivity(new Intent(Intent.ACTION_EDIT, noteUri));
188         }
189     }
190 }
191