View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient.activity;
20  
21  import java.util.List;
22  
23  import sk.baka.ambient.AmbientApplication;
24  import sk.baka.ambient.ErrorHandler;
25  import sk.baka.ambient.R;
26  import sk.baka.ambient.ErrorHandler.ErrorInfo;
27  import android.app.ListActivity;
28  import android.content.Context;
29  import android.os.Bundle;
30  import android.view.LayoutInflater;
31  import android.view.View;
32  import android.view.ViewGroup;
33  import android.widget.BaseAdapter;
34  import android.widget.ImageView;
35  import android.widget.ListView;
36  import android.widget.TextView;
37  
38  /***
39   * Shows errors and their stack traces.
40   * 
41   * @author Martin Vysny
42   */
43  public final class ErrorActivity extends ListActivity {
44  	@Override
45  	protected void onCreate(Bundle savedInstanceState) {
46  		super.onCreate(savedInstanceState);
47  		setContentView(R.layout.error_activity);
48  		findViewById(R.id.errorClear).setOnClickListener(
49  				new View.OnClickListener() {
50  					public void onClick(View v) {
51  						AmbientApplication.getInstance().getErrorHandler()
52  								.clear();
53  						update();
54  					}
55  				});
56  		update();
57  	}
58  
59  	private void update() {
60  		final List<ErrorHandler.ErrorInfo> errors = AmbientApplication
61  				.getInstance().getErrorHandler().getErrors();
62  		setListAdapter(new ErrorListAdapter(errors));
63  	}
64  
65  	@Override
66  	protected void onListItemClick(ListView l, View v, int position, long id) {
67  		((ErrorListAdapter) getListAdapter()).toggle(position);
68  	}
69  
70  	/***
71  	 * Shows a list of errors.
72  	 */
73  	private class ErrorListAdapter extends BaseAdapter {
74  		private final List<ErrorInfo> errors;
75  		private final LayoutInflater inflater;
76  
77  		public ErrorListAdapter(final List<ErrorHandler.ErrorInfo> errors) {
78  			this.errors = errors;
79  			this.inflater = (LayoutInflater) ErrorActivity.this
80  					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
81  		}
82  
83  		public int getCount() {
84  			return errors.size();
85  		}
86  
87  		public Object getItem(int position) {
88  			return position;
89  		}
90  
91  		public long getItemId(int position) {
92  			return position;
93  		}
94  
95  		public View getView(int position, View convertView, ViewGroup parent) {
96  			final View view;
97  			if (convertView == null) {
98  				view = inflater.inflate(R.layout.error_activity_item,
99  						ErrorActivity.this.getListView(), false);
100 			} else {
101 				view = convertView;
102 			}
103 			final boolean isExpanded = position == expanded;
104 			final ErrorInfo info = errors.get(position);
105 			((TextView) view.findViewById(R.id.errorItemMessage))
106 					.setText(info.message);
107 			((ImageView) view.findViewById(R.id.errorItemExpanded))
108 					.setImageResource(isExpanded ? R.drawable.expander_ic_maximized
109 							: R.drawable.expander_ic_minimized);
110 			final TextView stacktrace = (TextView) view
111 					.findViewById(R.id.errorItemStacktrace);
112 			stacktrace.setText(isExpanded ? info.stacktrace : "");
113 			stacktrace.findViewById(R.id.errorItemStacktrace).setVisibility(
114 					isExpanded ? View.VISIBLE : View.GONE);
115 			return view;
116 		}
117 
118 		private int expanded = -1;
119 
120 		public void toggle(int position) {
121 			if (expanded == position) {
122 				expanded = -1;
123 			} else {
124 				expanded = position;
125 			}
126 			notifyDataSetChanged();
127 		}
128 	}
129 }