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; |
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; |
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 { |
| 129 | final AccountWithDataSet currentDefaultAccount = AccountWithDataSet |
| 130 | .getDefaultOrBestFallback(mPreferences, mAccountTypeManager); |
| 131 | mAccountHeaderPresenter.setCurrentAccount(currentDefaultAccount); |
| 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(); |
| 176 | final long[] ids = mListView.getCheckedItemIds(); |
| 177 | Arrays.sort(ids); |
| 178 | mPerAccountCheckedIds.put(current, ids); |
| 179 | } |
| 180 | |
| 181 | private void showSelectionsForCurrentAccount() { |
| 182 | final long[] ids = mPerAccountCheckedIds.get(mAdapter.getAccount()); |
| 183 | if (ids == null) { |
| 184 | selectAll(); |
| 185 | return; |
| 186 | } |
| 187 | for (int i = 0, len = mListView.getCount(); i < len; i++) { |
| 188 | mListView.setItemChecked(i, |
| 189 | Arrays.binarySearch(ids, mListView.getItemIdAtPosition(i)) >= 0); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private void selectAll() { |
| 194 | for (int i = 0, len = mListView.getCount(); i < len; i++) { |
| 195 | mListView.setItemChecked(i, true); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private void updateToolbarWithCurrentSelections() { |
| 200 | // The ListView keeps checked state for items that are disabled but we only want to |
| 201 | // consider items that don't exist in the current account when updating the toolbar |
| 202 | int importableCount = 0; |
| 203 | final SparseBooleanArray checked = mListView.getCheckedItemPositions(); |
| 204 | for (int i = 0; i < checked.size(); i++) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 205 | if (checked.valueAt(i) && !mAdapter.existsInCurrentAccount(checked.keyAt(i))) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 206 | importableCount++; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (importableCount == 0) { |
| 211 | mImportButton.setVisibility(View.GONE); |
| 212 | mToolbar.setTitle(R.string.sim_import_title_none_selected); |
| 213 | } else { |
| 214 | mToolbar.setTitle(String.valueOf(importableCount)); |
| 215 | mImportButton.setVisibility(View.VISIBLE); |
| 216 | } |
| 217 | } |
| 218 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public void onStart() { |
| 221 | super.onStart(); |
| 222 | if (mAdapter.isEmpty() && getLoaderManager().getLoader(0).isStarted()) { |
| 223 | mLoadingIndicator.show(); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onSaveInstanceState(Bundle outState) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 229 | rememberSelectionsForCurrentAccount(); |
| 230 | // We'll restore this manually so we don't need the list to preserve it's own state. |
| 231 | mListView.clearChoices(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 232 | super.onSaveInstanceState(outState); |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 233 | mAccountHeaderPresenter.onSaveInstanceState(outState); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 234 | saveAdapterSelectedStates(outState); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 238 | public Loader<LoaderResult> onCreateLoader(int id, Bundle args) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 239 | return new SimContactLoader(getContext(), mSubscriptionId); |
| 240 | } |
| 241 | |
| 242 | @Override |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 243 | public void onLoadFinished(Loader<LoaderResult> loader, |
| 244 | LoaderResult data) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 245 | mLoadingIndicator.hide(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 246 | if (data == null) { |
| 247 | return; |
| 248 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 249 | mAccountHeaderPresenter.setAccounts(data.accounts); |
| 250 | restoreAdapterSelectedStates(data.accounts); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 251 | mAdapter.setData(data); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 252 | mListView.setEmptyView(getView().findViewById(R.id.empty_message)); |
| 253 | |
| 254 | showSelectionsForCurrentAccount(); |
| 255 | updateToolbarWithCurrentSelections(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 259 | public void onLoaderReset(Loader<LoaderResult> loader) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 262 | private void restoreAdapterSelectedStates(List<AccountInfo> accounts) { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 263 | if (mSavedInstanceState == null) { |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 267 | for (AccountInfo account : accounts) { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 268 | final long[] selections = mSavedInstanceState.getLongArray( |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 269 | account.getAccount().stringify() + KEY_SUFFIX_SELECTED_IDS); |
| 270 | mPerAccountCheckedIds.put(account.getAccount(), selections); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 271 | } |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 272 | mSavedInstanceState = null; |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private void saveAdapterSelectedStates(Bundle outState) { |
| 276 | if (mAdapter == null) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Make sure the selections are up-to-date |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 281 | for (Map.Entry<AccountWithDataSet, long[]> entry : mPerAccountCheckedIds.entrySet()) { |
| 282 | outState.putLongArray(entry.getKey().stringify() + KEY_SUFFIX_SELECTED_IDS, |
| 283 | entry.getValue()); |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 287 | private void importCurrentSelections() { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 288 | final SparseBooleanArray checked = mListView.getCheckedItemPositions(); |
| 289 | final ArrayList<SimContact> importableContacts = new ArrayList<>(checked.size()); |
| 290 | for (int i = 0; i < checked.size(); i++) { |
| 291 | // It's possible for existing contacts to be "checked" but we only want to import the |
| 292 | // ones that don't already exist |
| 293 | if (checked.valueAt(i) && !mAdapter.existsInCurrentAccount(i)) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 294 | importableContacts.add(mAdapter.getItem(checked.keyAt(i))); |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
Marcus Hagerott | 95246bb | 2016-11-11 10:56:09 -0800 | [diff] [blame] | 297 | SimImportService.startImport(getContext(), mSubscriptionId, importableContacts, |
| 298 | mAccountHeaderPresenter.getCurrentAccount()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 301 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 302 | if (mAdapter.existsInCurrentAccount(position)) { |
| 303 | Snackbar.make(getView(), R.string.sim_import_contact_exists_toast, |
| 304 | Snackbar.LENGTH_LONG).show(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 305 | } else { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 306 | updateToolbarWithCurrentSelections(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | public Context getContext() { |
| 311 | if (CompatUtils.isMarshmallowCompatible()) { |
| 312 | return super.getContext(); |
| 313 | } |
| 314 | return getActivity(); |
| 315 | } |
| 316 | |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 317 | @Override |
| 318 | public void onScrollStateChanged(AbsListView view, int scrollState) { } |
| 319 | |
| 320 | @Override |
| 321 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, |
| 322 | int totalItemCount) { |
| 323 | int firstCompletelyVisibleItem = firstVisibleItem; |
| 324 | if (view != null && view.getChildAt(0) != null && view.getChildAt(0).getTop() < 0) { |
| 325 | firstCompletelyVisibleItem++; |
| 326 | } |
| 327 | |
| 328 | if (firstCompletelyVisibleItem == 0) { |
| 329 | ViewCompat.setElevation(mAccountHeaderContainer, 0); |
| 330 | } else { |
| 331 | ViewCompat.setElevation(mAccountHeaderContainer, mAccountScrolledElevationPixels); |
| 332 | } |
| 333 | } |
| 334 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 335 | /** |
| 336 | * Creates a fragment that will display contacts stored on the default SIM card |
| 337 | */ |
| 338 | public static SimImportFragment newInstance() { |
| 339 | return new SimImportFragment(); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Creates a fragment that will display the contacts stored on the SIM card that has the |
| 344 | * provided subscriptionId |
| 345 | */ |
| 346 | public static SimImportFragment newInstance(int subscriptionId) { |
| 347 | final SimImportFragment fragment = new SimImportFragment(); |
| 348 | final Bundle args = new Bundle(); |
| 349 | args.putInt(ARG_SUBSCRIPTION_ID, subscriptionId); |
| 350 | fragment.setArguments(args); |
| 351 | return fragment; |
| 352 | } |
| 353 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 354 | private static class SimContactAdapter extends ArrayAdapter<SimContact> { |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 355 | private Map<AccountWithDataSet, Set<SimContact>> mExistingMap; |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 356 | private AccountWithDataSet mSelectedAccount; |
| 357 | private LayoutInflater mInflater; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 358 | |
| 359 | public SimContactAdapter(Context context) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 360 | super(context, 0); |
| 361 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 365 | public long getItemId(int position) { |
Marcus Hagerott | c8b04ba | 2016-11-23 18:49:57 -0800 | [diff] [blame] | 366 | // This can be called by the framework when the adapter hasn't been initialized for |
| 367 | // checking the checked state of items. See b/33108913 |
| 368 | if (position < 0 || position >= getCount()) { |
| 369 | return View.NO_ID; |
| 370 | } |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 371 | return getItem(position).getId(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 375 | public boolean hasStableIds() { |
| 376 | return true; |
| 377 | } |
Marcus Hagerott | da071fb | 2016-10-13 10:29:15 -0700 | [diff] [blame] | 378 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 379 | @Override |
| 380 | public int getViewTypeCount() { |
| 381 | return 2; |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public int getItemViewType(int position) { |
| 386 | return !existsInCurrentAccount(position) ? 0 : 1; |
| 387 | } |
| 388 | |
| 389 | @NonNull |
| 390 | @Override |
| 391 | public View getView(int position, View convertView, ViewGroup parent) { |
| 392 | TextView text = (TextView) convertView; |
| 393 | if (text == null) { |
| 394 | final int layoutRes = existsInCurrentAccount(position) ? |
| 395 | R.layout.sim_import_list_item_disabled : |
| 396 | R.layout.sim_import_list_item; |
| 397 | text = (TextView) mInflater.inflate(layoutRes, parent, false); |
| 398 | } |
| 399 | text.setText(getItemLabel(getItem(position))); |
| 400 | |
| 401 | return text; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 404 | public void setData(LoaderResult result) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 405 | clear(); |
| 406 | addAll(result.contacts); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 407 | mExistingMap = result.accountsMap; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 410 | public void setAccount(AccountWithDataSet account) { |
Marcus Hagerott | 48a2936 | 2016-11-14 10:09:46 -0800 | [diff] [blame] | 411 | mSelectedAccount = account; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 412 | notifyDataSetChanged(); |
| 413 | } |
| 414 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 415 | public AccountWithDataSet getAccount() { |
| 416 | return mSelectedAccount; |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 417 | } |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 418 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 419 | public boolean existsInCurrentAccount(int position) { |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 420 | return existsInCurrentAccount(getItem(position)); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | public boolean existsInCurrentAccount(SimContact contact) { |
| 424 | if (mSelectedAccount == null || !mExistingMap.containsKey(mSelectedAccount)) { |
| 425 | return false; |
| 426 | } |
| 427 | return mExistingMap.get(mSelectedAccount).contains(contact); |
| 428 | } |
| 429 | |
Marcus Hagerott | e127579 | 2016-11-17 10:41:12 -0800 | [diff] [blame] | 430 | private String getItemLabel(SimContact contact) { |
| 431 | if (contact.hasName()) { |
| 432 | return contact.getName(); |
| 433 | } else if (contact.hasPhone()) { |
| 434 | return contact.getPhone(); |
| 435 | } else if (contact.hasEmails()) { |
| 436 | return contact.getEmails()[0]; |
| 437 | } else { |
| 438 | // This isn't really possible because we skip empty SIM contacts during loading |
| 439 | return ""; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 440 | } |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 441 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 444 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 445 | private static class SimContactLoader extends ListenableFutureLoader<LoaderResult> { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 446 | private SimContactDao mDao; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 447 | private AccountTypeManager mAccountTypeManager; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 448 | private final int mSubscriptionId; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 449 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 450 | public SimContactLoader(Context context, int subscriptionId) { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 451 | super(context, new IntentFilter(AccountTypeManager.BROADCAST_ACCOUNTS_CHANGED)); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 452 | mDao = SimContactDao.create(context); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 453 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 454 | mSubscriptionId = subscriptionId; |
| 455 | } |
| 456 | |
| 457 | @Override |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 458 | protected ListenableFuture<LoaderResult> loadData() { |
| 459 | final ListenableFuture<List<Object>> future = Futures.<Object>allAsList( |
| 460 | mAccountTypeManager |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 461 | .filterAccountsAsync(AccountTypeManager.writableFilter()), |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 462 | ContactsExecutors.getSimReadExecutor().<Object>submit( |
| 463 | new Callable<Object>() { |
| 464 | @Override |
| 465 | public LoaderResult call() throws Exception { |
| 466 | return loadFromSim(); |
| 467 | } |
| 468 | })); |
| 469 | return Futures.transform(future, new Function<List<Object>, LoaderResult>() { |
| 470 | @Override |
| 471 | public LoaderResult apply(List<Object> input) { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 472 | final List<AccountInfo> accounts = (List<AccountInfo>) input.get(0); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 473 | final LoaderResult simLoadResult = (LoaderResult) input.get(1); |
| 474 | simLoadResult.accounts = accounts; |
| 475 | return simLoadResult; |
| 476 | } |
| 477 | }); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 480 | private LoaderResult loadFromSim() { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 481 | final SimCard sim = mDao.getSimBySubscriptionId(mSubscriptionId); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 482 | LoaderResult result = new LoaderResult(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 483 | if (sim == null) { |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 484 | result.contacts = new ArrayList<>(); |
| 485 | result.accountsMap = Collections.emptyMap(); |
| 486 | return result; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 487 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 488 | result.contacts = mDao.loadContactsForSim(sim); |
| 489 | result.accountsMap = mDao.findAccountsOfExistingSimContacts(result.contacts); |
| 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 | } |
| 493 | |
| 494 | public static class LoaderResult { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 495 | public List<AccountInfo> accounts; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 496 | public ArrayList<SimContact> contacts; |
| 497 | public Map<AccountWithDataSet, Set<SimContact>> accountsMap; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 498 | } |
| 499 | } |