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.codelab.rssexample; 18 19 import android.app.Activity; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.graphics.Typeface; 24 import android.view.Menu; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ListView; 28 import android.widget.TextView; 29 import android.widget.SimpleCursorAdapter; 30 import android.database.Cursor; 31 import android.net.Uri; 32 import android.os.Bundle; 33 34 35 public class MyRssReader4 extends Activity { 36 37 ListView mRssList; 38 Cursor mCur; 39 RssCursorAdapter mAdap; 40 private static final int ADD_ELEMENT_REQUEST = 1; 41 42 @Override onCreate(Bundle savedInstanceState)43 public void onCreate(Bundle savedInstanceState){ 44 super.onCreate(savedInstanceState); 45 46 // Load screen layout. 47 setContentView(R.layout.main_screen2); 48 49 // Populate ArrayAdapter and bind it to ListView 50 mRssList = (ListView)findViewById(R.id.rssListView); 51 52 mCur = managedQuery(RssContentProvider.CONTENT_URI, // Query for all items. 53 null, 54 null, 55 RssContentProvider.DEFAULT_SORT_ORDER); 56 // BEGIN_INCLUDE(4_1) 57 mAdap = new RssCursorAdapter( 58 this, 59 R.layout.list_element, // Our layout resource. 60 mCur, 61 new String[]{RssContentProvider.TITLE}, // Columns to retrieve. 62 new int[]{R.id.list_item}); // IDs of widgets to display 63 mRssList.setAdapter(mAdap); // the corresponding column. 64 // END_INCLUDE(4_1) 65 66 // Set the last selected item. 67 // (icicle is only set if this is being restarted). 68 if(savedInstanceState != null && savedInstanceState.containsKey("lastIndexItem")){ 69 mRssList.setSelection(savedInstanceState.getInteger("lastIndexItem")); 70 } 71 } 72 73 // Store our state before we are potentially bumped from memory. 74 // We'd like to store the current ListView selection. 75 @Override onSaveInstanceState(Bundle outState)76 protected void onSaveInstanceState(Bundle outState){ 77 int index = mRssList.getSelectedItemIndex(); 78 if(index > -1){ 79 outState.putInteger("lastIndexItem", index); 80 } 81 } 82 83 84 // Add our initial menu options. We will tweak this menu when it's loaded swap out 85 // "start service" or "stop service", depending on whether the service is currently running. 86 @Override onCreateOptionsMenu(Menu menu)87 public boolean onCreateOptionsMenu(Menu menu){ 88 // Always call the superclass implementation to 89 // provide standard items. 90 super.onCreateOptionsMenu(menu); 91 92 menu.add(0, 0, R.string.menu_option_start, null); 93 menu.add(0, 1, R.string.menu_option_stop, null); 94 menu.add(0, 2, R.string.menu_option_add, null); 95 menu.add(0, 3, R.string.menu_option_delete, null); 96 menu.add(0, 4, R.string.menu_option_update, null); 97 98 return true; 99 } 100 101 // Toggle out start service/stop service depending on whether the service is running. 102 @Override onPrepareOptionsMenu(Menu menu)103 public boolean onPrepareOptionsMenu(Menu menu){ 104 return true; 105 } 106 107 // Handle our menu clicks. 108 @Override onOptionsItemSelected(Menu.Item item)109 public boolean onOptionsItemSelected(Menu.Item item){ 110 super.onOptionsItemSelected(item); 111 112 switch (item.getId()){ 113 case 0: // Start service 114 showAlert(null, "You clicked 'start'!", "ok", null, false, null); 115 break; 116 case 1: // Stop service 117 showAlert(null, "You clicked stop!", "ok", null, false, null); 118 break; 119 case 2: // Add Item 120 Intent addIntent = new Intent(AddRssItem.class); 121 122 // Use an ID so that if we create a "remove item" form we 123 // can tell which form is returning a value. 124 startActivityForResult(addIntent, ADD_ELEMENT_REQUEST); 125 break; 126 case 3: // Delete item. 127 if(mRssList.hasFocus()){ 128 int currentSelectionIndex = mRssList.getSelectedItemIndex(); 129 130 // Create our content URI by adding the ID of the currently selected item using a 131 // convenience method. 132 Long itemID = mAdap.getItemId(currentSelectionIndex); 133 getContentResolver().delete(RssContentProvider.CONTENT_URI.addId(itemID), null); 134 } 135 break; 136 case 4: // Update all 137 showAlert(null, "You clicked 'Update'!", "ok", null, false, null); 138 break; 139 default: 140 showAlert(null, "I have no idea what you clicked!", "ok", null, false, null); 141 break; 142 } 143 return true; 144 } 145 146 // Called by the "Add RSS Item" floating screen when it closes. 147 @Override onActivityResult(int requestCode, int resultCode, Intent data)148 protected void onActivityResult(int requestCode, int resultCode, Intent data){ 149 if(resultCode == RESULT_OK){ 150 switch (requestCode){ 151 case ADD_ELEMENT_REQUEST: 152 ContentValues vals = new ContentValues(4); 153 vals.put(RssContentProvider.TITLE, data.getStringExtra(RssContentProvider.TITLE)); 154 vals.put(RssContentProvider.URL, data.getStringExtra(RssContentProvider.URL)); 155 vals.put(RssContentProvider.CONTENT, data.getStringExtra(RssContentProvider.CONTENT)); 156 vals.put(RssContentProvider.LAST_UPDATED, data.getIntExtra(RssContentProvider.LAST_UPDATED, 0)); 157 Uri uri = getContentResolver().insert( 158 RssContentProvider.CONTENT_URI, 159 vals); 160 if(uri != null){ 161 mRssList.setSelection(mRssList.getCount() - 1); 162 } 163 break; 164 default: 165 break; 166 } 167 } 168 } 169 170 // Our private ArrayAdapter implementation that returns a bold TextView for 171 // RSS items that are unread, or a normal TextView for items that have been read. 172 private class RssCursorAdapter extends SimpleCursorAdapter { RssCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)173 public RssCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to){ 174 super(context, layout, c, from, to); 175 } 176 177 // Here's our only important override--returning the list item. 178 @Override getView(int position, View convertView, ViewGroup parent)179 public View getView(int position, View convertView, ViewGroup parent){ 180 TextView view = (TextView)super.getView(position, convertView, parent); 181 182 if(view != null){ 183 184 // Now get the hasBeenRead value to determine the font. 185 int hasBeenReadColumnIndex = getCursor().getColumnIndex( 186 RssContentProvider.HAS_BEEN_READ); 187 boolean hasBeenRead = (getCursor().getInt(hasBeenReadColumnIndex) == 1 ? true : false); 188 if(! hasBeenRead){ 189 Typeface type = view.getTypeface(); 190 view.setTypeface(Typeface.create(type, Typeface.BOLD_ITALIC)); 191 } 192 } 193 return view; 194 } 195 } 196 } 197 198