summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/adapter/SCListAdapter.java
blob: 0174b4848ecc3fa2fc0a6dc074b1f15048db97f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package de.danoeh.antennapod.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.SimpleChapter;
import de.danoeh.antennapod.util.Converter;

public class SCListAdapter extends ArrayAdapter<SimpleChapter> {

	private static final String TAG = "SCListAdapter";

	public SCListAdapter(Context context, int textViewResourceId,
			List<SimpleChapter> objects) {
		super(context, textViewResourceId, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;

		SimpleChapter sc = getItem(position);

		// Inflate Layout
		if (convertView == null) {
			holder = new Holder();
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			convertView = inflater.inflate(R.layout.simplechapter_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
			holder.start = (TextView) convertView.findViewById(R.id.txtvStart);
			holder.link = (TextView) convertView.findViewById(R.id.txtvLink);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();

		}

		holder.title.setText(sc.getTitle());
		holder.start.setText(Converter.getDurationStringLong((int) sc
				.getStart()));
		if (sc.getLink() != null) {
			holder.link.setVisibility(View.VISIBLE);
			holder.link.setText(sc.getLink());
			Linkify.addLinks(holder.link, Linkify.WEB_URLS);
		} else {
			holder.link.setVisibility(View.GONE);
		}
		holder.link.setMovementMethod(null);
		holder.link.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// from
				// http://stackoverflow.com/questions/7236840/android-textview-linkify-intercepts-with-parent-view-gestures
				TextView widget = (TextView) v;
				Object text = widget.getText();
				if (text instanceof Spanned) {
					Spannable buffer = (Spannable) text;

					int action = event.getAction();

					if (action == MotionEvent.ACTION_UP
							|| action == MotionEvent.ACTION_DOWN) {
						int x = (int) event.getX();
						int y = (int) event.getY();

						x -= widget.getTotalPaddingLeft();
						y -= widget.getTotalPaddingTop();

						x += widget.getScrollX();
						y += widget.getScrollY();

						Layout layout = widget.getLayout();
						int line = layout.getLineForVertical(y);
						int off = layout.getOffsetForHorizontal(line, x);

						ClickableSpan[] link = buffer.getSpans(off, off,
								ClickableSpan.class);

						if (link.length != 0) {
							if (action == MotionEvent.ACTION_UP) {
								link[0].onClick(widget);
							} else if (action == MotionEvent.ACTION_DOWN) {
								Selection.setSelection(buffer,
										buffer.getSpanStart(link[0]),
										buffer.getSpanEnd(link[0]));
							}
							return true;
						}
					}

				}

				return false;

			}
		});
		SimpleChapter current = sc.getItem().getCurrentChapter();
		if (current != null) {
			if (current == sc) {
				holder.title.setTextColor(convertView.getResources().getColor(
						R.color.bright_blue));
			} else {
				holder.title.setTextColor(Color.parseColor("black"));
			}
		} else {
			Log.w(TAG, "Could not find out what the current chapter is.");
		}

		return convertView;
	}

	static class Holder {
		TextView title;
		TextView start;
		TextView link;
	}

	private LinkMovementMethod linkMovementMethod = new LinkMovementMethod() {

		@Override
		public boolean onTouchEvent(TextView widget, Spannable buffer,
				MotionEvent event) {
			Object text = widget.getText();
			if (text instanceof Spanned) {
				int action = event.getAction();

				if (action == MotionEvent.ACTION_UP
						|| action == MotionEvent.ACTION_DOWN) {
					int x = (int) event.getX();
					int y = (int) event.getY();

					x -= widget.getTotalPaddingLeft();
					y -= widget.getTotalPaddingTop();

					x += widget.getScrollX();
					y += widget.getScrollY();

					Layout layout = widget.getLayout();
					int line = layout.getLineForVertical(y);
					int off = layout.getOffsetForHorizontal(line, x);

					ClickableSpan[] link = buffer.getSpans(off, off,
							ClickableSpan.class);

					if (link.length != 0) {
						if (action == MotionEvent.ACTION_UP) {
							link[0].onClick(widget);
						} else if (action == MotionEvent.ACTION_DOWN) {
							Selection.setSelection(buffer,
									buffer.getSpanStart(link[0]),
									buffer.getSpanEnd(link[0]));
						}
						return true;
					}
				}

			}

			return false;

		}

	};
}