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
17package com.android.contacts.preference;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.preference.DialogPreference;
23import android.util.AttributeSet;
24import android.view.View;
25
26import com.android.contacts.model.account.AccountInfo;
27import com.android.contacts.model.account.AccountWithDataSet;
28import com.android.contacts.util.AccountsListAdapter;
29
30import java.util.List;
31
32public class DefaultAccountPreference extends DialogPreference {
33    private ContactsPreferences mPreferences;
34    private AccountsListAdapter mListAdapter;
35    private List<AccountInfo> mAccounts;
36    private int mChosenIndex = -1;
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
48    public void setAccounts(List<AccountInfo> accounts) {
49        mAccounts = accounts;
50        if (mListAdapter != null) {
51            mListAdapter.setAccounts(accounts, null);
52            notifyChanged();
53        }
54    }
55
56    @Override
57    protected View onCreateDialogView() {
58        prepare();
59        return super.onCreateDialogView();
60    }
61
62    private void prepare() {
63        mPreferences = new ContactsPreferences(getContext());
64        mListAdapter = new AccountsListAdapter(getContext());
65        if (mAccounts != null) {
66            mListAdapter.setAccounts(mAccounts, null);
67        }
68    }
69
70    @Override
71    protected boolean shouldPersist() {
72        return false;   // This preference takes care of its own storage
73    }
74
75    @Override
76    public CharSequence getSummary() {
77        final AccountWithDataSet defaultAccount = mPreferences.getDefaultAccount();
78        if (defaultAccount == null || mAccounts == null ||
79                !AccountInfo.contains(mAccounts, defaultAccount)) {
80            return null;
81        } else {
82            return AccountInfo.getAccount(mAccounts, defaultAccount).getNameLabel();
83        }
84    }
85
86    @Override
87    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
88        super.onPrepareDialogBuilder(builder);
89        // UX recommendation is not to show buttons on such lists.
90        builder.setNegativeButton(null, null);
91        builder.setPositiveButton(null, null);
92        builder.setAdapter(mListAdapter, new DialogInterface.OnClickListener() {
93            @Override
94            public void onClick(DialogInterface dialog, int which) {
95                mChosenIndex = which;
96            }
97        });
98    }
99
100    @Override
101    protected void onDialogClosed(boolean positiveResult) {
102        final AccountWithDataSet currentDefault = mPreferences.getDefaultAccount();
103
104        if (mChosenIndex != -1) {
105            final AccountWithDataSet chosenAccount = mListAdapter.getItem(mChosenIndex);
106            if (!chosenAccount.equals(currentDefault)) {
107                mPreferences.setDefaultAccount(chosenAccount);
108                notifyChanged();
109            }
110        } // else the user dismissed this dialog so leave the preference unchanged.
111    }
112}
113