/
SelectSyncedCalendarsMultiAccountActivity.java
168 lines (151 loc) · 5.83 KB
/
SelectSyncedCalendarsMultiAccountActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.calendar.selectcalendars;
import android.app.ActionBar;
import android.app.ExpandableListActivity;
import android.content.AsyncQueryHandler;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.provider.CalendarContract.Calendars;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import com.android.calendar.R;
import com.android.calendar.Utils;
public class SelectSyncedCalendarsMultiAccountActivity extends ExpandableListActivity
implements View.OnClickListener {
private static final String TAG = "Calendar";
private static final String EXPANDED_KEY = "is_expanded";
private static final String ACCOUNT_UNIQUE_KEY = "ACCOUNT_KEY";
private MatrixCursor mAccountsCursor = null;
private ExpandableListView mList;
private SelectSyncedCalendarsMultiAccountAdapter mAdapter;
private static final String[] PROJECTION = new String[] {
Calendars._ID,
Calendars.ACCOUNT_TYPE,
Calendars.ACCOUNT_NAME,
Calendars.ACCOUNT_TYPE + " || " + Calendars.ACCOUNT_NAME + " AS " +
ACCOUNT_UNIQUE_KEY,
};
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.select_calendars_multi_accounts_fragment);
mList = getExpandableListView();
mList.setEmptyView(findViewById(R.id.loading));
// Start a background sync to get the list of calendars from the server.
Utils.startCalendarMetafeedSync(null);
findViewById(R.id.btn_done).setOnClickListener(this);
findViewById(R.id.btn_discard).setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_done) {
mAdapter.doSaveAction();
finish();
} else if (view.getId() == R.id.btn_discard) {
finish();
}
}
@Override
protected void onResume() {
super.onResume();
if (mAdapter != null) {
mAdapter.startRefreshStopDelay();
}
new AsyncQueryHandler(getContentResolver()) {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
mAccountsCursor = Utils.matrixCursorFromCursor(cursor);
mAdapter = new SelectSyncedCalendarsMultiAccountAdapter(
findViewById(R.id.calendars).getContext(), mAccountsCursor,
SelectSyncedCalendarsMultiAccountActivity.this);
mList.setAdapter(mAdapter);
// TODO initialize from sharepref
int count = mList.getCount();
for(int i = 0; i < count; i++) {
mList.expandGroup(i);
}
}
}.startQuery(0, null, Calendars.CONTENT_URI, PROJECTION,
"1) GROUP BY (" + ACCOUNT_UNIQUE_KEY, //Cheap hack to make WHERE a GROUP BY query
null /* selectionArgs */,
Calendars.ACCOUNT_NAME /*sort order*/);
//TODO change to something that supports group by queries.
}
@Override
protected void onPause() {
super.onPause();
mAdapter.cancelRefreshStopDelay();
}
@Override
protected void onStop() {
super.onStop();
mAdapter.closeChildrenCursors();
if (mAccountsCursor != null && !mAccountsCursor.isClosed()) {
mAccountsCursor.close();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
boolean[] isExpanded;
mList = getExpandableListView();
if(mList != null) {
int count = mList.getCount();
isExpanded = new boolean[count];
for(int i = 0; i < count; i++) {
isExpanded[i] = mList.isGroupExpanded(i);
}
} else {
isExpanded = null;
}
outState.putBooleanArray(EXPANDED_KEY, isExpanded);
//TODO Store this to preferences instead so it remains on restart
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
mList = getExpandableListView();
boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY);
if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) {
for(int i = 0; i < isExpanded.length; i++) {
if(isExpanded[i] && !mList.isGroupExpanded(i)) {
mList.expandGroup(i);
} else if(!isExpanded[i] && mList.isGroupExpanded(i)){
mList.collapseGroup(i);
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getActionBar()
.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Utils.returnToCalendarHome(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}