2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.content.Context;
20 import android.database.DataSetObserver;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.View.OnClickListener;
26 import android.webkit.WebView;
27 import android.widget.Button;
28 import android.widget.EditText;
29 import android.widget.LinearLayout;
30 import android.widget.ListView;
31 import android.widget.TextView;
32 import android.widget.TwoLineListItem;
34 import java.util.Vector;
36 /* package */ class ErrorConsoleView extends LinearLayout {
39 * Define some constants to describe the visibility of the error console.
41 public static final int SHOW_MINIMIZED = 0;
42 public static final int SHOW_MAXIMIZED = 1;
43 public static final int SHOW_NONE = 2;
45 private TextView mConsoleHeader;
46 private ErrorConsoleListView mErrorList;
47 private LinearLayout mEvalJsViewGroup;
48 private EditText mEvalEditText;
49 private Button mEvalButton;
50 private WebView mWebView;
51 private int mCurrentShowState = SHOW_NONE;
53 private boolean mSetupComplete = false;
55 // Before we've been asked to display the console, cache any messages that should
56 // be added to the console. Then when we do display the console, add them to the view
58 private Vector<ErrorConsoleMessage> mErrorMessageCache;
60 public ErrorConsoleView(Context context) {
64 public ErrorConsoleView(Context context, AttributeSet attributes) {
65 super(context, attributes);
68 private void commonSetupIfNeeded() {
73 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
74 Context.LAYOUT_INFLATER_SERVICE);
75 inflater.inflate(R.layout.error_console, this);
77 // Get references to each ui element.
78 mConsoleHeader = (TextView) findViewById(R.id.error_console_header_id);
79 mErrorList = (ErrorConsoleListView) findViewById(R.id.error_console_list_id);
80 mEvalJsViewGroup = (LinearLayout) findViewById(R.id.error_console_eval_view_group_id);
81 mEvalEditText = (EditText) findViewById(R.id.error_console_eval_text_id);
82 mEvalButton = (Button) findViewById(R.id.error_console_eval_button_id);
84 mEvalButton.setOnClickListener(new OnClickListener() {
85 public void onClick(View v) {
86 // Send the javascript to be evaluated to webkit as a javascript: url
87 // TODO: Can we expose access to webkit's JS interpreter here and evaluate it that
88 // way? Note that this is called on the UI thread so we will need to post a message
89 // to the WebCore thread to implement this.
90 if (mWebView != null) {
91 mWebView.loadUrl("javascript:" + mEvalEditText.getText());
94 mEvalEditText.setText("");
98 // Make clicking on the console title bar min/maximse it.
99 mConsoleHeader.setOnClickListener(new OnClickListener() {
100 public void onClick(View v) {
101 if (mCurrentShowState == SHOW_MINIMIZED) {
102 showConsole(SHOW_MAXIMIZED);
104 showConsole(SHOW_MINIMIZED);
109 // Add any cached messages to the list now that we've assembled the view.
110 if (mErrorMessageCache != null) {
111 for (ErrorConsoleMessage msg : mErrorMessageCache) {
112 mErrorList.addErrorMessage(msg.getMessage(), msg.getSourceID(), msg.getLineNumber());
114 mErrorMessageCache.clear();
117 mSetupComplete = true;
121 * Adds a message to the set of messages the console uses.
123 public void addErrorMessage(String msg, String sourceId, int lineNumber) {
124 if (mSetupComplete) {
125 mErrorList.addErrorMessage(msg, sourceId, lineNumber);
127 if (mErrorMessageCache == null) {
128 mErrorMessageCache = new Vector<ErrorConsoleMessage>();
130 mErrorMessageCache.add(new ErrorConsoleMessage(msg, sourceId, lineNumber));
135 * Removes all error messages from the console.
137 public void clearErrorMessages() {
138 if (mSetupComplete) {
139 mErrorList.clearErrorMessages();
140 } else if (mErrorMessageCache != null) {
141 mErrorMessageCache.clear();
146 * Returns the current number of errors displayed in the console.
148 public int numberOfErrors() {
149 if (mSetupComplete) {
150 return mErrorList.getCount();
152 return (mErrorMessageCache == null) ? 0 : mErrorMessageCache.size();
157 * Sets the webview that this console is associated with. Currently this is used so
158 * we can call into webkit to evaluate JS expressions in the console.
160 public void setWebView(WebView webview) {
165 * Sets the visibility state of the console.
167 public void showConsole(int show_state) {
168 commonSetupIfNeeded();
169 switch (show_state) {
171 mConsoleHeader.setVisibility(View.VISIBLE);
172 mConsoleHeader.setText(R.string.error_console_header_text_minimized);
173 mErrorList.setVisibility(View.GONE);
174 mEvalJsViewGroup.setVisibility(View.GONE);
178 mConsoleHeader.setVisibility(View.VISIBLE);
179 mConsoleHeader.setText(R.string.error_console_header_text_maximized);
180 mErrorList.setVisibility(View.VISIBLE);
181 mEvalJsViewGroup.setVisibility(View.VISIBLE);
185 mConsoleHeader.setVisibility(View.GONE);
186 mErrorList.setVisibility(View.GONE);
187 mEvalJsViewGroup.setVisibility(View.GONE);
190 mCurrentShowState = show_state;
194 * Returns the current visibility state of the console.
196 public int getShowState() {
197 if (mSetupComplete) {
198 return mCurrentShowState;
205 * This class extends ListView to implement the View that will actually display the set of
206 * errors encountered on the current page.
208 private static class ErrorConsoleListView extends ListView {
209 // An adapter for this View that contains a list of error messages.
210 private ErrorConsoleMessageList mConsoleMessages;
212 public ErrorConsoleListView(Context context, AttributeSet attributes) {
213 super(context, attributes);
214 mConsoleMessages = new ErrorConsoleMessageList(context);
215 setAdapter(mConsoleMessages);
218 public void addErrorMessage(String msg, String sourceId, int lineNumber) {
219 mConsoleMessages.add(msg, sourceId, lineNumber);
220 setSelection(mConsoleMessages.getCount());
223 public void clearErrorMessages() {
224 mConsoleMessages.clear();
228 * This class is an adapter for ErrorConsoleListView that contains the error console
231 private class ErrorConsoleMessageList extends android.widget.BaseAdapter
232 implements android.widget.ListAdapter {
234 private Vector<ErrorConsoleMessage> mMessages;
235 private LayoutInflater mInflater;
237 public ErrorConsoleMessageList(Context context) {
238 mMessages = new Vector<ErrorConsoleMessage>();
239 mInflater = (LayoutInflater)context.getSystemService(
240 Context.LAYOUT_INFLATER_SERVICE);
244 * Add a new message to the list and update the View.
246 public void add(String msg, String sourceID, int lineNumber) {
247 mMessages.add(new ErrorConsoleMessage(msg, sourceID, lineNumber));
248 notifyDataSetChanged();
252 * Remove all messages from the list and update the view.
254 public void clear() {
256 notifyDataSetChanged();
260 public boolean areAllItemsEnabled() {
265 public boolean isEnabled(int position) {
269 public long getItemId(int position) {
273 public Object getItem(int position) {
274 return mMessages.get(position);
277 public int getCount() {
278 return mMessages.size();
282 public boolean hasStableIds() {
287 * Constructs a TwoLineListItem for the error at position.
289 public View getView(int position, View convertView, ViewGroup parent) {
291 ErrorConsoleMessage error = mMessages.get(position);
297 if (convertView == null) {
298 view = mInflater.inflate(android.R.layout.two_line_list_item, parent, false);
303 TextView headline = (TextView) view.findViewById(android.R.id.text1);
304 TextView subText = (TextView) view.findViewById(android.R.id.text2);
305 headline.setText(error.getSourceID() + ":" + error.getLineNumber());
306 subText.setText(error.getMessage());
314 * This class holds the data for a single error message in the console.
316 private static class ErrorConsoleMessage {
317 private String mMessage;
318 private String mSourceID;
319 private int mLineNumber;
321 public ErrorConsoleMessage(String msg, String sourceID, int lineNumber) {
323 mSourceID = sourceID;
324 mLineNumber = lineNumber;
327 public String getMessage() {
331 public String getSourceID() {
335 public int getLineNumber() {