blob: 0fcc8cb66fa1e4de5debc9cc45011334ecbe48c2 [file] [log] [blame]
Tingting Wang20c6ec52015-08-20 16:35:36 -07001/*
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
17package com.android.contacts.common.preference;
18
19import android.app.AlertDialog;
20import android.content.Context;
Gary Maifd866c02016-09-06 11:01:00 -070021import android.content.DialogInterface;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070022import android.preference.DialogPreference;
Tingting Wang20c6ec52015-08-20 16:35:36 -070023import android.util.AttributeSet;
yaolu6d29c042016-07-28 14:19:43 -070024import android.view.View;
Tingting Wang20c6ec52015-08-20 16:35:36 -070025
Marcus Hagerott949d4e82016-09-20 13:23:05 -070026import com.android.contacts.common.model.AccountTypeManager;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070027import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Tingting Wang20c6ec52015-08-20 16:35:36 -070028import com.android.contacts.common.model.account.AccountWithDataSet;
Gary Maifd866c02016-09-06 11:01:00 -070029import com.android.contacts.common.util.AccountsListAdapter;
Tingting Wang20c6ec52015-08-20 16:35:36 -070030
Marcus Hagerottfac695a2016-08-24 17:02:40 -070031public class DefaultAccountPreference extends DialogPreference {
Tingting Wang20c6ec52015-08-20 16:35:36 -070032 private ContactsPreferences mPreferences;
Gary Maifd866c02016-09-06 11:01:00 -070033 private AccountsListAdapter mListAdapter;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070034 private AccountDisplayInfoFactory mAccountDisplayInfoFactory;
Marcus Hagerott949d4e82016-09-20 13:23:05 -070035 private AccountTypeManager mAccountTypeManager;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070036 private int mChosenIndex = -1;
Tingting Wang20c6ec52015-08-20 16:35:36 -070037
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
yaolu6d29c042016-07-28 14:19:43 -070048 @Override
49 protected View onCreateDialogView() {
50 prepare();
51 return super.onCreateDialogView();
52 }
53
Tingting Wang20c6ec52015-08-20 16:35:36 -070054 private void prepare() {
55 mPreferences = new ContactsPreferences(getContext());
Gary Maifd866c02016-09-06 11:01:00 -070056 mListAdapter = new AccountsListAdapter(getContext(),
57 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE);
Marcus Hagerott949d4e82016-09-20 13:23:05 -070058 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
Marcus Hagerottfac695a2016-08-24 17:02:40 -070059 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext());
Tingting Wang20c6ec52015-08-20 16:35:36 -070060 }
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 Hagerottfac695a2016-08-24 17:02:40 -070069 final AccountWithDataSet defaultAccount = mPreferences.getDefaultAccount();
Marcus Hagerott949d4e82016-09-20 13:23:05 -070070 if (defaultAccount == null ||
71 !mAccountTypeManager.getAccounts(/* writable */ true).contains(defaultAccount)) {
72 return null;
73 } else {
74 return mAccountDisplayInfoFactory.getAccountDisplayInfo(defaultAccount).getNameLabel();
75 }
Tingting Wang20c6ec52015-08-20 16:35:36 -070076 }
77
78 @Override
Tingting Wang20c6ec52015-08-20 16:35:36 -070079 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
80 super.onPrepareDialogBuilder(builder);
Marcus Hagerottfac695a2016-08-24 17:02:40 -070081 // UX recommendation is not to show buttons on such lists.
Tingting Wang20c6ec52015-08-20 16:35:36 -070082 builder.setNegativeButton(null, null);
Marcus Hagerottfac695a2016-08-24 17:02:40 -070083 builder.setPositiveButton(null, null);
Gary Maifd866c02016-09-06 11:01:00 -070084 builder.setAdapter(mListAdapter, new DialogInterface.OnClickListener() {
85 @Override
86 public void onClick(DialogInterface dialog, int which) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070087 mChosenIndex = which;
Gary Maifd866c02016-09-06 11:01:00 -070088 }
89 });
90 }
91
92 @Override
93 protected void onDialogClosed(boolean positiveResult) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070094 final AccountWithDataSet currentDefault = mPreferences.getDefaultAccount();
95
Marcus Hagerott949d4e82016-09-20 13:23:05 -070096 if (mChosenIndex != -1) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070097 final AccountWithDataSet chosenAccount = mListAdapter.getItem(mChosenIndex);
98 if (!chosenAccount.equals(currentDefault)) {
99 mPreferences.setDefaultAccount(chosenAccount);
100 notifyChanged();
Gary Maifd866c02016-09-06 11:01:00 -0700101 }
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700102 } // else the user dismissed this dialog so leave the preference unchanged.
Tingting Wang20c6ec52015-08-20 16:35:36 -0700103 }
104}