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 setWidgetLayoutResource(R.layout.account_type_preference); 83 84 setKey(buildKey(account)); 85 setTitle(mTitle); 86 setSummary(summary); 87 setIcon(icon); 88 89 setOnPreferenceClickListener(this); 90 } 91 92 @Override 93 public boolean onPreferenceClick(Preference preference) { 94 if (mFragment != null) { 95 UserManager userManager = 96 (UserManager) getContext().getSystemService(Context.USER_SERVICE); 97 UserHandle user = mFragmentArguments.getParcelable(EXTRA_USER); 98 if (user != null && Utils.startQuietModeDialogIfNecessary(getContext(), userManager, 99 user.getIdentifier())) { 100 return true; 101 } else if (user != null && Utils.unlockWorkProfileIfNecessary(getContext(), 102 user.getIdentifier())) { 103 return true; 104 } 105 Utils.startWithFragment(getContext(), mFragment, mFragmentArguments, 106 null /* resultTo */, 0 /* resultRequestCode */, mTitleResPackageName, 107 mTitleResId, null /* title */, mMetricsCategory); 108 return true; 109 } 110 return false; 111 } 112 113 /** 114 * Build a unique preference key based on account. 115 */ 116 public static String buildKey(Account account) { 117 return String.valueOf(account.hashCode()); 118 } 119 120 public CharSequence getTitle() { 121 return mTitle; 122 } 123 124 public CharSequence getSummary() { 125 return mSummary; 126 } 127} 128