1 /*
2 * Copyright 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 
17 package com.example.android.directboot.alarms;
18 
19 import com.example.android.directboot.R;
20 
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.drawable.Drawable;
24 import android.support.v7.util.SortedList;
25 import android.support.v7.widget.RecyclerView;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31 import android.widget.Toast;
32 
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Calendar;
36 import java.util.Locale;
37 import java.util.Set;
38 
39 /**
40  * Adapter responsible for interactions between the {@link RecyclerView} and the
41  * scheduled alarms.
42  */
43 public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.AlarmViewHolder> {
44 
45     private SortedList<Alarm> mAlarmList;
46     private AlarmStorage mAlarmStorage;
47     private AlarmUtil mAlarmUtil;
48     private DateFormat mDateFormat;
49     private DateFormat mTimeFormat;
50     private Context mContext;
51 
AlarmAdapter(Context context, Set<Alarm> alarms)52     public AlarmAdapter(Context context, Set<Alarm> alarms) {
53         mAlarmList = new SortedList<>(Alarm.class, new SortedListCallback());
54         mAlarmList.addAll(alarms);
55         mAlarmStorage = new AlarmStorage(context);
56         mContext = context;
57         mAlarmUtil = new AlarmUtil(context);
58         mDateFormat = new SimpleDateFormat("MMM dd", Locale.getDefault());
59         mTimeFormat = new SimpleDateFormat("kk:mm", Locale.getDefault());
60     }
61 
62     @Override
onCreateViewHolder(ViewGroup parent, int viewType)63     public AlarmViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
64         View v = LayoutInflater.from(parent.getContext())
65                 .inflate(R.layout.alarm_row, parent, false);
66         return new AlarmViewHolder(v);
67     }
68 
69     @Override
onBindViewHolder(AlarmViewHolder holder, final int position)70     public void onBindViewHolder(AlarmViewHolder holder, final int position) {
71         Alarm alarm = mAlarmList.get(position);
72         Calendar alarmTime = Calendar.getInstance();
73         alarmTime.set(Calendar.MONTH, alarm.month);
74         alarmTime.set(Calendar.DATE, alarm.date);
75         alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
76         alarmTime.set(Calendar.MINUTE, alarm.minute);
77         holder.mAlarmTimeTextView
78                 .setText(mTimeFormat.format(alarmTime.getTime()));
79         holder.mAlarmDateTextView
80                 .setText(mDateFormat.format(alarmTime.getTime()));
81         holder.mDeleteImageView.setOnClickListener(new View.OnClickListener() {
82             @Override
83             public void onClick(View view) {
84                 Alarm toBeDeleted = mAlarmList.get(position);
85                 mAlarmList.removeItemAt(position);
86                 mAlarmStorage.deleteAlarm(toBeDeleted);
87                 mAlarmUtil.cancelAlarm(toBeDeleted);
88                 notifyDataSetChanged();
89                 Toast.makeText(mContext, mContext.getString(R.string.alarm_deleted,
90                         toBeDeleted.hour, toBeDeleted.minute), Toast.LENGTH_SHORT).show();
91             }
92         });
93     }
94 
95     @Override
getItemCount()96     public int getItemCount() {
97         return mAlarmList.size();
98     }
99 
addAlarm(Alarm alarm)100     public void addAlarm(Alarm alarm) {
101         mAlarmList.add(alarm);
102         notifyDataSetChanged();
103     }
104 
deleteAlarm(Alarm alarm)105     public void deleteAlarm(Alarm alarm) {
106         mAlarmList.remove(alarm);
107         notifyDataSetChanged();
108     }
109 
110     public static class AlarmViewHolder extends RecyclerView.ViewHolder {
111 
112         private TextView mAlarmTimeTextView;
113         private TextView mAlarmDateTextView;
114         private ImageView mDeleteImageView;
115 
AlarmViewHolder(View itemView)116         public AlarmViewHolder(View itemView) {
117             super(itemView);
118             mAlarmTimeTextView = (TextView) itemView.findViewById(R.id.text_alarm_time);
119             mAlarmDateTextView = (TextView) itemView.findViewById(R.id.text_alarm_date);
120             mDeleteImageView = (ImageView) itemView.findViewById(R.id.image_delete_alarm);
121         }
122     }
123 
124 
125     private static class SortedListCallback extends SortedList.Callback<Alarm> {
126 
127         @Override
compare(Alarm o1, Alarm o2)128         public int compare(Alarm o1, Alarm o2) {
129             return o1.compareTo(o2);
130         }
131 
132         @Override
onInserted(int position, int count)133         public void onInserted(int position, int count) {
134             //No op
135         }
136 
137         @Override
onRemoved(int position, int count)138         public void onRemoved(int position, int count) {
139             //No op
140         }
141 
142         @Override
onMoved(int fromPosition, int toPosition)143         public void onMoved(int fromPosition, int toPosition) {
144             //No op
145         }
146 
147         @Override
onChanged(int position, int count)148         public void onChanged(int position, int count) {
149             //No op
150         }
151 
152         @Override
areContentsTheSame(Alarm oldItem, Alarm newItem)153         public boolean areContentsTheSame(Alarm oldItem, Alarm newItem) {
154             return oldItem.equals(newItem);
155         }
156 
157         @Override
areItemsTheSame(Alarm item1, Alarm item2)158         public boolean areItemsTheSame(Alarm item1, Alarm item2) {
159             return item1.equals(item2);
160         }
161     }
162 
163     /**
164      * ItemDecoration that draws an divider between items in a RecyclerView.
165      */
166     public static class DividerItemDecoration extends RecyclerView.ItemDecoration {
167 
168         private Drawable mDivider;
169 
DividerItemDecoration(Context context)170         public DividerItemDecoration(Context context) {
171             mDivider = context.getDrawable(R.drawable.divider);
172         }
173 
174         @Override
onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)175         public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
176             int left = parent.getPaddingLeft();
177             int right = parent.getWidth() - parent.getPaddingRight();
178             for (int i = 0; i < parent.getChildCount(); i++) {
179                 View child = parent.getChildAt(i);
180                 RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
181                         .getLayoutParams();
182                 int top = child.getBottom() + params.bottomMargin;
183                 int bottom = top + mDivider.getIntrinsicHeight();
184                 mDivider.setBounds(left, top, right, bottom);
185                 mDivider.draw(c);
186             }
187         }
188     }
189 }
190