SelectSyncedCalendarsMultiAccountActivity.java revision 48bcc4cb015dbf802341698a3a04e28d2cfb2ddc
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 17package com.android.calendar.selectcalendars; 18 19import android.app.ActionBar; 20import android.app.ExpandableListActivity; 21import android.content.AsyncQueryHandler; 22import android.database.Cursor; 23import android.database.MatrixCursor; 24import android.os.Bundle; 25import android.provider.CalendarContract.Calendars; 26import android.view.Menu; 27import android.view.MenuItem; 28import android.view.View; 29import android.widget.ExpandableListView; 30 31import com.android.calendar.R; 32import com.android.calendar.Utils; 33 34public 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 MatrixCursor mAccountsCursor = 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 mList.setEmptyView(findViewById(R.id.loading)); 57 // Start a background sync to get the list of calendars from the server. 58 Utils.startCalendarMetafeedSync(null); 59 60 findViewById(R.id.btn_done).setOnClickListener(this); 61 findViewById(R.id.btn_discard).setOnClickListener(this); 62 } 63 64 @Override 65 public void onClick(View view) { 66 if (view.getId() == R.id.btn_done) { 67 mAdapter.doSaveAction(); 68 finish(); 69 } else if (view.getId() == R.id.btn_discard) { 70 finish(); 71 } 72 } 73 74 @Override 75 protected void onResume() { 76 super.onResume(); 77 if (mAdapter != null) { 78 mAdapter.startRefreshStopDelay(); 79 } 80 new AsyncQueryHandler(getContentResolver()) { 81 @Override 82 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 83 mAccountsCursor = Utils.matrixCursorFromCursor(cursor); 84 85 mAdapter = new SelectSyncedCalendarsMultiAccountAdapter( 86 findViewById(R.id.calendars).getContext(), mAccountsCursor, 87 SelectSyncedCalendarsMultiAccountActivity.this); 88 mList.setAdapter(mAdapter); 89 90 // TODO initialize from sharepref 91 int count = mList.getCount(); 92 for(int i = 0; i < count; i++) { 93 mList.expandGroup(i); 94 } 95 } 96 }.startQuery(0, null, Calendars.CONTENT_URI, PROJECTION, 97 "1) GROUP BY (" + ACCOUNT_UNIQUE_KEY, //Cheap hack to make WHERE a GROUP BY query 98 null /* selectionArgs */, 99 Calendars.ACCOUNT_NAME /*sort order*/); 100 //TODO change to something that supports group by queries. 101 } 102 103 @Override 104 protected void onPause() { 105 super.onPause(); 106 mAdapter.cancelRefreshStopDelay(); 107 } 108 109 @Override 110 protected void onStop() { 111 super.onStop(); 112 mAdapter.closeChildrenCursors(); 113 if (!mAccountsCursor.isClosed()) { 114 mAccountsCursor.close(); 115 } 116 } 117 118 @Override 119 protected void onSaveInstanceState(Bundle outState) { 120 super.onSaveInstanceState(outState); 121 boolean[] isExpanded; 122 mList = getExpandableListView(); 123 if(mList != null) { 124 int count = mList.getCount(); 125 isExpanded = new boolean[count]; 126 for(int i = 0; i < count; i++) { 127 isExpanded[i] = mList.isGroupExpanded(i); 128 } 129 } else { 130 isExpanded = null; 131 } 132 outState.putBooleanArray(EXPANDED_KEY, isExpanded); 133 //TODO Store this to preferences instead so it remains on restart 134 } 135 136 @Override 137 protected void onRestoreInstanceState(Bundle state) { 138 super.onRestoreInstanceState(state); 139 mList = getExpandableListView(); 140 boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY); 141 if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) { 142 for(int i = 0; i < isExpanded.length; i++) { 143 if(isExpanded[i] && !mList.isGroupExpanded(i)) { 144 mList.expandGroup(i); 145 } else if(!isExpanded[i] && mList.isGroupExpanded(i)){ 146 mList.collapseGroup(i); 147 } 148 } 149 } 150 } 151 152 @Override 153 public boolean onCreateOptionsMenu(Menu menu) { 154 getActionBar() 155 .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 156 return true; 157 } 158 159 @Override 160 public boolean onOptionsItemSelected(MenuItem item) { 161 switch (item.getItemId()) { 162 case android.R.id.home: 163 Utils.returnToCalendarHome(this); 164 return true; 165 } 166 return super.onOptionsItemSelected(item); 167 } 168} 169