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 
17 package com.android.deskclock.alarms.dataadapter;
18 
19 import android.os.Bundle;
20 
21 import com.android.deskclock.ItemAdapter;
22 import com.android.deskclock.alarms.AlarmTimeClickHandler;
23 import com.android.deskclock.provider.Alarm;
24 import com.android.deskclock.provider.AlarmInstance;
25 
26 public class AlarmItemHolder extends ItemAdapter.ItemHolder<Alarm> {
27 
28     private static final java.lang.String EXPANDED_KEY = "expanded";
29     private final AlarmInstance mAlarmInstance;
30     private final AlarmTimeClickHandler mAlarmTimeClickHandler;
31     private boolean mExpanded;
32 
AlarmItemHolder(Alarm alarm, AlarmInstance alarmInstance, AlarmTimeClickHandler alarmTimeClickHandler)33     public AlarmItemHolder(Alarm alarm, AlarmInstance alarmInstance,
34             AlarmTimeClickHandler alarmTimeClickHandler) {
35         super(alarm, alarm.id);
36         mAlarmInstance = alarmInstance;
37         mAlarmTimeClickHandler = alarmTimeClickHandler;
38     }
39 
40     @Override
getItemViewType()41     public int getItemViewType() {
42         return isExpanded() ?
43                 ExpandedAlarmViewHolder.VIEW_TYPE : CollapsedAlarmViewHolder.VIEW_TYPE;
44     }
45 
getAlarmTimeClickHandler()46     public AlarmTimeClickHandler getAlarmTimeClickHandler() {
47         return mAlarmTimeClickHandler;
48     }
49 
getAlarmInstance()50     public AlarmInstance getAlarmInstance() {
51         return mAlarmInstance;
52     }
53 
expand()54     public void expand() {
55         if (!isExpanded()) {
56             mExpanded = true;
57             notifyItemChanged();
58         }
59     }
60 
collapse()61     public void collapse() {
62         if (isExpanded()) {
63             mExpanded = false;
64             notifyItemChanged();
65         }
66     }
67 
isExpanded()68     public boolean isExpanded() {
69         return mExpanded;
70     }
71 
72     @Override
onSaveInstanceState(Bundle bundle)73     public void onSaveInstanceState(Bundle bundle) {
74         super.onSaveInstanceState(bundle);
75         bundle.putBoolean(EXPANDED_KEY, mExpanded);
76     }
77 
78     @Override
onRestoreInstanceState(Bundle bundle)79     public void onRestoreInstanceState(Bundle bundle) {
80         super.onRestoreInstanceState(bundle);
81         mExpanded = bundle.getBoolean(EXPANDED_KEY);
82     }
83 }