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