blob: 78f309b12af7ffbe803339e42c0e38bf28d29a2a [file] [log] [blame]
Marcus Hagerott9a679e72016-12-15 09:30:17 -08001/*
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 */
16package com.android.contacts.model.account;
17
18import android.content.Context;
19import android.content.IntentFilter;
20
21import com.android.contacts.model.AccountTypeManager;
22import com.android.contacts.util.concurrent.ListenableFutureLoader;
23import com.google.common.base.Objects;
24import com.google.common.base.Predicate;
25import com.google.common.base.Predicates;
26import com.google.common.util.concurrent.ListenableFuture;
27
28import java.util.List;
29
30/**
31 * Loads the accounts from AccountTypeManager
32 */
33public 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}