Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2008 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.calendar;
     18 
     19 import static android.provider.Calendar.EVENT_BEGIN_TIME;
     20 import static android.provider.Calendar.EVENT_END_TIME;
     21 import android.app.Activity;
     22 import android.content.Intent;
     23 import android.text.format.DateUtils;
     24 import android.view.Menu;
     25 import android.view.MenuItem;
     26 
     27 public class MenuHelper {
     28     private static final int MENU_GROUP_AGENDA = 1;
     29     private static final int MENU_GROUP_DAY = 2;
     30     private static final int MENU_GROUP_WEEK = 3;
     31     private static final int MENU_GROUP_MONTH = 4;
     32     private static final int MENU_GROUP_EVENT_CREATE = 5;
     33     private static final int MENU_GROUP_TODAY = 6;
     34     private static final int MENU_GROUP_SELECT_CALENDARS = 7;
     35     private static final int MENU_GROUP_PREFERENCES = 8;
     36 
     37     public static final int MENU_GOTO_TODAY = 1;
     38     public static final int MENU_AGENDA = 2;
     39     public static final int MENU_DAY = 3;
     40     public static final int MENU_WEEK = 4;
     41     public static final int MENU_EVENT_VIEW = 5;
     42     public static final int MENU_EVENT_CREATE = 6;
     43     public static final int MENU_EVENT_EDIT = 7;
     44     public static final int MENU_EVENT_DELETE = 8;
     45     public static final int MENU_MONTH = 9;
     46     public static final int MENU_SELECT_CALENDARS = 10;
     47     public static final int MENU_PREFERENCES = 11;
     48 
     49     public static void onPrepareOptionsMenu(Activity activity, Menu menu) {
     50 
     51         if (activity instanceof AgendaActivity) {
     52             menu.setGroupVisible(MENU_GROUP_AGENDA, true);
     53             menu.setGroupEnabled(MENU_GROUP_AGENDA, false);
     54         } else {
     55             menu.setGroupVisible(MENU_GROUP_AGENDA, true);
     56             menu.setGroupEnabled(MENU_GROUP_AGENDA, true);
     57         }
     58 
     59         if (activity instanceof DayActivity) {
     60             menu.setGroupVisible(MENU_GROUP_DAY, true);
     61             menu.setGroupEnabled(MENU_GROUP_DAY, false);
     62         } else {
     63             menu.setGroupVisible(MENU_GROUP_DAY, true);
     64             menu.setGroupEnabled(MENU_GROUP_DAY, true);
     65         }
     66 
     67         if (activity instanceof WeekActivity) {
     68             menu.setGroupVisible(MENU_GROUP_WEEK, true);
     69             menu.setGroupEnabled(MENU_GROUP_WEEK, false);
     70         } else {
     71             menu.setGroupVisible(MENU_GROUP_WEEK, true);
     72             menu.setGroupEnabled(MENU_GROUP_WEEK, true);
     73         }
     74 
     75         if (activity instanceof MonthActivity) {
     76             menu.setGroupVisible(MENU_GROUP_MONTH, true);
     77             menu.setGroupEnabled(MENU_GROUP_MONTH, false);
     78         } else {
     79             menu.setGroupVisible(MENU_GROUP_MONTH, true);
     80             menu.setGroupEnabled(MENU_GROUP_MONTH, true);
     81         }
     82 
     83         if (activity instanceof EventInfoActivity) {
     84             menu.setGroupVisible(MENU_GROUP_TODAY, false);
     85             menu.setGroupEnabled(MENU_GROUP_TODAY, false);
     86         } else {
     87             menu.setGroupVisible(MENU_GROUP_TODAY, true);
     88             menu.setGroupEnabled(MENU_GROUP_TODAY, true);
     89         }
     90     }
     91 
     92     public static boolean onCreateOptionsMenu(Menu menu) {
     93 
     94         MenuItem item;
     95         item = menu.add(MENU_GROUP_DAY, MENU_DAY, 0, R.string.day_view);
     96         item.setIcon(android.R.drawable.ic_menu_day);
     97         item.setAlphabeticShortcut('d');
     98 
     99         item = menu.add(MENU_GROUP_WEEK, MENU_WEEK, 0, R.string.week_view);
    100         item.setIcon(android.R.drawable.ic_menu_week);
    101         item.setAlphabeticShortcut('w');
    102 
    103         item = menu.add(MENU_GROUP_MONTH, MENU_MONTH, 0, R.string.month_view);
    104         item.setIcon(android.R.drawable.ic_menu_month);
    105         item.setAlphabeticShortcut('m');
    106 
    107         item = menu.add(MENU_GROUP_AGENDA, MENU_AGENDA, 0, R.string.agenda_view);
    108         item.setIcon(android.R.drawable.ic_menu_agenda);
    109         item.setAlphabeticShortcut('a');
    110 
    111         item = menu.add(MENU_GROUP_TODAY, MENU_GOTO_TODAY, 0, R.string.goto_today);
    112         item.setIcon(android.R.drawable.ic_menu_today);
    113         item.setAlphabeticShortcut('t');
    114 
    115         item = menu.add(MENU_GROUP_EVENT_CREATE, MENU_EVENT_CREATE, 0, R.string.event_create);
    116         item.setIcon(android.R.drawable.ic_menu_add);
    117         item.setAlphabeticShortcut('n');
    118 
    119         item = menu.add(MENU_GROUP_SELECT_CALENDARS, MENU_SELECT_CALENDARS,
    120                 0, R.string.menu_select_calendars);
    121         item.setIcon(android.R.drawable.ic_menu_manage);
    122 
    123         item = menu.add(MENU_GROUP_PREFERENCES, MENU_PREFERENCES, 0, R.string.menu_preferences);
    124         item.setIcon(android.R.drawable.ic_menu_preferences);
    125         item.setAlphabeticShortcut('p');
    126 
    127         return true;
    128     }
    129 
    130     public static boolean onOptionsItemSelected(Activity activity, MenuItem item, Navigator nav) {
    131         switch (item.getItemId()) {
    132         case MENU_SELECT_CALENDARS: {
    133             Intent intent = new Intent(Intent.ACTION_VIEW);
    134             intent.setClass(activity, SelectCalendarsActivity.class);
    135             activity.startActivity(intent);
    136             return true;
    137         }
    138         case MENU_GOTO_TODAY:
    139             nav.goToToday();
    140             return true;
    141         case MENU_PREFERENCES:
    142             Utils.startActivity(activity, CalendarPreferenceActivity.class.getName(), nav.getSelectedTime());
    143             return true;
    144         case MENU_AGENDA:
    145             Utils.startActivity(activity, AgendaActivity.class.getName(), nav.getSelectedTime());
    146             return true;
    147         case MENU_DAY:
    148             Utils.startActivity(activity, DayActivity.class.getName(), nav.getSelectedTime());
    149             return true;
    150         case MENU_WEEK:
    151             Utils.startActivity(activity, WeekActivity.class.getName(), nav.getSelectedTime());
    152             return true;
    153         case MENU_MONTH:
    154             Utils.startActivity(activity, MonthActivity.class.getName(), nav.getSelectedTime());
    155             return true;
    156         case MENU_EVENT_CREATE: {
    157             long startMillis = nav.getSelectedTime();
    158             long endMillis = startMillis + DateUtils.HOUR_IN_MILLIS;
    159             Intent intent = new Intent(Intent.ACTION_EDIT);
    160             intent.setClassName(activity, EditEvent.class.getName());
    161             intent.putExtra(EVENT_BEGIN_TIME, startMillis);
    162             intent.putExtra(EVENT_END_TIME, endMillis);
    163             intent.putExtra(EditEvent.EVENT_ALL_DAY, nav.getAllDay());
    164             activity.startActivity(intent);
    165             return true;
    166         }
    167         }
    168         return false;
    169     }
    170 }
    171