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 android.app.ActionBar; 20 import android.app.ExpandableListActivity; 21 import android.content.ContentResolver; 22 import android.database.Cursor; 23 import android.database.MatrixCursor; 24 import android.os.Bundle; 25 import android.provider.CalendarContract.Calendars; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.view.View; 29 import android.widget.ExpandableListView; 30 31 import com.android.calendar.R; 32 import com.android.calendar.Utils; 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 onCreate(Bundle icicle)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 if (accountsCursor != null) { 64 startManagingCursor(accountsCursor); 65 } 66 67 mAdapter = new SelectSyncedCalendarsMultiAccountAdapter(findViewById(R.id.calendars) 68 .getContext(), accountsCursor, this); 69 mList.setAdapter(mAdapter); 70 71 // TODO initialize from sharepref 72 int count = mList.getCount(); 73 for(int i = 0; i < count; i++) { 74 mList.expandGroup(i); 75 } 76 77 // Start a background sync to get the list of calendars from the server. 78 startCalendarMetafeedSync(); 79 80 findViewById(R.id.btn_done).setOnClickListener(this); 81 findViewById(R.id.btn_discard).setOnClickListener(this); 82 } 83 84 @Override onClick(View view)85 public void onClick(View view) { 86 if (view.getId() == R.id.btn_done) { 87 mAdapter.doSaveAction(); 88 finish(); 89 } else if (view.getId() == R.id.btn_discard) { 90 finish(); 91 } 92 } 93 94 @Override onResume()95 protected void onResume() { 96 super.onResume(); 97 if (mAdapter != null) { 98 mAdapter.startRefreshStopDelay(); 99 } 100 } 101 102 @Override onPause()103 protected void onPause() { 104 super.onPause(); 105 mAdapter.cancelRefreshStopDelay(); 106 } 107 108 @Override onSaveInstanceState(Bundle outState)109 protected void onSaveInstanceState(Bundle outState) { 110 super.onSaveInstanceState(outState); 111 boolean[] isExpanded; 112 mList = getExpandableListView(); 113 if(mList != null) { 114 int count = mList.getCount(); 115 isExpanded = new boolean[count]; 116 for(int i = 0; i < count; i++) { 117 isExpanded[i] = mList.isGroupExpanded(i); 118 } 119 } else { 120 isExpanded = null; 121 } 122 outState.putBooleanArray(EXPANDED_KEY, isExpanded); 123 //TODO Store this to preferences instead so it remains on restart 124 } 125 126 @Override onRestoreInstanceState(Bundle state)127 protected void onRestoreInstanceState(Bundle state) { 128 super.onRestoreInstanceState(state); 129 mList = getExpandableListView(); 130 boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY); 131 if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) { 132 for(int i = 0; i < isExpanded.length; i++) { 133 if(isExpanded[i] && !mList.isGroupExpanded(i)) { 134 mList.expandGroup(i); 135 } else if(!isExpanded[i] && mList.isGroupExpanded(i)){ 136 mList.collapseGroup(i); 137 } 138 } 139 } 140 } 141 142 @Override onCreateOptionsMenu(Menu menu)143 public boolean onCreateOptionsMenu(Menu menu) { 144 getActionBar() 145 .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 146 return true; 147 } 148 149 @Override onOptionsItemSelected(MenuItem item)150 public boolean onOptionsItemSelected(MenuItem item) { 151 switch (item.getItemId()) { 152 case android.R.id.home: 153 Utils.returnToCalendarHome(this); 154 return true; 155 } 156 return super.onOptionsItemSelected(item); 157 } 158 159 // startCalendarMetafeedSync() checks the server for an updated list of 160 // Calendars (in the background). 161 // 162 // If a Calendar is added on the web (and it is selected and not 163 // hidden) then it will be added to the list of calendars on the phone 164 // (when this finishes). When a new calendar from the 165 // web is added to the phone, then the events for that calendar are also 166 // downloaded from the web. 167 // 168 // This sync is done automatically in the background when the 169 // SelectCalendars activity is started. startCalendarMetafeedSync()170 private void startCalendarMetafeedSync() { 171 Bundle extras = new Bundle(); 172 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 173 extras.putBoolean("metafeedonly", true); 174 ContentResolver.requestSync(null /* all accounts */, 175 Calendars.CONTENT_URI.getAuthority(), extras); 176 } 177 } 178