1/*
2 * Copyright (C) 2015 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 */
16package com.android.deskclock.widget.selector;
17
18import android.content.Context;
19import android.support.annotation.NonNull;
20import android.support.annotation.Nullable;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.ArrayAdapter;
25import android.widget.TextView;
26
27import com.android.deskclock.R;
28import com.android.deskclock.data.DataModel;
29import com.android.deskclock.data.Weekdays;
30import com.android.deskclock.provider.Alarm;
31import com.android.deskclock.widget.TextTime;
32
33import java.util.Calendar;
34import java.util.List;
35
36public class AlarmSelectionAdapter extends ArrayAdapter<AlarmSelection> {
37
38    public AlarmSelectionAdapter(Context context, int id, List<AlarmSelection> alarms) {
39        super(context, id, alarms);
40    }
41
42    @Override
43    public @NonNull View getView(int position, @Nullable View convertView,
44            @NonNull ViewGroup parent) {
45        final Context context = getContext();
46        View row = convertView;
47        if (row == null) {
48            final LayoutInflater inflater = LayoutInflater.from(context);
49            row = inflater.inflate(R.layout.alarm_row, parent, false);
50        }
51
52        final AlarmSelection selection = getItem(position);
53        final Alarm alarm = selection.getAlarm();
54
55        final TextTime alarmTime = (TextTime) row.findViewById(R.id.digital_clock);
56        alarmTime.setTime(alarm.hour, alarm.minutes);
57
58        final TextView alarmLabel = (TextView) row.findViewById(R.id.label);
59        alarmLabel.setText(alarm.label);
60
61        // find days when alarm is firing
62        final String daysOfWeek;
63        if (!alarm.daysOfWeek.isRepeating()) {
64            daysOfWeek = Alarm.isTomorrow(alarm, Calendar.getInstance()) ?
65                    context.getResources().getString(R.string.alarm_tomorrow) :
66                    context.getResources().getString(R.string.alarm_today);
67        } else {
68            final Weekdays.Order weekdayOrder = DataModel.getDataModel().getWeekdayOrder();
69            daysOfWeek = alarm.daysOfWeek.toString(context, weekdayOrder);
70        }
71
72        final TextView daysOfWeekView = (TextView) row.findViewById(R.id.daysOfWeek);
73        daysOfWeekView.setText(daysOfWeek);
74
75        return row;
76    }
77}