1/*
2 * Copyright (C) 2014 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.tv.settings.accounts;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.app.Fragment;
22import android.os.Bundle;
23import android.text.TextUtils;
24
25import com.android.tv.settings.BaseSettingsFragment;
26import com.android.tv.settings.TvSettingsActivity;
27
28/**
29 * Displays the sync settings for a given account.
30 */
31public class AccountSyncActivity extends TvSettingsActivity {
32
33    public static final String EXTRA_ACCOUNT = "account_name";
34
35    @Override
36    protected Fragment createSettingsFragment() {
37        String accountName = getIntent().getStringExtra(EXTRA_ACCOUNT);
38        Account account = null;
39        if (!TextUtils.isEmpty(accountName)) {
40            // Search for the account.
41            for (Account candidateAccount : AccountManager.get(this).getAccounts()) {
42                if (candidateAccount.name.equals(accountName)) {
43                    account = candidateAccount;
44                    break;
45                }
46            }
47        }
48
49        return SettingsFragment.newInstance(account);
50    }
51
52    public static class SettingsFragment extends BaseSettingsFragment {
53        private static final String ARG_ACCOUNT = "account";
54
55        public static SettingsFragment newInstance(Account account) {
56            final Bundle b = new Bundle(1);
57            b.putParcelable(ARG_ACCOUNT, account);
58            final SettingsFragment f = new SettingsFragment();
59            f.setArguments(b);
60            return f;
61        }
62
63        @Override
64        public void onPreferenceStartInitialScreen() {
65            final Account account = getArguments().getParcelable(ARG_ACCOUNT);
66            startPreferenceFragment(AccountSyncFragment.newInstance(account));
67        }
68    }
69}
70