SelectSyncedCalendarsMultiAccountActivity.java revision bb6d9279d8c0f0fb746160ad428da4738ece175e
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.ContentResolver;
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 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        if (view.getId() == R.id.btn_done) {
85            mAdapter.doSaveAction();
86            finish();
87        } else if (view.getId() == R.id.btn_discard) {
88            finish();
89        }
90    }
91
92    @Override
93    protected void onResume() {
94        super.onResume();
95        if (mAdapter != null) {
96            mAdapter.startRefreshStopDelay();
97        }
98    }
99
100    @Override
101    protected void onPause() {
102        super.onPause();
103        mAdapter.cancelRefreshStopDelay();
104    }
105
106    @Override
107    protected void onSaveInstanceState(Bundle outState) {
108        super.onSaveInstanceState(outState);
109        boolean[] isExpanded;
110        mList = getExpandableListView();
111        if(mList != null) {
112            int count = mList.getCount();
113            isExpanded = new boolean[count];
114            for(int i = 0; i < count; i++) {
115                isExpanded[i] = mList.isGroupExpanded(i);
116            }
117        } else {
118            isExpanded = null;
119        }
120        outState.putBooleanArray(EXPANDED_KEY, isExpanded);
121        //TODO Store this to preferences instead so it remains on restart
122    }
123
124    @Override
125    protected void onRestoreInstanceState(Bundle state) {
126        super.onRestoreInstanceState(state);
127        mList = getExpandableListView();
128        boolean[] isExpanded = state.getBooleanArray(EXPANDED_KEY);
129        if(mList != null && isExpanded != null && mList.getCount() >= isExpanded.length) {
130            for(int i = 0; i < isExpanded.length; i++) {
131                if(isExpanded[i] && !mList.isGroupExpanded(i)) {
132                    mList.expandGroup(i);
133                } else if(!isExpanded[i] && mList.isGroupExpanded(i)){
134                    mList.collapseGroup(i);
135                }
136            }
137        }
138    }
139
140    @Override
141    public boolean onCreateOptionsMenu(Menu menu) {
142        getActionBar()
143                .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
144        return true;
145    }
146
147    @Override
148    public boolean onOptionsItemSelected(MenuItem item) {
149        switch (item.getItemId()) {
150            case android.R.id.home:
151                Utils.returnToCalendarHome(this);
152                return true;
153        }
154        return super.onOptionsItemSelected(item);
155    }
156
157    // startCalendarMetafeedSync() checks the server for an updated list of
158    // Calendars (in the background).
159    //
160    // If a Calendar is added on the web (and it is selected and not
161    // hidden) then it will be added to the list of calendars on the phone
162    // (when this finishes).  When a new calendar from the
163    // web is added to the phone, then the events for that calendar are also
164    // downloaded from the web.
165    //
166    // This sync is done automatically in the background when the
167    // SelectCalendars activity is started.
168    private void startCalendarMetafeedSync() {
169        Bundle extras = new Bundle();
170        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
171        extras.putBoolean("metafeedonly", true);
172        ContentResolver.requestSync(null /* all accounts */,
173                Calendars.CONTENT_URI.getAuthority(), extras);
174    }
175}
176