Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2007 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 android.app.ExpandableListActivity;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.database.Cursor;
     23 import android.database.MatrixCursor;
     24 import android.os.Bundle;
     25 import android.provider.Calendar.Calendars;
     26 import android.view.View;
     27 import android.view.Window;
     28 import android.widget.AdapterView;
     29 import android.widget.ExpandableListView;
     30 
     31 
     32 public class SelectCalendarsActivity extends ExpandableListActivity
     33     implements AdapterView.OnItemClickListener, View.OnClickListener {
     34 
     35     private static final String TAG = "Calendar";
     36     private static final String EXPANDED_KEY = "is_expanded";
     37     private static final String ACCOUNT_UNIQUE_KEY = "ACCOUNT_KEY";
     38     private View mView = null;
     39     private Cursor mCursor = null;
     40     private ExpandableListView mList;
     41     private SelectCalendarsAdapter mAdapter;
     42     private static final String[] PROJECTION = new String[] {
     43         Calendars._ID,
     44         Calendars._SYNC_ACCOUNT_TYPE,
     45         Calendars._SYNC_ACCOUNT,
     46         Calendars._SYNC_ACCOUNT_TYPE + " || " + Calendars._SYNC_ACCOUNT + " AS " +
     47                 ACCOUNT_UNIQUE_KEY,
     48     };
     49 
     50     @Override
     51     protected void onCreate(Bundle icicle) {
     52         super.onCreate(icicle);
     53         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     54         setContentView(R.layout.calendars_activity);
     55         getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
     56                 Window.PROGRESS_INDETERMINATE_ON);
     57         mList = getExpandableListView();
     58         mView = findViewById(R.id.calendars);
     59         Context context = mView.getContext();
     60         //TODO Move managedQuery into a background thread.
     61         //TODO change to something that supports group by queries.
     62         mCursor = managedQuery(Calendars.CONTENT_URI, PROJECTION,
     63                 "1) GROUP BY (" + ACCOUNT_UNIQUE_KEY, //Cheap hack to make WHERE a GROUP BY query
     64                 null /* selectionArgs */,
     65                 Calendars._SYNC_ACCOUNT /*sort order*/);
     66         MatrixCursor accountsCursor = Utils.matrixCursorFromCursor(mCursor);
     67         startManagingCursor(accountsCursor);
     68         mAdapter = new SelectCalendarsAdapter(context, accountsCursor, this);
     69         mList.setAdapter(mAdapter);
     70 
     71         mList.setOnChildClickListener(this);
     72 
     73         findViewById(R.id.btn_done).setOnClickListener(this);
     74         findViewById(R.id.btn_discard).setOnClickListener(this);
     75 
     76         // Start a background sync to get the list of calendars from the server.
     77         startCalendarMetafeedSync();
     78         int count = mList.getCount();
     79         for(int i = 0; i < count; i++) {
     80             mList.expandGroup(i);
     81         }
     82     }
     83 
     84     @Override
     85     protected void onResume() {
     86         super.onResume();
     87         mAdapter.startRefreshStopDelay();
     88     }
     89 
     90     @Override
     91     protected void onPause() {
     92         super.onPause();
     93         mAdapter.cancelRefreshStopDelay();
     94     }
     95 
     96     @Override
     97     protected void onSaveInstanceState(Bundle outState) {
     98         super.onSaveInstanceState(outState);
     99         boolean[] isExpanded;
    100         mList = getExpandableListView();
    101         if(mList != null) {
    102             int count = mList.getCount();
    103             isExpanded = new boolean[count];
    104             for(int i = 0; i < count; i++) {
    105                 isExpanded[i] = mList.isGroupExpanded(i);
    106             }
    107         } else {
    108             isExpanded = null;
    109         }
    110         outState.putBooleanArray(EXPANDED_KEY, isExpanded);
    111         //TODO Store this to preferences instead so it remains on restart
    112     }
    113 
    114     @Override
    115     protected void onRestoreInstanceState(Bundle state) {
    116         super.onRestoreInstanceState(state);
    117         mList = getExpandableListView();
    118         boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY);
    119         if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) {
    120             for(int i = 0; i < isExpanded.length; i++) {
    121                 if(isExpanded[i] && !mList.isGroupExpanded(i)) {
    122                     mList.expandGroup(i);
    123                 } else if(!isExpanded[i] && mList.isGroupExpanded(i)){
    124                     mList.collapseGroup(i);
    125                 }
    126             }
    127         }
    128     }
    129 
    130     @Override
    131     public boolean onChildClick(ExpandableListView parent, View view, int groupPosition,
    132             int childPosition, long id) {
    133         MultiStateButton button = (MultiStateButton) view.findViewById(R.id.multiStateButton);
    134         return button.performClick();
    135     }
    136 
    137     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    138         MultiStateButton button = (MultiStateButton) view.findViewById(R.id.multiStateButton);
    139         button.performClick();
    140     }
    141 
    142     /** {@inheritDoc} */
    143     public void onClick(View view) {
    144         switch (view.getId()) {
    145             case R.id.btn_done: {
    146                 doSaveAction();
    147                 break;
    148             }
    149             case R.id.btn_discard: {
    150                 finish();
    151                 break;
    152             }
    153         }
    154     }
    155 
    156     /*TODO*/
    157     private void doSaveAction() {
    158         mAdapter.doSaveAction();
    159         finish();
    160     }
    161 
    162     // startCalendarMetafeedSync() checks the server for an updated list of
    163     // Calendars (in the background).
    164     //
    165     // If a Calendar is added on the web (and it is selected and not
    166     // hidden) then it will be added to the list of calendars on the phone
    167     // (when this finishes).  When a new calendar from the
    168     // web is added to the phone, then the events for that calendar are also
    169     // downloaded from the web.
    170     //
    171     // This sync is done automatically in the background when the
    172     // SelectCalendars activity is started.
    173     private void startCalendarMetafeedSync() {
    174         Bundle extras = new Bundle();
    175         extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    176         extras.putBoolean("metafeedonly", true);
    177         ContentResolver.requestSync(null /* all accounts */,
    178                 Calendars.CONTENT_URI.getAuthority(), extras);
    179     }
    180 }
    181