2017-10-16 20:11:42 +00:00
|
|
|
/*
|
2015-07-14 21:31:03 +00:00
|
|
|
* Copyright (C) 2015 Open Whisper Systems
|
2014-01-19 02:17:08 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.SuppressLint;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.database.Cursor;
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.os.AsyncTask;
|
2015-07-17 05:36:40 +00:00
|
|
|
import android.os.Build;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.os.Bundle;
|
2015-10-19 18:23:12 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-03-18 06:25:09 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2015-11-03 01:40:41 +00:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.Button;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.widget.TextView;
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.pnikosis.materialishprogress.ProgressWheel;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.components.RecyclerViewFastScroller;
|
2014-03-18 06:25:09 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListAdapter;
|
2015-05-19 21:00:54 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListItem;
|
2015-07-14 21:31:03 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactsCursorLoader;
|
2018-03-28 05:13:27 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactsCursorLoader.DisplayMode;
|
2015-11-03 01:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
2017-01-24 00:44:38 +00:00
|
|
|
import org.thoughtcrime.securesms.util.StickyHeaderDecoration;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2015-11-03 01:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import java.io.IOException;
|
2014-01-19 02:17:08 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-11-16 23:21:46 +00:00
|
|
|
import java.util.Set;
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
/**
|
2014-03-18 06:25:09 +00:00
|
|
|
* Fragment for selecting a one or more contacts from a list.
|
2014-01-19 02:17:08 +00:00
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*
|
|
|
|
*/
|
2015-07-14 21:31:03 +00:00
|
|
|
public class ContactSelectionListFragment extends Fragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>
|
2014-01-19 02:17:08 +00:00
|
|
|
{
|
2017-11-16 23:21:46 +00:00
|
|
|
@SuppressWarnings("unused")
|
2015-07-14 21:31:03 +00:00
|
|
|
private static final String TAG = ContactSelectionListFragment.class.getSimpleName();
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2017-11-16 23:21:46 +00:00
|
|
|
public static final String DISPLAY_MODE = "display_mode";
|
|
|
|
public static final String MULTI_SELECT = "multi_select";
|
|
|
|
public static final String REFRESHABLE = "refreshable";
|
|
|
|
public static final String RECENTS = "recents";
|
2015-10-19 18:23:12 +00:00
|
|
|
|
2018-03-28 05:13:27 +00:00
|
|
|
private TextView emptyText;
|
2017-11-16 23:21:46 +00:00
|
|
|
private Set<String> selectedContacts;
|
2014-03-18 06:25:09 +00:00
|
|
|
private OnContactSelectedListener onContactSelectedListener;
|
2015-07-14 21:31:03 +00:00
|
|
|
private SwipeRefreshLayout swipeRefresh;
|
2017-11-25 06:00:30 +00:00
|
|
|
private View showContactsLayout;
|
|
|
|
private Button showContactsButton;
|
|
|
|
private TextView showContactsDescription;
|
|
|
|
private ProgressWheel showContactsProgress;
|
2014-04-01 23:40:16 +00:00
|
|
|
private String cursorFilter;
|
2015-11-03 01:40:41 +00:00
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private RecyclerViewFastScroller fastScroller;
|
2014-02-24 22:43:38 +00:00
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle icicle) {
|
2017-05-08 01:59:35 +00:00
|
|
|
super.onActivityCreated(icicle);
|
2017-11-25 06:00:30 +00:00
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
initializeCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-11-25 06:00:30 +00:00
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(this)
|
|
|
|
.request(Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS)
|
|
|
|
.ifNecessary()
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
if (!TextSecurePreferences.hasSuccessfullyRetrievedDirectory(getActivity())) {
|
|
|
|
handleContactPermissionGranted();
|
|
|
|
} else {
|
|
|
|
this.getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.onAnyDenied(() -> {
|
|
|
|
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
|
|
|
|
|
|
|
|
if (getActivity().getIntent().getBooleanExtra(RECENTS, false)) {
|
|
|
|
getLoaderManager().initLoader(0, null, ContactSelectionListFragment.this);
|
|
|
|
} else {
|
|
|
|
initializeNoContactsPermission();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.execute();
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-18 06:25:09 +00:00
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2015-07-14 21:31:03 +00:00
|
|
|
View view = inflater.inflate(R.layout.contact_selection_list_fragment, container, false);
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
emptyText = ViewUtil.findById(view, android.R.id.empty);
|
|
|
|
recyclerView = ViewUtil.findById(view, R.id.recycler_view);
|
|
|
|
swipeRefresh = ViewUtil.findById(view, R.id.swipe_refresh);
|
|
|
|
fastScroller = ViewUtil.findById(view, R.id.fast_scroller);
|
|
|
|
showContactsLayout = view.findViewById(R.id.show_contacts_container);
|
|
|
|
showContactsButton = view.findViewById(R.id.show_contacts_button);
|
|
|
|
showContactsDescription = view.findViewById(R.id.show_contacts_description);
|
|
|
|
showContactsProgress = view.findViewById(R.id.progress);
|
2015-11-03 01:40:41 +00:00
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
2015-07-14 21:31:03 +00:00
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) &&
|
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN);
|
2015-07-17 05:36:40 +00:00
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
return view;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
public @NonNull List<String> getSelectedContacts() {
|
2015-05-19 21:00:54 +00:00
|
|
|
List<String> selected = new LinkedList<>();
|
2015-10-19 18:23:12 +00:00
|
|
|
if (selectedContacts != null) {
|
2017-11-16 23:21:46 +00:00
|
|
|
selected.addAll(selectedContacts);
|
2015-10-19 18:23:12 +00:00
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2014-02-03 19:52:27 +00:00
|
|
|
return selected;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
private boolean isMulti() {
|
|
|
|
return getActivity().getIntent().getBooleanExtra(MULTI_SELECT, false);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeCursor() {
|
2015-11-03 01:40:41 +00:00
|
|
|
ContactSelectionListAdapter adapter = new ContactSelectionListAdapter(getActivity(),
|
2017-10-16 20:11:42 +00:00
|
|
|
GlideApp.with(this),
|
2015-11-03 01:40:41 +00:00
|
|
|
null,
|
|
|
|
new ListClickListener(),
|
|
|
|
isMulti());
|
2014-03-18 06:25:09 +00:00
|
|
|
selectedContacts = adapter.getSelectedContacts();
|
2015-11-03 01:40:41 +00:00
|
|
|
recyclerView.setAdapter(adapter);
|
2017-01-26 00:38:36 +00:00
|
|
|
recyclerView.addItemDecoration(new StickyHeaderDecoration(adapter, true, true));
|
2017-11-25 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeNoContactsPermission() {
|
|
|
|
swipeRefresh.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
showContactsLayout.setVisibility(View.VISIBLE);
|
|
|
|
showContactsProgress.setVisibility(View.INVISIBLE);
|
|
|
|
showContactsDescription.setText(R.string.contact_selection_list_fragment__signal_needs_access_to_your_contacts_in_order_to_display_them);
|
|
|
|
showContactsButton.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
showContactsButton.setOnClickListener(v -> {
|
|
|
|
Permissions.with(this)
|
|
|
|
.request(Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(getString(R.string.ContactSelectionListFragment_signal_requires_the_contacts_permission_in_order_to_display_your_contacts))
|
|
|
|
.onSomeGranted(permissions -> {
|
|
|
|
if (permissions.contains(Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
handleContactPermissionGranted();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
});
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void setQueryFilter(String filter) {
|
|
|
|
this.cursorFilter = filter;
|
|
|
|
this.getLoaderManager().restartLoader(0, null, this);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void resetQueryFilter() {
|
|
|
|
setQueryFilter(null);
|
|
|
|
swipeRefresh.setRefreshing(false);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 23:25:39 +00:00
|
|
|
public void setRefreshing(boolean refreshing) {
|
|
|
|
swipeRefresh.setRefreshing(refreshing);
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:23:12 +00:00
|
|
|
public void reset() {
|
|
|
|
selectedContacts.clear();
|
2017-12-20 19:10:12 +00:00
|
|
|
|
|
|
|
if (!isDetached() && !isRemoving() && getActivity() != null && !getActivity().isFinishing()) {
|
|
|
|
getLoaderManager().restartLoader(0, null, this);
|
|
|
|
}
|
2015-10-19 18:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2018-01-25 03:17:44 +00:00
|
|
|
return new ContactsCursorLoader(getActivity(),
|
2018-03-28 05:13:27 +00:00
|
|
|
getActivity().getIntent().getIntExtra(DISPLAY_MODE, DisplayMode.FLAG_ALL),
|
2017-11-16 23:21:46 +00:00
|
|
|
cursorFilter, getActivity().getIntent().getBooleanExtra(RECENTS, false));
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
2017-11-25 06:00:30 +00:00
|
|
|
swipeRefresh.setVisibility(View.VISIBLE);
|
|
|
|
showContactsLayout.setVisibility(View.GONE);
|
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(data);
|
2014-03-18 06:25:09 +00:00
|
|
|
emptyText.setText(R.string.contact_selection_group_activity__no_contacts);
|
2016-09-07 19:26:34 +00:00
|
|
|
boolean useFastScroller = (recyclerView.getAdapter().getItemCount() > 20);
|
|
|
|
recyclerView.setVerticalScrollBarEnabled(!useFastScroller);
|
|
|
|
if (useFastScroller) {
|
2015-11-23 18:46:01 +00:00
|
|
|
fastScroller.setVisibility(View.VISIBLE);
|
|
|
|
fastScroller.setRecyclerView(recyclerView);
|
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
2015-11-03 01:40:41 +00:00
|
|
|
((CursorRecyclerViewAdapter) recyclerView.getAdapter()).changeCursor(null);
|
2015-11-23 18:46:01 +00:00
|
|
|
fastScroller.setVisibility(View.GONE);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
private void handleContactPermissionGranted() {
|
|
|
|
new AsyncTask<Void, Void, Boolean>() {
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
swipeRefresh.setVisibility(View.GONE);
|
|
|
|
showContactsLayout.setVisibility(View.VISIBLE);
|
|
|
|
showContactsButton.setVisibility(View.INVISIBLE);
|
2017-11-27 19:14:09 +00:00
|
|
|
showContactsDescription.setText(R.string.ConversationListFragment_loading);
|
2017-11-25 06:00:30 +00:00
|
|
|
showContactsProgress.setVisibility(View.VISIBLE);
|
|
|
|
showContactsProgress.spin();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... voids) {
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
DirectoryHelper.refreshDirectory(getContext(), false);
|
2017-11-25 06:00:30 +00:00
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean result) {
|
|
|
|
if (result) {
|
|
|
|
showContactsLayout.setVisibility(View.GONE);
|
|
|
|
swipeRefresh.setVisibility(View.VISIBLE);
|
|
|
|
reset();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getContext(), R.string.ContactSelectionListFragment_error_retrieving_contacts_check_your_network_connection, Toast.LENGTH_LONG).show();
|
|
|
|
initializeNoContactsPermission();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:40:41 +00:00
|
|
|
private class ListClickListener implements ContactSelectionListAdapter.ItemClickListener {
|
2014-03-18 06:25:09 +00:00
|
|
|
@Override
|
2015-11-03 01:40:41 +00:00
|
|
|
public void onItemClick(ContactSelectionListItem contact) {
|
2017-11-16 23:21:46 +00:00
|
|
|
if (!isMulti() || !selectedContacts.contains(contact.getNumber())) {
|
|
|
|
selectedContacts.add(contact.getNumber());
|
2015-05-19 21:00:54 +00:00
|
|
|
contact.setChecked(true);
|
|
|
|
if (onContactSelectedListener != null) onContactSelectedListener.onContactSelected(contact.getNumber());
|
|
|
|
} else {
|
2017-11-16 23:21:46 +00:00
|
|
|
selectedContacts.remove(contact.getNumber());
|
2015-05-19 21:00:54 +00:00
|
|
|
contact.setChecked(false);
|
2015-10-19 18:23:12 +00:00
|
|
|
if (onContactSelectedListener != null) onContactSelectedListener.onContactDeselected(contact.getNumber());
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
|
|
|
|
this.onContactSelectedListener = onContactSelectedListener;
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:31:03 +00:00
|
|
|
public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener) {
|
|
|
|
this.swipeRefresh.setOnRefreshListener(onRefreshListener);
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public interface OnContactSelectedListener {
|
2015-10-19 18:23:12 +00:00
|
|
|
void onContactSelected(String number);
|
|
|
|
void onContactDeselected(String number);
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
2015-11-03 01:40:41 +00:00
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|