Home | History | Annotate | Download | only in notepad
      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.Button;
     26 import android.widget.EditText;
     27 
     28 import com.example.android.notepad.NotePad.NoteColumns;
     29 
     30 /**
     31  * An activity that will edit the title of a note. Displays a floating
     32  * window with a text field.
     33  */
     34 public class TitleEditor extends Activity implements View.OnClickListener {
     35 
     36     /**
     37      * This is a special intent action that means "edit the title of a note".
     38      */
     39     public static final String EDIT_TITLE_ACTION = "com.android.notepad.action.EDIT_TITLE";
     40 
     41     /**
     42      * An array of the columns we are interested in.
     43      */
     44     private static final String[] PROJECTION = new String[] {
     45         NoteColumns._ID, // 0
     46         NoteColumns.TITLE, // 1
     47     };
     48     /** Index of the title column */
     49     private static final int COLUMN_INDEX_TITLE = 1;
     50 
     51     /**
     52      * Cursor which will provide access to the note whose title we are editing.
     53      */
     54     private Cursor mCursor;
     55 
     56     /**
     57      * The EditText field from our UI. Keep track of this so we can extract the
     58      * text when we are finished.
     59      */
     60     private EditText mText;
     61 
     62     /**
     63      * The content URI to the note that's being edited.
     64      */
     65     private Uri mUri;
     66 
     67     @Override
     68     public void onCreate(Bundle savedInstanceState) {
     69         super.onCreate(savedInstanceState);
     70 
     71         setContentView(R.layout.title_editor);
     72 
     73         // Get the uri of the note whose title we want to edit
     74         mUri = getIntent().getData();
     75 
     76         // Get a cursor to access the note
     77         mCursor = managedQuery(mUri, PROJECTION, null, null, null);
     78 
     79         // Set up click handlers for the text field and button
     80         mText = (EditText) this.findViewById(R.id.title);
     81         mText.setOnClickListener(this);
     82 
     83         Button b = (Button) findViewById(R.id.ok);
     84         b.setOnClickListener(this);
     85     }
     86 
     87     @Override
     88     protected void onResume() {
     89         super.onResume();
     90 
     91         // Initialize the text with the title column from the cursor
     92         if (mCursor != null) {
     93             mCursor.moveToFirst();
     94             mText.setText(mCursor.getString(COLUMN_INDEX_TITLE));
     95         }
     96     }
     97 
     98     @Override
     99     protected void onPause() {
    100         super.onPause();
    101 
    102         if (mCursor != null) {
    103             // Write the title back to the note
    104             ContentValues values = new ContentValues();
    105             values.put(NoteColumns.TITLE, mText.getText().toString());
    106             getContentResolver().update(mUri, values, null, null);
    107         }
    108     }
    109 
    110     public void onClick(View v) {
    111         // When the user clicks, just finish this activity.
    112         // onPause will be called, and we save our data there.
    113         finish();
    114     }
    115 }
    116