Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.contacts.common.preference; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 21 | import android.content.DialogInterface; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 22 | import android.preference.DialogPreference; |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
yaolu | 6d29c04 | 2016-07-28 14:19:43 -0700 | [diff] [blame] | 24 | import android.view.View; |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 25 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 26 | import com.android.contacts.common.model.AccountTypeManager; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.model.account.AccountDisplayInfoFactory; |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 28 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.util.AccountsListAdapter; |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 30 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 31 | public class DefaultAccountPreference extends DialogPreference { |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 32 | private ContactsPreferences mPreferences; |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 33 | private AccountsListAdapter mListAdapter; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 34 | private AccountDisplayInfoFactory mAccountDisplayInfoFactory; |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 35 | private AccountTypeManager mAccountTypeManager; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 36 | private int mChosenIndex = -1; |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 37 | |
| 38 | public DefaultAccountPreference(Context context) { |
| 39 | super(context); |
| 40 | prepare(); |
| 41 | } |
| 42 | |
| 43 | public DefaultAccountPreference(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | prepare(); |
| 46 | } |
| 47 | |
yaolu | 6d29c04 | 2016-07-28 14:19:43 -0700 | [diff] [blame] | 48 | @Override |
| 49 | protected View onCreateDialogView() { |
| 50 | prepare(); |
| 51 | return super.onCreateDialogView(); |
| 52 | } |
| 53 | |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 54 | private void prepare() { |
| 55 | mPreferences = new ContactsPreferences(getContext()); |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 56 | mListAdapter = new AccountsListAdapter(getContext(), |
| 57 | AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE); |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 58 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 59 | mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext()); |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected boolean shouldPersist() { |
| 64 | return false; // This preference takes care of its own storage |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public CharSequence getSummary() { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 69 | final AccountWithDataSet defaultAccount = mPreferences.getDefaultAccount(); |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 70 | if (defaultAccount == null || |
| 71 | !mAccountTypeManager.getAccounts(/* writable */ true).contains(defaultAccount)) { |
| 72 | return null; |
| 73 | } else { |
| 74 | return mAccountDisplayInfoFactory.getAccountDisplayInfo(defaultAccount).getNameLabel(); |
| 75 | } |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 79 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 80 | super.onPrepareDialogBuilder(builder); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 81 | // UX recommendation is not to show buttons on such lists. |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 82 | builder.setNegativeButton(null, null); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 83 | builder.setPositiveButton(null, null); |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 84 | builder.setAdapter(mListAdapter, new DialogInterface.OnClickListener() { |
| 85 | @Override |
| 86 | public void onClick(DialogInterface dialog, int which) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 87 | mChosenIndex = which; |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void onDialogClosed(boolean positiveResult) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 94 | final AccountWithDataSet currentDefault = mPreferences.getDefaultAccount(); |
| 95 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 96 | if (mChosenIndex != -1) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 97 | final AccountWithDataSet chosenAccount = mListAdapter.getItem(mChosenIndex); |
| 98 | if (!chosenAccount.equals(currentDefault)) { |
| 99 | mPreferences.setDefaultAccount(chosenAccount); |
| 100 | notifyChanged(); |
Gary Mai | fd866c0 | 2016-09-06 11:01:00 -0700 | [diff] [blame] | 101 | } |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 102 | } // else the user dismissed this dialog so leave the preference unchanged. |
Tingting Wang | 20c6ec5 | 2015-08-20 16:35:36 -0700 | [diff] [blame] | 103 | } |
| 104 | } |