Marcus Hagerott | 9a679e7 | 2016-12-15 09:30:17 -0800 | [diff] [blame] | 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 | package com.android.contacts.model.account; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.IntentFilter; |
| 20 | |
| 21 | import com.android.contacts.model.AccountTypeManager; |
| 22 | import com.android.contacts.util.concurrent.ListenableFutureLoader; |
| 23 | import com.google.common.base.Objects; |
| 24 | import com.google.common.base.Predicate; |
| 25 | import com.google.common.base.Predicates; |
| 26 | import com.google.common.util.concurrent.ListenableFuture; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Loads the accounts from AccountTypeManager |
| 32 | */ |
| 33 | public class AccountsLoader extends ListenableFutureLoader<List<AccountInfo>> { |
| 34 | private final AccountTypeManager mAccountTypeManager; |
| 35 | private final Predicate<AccountInfo> mFilter; |
| 36 | |
| 37 | public AccountsLoader(Context context) { |
| 38 | this(context, Predicates.<AccountInfo>alwaysTrue()); |
| 39 | } |
| 40 | |
| 41 | public AccountsLoader(Context context, Predicate<AccountInfo> filter) { |
| 42 | super(context, new IntentFilter(AccountTypeManager.BROADCAST_ACCOUNTS_CHANGED)); |
| 43 | mAccountTypeManager = AccountTypeManager.getInstance(context); |
| 44 | mFilter = filter; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected ListenableFuture<List<AccountInfo>> loadData() { |
| 49 | return mAccountTypeManager.filterAccountsAsync(mFilter); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected boolean isSameData(List<AccountInfo> previous, List<AccountInfo> next) { |
| 54 | return Objects.equal(AccountInfo.extractAccounts(previous), |
| 55 | AccountInfo.extractAccounts(next)); |
| 56 | } |
| 57 | |
| 58 | } |