EventViewUtils.java revision ae5bcce4a24c1ef3a2db2493b3dedd64a6aa35ff
1/* 2 * Copyright (C) 2010 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.calendar.event; 17 18import com.android.calendar.R; 19 20import android.app.Activity; 21import android.content.Context; 22import android.content.res.Resources; 23import android.util.Log; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.widget.ArrayAdapter; 27import android.widget.ImageButton; 28import android.widget.LinearLayout; 29import android.widget.Spinner; 30 31import java.util.ArrayList; 32 33public class EventViewUtils { 34 35 private EventViewUtils() { 36 } 37 38 // Constructs a label given an arbitrary number of minutes. For example, 39 // if the given minutes is 63, then this returns the string "63 minutes". 40 // As another example, if the given minutes is 120, then this returns 41 // "2 hours". 42 public static String constructReminderLabel(Context context, int minutes, boolean abbrev) { 43 Resources resources = context.getResources(); 44 int value, resId; 45 46 if (minutes % 60 != 0) { 47 value = minutes; 48 if (abbrev) { 49 resId = R.plurals.Nmins; 50 } else { 51 resId = R.plurals.Nminutes; 52 } 53 } else if (minutes % (24 * 60) != 0) { 54 value = minutes / 60; 55 resId = R.plurals.Nhours; 56 } else { 57 value = minutes / (24 * 60); 58 resId = R.plurals.Ndays; 59 } 60 61 String format = resources.getQuantityString(resId, value); 62 return String.format(format, value); 63 } 64 65 /** 66 * Finds the index of the given "minutes" in the "values" list. 67 * 68 * @param values the list of minutes corresponding to the spinner choices 69 * @param minutes the minutes to search for in the values list 70 * @return the index of "minutes" in the "values" list 71 */ 72 public static int findMinutesInReminderList(ArrayList<Integer> values, int minutes) { 73 int index = values.indexOf(minutes); 74 if (index == -1) { 75 // This should never happen. 76 Log.e("Cal", "Cannot find minutes (" + minutes + ") in list"); 77 return 0; 78 } 79 return index; 80 } 81 82 public static ArrayList<Integer> reminderItemsToMinutes(ArrayList<LinearLayout> reminderItems, 83 ArrayList<Integer> reminderValues) { 84 int len = reminderItems.size(); 85 ArrayList<Integer> reminderMinutes = new ArrayList<Integer>(len); 86 for (int index = 0; index < len; index++) { 87 LinearLayout layout = reminderItems.get(index); 88 Spinner spinner = (Spinner) layout.findViewById(R.id.reminder_value); 89 int minutes = reminderValues.get(spinner.getSelectedItemPosition()); 90 reminderMinutes.add(minutes); 91 } 92 return reminderMinutes; 93 } 94 95 /** 96 * Checks our list of minute value-label pairs and adds any custom times 97 * this event might contain. 98 */ 99 public static void addMinutesToList(Context context, ArrayList<Integer> values, 100 ArrayList<String> labels, int minutes) { 101 int index = values.indexOf(minutes); 102 if (index != -1) { 103 return; 104 } 105 106 // The requested "minutes" does not exist in the list, so insert it 107 // into the list. 108 109 String label = constructReminderLabel(context, minutes, false); 110 int len = values.size(); 111 for (int i = 0; i < len; i++) { 112 if (minutes < values.get(i)) { 113 values.add(i, minutes); 114 labels.add(i, label); 115 return; 116 } 117 } 118 119 values.add(minutes); 120 labels.add(len, label); 121 } 122 123 /** 124 * Adds a reminder to the displayed list of reminders. 125 * Returns true if successfully added reminder, false if no reminders can 126 * be added. 127 */ 128 public static boolean addReminder(Activity activity, View view, View.OnClickListener listener, 129 ArrayList<LinearLayout> items, ArrayList<Integer> values, ArrayList<String> labels, 130 int minutes) { 131 132 if (items.size() >= EditEventHelper.MAX_REMINDERS) { 133 return false; 134 } 135 136 LayoutInflater inflater = activity.getLayoutInflater(); 137 LinearLayout parent = (LinearLayout) view.findViewById(R.id.reminder_items_container); 138 LinearLayout reminderItem = (LinearLayout) inflater.inflate(R.layout.edit_reminder_item, 139 null); 140 parent.addView(reminderItem); 141 142 Spinner spinner = (Spinner) reminderItem.findViewById(R.id.reminder_value); 143 Resources res = activity.getResources(); 144 spinner.setPrompt(res.getString(R.string.reminders_label)); 145 int resource = android.R.layout.simple_spinner_item; 146 ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, resource, labels); 147 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 148 spinner.setAdapter(adapter); 149 150 ImageButton reminderRemoveButton; 151 reminderRemoveButton = (ImageButton) reminderItem.findViewById(R.id.reminder_remove); 152 reminderRemoveButton.setOnClickListener(listener); 153 154 int index = findMinutesInReminderList(values, minutes); 155 spinner.setSelection(index); 156 157 items.add(reminderItem); 158 159 return true; 160 } 161 162} 163