Home | History | Annotate | Download | only in selectcalendars
      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  */
     16 
     17 package com.android.calendar.selectcalendars;
     18 
     19 import com.android.calendar.AsyncQueryService;
     20 import com.android.calendar.CalendarController.EventInfo;
     21 import com.android.calendar.CalendarController.EventType;
     22 import com.android.calendar.R;
     23 import com.android.calendar.CalendarController;
     24 import com.android.calendar.Utils;
     25 
     26 import android.app.Activity;
     27 import android.app.Fragment;
     28 import android.content.ContentUris;
     29 import android.content.ContentValues;
     30 import android.database.Cursor;
     31 import android.net.Uri;
     32 import android.os.Bundle;
     33 import android.provider.CalendarContract.Calendars;
     34 import android.util.Log;
     35 import android.view.LayoutInflater;
     36 import android.view.View;
     37 import android.view.ViewGroup;
     38 import android.widget.AdapterView;
     39 import android.widget.ListView;
     40 
     41 public class SelectVisibleCalendarsFragment extends Fragment
     42         implements AdapterView.OnItemClickListener, CalendarController.EventHandler {
     43 
     44     private static final String TAG = "Calendar";
     45     private static final String IS_PRIMARY = "\"primary\"";
     46     private static final String SELECTION = Calendars.SYNC_EVENTS + "=?";
     47     private static final String[] SELECTION_ARGS = new String[] {"1"};
     48 
     49     private static final String[] PROJECTION = new String[] {
     50         Calendars._ID,
     51         Calendars.ACCOUNT_NAME,
     52         Calendars.OWNER_ACCOUNT,
     53         Calendars.CALENDAR_DISPLAY_NAME,
     54         Calendars.CALENDAR_COLOR,
     55         Calendars.VISIBLE,
     56         Calendars.SYNC_EVENTS,
     57         "(" + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT + ") AS " + IS_PRIMARY,
     58       };
     59     private static int mUpdateToken;
     60     private static int mQueryToken;
     61     private static int mCalendarItemLayout = R.layout.mini_calendar_item;
     62 
     63     private View mView = null;
     64     private ListView mList;
     65     private SelectCalendarsSimpleAdapter mAdapter;
     66     private Activity mContext;
     67     private AsyncQueryService mService;
     68     private Cursor mCursor;
     69 
     70     public SelectVisibleCalendarsFragment() {
     71     }
     72 
     73     public SelectVisibleCalendarsFragment(int itemLayout) {
     74         mCalendarItemLayout = itemLayout;
     75     }
     76 
     77     @Override
     78     public void onAttach(Activity activity) {
     79         super.onAttach(activity);
     80         mContext = activity;
     81         mService = new AsyncQueryService(activity) {
     82             @Override
     83             protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
     84                 mAdapter.changeCursor(cursor);
     85                 mCursor = cursor;
     86             }
     87         };
     88     }
     89 
     90     @Override
     91     public void onDetach() {
     92         super.onDetach();
     93         if (mCursor != null) {
     94             mAdapter.changeCursor(null);
     95             mCursor.close();
     96             mCursor = null;
     97         }
     98     }
     99 
    100     @Override
    101     public void onCreate(Bundle icicle) {
    102         super.onCreate(icicle);
    103     }
    104 
    105     @Override
    106     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    107             Bundle savedInstanceState) {
    108         super.onCreateView(inflater, container, savedInstanceState);
    109         mView = inflater.inflate(R.layout.select_calendars_fragment, null);
    110         mList = (ListView)mView.findViewById(R.id.list);
    111 
    112         // Hide the Calendars to Sync button on tablets for now.
    113         // Long terms stick it in the list of calendars
    114         if (Utils.getConfigBool(getActivity(), R.bool.multiple_pane_config)) {
    115             // Don't show dividers on tablets
    116             mList.setDivider(null);
    117             View v = mView.findViewById(R.id.manage_sync_set);
    118             if (v != null) {
    119                 v.setVisibility(View.GONE);
    120             }
    121         }
    122         return mView;
    123     }
    124 
    125     @Override
    126     public void onActivityCreated(Bundle savedInstanceState) {
    127         super.onActivityCreated(savedInstanceState);
    128         mAdapter = new SelectCalendarsSimpleAdapter(mContext, mCalendarItemLayout, null);
    129         mList.setAdapter(mAdapter);
    130         mList.setOnItemClickListener(this);
    131     }
    132 
    133     public void onItemClick(AdapterView<?> parent, View view, int position, long id)  {
    134         if (mAdapter == null || mAdapter.getCount() <= position) {
    135             return;
    136         }
    137         toggleVisibility(position);
    138     }
    139 
    140     @Override
    141     public void onResume() {
    142         super.onResume();
    143         mQueryToken = mService.getNextToken();
    144         mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
    145                 SELECTION_ARGS, Calendars.ACCOUNT_NAME);
    146     }
    147 
    148     /*
    149      * Write back the changes that have been made.
    150      */
    151     public void toggleVisibility(int position) {
    152         Log.d(TAG, "Toggling calendar at " + position);
    153         mUpdateToken = mService.getNextToken();
    154         Uri uri = ContentUris.withAppendedId(Calendars.CONTENT_URI, mAdapter.getItemId(position));
    155         ContentValues values = new ContentValues();
    156         // Toggle the current setting
    157         int visibility = mAdapter.getVisible(position)^1;
    158         values.put(Calendars.VISIBLE, visibility);
    159         mService.startUpdate(mUpdateToken, null, uri, values, null, null, 0);
    160         mAdapter.setVisible(position, visibility);
    161     }
    162 
    163     @Override
    164     public void eventsChanged() {
    165         if (mService != null) {
    166             mService.cancelOperation(mQueryToken);
    167             mQueryToken = mService.getNextToken();
    168             mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
    169                     SELECTION_ARGS, Calendars.ACCOUNT_NAME);
    170         }
    171     }
    172 
    173     @Override
    174     public long getSupportedEventTypes() {
    175         return EventType.EVENTS_CHANGED;
    176     }
    177 
    178     @Override
    179     public void handleEvent(EventInfo event) {
    180         if (event.eventType == EventType.EVENTS_CHANGED) {
    181             eventsChanged();
    182         }
    183     }
    184 }
    185