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