• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.deskclock.widget.selector;
17 
18 import android.content.Context;
19 import android.support.annotation.NonNull;
20 import android.support.annotation.Nullable;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ArrayAdapter;
25 import android.widget.TextView;
26 
27 import com.android.deskclock.R;
28 import com.android.deskclock.data.DataModel;
29 import com.android.deskclock.data.Weekdays;
30 import com.android.deskclock.provider.Alarm;
31 import com.android.deskclock.widget.TextTime;
32 
33 import java.util.Calendar;
34 import java.util.List;
35 
36 public class AlarmSelectionAdapter extends ArrayAdapter<AlarmSelection> {
37 
AlarmSelectionAdapter(Context context, int id, List<AlarmSelection> alarms)38     public AlarmSelectionAdapter(Context context, int id, List<AlarmSelection> alarms) {
39         super(context, id, alarms);
40     }
41 
42     @Override
getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)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 }