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 android.widget.focus;
18 
19 import com.android.frameworks.coretests.R;
20 import com.google.android.collect.Lists;
21 
22 import android.app.ListActivity;
23 import android.os.Bundle;
24 import android.widget.ArrayAdapter;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import android.content.Context;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.LayoutInflater;
31 import android.webkit.WebView;
32 
33 import java.util.List;
34 
35 public class ListWithMailMessages extends ListActivity {
36 
37 
38     @Override
onCreate(Bundle icicle)39     protected void onCreate(Bundle icicle) {
40         super.onCreate(icicle);
41         setContentView(R.layout.list_with_button_above);
42 
43         List<MailMessage> messages = Lists.newArrayList();
44         messages.add(new MailMessage("hello!", "<p>this is a test "
45                 + "message, with a bunch of text and stuff.</p>", true));
46 
47 //        String android = "android";
48         String android = "<a href=\"www.android.com\">android</a>";
49 
50         String sentance = "all work and no play makes "
51         + android + " a dull... robot!";
52         StringBuffer longBody = new StringBuffer().append("<ol>\n");
53         for (int i = 0; i < 12; i++) {
54             longBody.append("<li>").append(sentance).append("</li>");
55         }
56         longBody.append("</ol>");
57 
58         messages.add(new MailMessage("hello2!", longBody.toString(), true));
59         messages.add(new MailMessage("phone number?", "<p>hey man, what's ur "
60                 + "contact info? i need to mail you this photo of my two"
61                 + " cats, they've gotten soooo fat!</p>", true));
62 
63         setListAdapter(new MyAdapter(this, R.layout.mail_message, messages));
64         getListView().setItemsCanFocus(true);
65     }
66 
67 
68     /**
69      * POJO mail message.
70      */
71     static class MailMessage {
72         private String mSubject;
73         private String mBody;
74         private boolean mFocusable;
75 
76 
MailMessage(String subject, String body)77         public MailMessage(String subject, String body) {
78             this(subject, body, false);
79         }
80 
81 
MailMessage(String subject, String body, boolean focusable)82         public MailMessage(String subject, String body, boolean focusable) {
83             mSubject = subject;
84             mBody = body;
85             mFocusable = focusable;
86         }
87 
getSubject()88         public String getSubject() {
89             return mSubject;
90         }
91 
setSubject(String subject)92         public void setSubject(String subject) {
93             this.mSubject = subject;
94         }
95 
getBody()96         public String getBody() {
97             return mBody;
98         }
99 
setBody(String body)100         public void setBody(String body) {
101             this.mBody = body;
102         }
103 
104 
isFocusable()105         public boolean isFocusable() {
106             return mFocusable;
107         }
108 
setFocusable(boolean focusable)109         public void setFocusable(boolean focusable) {
110             mFocusable = focusable;
111         }
112     }
113 
114 
115     public static class MyAdapter extends ArrayAdapter<MailMessage> {
116 
MyAdapter(Context context, int resource, List<MailMessage> objects)117         public MyAdapter(Context context, int resource,
118                 List<MailMessage> objects) {
119             super(context, resource, objects);
120         }
121 
122         final String mimeType = "text/html";
123 
124 
125         @Override
getView(int position, View convertView, ViewGroup parent)126         public View getView(int position, View convertView, ViewGroup parent) {
127             MailMessage message = getItem(position);
128 
129             LayoutInflater inflater = (LayoutInflater)
130                     getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
131 
132             LinearLayout messageUi = (LinearLayout) inflater
133                     .inflate(R.layout.mail_message, null);
134 
135             TextView subject = (TextView) messageUi.findViewById(R.id.subject);
136             subject.setText(message.getSubject());
137 
138             WebView body = (WebView) messageUi.findViewById(R.id.body);
139             body.loadData(message.getBody(), mimeType, null);
140 //            body.setText(message.getBody());
141             body.setFocusable(message.isFocusable());
142 
143             return messageUi;
144         }
145     }
146 }
147