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