Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [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; |
| 17 | |
Marcus Hagerott | a8b448a | 2016-11-18 12:51:39 -0800 | [diff] [blame] | 18 | import android.app.Activity; |
| 19 | import android.app.Fragment; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 20 | import android.app.LoaderManager; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 23 | import android.content.IntentFilter; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 24 | import android.content.Loader; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 26 | import android.support.annotation.NonNull; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 27 | import android.support.annotation.Nullable; |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 28 | import android.support.design.widget.Snackbar; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 29 | import android.support.v4.content.LocalBroadcastManager; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 30 | import android.support.v4.util.ArrayMap; |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 31 | import android.support.v4.view.ViewCompat; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 32 | import android.support.v4.widget.ContentLoadingProgressBar; |
| 33 | import android.support.v7.widget.Toolbar; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 34 | import android.util.SparseBooleanArray; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 38 | import android.widget.AbsListView; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 39 | import android.widget.AdapterView; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 40 | import android.widget.ArrayAdapter; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 41 | import android.widget.ListView; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 42 | import android.widget.TextView; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 43 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 44 | import com.android.contacts.compat.CompatUtils; |
| 45 | import com.android.contacts.database.SimContactDao; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 46 | import com.android.contacts.editor.AccountHeaderPresenter; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 47 | import com.android.contacts.model.AccountTypeManager; |
| 48 | import com.android.contacts.model.SimCard; |
| 49 | import com.android.contacts.model.SimContact; |
| 50 | import com.android.contacts.model.account.AccountWithDataSet; |
| 51 | import com.android.contacts.preference.ContactsPreferences; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 52 | import com.android.contacts.util.concurrent.ContactsExecutors; |
| 53 | import com.android.contacts.util.concurrent.ListenableFutureLoader; |
| 54 | import com.google.common.base.Function; |
| 55 | import com.google.common.util.concurrent.Futures; |
| 56 | import com.google.common.util.concurrent.ListenableFuture; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 57 | |
| 58 | import java.util.ArrayList; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 59 | import java.util.Arrays; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 60 | import java.util.Collections; |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 61 | import java.util.List; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 62 | import java.util.Map; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 63 | import java.util.Set; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 64 | import java.util.concurrent.Callable; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Dialog that presents a list of contacts from a SIM card that can be imported into a selected |
| 68 | * account |
| 69 | */ |
Marcus Hagerott | a8b448a | 2016-11-18 12:51:39 -0800 | [diff] [blame] | 70 | public class SimImportFragment extends Fragment |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 71 | implements LoaderManager.LoaderCallbacks<SimImportFragment.LoaderResult>, |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 72 | AdapterView.OnItemClickListener, AbsListView.OnScrollListener { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 73 | |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 74 | private static final String KEY_SUFFIX_SELECTED_IDS = "_selectedIds"; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 75 | private static final String ARG_SUBSCRIPTION_ID = "subscriptionId"; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 76 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 77 | private ContactsPreferences mPreferences; |
| 78 | private AccountTypeManager mAccountTypeManager; |
| 79 | private SimContactAdapter mAdapter; |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 80 | private View mAccountHeaderContainer; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 81 | private AccountHeaderPresenter mAccountHeaderPresenter; |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 82 | private float mAccountScrolledElevationPixels; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 83 | private ContentLoadingProgressBar mLoadingIndicator; |
| 84 | private Toolbar mToolbar; |
| 85 | private ListView mListView; |
| 86 | private View mImportButton; |
| 87 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 88 | private Bundle mSavedInstanceState; |
| 89 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 90 | private final Map<AccountWithDataSet, long[]> mPerAccountCheckedIds = new ArrayMap<>(); |
| 91 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 92 | private int mSubscriptionId; |
| 93 | |
| 94 | @Override |
| 95 | public void onCreate(final Bundle savedInstanceState) { |
| 96 | super.onCreate(savedInstanceState); |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 97 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 98 | mSavedInstanceState = savedInstanceState; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 99 | mPreferences = new ContactsPreferences(getContext()); |
| 100 | mAccountTypeManager = AccountTypeManager.getInstance(getActivity()); |
| 101 | mAdapter = new SimContactAdapter(getActivity()); |
| 102 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 103 | final Bundle args = getArguments(); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 104 | mSubscriptionId = args == null ? SimCard.NO_SUBSCRIPTION_ID : |
| 105 | args.getInt(ARG_SUBSCRIPTION_ID, SimCard.NO_SUBSCRIPTION_ID); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onActivityCreated(Bundle savedInstanceState) { |
| 110 | super.onActivityCreated(savedInstanceState); |
| 111 | getLoaderManager().initLoader(0, null, this); |
| 112 | } |
| 113 | |
| 114 | @Nullable |
| 115 | @Override |
Marcus Hagerott | a8b448a | 2016-11-18 12:51:39 -0800 | [diff] [blame] | 116 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 117 | Bundle savedInstanceState) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 118 | final View view = inflater.inflate(R.layout.fragment_sim_import, container, false); |
| 119 | |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 120 | mAccountHeaderContainer = view.findViewById(R.id.account_header_container); |
| 121 | mAccountScrolledElevationPixels = getResources() |
| 122 | .getDimension(R.dimen.contact_list_header_elevation); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 123 | mAccountHeaderPresenter = new AccountHeaderPresenter( |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 124 | mAccountHeaderContainer); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 125 | if (savedInstanceState != null) { |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 126 | mAccountHeaderPresenter.onRestoreInstanceState(savedInstanceState); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 127 | } else { |
| 128 | final AccountWithDataSet currentDefaultAccount = AccountWithDataSet |
| 129 | .getDefaultOrBestFallback(mPreferences, mAccountTypeManager); |
| 130 | mAccountHeaderPresenter.setCurrentAccount(currentDefaultAccount); |
| 131 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 132 | mAccountHeaderPresenter.setObserver(new AccountHeaderPresenter.Observer() { |
| 133 | @Override |
| 134 | public void onChange(AccountHeaderPresenter sender) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 135 | rememberSelectionsForCurrentAccount(); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 136 | mAdapter.setAccount(sender.getCurrentAccount()); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 137 | showSelectionsForCurrentAccount(); |
| 138 | updateToolbarWithCurrentSelections(); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 139 | } |
| 140 | }); |
| 141 | mAdapter.setAccount(mAccountHeaderPresenter.getCurrentAccount()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 142 | |
| 143 | mListView = (ListView) view.findViewById(R.id.list); |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 144 | mListView.setOnScrollListener(this); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 145 | mListView.setAdapter(mAdapter); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 146 | mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); |
| 147 | mListView.setOnItemClickListener(this); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 148 | mImportButton = view.findViewById(R.id.import_button); |
| 149 | mImportButton.setOnClickListener(new View.OnClickListener() { |
| 150 | @Override |
| 151 | public void onClick(View v) { |
| 152 | importCurrentSelections(); |
| 153 | // Do we wait for import to finish? |
Marcus Hagerott | a8b448a | 2016-11-18 12:51:39 -0800 | [diff] [blame] | 154 | getActivity().setResult(Activity.RESULT_OK); |
| 155 | getActivity().finish(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 156 | } |
| 157 | }); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 158 | |
| 159 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar); |
| 160 | mToolbar.setNavigationOnClickListener(new View.OnClickListener() { |
| 161 | @Override |
| 162 | public void onClick(View v) { |
Marcus Hagerott | a8b448a | 2016-11-18 12:51:39 -0800 | [diff] [blame] | 163 | getActivity().setResult(Activity.RESULT_CANCELED); |
| 164 | getActivity().finish(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 165 | } |
| 166 | }); |
| 167 | |
| 168 | mLoadingIndicator = (ContentLoadingProgressBar) view.findViewById(R.id.loading_progress); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 169 | |
| 170 | return view; |
| 171 | } |
| 172 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 173 | private void rememberSelectionsForCurrentAccount() { |
| 174 | final AccountWithDataSet current = mAdapter.getAccount(); |
| 175 | final long[] ids = mListView.getCheckedItemIds(); |
| 176 | Arrays.sort(ids); |
| 177 | mPerAccountCheckedIds.put(current, ids); |
| 178 | } |
| 179 | |
| 180 | private void showSelectionsForCurrentAccount() { |
| 181 | final long[] ids = mPerAccountCheckedIds.get(mAdapter.getAccount()); |
| 182 | if (ids == null) { |
| 183 | selectAll(); |
| 184 | return; |
| 185 | } |
| 186 | for (int i = 0, len = mListView.getCount(); i < len; i++) { |
| 187 | mListView.setItemChecked(i, |
| 188 | Arrays.binarySearch(ids, mListView.getItemIdAtPosition(i)) >= 0); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private void selectAll() { |
| 193 | for (int i = 0, len = mListView.getCount(); i < len; i++) { |
| 194 | mListView.setItemChecked(i, true); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void updateToolbarWithCurrentSelections() { |
| 199 | // The ListView keeps checked state for items that are disabled but we only want to |
| 200 | // consider items that don't exist in the current account when updating the toolbar |
| 201 | int importableCount = 0; |
| 202 | final SparseBooleanArray checked = mListView.getCheckedItemPositions(); |
| 203 | for (int i = 0; i < checked.size(); i++) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 204 | if (checked.valueAt(i) && !mAdapter.existsInCurrentAccount(checked.keyAt(i))) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 205 | importableCount++; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (importableCount == 0) { |
| 210 | mImportButton.setVisibility(View.GONE); |
| 211 | mToolbar.setTitle(R.string.sim_import_title_none_selected); |
| 212 | } else { |
| 213 | mToolbar.setTitle(String.valueOf(importableCount)); |
| 214 | mImportButton.setVisibility(View.VISIBLE); |
| 215 | } |
| 216 | } |
| 217 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 218 | @Override |
| 219 | public void onStart() { |
| 220 | super.onStart(); |
| 221 | if (mAdapter.isEmpty() && getLoaderManager().getLoader(0).isStarted()) { |
| 222 | mLoadingIndicator.show(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void onSaveInstanceState(Bundle outState) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 228 | rememberSelectionsForCurrentAccount(); |
| 229 | // We'll restore this manually so we don't need the list to preserve it's own state. |
| 230 | mListView.clearChoices(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 231 | super.onSaveInstanceState(outState); |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 232 | mAccountHeaderPresenter.onSaveInstanceState(outState); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 233 | saveAdapterSelectedStates(outState); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 237 | public Loader<LoaderResult> onCreateLoader(int id, Bundle args) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 238 | return new SimContactLoader(getContext(), mSubscriptionId); |
| 239 | } |
| 240 | |
| 241 | @Override |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 242 | public void onLoadFinished(Loader<LoaderResult> loader, |
| 243 | LoaderResult data) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 244 | mLoadingIndicator.hide(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 245 | if (data == null) { |
| 246 | return; |
| 247 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 248 | mAccountHeaderPresenter.setAccounts(data.accounts); |
| 249 | restoreAdapterSelectedStates(data.accounts); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 250 | mAdapter.setData(data); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 251 | mListView.setEmptyView(getView().findViewById(R.id.empty_message)); |
| 252 | |
| 253 | showSelectionsForCurrentAccount(); |
| 254 | updateToolbarWithCurrentSelections(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 258 | public void onLoaderReset(Loader<LoaderResult> loader) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 261 | private void restoreAdapterSelectedStates(List<AccountWithDataSet> accounts) { |
| 262 | if (mSavedInstanceState == null) { |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 266 | for (AccountWithDataSet account : accounts) { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 267 | final long[] selections = mSavedInstanceState.getLongArray( |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 268 | account.stringify() + KEY_SUFFIX_SELECTED_IDS); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 269 | mPerAccountCheckedIds.put(account, selections); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 270 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 271 | mSavedInstanceState = null; |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | private void saveAdapterSelectedStates(Bundle outState) { |
| 275 | if (mAdapter == null) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // Make sure the selections are up-to-date |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 280 | for (Map.Entry<AccountWithDataSet, long[]> entry : mPerAccountCheckedIds.entrySet()) { |
| 281 | outState.putLongArray(entry.getKey().stringify() + KEY_SUFFIX_SELECTED_IDS, |
| 282 | entry.getValue()); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 286 | private void importCurrentSelections() { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 287 | final SparseBooleanArray checked = mListView.getCheckedItemPositions(); |
| 288 | final ArrayList<SimContact> importableContacts = new ArrayList<>(checked.size()); |
| 289 | for (int i = 0; i < checked.size(); i++) { |
| 290 | // It's possible for existing contacts to be "checked" but we only want to import the |
| 291 | // ones that don't already exist |
| 292 | if (checked.valueAt(i) && !mAdapter.existsInCurrentAccount(i)) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 293 | importableContacts.add(mAdapter.getItem(checked.keyAt(i))); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
Marcus Hagerott | 95246bb | 2016-11-11 10:56:09 -0800 | [diff] [blame] | 296 | SimImportService.startImport(getContext(), mSubscriptionId, importableContacts, |
| 297 | mAccountHeaderPresenter.getCurrentAccount()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 300 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 301 | if (mAdapter.existsInCurrentAccount(position)) { |
| 302 | Snackbar.make(getView(), R.string.sim_import_contact_exists_toast, |
| 303 | Snackbar.LENGTH_LONG).show(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 304 | } else { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 305 | updateToolbarWithCurrentSelections(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | public Context getContext() { |
| 310 | if (CompatUtils.isMarshmallowCompatible()) { |
| 311 | return super.getContext(); |
| 312 | } |
| 313 | return getActivity(); |
| 314 | } |
| 315 | |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 316 | @Override |
| 317 | public void onScrollStateChanged(AbsListView view, int scrollState) { } |
| 318 | |
| 319 | @Override |
| 320 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, |
| 321 | int totalItemCount) { |
| 322 | int firstCompletelyVisibleItem = firstVisibleItem; |
| 323 | if (view != null && view.getChildAt(0) != null && view.getChildAt(0).getTop() < 0) { |
| 324 | firstCompletelyVisibleItem++; |
| 325 | } |
| 326 | |
| 327 | if (firstCompletelyVisibleItem == 0) { |
| 328 | ViewCompat.setElevation(mAccountHeaderContainer, 0); |
| 329 | } else { |
| 330 | ViewCompat.setElevation(mAccountHeaderContainer, mAccountScrolledElevationPixels); |
| 331 | } |
| 332 | } |
| 333 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Creates a fragment that will display contacts stored on the default SIM card |
| 336 | */ |
| 337 | public static SimImportFragment newInstance() { |
| 338 | return new SimImportFragment(); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Creates a fragment that will display the contacts stored on the SIM card that has the |
| 343 | * provided subscriptionId |
| 344 | */ |
| 345 | public static SimImportFragment newInstance(int subscriptionId) { |
| 346 | final SimImportFragment fragment = new SimImportFragment(); |
| 347 | final Bundle args = new Bundle(); |
| 348 | args.putInt(ARG_SUBSCRIPTION_ID, subscriptionId); |
| 349 | fragment.setArguments(args); |
| 350 | return fragment; |
| 351 | } |
| 352 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 353 | private static class SimContactAdapter extends ArrayAdapter<SimContact> { |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 354 | private Map<AccountWithDataSet, Set<SimContact>> mExistingMap; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 355 | private AccountWithDataSet mSelectedAccount; |
| 356 | private LayoutInflater mInflater; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 357 | |
| 358 | public SimContactAdapter(Context context) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 359 | super(context, 0); |
| 360 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 364 | public long getItemId(int position) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 365 | // This can be called by the framework when the adapter hasn't been initialized for |
| 366 | // checking the checked state of items. See b/33108913 |
| 367 | if (position < 0 || position >= getCount()) { |
| 368 | return View.NO_ID; |
| 369 | } |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 370 | return getItem(position).getId(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 374 | public boolean hasStableIds() { |
| 375 | return true; |
| 376 | } |
Marcus Hagerott | da071fb | 2016-10-13 10:29:15 -0700 | [diff] [blame] | 377 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 378 | @Override |
| 379 | public int getViewTypeCount() { |
| 380 | return 2; |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public int getItemViewType(int position) { |
| 385 | return !existsInCurrentAccount(position) ? 0 : 1; |
| 386 | } |
| 387 | |
| 388 | @NonNull |
| 389 | @Override |
| 390 | public View getView(int position, View convertView, ViewGroup parent) { |
| 391 | TextView text = (TextView) convertView; |
| 392 | if (text == null) { |
| 393 | final int layoutRes = existsInCurrentAccount(position) ? |
| 394 | R.layout.sim_import_list_item_disabled : |
| 395 | R.layout.sim_import_list_item; |
| 396 | text = (TextView) mInflater.inflate(layoutRes, parent, false); |
| 397 | } |
| 398 | text.setText(getItemLabel(getItem(position))); |
| 399 | |
| 400 | return text; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 403 | public void setData(LoaderResult result) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 404 | clear(); |
| 405 | addAll(result.contacts); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 406 | mExistingMap = result.accountsMap; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 409 | public void setAccount(AccountWithDataSet account) { |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 410 | mSelectedAccount = account; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 411 | notifyDataSetChanged(); |
| 412 | } |
| 413 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 414 | public AccountWithDataSet getAccount() { |
| 415 | return mSelectedAccount; |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 416 | } |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 417 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 418 | public boolean existsInCurrentAccount(int position) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 419 | return existsInCurrentAccount(getItem(position)); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | public boolean existsInCurrentAccount(SimContact contact) { |
| 423 | if (mSelectedAccount == null || !mExistingMap.containsKey(mSelectedAccount)) { |
| 424 | return false; |
| 425 | } |
| 426 | return mExistingMap.get(mSelectedAccount).contains(contact); |
| 427 | } |
| 428 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 429 | private String getItemLabel(SimContact contact) { |
| 430 | if (contact.hasName()) { |
| 431 | return contact.getName(); |
| 432 | } else if (contact.hasPhone()) { |
| 433 | return contact.getPhone(); |
| 434 | } else if (contact.hasEmails()) { |
| 435 | return contact.getEmails()[0]; |
| 436 | } else { |
| 437 | // This isn't really possible because we skip empty SIM contacts during loading |
| 438 | return ""; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 439 | } |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 440 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 443 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 444 | private static class SimContactLoader extends ListenableFutureLoader<LoaderResult> { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 445 | private SimContactDao mDao; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 446 | private AccountTypeManager mAccountTypeManager; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 447 | private final int mSubscriptionId; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 448 | |
| 449 | private BroadcastReceiver mReceiver; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 450 | |
| 451 | public SimContactLoader(Context context, int subscriptionId) { |
| 452 | super(context); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 453 | mDao = SimContactDao.create(context); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 454 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 455 | mSubscriptionId = subscriptionId; |
| 456 | } |
| 457 | |
| 458 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 459 | protected ListenableFuture<LoaderResult> loadData() { |
| 460 | final ListenableFuture<List<Object>> future = Futures.<Object>allAsList( |
| 461 | mAccountTypeManager |
| 462 | .filterAccountsByTypeAsync(AccountTypeManager.writableFilter()), |
| 463 | ContactsExecutors.getSimReadExecutor().<Object>submit( |
| 464 | new Callable<Object>() { |
| 465 | @Override |
| 466 | public LoaderResult call() throws Exception { |
| 467 | return loadFromSim(); |
| 468 | } |
| 469 | })); |
| 470 | return Futures.transform(future, new Function<List<Object>, LoaderResult>() { |
| 471 | @Override |
| 472 | public LoaderResult apply(List<Object> input) { |
| 473 | final List<AccountWithDataSet> accounts = (List<AccountWithDataSet>) input.get(0); |
| 474 | final LoaderResult simLoadResult = (LoaderResult) input.get(1); |
| 475 | simLoadResult.accounts = accounts; |
| 476 | return simLoadResult; |
| 477 | } |
| 478 | }); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 481 | private LoaderResult loadFromSim() { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 482 | final SimCard sim = mDao.getSimBySubscriptionId(mSubscriptionId); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 483 | LoaderResult result = new LoaderResult(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 484 | if (sim == null) { |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 485 | result.contacts = new ArrayList<>(); |
| 486 | result.accountsMap = Collections.emptyMap(); |
| 487 | return result; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 488 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 489 | result.contacts = mDao.loadContactsForSim(sim); |
| 490 | result.accountsMap = mDao.findAccountsOfExistingSimContacts(result.contacts); |
| 491 | return result; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 495 | protected void onStartLoading() { |
| 496 | super.onStartLoading(); |
| 497 | if (mReceiver == null) { |
| 498 | mReceiver = new ForceLoadReceiver(); |
| 499 | LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver, |
| 500 | new IntentFilter(AccountTypeManager.BROADCAST_ACCOUNTS_CHANGED)); |
| 501 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 502 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 503 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 504 | @Override |
| 505 | protected void onReset() { |
| 506 | super.onReset(); |
| 507 | LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mReceiver); |
| 508 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | public static class LoaderResult { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 512 | public List<AccountWithDataSet> accounts; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 513 | public ArrayList<SimContact> contacts; |
| 514 | public Map<AccountWithDataSet, Set<SimContact>> accountsMap; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 515 | } |
| 516 | } |