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