1 /* //device/apps/Notes/NotesList.java 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 package com.android.development; 18 19 import java.util.ArrayList; 20 21 import android.content.Intent; 22 import android.app.Activity; 23 import android.database.Cursor; 24 import android.graphics.Typeface; 25 import android.widget.LinearLayout; 26 import android.widget.ScrollView; 27 import android.os.Bundle; 28 import android.view.Menu; 29 import android.view.MenuItem; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.TextView; 33 34 public class Details extends Activity 35 { onCreate(Bundle icicle)36 public void onCreate(Bundle icicle) 37 { 38 super.onCreate(icicle); 39 40 Intent intent = getIntent(); 41 42 String title = intent.getStringExtra("title"); 43 if (title == null) { 44 title = "Details"; 45 } 46 setTitle(title); 47 48 mScrollView = new ScrollView(this); 49 setContentView(mScrollView); 50 mScrollView.setFocusable(true); 51 52 mData = (ArrayList<ColumnData>)getIntent().getExtra("data"); 53 addDataViews(); 54 } 55 onResume()56 public void onResume() 57 { 58 super.onResume(); 59 } 60 onCreateOptionsMenu(Menu menu)61 public boolean onCreateOptionsMenu(Menu menu) 62 { 63 super.onCreateOptionsMenu(menu); 64 menu.add(0, 0, 0, "Requery").setOnMenuItemClickListener(mRequery); 65 menu.add(0, 0, 0, "Print to stdout").setOnMenuItemClickListener(mPrintToStdout); 66 return true; 67 } 68 addDataViews()69 void addDataViews() 70 { 71 int oldScroll = 0; 72 73 if (mLinearLayout != null) { 74 mScrollView.removeView(mLinearLayout); 75 } 76 mLinearLayout = new LinearLayout(this); 77 mScrollView.addView(mLinearLayout, new ViewGroup.LayoutParams( 78 ViewGroup.LayoutParams.FILL_PARENT, 79 ViewGroup.LayoutParams.FILL_PARENT)); 80 mLinearLayout.setOrientation(LinearLayout.VERTICAL); 81 82 // Here in onStart, we're given data. We use that because some 83 // data that we show is transient and can't be retrieved from a url. 84 // We'll try to use that in requery 85 int count = mData.size(); 86 for (int i=0; i<count; i++) { 87 ColumnData cd = mData.get(i); 88 TextView label = makeView(cd.key, true, 12); 89 TextView contents = makeView(cd.value, false, 12); 90 contents.setPadding(3, 0, 0, i==count-1?0:3); 91 mLinearLayout.addView(label, lazy()); 92 mLinearLayout.addView(contents, lazy()); 93 } 94 } 95 makeView(String str, boolean bold, int fontSize)96 TextView makeView(String str, boolean bold, int fontSize) 97 { 98 if (str == null) { 99 str = "(null)"; 100 } 101 TextView v = new TextView(this); 102 v.setText(str); 103 v.setTextSize(fontSize); 104 if (bold) { 105 v.setTypeface(Typeface.DEFAULT_BOLD); 106 } 107 return v; 108 } 109 lazy()110 LinearLayout.LayoutParams lazy() 111 { 112 return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 113 ViewGroup.LayoutParams.WRAP_CONTENT, 0); 114 } 115 116 MenuItem.OnMenuItemClickListener mRequery = new MenuItem.OnMenuItemClickListener() { 117 public boolean onMenuItemClick(MenuItem item) { 118 Intent intent = getIntent(); 119 Cursor c = getContentResolver().query(intent.getData(), null, null, null, null); 120 if (c != null && c.moveToNext()) { 121 mData.clear(); 122 String[] columnNames = c.getColumnNames(); 123 for (int i=0; i<columnNames.length; i++) { 124 String str = c.getString(i); 125 ColumnData cd = new ColumnData(columnNames[i], str); 126 mData.add(cd); 127 } 128 addDataViews(); 129 } else { 130 TextView error = new TextView(Details.this); 131 error.setText("Showing old data.\nURL couldn't be requeried:\n" 132 + intent.getData()); 133 error.setTextColor(0xffff0000); 134 error.setTextSize(11); 135 mLinearLayout.addView(error, 0, lazy()); 136 } 137 return true; 138 } 139 }; 140 141 MenuItem.OnMenuItemClickListener mPrintToStdout = new MenuItem.OnMenuItemClickListener() { 142 public boolean onMenuItemClick(MenuItem item) { 143 System.out.println("=== begin data ==="); 144 int count = mData.size(); 145 for (int i=0; i<count; i++) { 146 ColumnData cd = mData.get(i); 147 System.out.println(" " + cd.key + ": " + cd.value); 148 } 149 System.out.println("=== end data ==="); 150 return true; 151 } 152 }; 153 154 LinearLayout mLinearLayout; 155 ScrollView mScrollView; 156 ArrayList<ColumnData> mData; 157 } 158