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