1/*
2 * Copyright (C) 2008 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.settings.accounts;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.accounts.OnAccountsUpdateListener;
22import android.app.Activity;
23import android.app.ActivityManager;
24import android.content.ContentResolver;
25import android.content.Intent;
26import android.graphics.drawable.Drawable;
27import android.os.Bundle;
28import android.preference.CheckBoxPreference;
29import android.preference.Preference;
30import android.preference.Preference.OnPreferenceChangeListener;
31import android.preference.PreferenceScreen;
32import android.util.Log;
33
34import com.android.settings.AccountPreference;
35import com.android.settings.DialogCreatable;
36import com.android.settings.R;
37
38import java.util.ArrayList;
39
40public class SyncSettings extends AccountPreferenceBase
41        implements OnAccountsUpdateListener, DialogCreatable {
42
43    private static final String KEY_SYNC_SWITCH = "sync_switch";
44
45    private String[] mAuthorities;
46
47    private SettingsDialogFragment mDialogFragment;
48    private CheckBoxPreference mAutoSyncPreference;
49
50    @Override
51    public void onCreate(Bundle icicle) {
52        super.onCreate(icicle);
53
54        addPreferencesFromResource(R.xml.sync_settings);
55        mAutoSyncPreference =
56                (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SYNC_SWITCH);
57        mAutoSyncPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
58            @Override
59            public boolean onPreferenceChange(Preference preference, Object newValue) {
60                if (ActivityManager.isUserAMonkey()) {
61                    Log.d("SyncSettings", "ignoring monkey's attempt to flip sync state");
62                } else {
63                    ContentResolver.setMasterSyncAutomatically((Boolean) newValue);
64                }
65                return true;
66            }
67        });
68
69        setHasOptionsMenu(true);
70    }
71
72    @Override
73    public void onStart() {
74        super.onStart();
75        Activity activity = getActivity();
76        AccountManager.get(activity).addOnAccountsUpdatedListener(this, null, true);
77    }
78
79    @Override
80    public void onActivityCreated(Bundle savedInstanceState) {
81        super.onActivityCreated(savedInstanceState);
82
83        final Activity activity = getActivity();
84        mAutoSyncPreference.setChecked(ContentResolver.getMasterSyncAutomatically());
85        mAuthorities = activity.getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY);
86
87        updateAuthDescriptions();
88    }
89
90    @Override
91    public void onStop() {
92        super.onStop();
93        final Activity activity = getActivity();
94        AccountManager.get(activity).removeOnAccountsUpdatedListener(this);
95    }
96
97    @Override
98    public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
99        if (preference instanceof AccountPreference) {
100            startAccountSettings((AccountPreference) preference);
101        } else {
102            return false;
103        }
104        return true;
105    }
106
107    private void startAccountSettings(AccountPreference acctPref) {
108        Intent intent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS");
109        intent.putExtra(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount());
110        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111        startActivity(intent);
112        finish();
113    }
114
115    @Override
116    public void showDialog(int dialogId) {
117        if (mDialogFragment != null) {
118            Log.e(TAG, "Old dialog fragment not null!");
119        }
120        mDialogFragment = new SettingsDialogFragment(this, dialogId);
121        mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
122    }
123
124    private void removeAccountPreferences() {
125        PreferenceScreen parent = getPreferenceScreen();
126        for (int i = 0; i < parent.getPreferenceCount(); ) {
127            if (parent.getPreference(i) instanceof AccountPreference) {
128                parent.removePreference(parent.getPreference(i));
129            } else {
130                i++;
131            }
132        }
133    }
134
135    @Override
136    public void onAccountsUpdated(Account[] accounts) {
137        if (getActivity() == null) return;
138
139        removeAccountPreferences();
140        for (int i = 0, n = accounts.length; i < n; i++) {
141            final Account account = accounts[i];
142            final ArrayList<String> auths = getAuthoritiesForAccountType(account.type);
143
144            boolean showAccount = true;
145            if (mAuthorities != null && auths != null) {
146                showAccount = false;
147                for (String requestedAuthority : mAuthorities) {
148                    if (auths.contains(requestedAuthority)) {
149                        showAccount = true;
150                        break;
151                    }
152                }
153            }
154
155            if (showAccount) {
156                final Drawable icon = getDrawableForType(account.type);
157                final AccountPreference preference =
158                        new AccountPreference(getActivity(), account, icon, auths, true);
159                getPreferenceScreen().addPreference(preference);
160                preference.setSummary(getLabelForType(account.type));
161            }
162        }
163        onSyncStateUpdated();
164    }
165
166    @Override
167    protected void onAuthDescriptionsUpdated() {
168        // Update account icons for all account preference items
169        for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
170            Preference pref = getPreferenceScreen().getPreference(i);
171            if (pref instanceof AccountPreference) {
172                AccountPreference accPref = (AccountPreference)
173                        getPreferenceScreen().getPreference(i);
174                accPref.setIcon(getDrawableForType(accPref.getAccount().type));
175                accPref.setSummary(getLabelForType(accPref.getAccount().type));
176            }
177        }
178    }
179}
180