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.Activity; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.view.View; 25 import android.widget.EditText; 26 27 /** 28 * This Activity allows the user to edit a note's title. It displays a floating window 29 * containing an EditText. 30 * 31 * NOTE: Notice that the provider operations in this Activity are taking place on the UI thread. 32 * This is not a good practice. It is only done here to make the code more readable. A real 33 * application should use the {@link android.content.AsyncQueryHandler} 34 * or {@link android.os.AsyncTask} object to perform operations asynchronously on a separate thread. 35 */ 36 public class TitleEditor extends Activity { 37 38 /** 39 * This is a special intent action that means "edit the title of a note". 40 */ 41 public static final String EDIT_TITLE_ACTION = "com.android.notepad.action.EDIT_TITLE"; 42 43 // Creates a projection that returns the note ID and the note contents. 44 private static final String[] PROJECTION = new String[] { 45 NotePad.Notes._ID, // 0 46 NotePad.Notes.COLUMN_NAME_TITLE, // 1 47 }; 48 49 // The position of the title column in a Cursor returned by the provider. 50 private static final int COLUMN_INDEX_TITLE = 1; 51 52 // A Cursor object that will contain the results of querying the provider for a note. 53 private Cursor mCursor; 54 55 // An EditText object for preserving the edited title. 56 private EditText mText; 57 58 // A URI object for the note whose title is being edited. 59 private Uri mUri; 60 61 /** 62 * This method is called by Android when the Activity is first started. From the incoming 63 * Intent, it determines what kind of editing is desired, and then does it. 64 */ 65 @Override 66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 69 // Set the View for this Activity object's UI. 70 setContentView(R.layout.title_editor); 71 72 // Get the Intent that activated this Activity, and from it get the URI of the note whose 73 // title we need to edit. 74 mUri = getIntent().getData(); 75 76 /* 77 * Using the URI passed in with the triggering Intent, gets the note. 78 * 79 * Note: This is being done on the UI thread. It will block the thread until the query 80 * completes. In a sample app, going against a simple provider based on a local database, 81 * the block will be momentary, but in a real app you should use 82 * android.content.AsyncQueryHandler or android.os.AsyncTask. 83 */ 84 85 mCursor = managedQuery( 86 mUri, // The URI for the note that is to be retrieved. 87 PROJECTION, // The columns to retrieve 88 null, // No selection criteria are used, so no where columns are needed. 89 null, // No where columns are used, so no where values are needed. 90 null // No sort order is needed. 91 ); 92 93 // Gets the View ID for the EditText box 94 mText = (EditText) this.findViewById(R.id.title); 95 } 96 97 /** 98 * This method is called when the Activity is about to come to the foreground. This happens 99 * when the Activity comes to the top of the task stack, OR when it is first starting. 100 * 101 * Displays the current title for the selected note. 102 */ 103 @Override 104 protected void onResume() { 105 super.onResume(); 106 107 // Verifies that the query made in onCreate() actually worked. If it worked, then the 108 // Cursor object is not null. If it is *empty*, then mCursor.getCount() == 0. 109 if (mCursor != null) { 110 111 // The Cursor was just retrieved, so its index is set to one record *before* the first 112 // record retrieved. This moves it to the first record. 113 mCursor.moveToFirst(); 114 115 // Displays the current title text in the EditText object. 116 mText.setText(mCursor.getString(COLUMN_INDEX_TITLE)); 117 } 118 } 119 120 /** 121 * This method is called when the Activity loses focus. 122 * 123 * For Activity objects that edit information, onPause() may be the one place where changes are 124 * saved. The Android application model is predicated on the idea that "save" and "exit" aren't 125 * required actions. When users navigate away from an Activity, they shouldn't have to go back 126 * to it to complete their work. The act of going away should save everything and leave the 127 * Activity in a state where Android can destroy it if necessary. 128 * 129 * Updates the note with the text currently in the text box. 130 */ 131 @Override 132 protected void onPause() { 133 super.onPause(); 134 135 // Verifies that the query made in onCreate() actually worked. If it worked, then the 136 // Cursor object is not null. If it is *empty*, then mCursor.getCount() == 0. 137 138 if (mCursor != null) { 139 140 // Creates a values map for updating the provider. 141 ContentValues values = new ContentValues(); 142 143 // In the values map, sets the title to the current contents of the edit box. 144 values.put(NotePad.Notes.COLUMN_NAME_TITLE, mText.getText().toString()); 145 146 /* 147 * Updates the provider with the note's new title. 148 * 149 * Note: This is being done on the UI thread. It will block the thread until the 150 * update completes. In a sample app, going against a simple provider based on a 151 * local database, the block will be momentary, but in a real app you should use 152 * android.content.AsyncQueryHandler or android.os.AsyncTask. 153 */ 154 getContentResolver().update( 155 mUri, // The URI for the note to update. 156 values, // The values map containing the columns to update and the values to use. 157 null, // No selection criteria is used, so no "where" columns are needed. 158 null // No "where" columns are used, so no "where" values are needed. 159 ); 160 161 } 162 } 163 164 public void onClickOk(View v) { 165 finish(); 166 } 167 } 168