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