Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2010 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;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.app.ActionBar;
     22 import android.content.ContentResolver;
     23 import android.content.Intent;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.preference.PreferenceActivity;
     27 import android.provider.CalendarContract;
     28 import android.provider.CalendarContract.Calendars;
     29 import android.provider.Settings;
     30 import android.text.format.DateUtils;
     31 import android.view.Menu;
     32 import android.view.MenuItem;
     33 
     34 import java.util.List;
     35 
     36 public class CalendarSettingsActivity extends PreferenceActivity {
     37     private static final int CHECK_ACCOUNTS_DELAY = 3000;
     38     private Account[] mAccounts;
     39     private Handler mHandler = new Handler();
     40     private boolean mHideMenuButtons = false;
     41 
     42     @Override
     43     public void onBuildHeaders(List<Header> target) {
     44         loadHeadersFromResource(R.xml.calendar_settings_headers, target);
     45 
     46         Account[] accounts = AccountManager.get(this).getAccounts();
     47         if (accounts != null) {
     48             int length = accounts.length;
     49             for (int i = 0; i < length; i++) {
     50                 Account acct = accounts[i];
     51                 if (ContentResolver.getIsSyncable(acct, CalendarContract.AUTHORITY) > 0) {
     52                     Header accountHeader = new Header();
     53                     accountHeader.title = acct.name;
     54                     accountHeader.fragment =
     55                             "com.android.calendar.selectcalendars.SelectCalendarsSyncFragment";
     56                     Bundle args = new Bundle();
     57                     args.putString(Calendars.ACCOUNT_NAME, acct.name);
     58                     args.putString(Calendars.ACCOUNT_TYPE, acct.type);
     59                     accountHeader.fragmentArguments = args;
     60                     target.add(1, accountHeader);
     61                 }
     62             }
     63         }
     64         mAccounts = accounts;
     65         if (Utils.getTardis() + DateUtils.MINUTE_IN_MILLIS > System.currentTimeMillis()) {
     66             Header tardisHeader = new Header();
     67             tardisHeader.title = getString(R.string.tardis);
     68             tardisHeader.fragment = "com.android.calendar.OtherPreferences";
     69             target.add(tardisHeader);
     70         }
     71     }
     72 
     73     @Override
     74     public boolean onOptionsItemSelected(MenuItem item) {
     75         if (item.getItemId() == android.R.id.home) {
     76             finish();
     77             return true;
     78         } else if (item.getItemId() == R.id.action_add_account) {
     79             Intent nextIntent = new Intent(Settings.ACTION_ADD_ACCOUNT);
     80             final String[] array = { "com.android.calendar" };
     81             nextIntent.putExtra(Settings.EXTRA_AUTHORITIES, array);
     82             nextIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     83             startActivity(nextIntent);
     84             return true;
     85         }
     86         return super.onOptionsItemSelected(item);
     87     }
     88 
     89     @Override
     90     public boolean onCreateOptionsMenu(Menu menu) {
     91         if (!mHideMenuButtons) {
     92             getMenuInflater().inflate(R.menu.settings_title_bar, menu);
     93         }
     94         getActionBar()
     95                 .setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
     96         return true;
     97     }
     98 
     99     @Override
    100     public void onResume() {
    101         if (mHandler != null) {
    102             mHandler.postDelayed(mCheckAccounts, CHECK_ACCOUNTS_DELAY);
    103         }
    104         super.onResume();
    105     }
    106 
    107     @Override
    108     public void onPause() {
    109         if (mHandler != null) {
    110             mHandler.removeCallbacks(mCheckAccounts);
    111         }
    112         super.onPause();
    113     }
    114 
    115     Runnable mCheckAccounts = new Runnable() {
    116         @Override
    117         public void run() {
    118             Account[] accounts = AccountManager.get(CalendarSettingsActivity.this).getAccounts();
    119             if (accounts != null && !accounts.equals(mAccounts)) {
    120                 invalidateHeaders();
    121             }
    122         }
    123     };
    124 
    125     public void hideMenuButtons() {
    126         mHideMenuButtons = true;
    127     }
    128 }
    129