1/*
2 * Copyright (C) 2016 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
17package com.android.car.radio;
18
19import android.support.v7.widget.RecyclerView;
20import android.util.Log;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import com.android.car.radio.service.RadioStation;
25import com.android.car.view.PagedListView;
26
27import java.util.List;
28
29/**
30 * Adapter that will display a list of radio stations that represent the user's presets.
31 */
32public class PresetsAdapter extends RecyclerView.Adapter<PresetsViewHolder>
33        implements PresetsViewHolder.OnPresetClickListener, PagedListView.ItemCap {
34    private static final String TAG = "Em.PresetsAdapter";
35
36    // Only one type of view in this adapter.
37    private static final int PRESETS_VIEW_TYPE = 0;
38
39    private RadioStation mActiveRadioStation;
40
41    private List<RadioStation> mPresets;
42    private OnPresetItemClickListener mPresetClickListener;
43
44    /**
45     * Interface for a listener that will be notified when an item in the presets list has been
46     * clicked.
47     */
48    public interface OnPresetItemClickListener {
49        /**
50         * Method called when an item in the preset list has been clicked.
51         *
52         * @param radioStation The {@link RadioStation} corresponding to the clicked preset.
53         */
54        void onPresetItemClicked(RadioStation radioStation);
55    }
56
57    /**
58     * Set a listener to be notified whenever a preset card is pressed.
59     */
60    public void setOnPresetItemClickListener(OnPresetItemClickListener listener) {
61        mPresetClickListener = listener;
62    }
63
64    /**
65     * Sets the given list as the list of presets to display.
66     */
67    public void setPresets(List<RadioStation> presets) {
68        mPresets = presets;
69        notifyDataSetChanged();
70    }
71
72    /**
73     * Indicates which radio station is the active one inside the list of presets that are set on
74     * this adapter. This will cause that station to be highlighted in the list. If the station
75     * passed to this method does not match any of the presets, then none will be highlighted.
76     */
77    public void setActiveRadioStation(RadioStation station) {
78        mActiveRadioStation = station;
79        notifyDataSetChanged();
80    }
81
82    @Override
83    public PresetsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
84        View view = LayoutInflater.from(parent.getContext())
85                .inflate(R.layout.radio_preset_stream_card, parent, false);
86
87        return new PresetsViewHolder(view, this /* listener */);
88    }
89
90    @Override
91    public void onBindViewHolder(PresetsViewHolder holder, int position) {
92        RadioStation station = mPresets.get(position);
93        boolean isActiveStation = station.equals(mActiveRadioStation);
94
95        holder.bindPreset(station, isActiveStation, getItemCount());
96    }
97
98    @Override
99    public void onPresetClicked(int position) {
100        if (Log.isLoggable(TAG, Log.DEBUG)) {
101            Log.d(TAG, String.format("onPresetClicked(); item count: %d; position: %d",
102                    getItemCount(), position));
103        }
104
105        if (mPresetClickListener != null && getItemCount() > position) {
106            mPresetClickListener.onPresetItemClicked(mPresets.get(position));
107        }
108    }
109
110    @Override
111    public int getItemViewType(int position) {
112        return PRESETS_VIEW_TYPE;
113    }
114
115    @Override
116    public int getItemCount() {
117        return mPresets == null ? 0 : mPresets.size();
118    }
119
120    @Override
121    public void setMaxItems(int max) {
122        // No-op. A PagedListView needs the ItemCap interface to be implemented. However, the
123        // list of presets should not be limited.
124    }
125}
126