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 */
16
17package com.android.settings.accounts;
18
19import android.accounts.Account;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.os.UserHandle;
24import android.os.UserManager;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.Preference.OnPreferenceClickListener;
27
28import com.android.settings.R;
29import com.android.settings.Utils;
30
31import static android.content.Intent.EXTRA_USER;
32
33public class AccountTypePreference extends Preference implements OnPreferenceClickListener {
34    /**
35     * Title of the tile that is shown to the user.
36     * @attr ref android.R.styleable#PreferenceHeader_title
37     */
38    private final CharSequence mTitle;
39
40    /**
41     * Packange name used to resolve the resources of the title shown to the user in the new
42     * fragment.
43     */
44    private final String mTitleResPackageName;
45
46    /**
47     * Resource id of the title shown to the user in the new fragment.
48     */
49    private final int mTitleResId;
50
51    /**
52     * Summary of the tile that is shown to the user.
53     */
54    private final CharSequence mSummary;
55
56    /**
57     * Full class name of the fragment to display when this tile is
58     * selected.
59     * @attr ref android.R.styleable#PreferenceHeader_fragment
60     */
61    private final String mFragment;
62
63    /**
64     * Optional arguments to supply to the fragment when it is
65     * instantiated.
66     */
67    private final Bundle mFragmentArguments;
68
69    private final int mMetricsCategory;
70
71    public AccountTypePreference(Context context, int metricsCategory, Account account,
72            String titleResPackageName, int titleResId, CharSequence summary, String fragment,
73            Bundle fragmentArguments, Drawable icon) {
74        super(context);
75        mTitle = account.name;
76        mTitleResPackageName = titleResPackageName;
77        mTitleResId = titleResId;
78        mSummary = summary;
79        mFragment = fragment;
80        mFragmentArguments = fragmentArguments;
81        mMetricsCategory = metricsCategory;
82
83        setKey(buildKey(account));
84        setTitle(mTitle);
85        setSummary(summary);
86        setIcon(icon);
87
88        setOnPreferenceClickListener(this);
89    }
90
91    @Override
92    public boolean onPreferenceClick(Preference preference) {
93        if (mFragment != null) {
94            UserManager userManager =
95                (UserManager) getContext().getSystemService(Context.USER_SERVICE);
96            UserHandle user = mFragmentArguments.getParcelable(EXTRA_USER);
97            if (user != null && Utils.startQuietModeDialogIfNecessary(getContext(), userManager,
98                user.getIdentifier())) {
99                return true;
100            } else if (user != null && Utils.unlockWorkProfileIfNecessary(getContext(),
101                user.getIdentifier())) {
102                return true;
103            }
104            Utils.startWithFragment(getContext(), mFragment, mFragmentArguments,
105                null /* resultTo */, 0 /* resultRequestCode */, mTitleResPackageName,
106                mTitleResId, null /* title */, mMetricsCategory);
107            return true;
108        }
109        return false;
110    }
111
112    /**
113     * Build a unique preference key based on account.
114     */
115    public static String buildKey(Account account) {
116        return String.valueOf(account.hashCode());
117    }
118
119    public CharSequence getTitle() {
120        return mTitle;
121    }
122
123    public CharSequence getSummary() {
124        return mSummary;
125    }
126}
127