summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/adapter/ChaptersListAdapter.java
blob: 8bde1097bfb2da1057ed248358c6b773c0e5fee9 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package de.danoeh.antennapod.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.Spanned;
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.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.ChapterUtils;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;

public class ChaptersListAdapter extends ArrayAdapter<Chapter> {

    private static final String TAG = "ChapterListAdapter";

    private Playable media;

    private int defaultTextColor;
    private final Callback callback;

    public ChaptersListAdapter(Context context, int textViewResourceId, Callback callback) {
        super(context, textViewResourceId);
        this.callback = callback;
    }

    public void setMedia(Playable media) {
        this.media = media;
    }

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

        Chapter 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, parent, false);
            holder.view = convertView;
            holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
            defaultTextColor = holder.title.getTextColors().getDefaultColor();
            holder.start = (TextView) convertView.findViewById(R.id.txtvStart);
            holder.link = (TextView) convertView.findViewById(R.id.txtvLink);
            holder.butPlayChapter = (ImageButton) convertView.findViewById(R.id.butPlayChapter);
            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((v, 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;

        });
        holder.butPlayChapter.setOnClickListener(v -> {
            if (callback != null) {
                callback.onPlayChapterButtonClicked(position);
            }
        });
        Chapter current = ChapterUtils.getCurrentChapter(media);
        if (current != null) {
            if (current == sc) {
                int playingBackGroundColor;
                if(UserPreferences.getTheme() == R.style.Theme_AntennaPod_Dark) {
                    playingBackGroundColor = ContextCompat.getColor(getContext(), R.color.highlight_dark);
                } else {
                    playingBackGroundColor = ContextCompat.getColor(getContext(), R.color.highlight_light);
                }
                holder.view.setBackgroundColor(playingBackGroundColor);
            } else {
                holder.view.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
                holder.title.setTextColor(defaultTextColor);
                holder.start.setTextColor(defaultTextColor);
            }
        } else {
            Log.w(TAG, "Could not find out what the current chapter is.");
        }

        return convertView;
    }

    static class Holder {
        View view;
        TextView title;
        TextView start;
        TextView link;
        ImageButton butPlayChapter;
    }

    @Override
    public int getCount() {
        if(media == null || media.getChapters() == null) {
            return 0;
        }
        // ignore invalid chapters
        int counter = 0;
        for (Chapter chapter : media.getChapters()) {
            if (!ignoreChapter(chapter)) {
                counter++;
            }
        }
        return counter;
    }

    private boolean ignoreChapter(Chapter c) {
        return media.getDuration() > 0 && media.getDuration() < c.getStart();
    }

    @Override
    public Chapter getItem(int position) {
        int i = 0;
        for (Chapter chapter : media.getChapters()) {
            if (!ignoreChapter(chapter)) {
                if (i == position) {
                    return chapter;
                } else {
                    i++;
                }
            }
        }
        return super.getItem(position);
    }

    public interface Callback {
        void onPlayChapterButtonClicked(int position);
    }

}