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*/
17package com.android.development;
18
19import java.util.ArrayList;
20
21import android.content.Intent;
22import android.app.Activity;
23import android.database.Cursor;
24import android.graphics.Typeface;
25import android.widget.LinearLayout;
26import android.widget.ScrollView;
27import android.os.Bundle;
28import android.view.Menu;
29import android.view.MenuItem;
30import android.view.ViewGroup;
31import android.widget.TextView;
32
33public class Details extends Activity
34{
35    public void onCreate(Bundle icicle)
36    {
37        super.onCreate(icicle);
38
39        Intent intent = getIntent();
40
41        String title = intent.getStringExtra("title");
42        if (title == null) {
43            title = "Details";
44        }
45        setTitle(title);
46
47        mScrollView = new ScrollView(this);
48        setContentView(mScrollView);
49        mScrollView.setFocusable(true);
50
51        mData = (ArrayList<ColumnData>)getIntent().getExtra("data");
52        addDataViews();
53    }
54
55    public void onResume()
56    {
57        super.onResume();
58    }
59
60    public boolean onCreateOptionsMenu(Menu menu)
61    {
62        super.onCreateOptionsMenu(menu);
63        menu.add(0, 0, 0, "Requery").setOnMenuItemClickListener(mRequery);
64        menu.add(0, 0, 0, "Print to stdout").setOnMenuItemClickListener(mPrintToStdout);
65        return true;
66    }
67
68    void addDataViews()
69    {
70        int oldScroll = 0;
71
72        if (mLinearLayout != null) {
73            mScrollView.removeView(mLinearLayout);
74        }
75        mLinearLayout = new LinearLayout(this);
76        mScrollView.addView(mLinearLayout, new ViewGroup.LayoutParams(
77                                        ViewGroup.LayoutParams.MATCH_PARENT,
78                                        ViewGroup.LayoutParams.MATCH_PARENT));
79        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
80
81        // Here in onStart, we're given data.  We use that because some
82        // data that we show is transient and can't be retrieved from a url.
83        // We'll try to use that in requery
84        int count = mData.size();
85        for (int i=0; i<count; i++) {
86            ColumnData cd = mData.get(i);
87            TextView label = makeView(cd.key, true, 12);
88            TextView contents = makeView(cd.value, false, 12);
89            contents.setPadding(3, 0, 0, i==count-1?0:3);
90            mLinearLayout.addView(label, lazy());
91            mLinearLayout.addView(contents, lazy());
92        }
93    }
94
95    TextView makeView(String str, boolean bold, int fontSize)
96    {
97        if (str == null) {
98            str = "(null)";
99        }
100        TextView v = new TextView(this);
101        v.setText(str);
102        v.setTextSize(fontSize);
103        if (bold) {
104            v.setTypeface(Typeface.DEFAULT_BOLD);
105        }
106        return v;
107    }
108
109    LinearLayout.LayoutParams lazy()
110    {
111        return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
112                                 ViewGroup.LayoutParams.WRAP_CONTENT, 0);
113    }
114
115    MenuItem.OnMenuItemClickListener mRequery = new MenuItem.OnMenuItemClickListener() {
116        public boolean onMenuItemClick(MenuItem item) {
117            Intent intent = getIntent();
118            Cursor c = getContentResolver().query(intent.getData(), null, null, null, null);
119            if (c != null && c.moveToNext()) {
120                mData.clear();
121                String[] columnNames = c.getColumnNames();
122                for (int i=0; i<columnNames.length; i++) {
123                    String str = c.getString(i);
124                    ColumnData cd = new ColumnData(columnNames[i], str);
125                    mData.add(cd);
126                }
127                addDataViews();
128            } else {
129                TextView error = new TextView(Details.this);
130                error.setText("Showing old data.\nURL couldn't be requeried:\n"
131                        + intent.getData());
132                error.setTextColor(0xffff0000);
133                error.setTextSize(11);
134                mLinearLayout.addView(error, 0, lazy());
135            }
136            return true;
137        }
138    };
139
140    MenuItem.OnMenuItemClickListener mPrintToStdout = new MenuItem.OnMenuItemClickListener() {
141        public boolean onMenuItemClick(MenuItem item) {
142            System.out.println("=== begin data ===");
143            int count = mData.size();
144            for (int i=0; i<count; i++) {
145                ColumnData cd = mData.get(i);
146                System.out.println("  " + cd.key + ": " + cd.value);
147            }
148            System.out.println("=== end data ===");
149            return true;
150        }
151    };
152
153    LinearLayout mLinearLayout;
154    ScrollView mScrollView;
155    ArrayList<ColumnData> mData;
156}
157