EventViewUtils.java revision fbce65e53c7a111955f638db5bf8bee35381e5b7
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 // Checks our list of minute value-label pairs and adds any custom times 96 // this event 97 // might contain. 98 public static void addMinutesToList(Context context, ArrayList<Integer> values, 99 ArrayList<String> labels, int minutes) { 100 int index = values.indexOf(minutes); 101 if (index != -1) { 102 return; 103 } 104 105 // The requested "minutes" does not exist in the list, so insert it 106 // into the list. 107 108 String label = constructReminderLabel(context, minutes, false); 109 int len = values.size(); 110 for (int i = 0; i < len; i++) { 111 if (minutes < values.get(i)) { 112 values.add(i, minutes); 113 labels.add(i, label); 114 return; 115 } 116 } 117 118 values.add(minutes); 119 labels.add(len, label); 120 } 121 122 // Adds a reminder to the displayed list of reminders. 123 // Returns true if successfully added reminder, false if no reminders can 124 // be added. 125 public static boolean addReminder(Activity activity, View view, View.OnClickListener listener, 126 ArrayList<LinearLayout> items, ArrayList<Integer> values, ArrayList<String> labels, 127 int minutes) { 128 129 if (items.size() >= EditEventHelper.MAX_REMINDERS) { 130 return false; 131 } 132 133 LayoutInflater inflater = activity.getLayoutInflater(); 134 LinearLayout parent = (LinearLayout) view.findViewById(R.id.reminder_items_container); 135 LinearLayout reminderItem = (LinearLayout) inflater.inflate(R.layout.edit_reminder_item, 136 null); 137 parent.addView(reminderItem); 138 139 Spinner spinner = (Spinner) reminderItem.findViewById(R.id.reminder_value); 140 Resources res = activity.getResources(); 141 spinner.setPrompt(res.getString(R.string.reminders_label)); 142 int resource = android.R.layout.simple_spinner_item; 143 ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, resource, labels); 144 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 145 spinner.setAdapter(adapter); 146 147 ImageButton reminderRemoveButton; 148 reminderRemoveButton = (ImageButton) reminderItem.findViewById(R.id.reminder_remove); 149 reminderRemoveButton.setOnClickListener(listener); 150 151 int index = findMinutesInReminderList(values, minutes); 152 spinner.setSelection(index); 153 154 items.add(reminderItem); 155 156 return true; 157 } 158 159} 160