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.AsyncQueryHandler; 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 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 if (mAdapter != null) { 68 mAdapter.doSaveAction(); 69 } 70 finish(); 71 } else if (view.getId() == R.id.btn_discard) { 72 finish(); 73 } 74 } 75 76 @Override 77 protected void onResume() { 78 super.onResume(); 79 if (mAdapter != null) { 80 mAdapter.startRefreshStopDelay(); 81 } 82 new AsyncQueryHandler(getContentResolver()) { 83 @Override 84 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 85 mAccountsCursor = Utils.matrixCursorFromCursor(cursor); 86 87 mAdapter = new SelectSyncedCalendarsMultiAccountAdapter( 88 findViewById(R.id.calendars).getContext(), mAccountsCursor, 89 SelectSyncedCalendarsMultiAccountActivity.this); 90 mList.setAdapter(mAdapter); 91 92 // TODO initialize from sharepref 93 int count = mList.getCount(); 94 for(int i = 0; i < count; i++) { 95 mList.expandGroup(i); 96 } 97 } 98 }.startQuery(0, null, Calendars.CONTENT_URI, PROJECTION, 99 "1) GROUP BY (" + ACCOUNT_UNIQUE_KEY, //Cheap hack to make WHERE a GROUP BY query 100 null /* selectionArgs */, 101 Calendars.ACCOUNT_NAME /*sort order*/); 102 //TODO change to something that supports group by queries. 103 } 104 105 @Override 106 protected void onPause() { 107 super.onPause(); 108 if (mAdapter != null) { 109 mAdapter.cancelRefreshStopDelay(); 110 } 111 } 112 113 @Override 114 protected void onStop() { 115 super.onStop(); 116 if (mAdapter != null) { 117 mAdapter.closeChildrenCursors(); 118 } 119 if (mAccountsCursor != null && !mAccountsCursor.isClosed()) { 120 mAccountsCursor.close(); 121 } 122 } 123 124 @Override 125 protected void onSaveInstanceState(Bundle outState) { 126 super.onSaveInstanceState(outState); 127 boolean[] isExpanded; 128 mList = getExpandableListView(); 129 if(mList != null) { 130 int count = mList.getCount(); 131 isExpanded = new boolean[count]; 132 for(int i = 0; i < count; i++) { 133 isExpanded[i] = mList.isGroupExpanded(i); 134 } 135 } else { 136 isExpanded = null; 137 } 138 outState.putBooleanArray(EXPANDED_KEY, isExpanded); 139 //TODO Store this to preferences instead so it remains on restart 140 } 141 142 @Override 143 protected void onRestoreInstanceState(Bundle state) { 144 super.onRestoreInstanceState(state); 145 mList = getExpandableListView(); 146 boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY); 147 if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) { 148 for(int i = 0; i < isExpanded.length; i++) { 149 if(isExpanded[i] && !mList.isGroupExpanded(i)) { 150 mList.expandGroup(i); 151 } else if(!isExpanded[i] && mList.isGroupExpanded(i)){ 152 mList.collapseGroup(i); 153 } 154 } 155 } 156 } 157 158 @Override 159 public boolean onCreateOptionsMenu(Menu menu) { 160 getActionBar() 161 .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 162 return true; 163 } 164 165 @Override 166 public boolean onOptionsItemSelected(MenuItem item) { 167 switch (item.getItemId()) { 168 case android.R.id.home: 169 Utils.returnToCalendarHome(this); 170 return true; 171 } 172 return super.onOptionsItemSelected(item); 173 } 174 } 175