Home | History | Annotate | Download | only in dataadapter
      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.content.Context;
     20 import android.text.TextUtils;
     21 import android.view.View;
     22 import android.widget.TextView;
     23 
     24 import com.android.deskclock.R;
     25 import com.android.deskclock.Utils;
     26 import com.android.deskclock.alarms.AlarmTimeClickHandler;
     27 import com.android.deskclock.provider.Alarm;
     28 import com.android.deskclock.provider.AlarmInstance;
     29 
     30 import java.util.Calendar;
     31 
     32 /**
     33  * A ViewHolder containing views for an alarm item in collapsed stated.
     34  */
     35 public final class CollapsedAlarmViewHolder extends AlarmTimeViewHolder {
     36 
     37     public final TextView alarmLabel;
     38     public final TextView daysOfWeek;
     39     public final TextView upcomingInstanceLabel;
     40     public final View hairLine;
     41 
     42     public CollapsedAlarmViewHolder(View itemView,
     43             final AlarmTimeClickHandler alarmTimeClickHandler,
     44             final AlarmTimeAdapter alarmTimeAdapter) {
     45         super(itemView, alarmTimeClickHandler);
     46         alarmLabel = (TextView) itemView.findViewById(R.id.label);
     47         daysOfWeek = (TextView) itemView.findViewById(R.id.days_of_week);
     48         upcomingInstanceLabel = (TextView) itemView.findViewById(R.id.upcoming_instance_label);
     49         hairLine = itemView.findViewById(R.id.hairline);
     50 
     51         // Expand handler
     52         itemView.setOnClickListener(new View.OnClickListener() {
     53             @Override
     54             public void onClick(View v) {
     55                 alarmTimeAdapter.expand(getAdapterPosition());
     56             }
     57         });
     58         alarmLabel.setOnClickListener(new View.OnClickListener() {
     59             @Override
     60             public void onClick(View v) {
     61                 alarmTimeAdapter.expand(getAdapterPosition());
     62             }
     63         });
     64         arrow.setOnClickListener(new View.OnClickListener() {
     65             @Override
     66             public void onClick(View v) {
     67                 alarmTimeAdapter.expand(getAdapterPosition());
     68             }
     69         });
     70         // Edit time handler
     71         clock.setOnClickListener(new View.OnClickListener() {
     72             @Override
     73             public void onClick(View v) {
     74                 alarmTimeClickHandler.onClockClicked(mAlarm);
     75                 alarmTimeAdapter.expand(getAdapterPosition());
     76             }
     77         });
     78     }
     79 
     80     @Override
     81     public void bindAlarm(Context context, Alarm alarm, AlarmInstance alarmInstance) {
     82         setData(alarm, alarmInstance);
     83         bindOnOffSwitch(context, alarm);
     84         bindClock(context, alarm);
     85         bindRepeatText(context, alarm);
     86         bindReadOnlyLabel(context, alarm);
     87         bindUpcomingInstance(context, alarm);
     88         boolean boundPreemptiveDismiss =
     89                 bindPreemptiveDismissButton(context, alarm, alarmInstance);
     90         hairLine.setVisibility(boundPreemptiveDismiss ? View.GONE : View.VISIBLE);
     91         itemView.setContentDescription(clock.getContentDescription() + " " +
     92                 alarm.getLabelOrDefault(context));
     93     }
     94 
     95     private void bindReadOnlyLabel(Context context, Alarm alarm) {
     96         if (alarm.label != null && alarm.label.length() != 0) {
     97             alarmLabel.setText(alarm.label);
     98             alarmLabel.setVisibility(View.VISIBLE);
     99             alarmLabel.setContentDescription(context.getString(R.string.label_description)
    100                     + " " + alarm.label);
    101         } else {
    102             alarmLabel.setVisibility(View.GONE);
    103         }
    104     }
    105 
    106     private void bindRepeatText(Context context, Alarm alarm) {
    107         final String daysOfWeekText =
    108                 alarm.daysOfWeek.toString(context, Utils.getFirstDayOfWeek(context));
    109         if (!TextUtils.isEmpty(daysOfWeekText)) {
    110             daysOfWeek.setText(daysOfWeekText);
    111             daysOfWeek.setContentDescription(alarm.daysOfWeek.toAccessibilityString(
    112                     context, Utils.getFirstDayOfWeek(context)));
    113             daysOfWeek.setVisibility(View.VISIBLE);
    114         } else {
    115             daysOfWeek.setVisibility(View.GONE);
    116         }
    117     }
    118 
    119     private void bindUpcomingInstance(Context context, Alarm alarm) {
    120         if (alarm.daysOfWeek.isRepeating()) {
    121             upcomingInstanceLabel.setVisibility(View.GONE);
    122         } else {
    123             upcomingInstanceLabel.setVisibility(View.VISIBLE);
    124             final String labelText = Alarm.isTomorrow(alarm, Calendar.getInstance()) ?
    125                     context.getString(R.string.alarm_tomorrow) :
    126                     context.getString(R.string.alarm_today);
    127             upcomingInstanceLabel.setText(labelText);
    128         }
    129     }
    130 }
    131