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.FragmentTransaction;
21 import android.content.Intent;
22 import android.database.ContentObserver;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.provider.CalendarContract;
26 import android.view.Menu;
27 import android.view.MenuItem;
28 import android.view.View;
29 
30 import com.android.calendar.AbstractCalendarActivity;
31 import com.android.calendar.CalendarController;
32 import com.android.calendar.CalendarController.EventType;
33 import com.android.calendar.CalendarController.ViewType;
34 import com.android.calendar.R;
35 import com.android.calendar.Utils;
36 
37 public class SelectVisibleCalendarsActivity extends AbstractCalendarActivity {
38     private SelectVisibleCalendarsFragment mFragment;
39     private CalendarController mController;
40 
41     // Create an observer so that we can update the views whenever a
42     // Calendar event changes.
43     private final ContentObserver mObserver = new ContentObserver(new Handler()) {
44         @Override
45         public boolean deliverSelfNotifications() {
46             return true;
47         }
48 
49         @Override
50         public void onChange(boolean selfChange) {
51           mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
52         }
53     };
54 
55     @Override
onCreate(Bundle icicle)56     protected void onCreate(Bundle icicle) {
57         super.onCreate(icicle);
58 
59         setContentView(R.layout.simple_frame_layout);
60 
61         mController = CalendarController.getInstance(this);
62         mFragment = (SelectVisibleCalendarsFragment) getFragmentManager().findFragmentById(
63                 R.id.main_frame);
64 
65         if (mFragment == null) {
66             mFragment = new SelectVisibleCalendarsFragment(R.layout.calendar_sync_item);
67 
68             FragmentTransaction ft = getFragmentManager().beginTransaction();
69             ft.replace(R.id.main_frame, mFragment);
70             ft.show(mFragment);
71             ft.commit();
72         }
73     }
74 
75     @Override
onResume()76     public void onResume() {
77         super.onResume();
78         getContentResolver().registerContentObserver(CalendarContract.Events.CONTENT_URI,
79                 true, mObserver);
80     }
81 
82     @Override
onPause()83     public void onPause() {
84         super.onPause();
85         getContentResolver().unregisterContentObserver(mObserver);
86     }
87 
88     // Needs to be in proguard whitelist
89     // Specified as listener via android:onClick in a layout xml
handleSelectSyncedCalendarsClicked(View v)90     public void handleSelectSyncedCalendarsClicked(View v) {
91         Intent intent = new Intent(Intent.ACTION_VIEW);
92         intent.setClass(this, SelectSyncedCalendarsMultiAccountActivity.class);
93         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
94         startActivity(intent);
95     }
96 
97     @Override
onCreateOptionsMenu(Menu menu)98     public boolean onCreateOptionsMenu(Menu menu) {
99         getActionBar()
100                 .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
101         return true;
102     }
103 
104     @Override
onOptionsItemSelected(MenuItem item)105     public boolean onOptionsItemSelected(MenuItem item) {
106         switch (item.getItemId()) {
107             case android.R.id.home:
108                 Utils.returnToCalendarHome(this);
109                 return true;
110         }
111         return super.onOptionsItemSelected(item);
112     }
113 }
114