1/*
2 * Copyright (C) 2016 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 */
16package com.android.settings.accounts;
17
18import android.accounts.Account;
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.os.UserHandle;
23import android.os.UserManager;
24import android.support.annotation.VisibleForTesting;
25import android.support.v7.preference.Preference;
26
27import android.support.v7.preference.PreferenceScreen;
28import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
29import com.android.settings.R;
30import com.android.settings.Utils;
31import com.android.settings.core.PreferenceController;
32import com.android.settings.dashboard.DashboardFragment;
33import com.android.settingslib.accounts.AuthenticatorHelper;
34import com.android.settingslib.drawer.Tile;
35
36import java.util.ArrayList;
37import java.util.List;
38
39public class AccountDetailDashboardFragment extends DashboardFragment {
40
41    private static final String TAG = "AccountDetailDashboard";
42    private static final String METADATA_IA_ACCOUNT = "com.android.settings.ia.account";
43
44    public static final String KEY_ACCOUNT = "account";
45    public static final String KEY_ACCOUNT_TYPE = "account_type";
46    public static final String KEY_ACCOUNT_LABEL = "account_label";
47    public static final String KEY_ACCOUNT_TITLE_RES = "account_title_res";
48    public static final String KEY_ACCOUNT_HEADER = "account_header";
49    public static final String KEY_USER_HANDLE = "user_handle";
50
51    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
52    Account mAccount;
53    private String mAccountLabel;
54    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
55    String mAccountType;
56    private AccountSyncPreferenceController mAccountSynController;
57    private RemoveAccountPreferenceController mRemoveAccountController;
58
59    @Override
60    public void onCreate(Bundle icicle) {
61        super.onCreate(icicle);
62        Bundle args = getArguments();
63        final Activity activity = getActivity();
64        UserHandle userHandle = Utils.getSecureTargetUser(activity.getActivityToken(),
65                (UserManager) getSystemService(Context.USER_SERVICE), args,
66                activity.getIntent().getExtras());
67        if (args != null) {
68            if (args.containsKey(KEY_ACCOUNT)) {
69                mAccount = args.getParcelable(KEY_ACCOUNT);
70            }
71            if (args.containsKey(KEY_ACCOUNT_LABEL)) {
72                mAccountLabel = args.getString(KEY_ACCOUNT_LABEL);
73            }
74            if (args.containsKey(KEY_ACCOUNT_TYPE)) {
75                mAccountType = args.getString(KEY_ACCOUNT_TYPE);
76            }
77        }
78        mAccountSynController.init(mAccount, userHandle);
79        mRemoveAccountController.init(mAccount, userHandle);
80    }
81
82    @Override
83    public void onActivityCreated(Bundle savedInstanceState) {
84        super.onActivityCreated(savedInstanceState);
85        if (mAccountLabel != null) {
86            getActivity().setTitle(mAccountLabel);
87        }
88        updateUi();
89    }
90
91    @Override
92    public int getMetricsCategory() {
93        return MetricsEvent.ACCOUNT;
94    }
95
96    @Override
97    protected String getLogTag() {
98        return TAG;
99    }
100
101    @Override
102    protected int getPreferenceScreenResId() {
103        return R.xml.account_type_settings;
104    }
105
106    @Override
107    protected List<PreferenceController> getPreferenceControllers(Context context) {
108        final List<PreferenceController> controllers = new ArrayList<>();
109        mAccountSynController = new AccountSyncPreferenceController(context);
110        controllers.add(mAccountSynController);
111        mRemoveAccountController = new RemoveAccountPreferenceController(context, this);
112        controllers.add(mRemoveAccountController);
113        return controllers;
114    }
115
116    @Override
117    protected boolean displayTile(Tile tile) {
118        if (mAccountType == null) {
119            return false;
120        }
121        final Bundle metadata = tile.metaData;
122        if (metadata == null) {
123            return false;
124        }
125        return mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT));
126    }
127
128    @VisibleForTesting
129    void updateUi() {
130        final Preference headerPreference = findPreference(KEY_ACCOUNT_HEADER);
131        headerPreference.setTitle(mAccount.name);
132        final Context context = getContext();
133        UserHandle userHandle = null;
134        Bundle args = getArguments();
135        if (args != null && args.containsKey(KEY_USER_HANDLE)) {
136            userHandle = args.getParcelable(KEY_USER_HANDLE);
137        }
138        final AuthenticatorHelper helper = new AuthenticatorHelper(context, userHandle, null);
139        headerPreference.setIcon(helper.getDrawableForType(context, mAccountType));
140        final AccountTypePreferenceLoader accountTypePreferenceLoader =
141            new AccountTypePreferenceLoader(this, helper, userHandle);
142        PreferenceScreen prefs =
143            accountTypePreferenceLoader.addPreferencesForType(mAccountType, getPreferenceScreen());
144        if (prefs != null) {
145            accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount);
146        }
147    }
148
149}