2017-09-25 08:32:45 -07:00
|
|
|
/*
|
2015-01-18 16:11:30 -10:00
|
|
|
* Copyright (C) 2015 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.content.res.Configuration;
|
2018-03-15 11:17:40 -07:00
|
|
|
import android.content.res.Resources;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.database.Cursor;
|
2018-03-15 11:17:40 -07:00
|
|
|
import android.os.Build;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.os.Bundle;
|
2020-08-31 23:16:15 +10:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-08-19 10:06:26 +10:00
|
|
|
import androidx.annotation.NonNull;
|
2020-08-31 23:16:15 +10:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.appcompat.view.ActionMode;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2020-08-19 10:06:26 +10:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
|
|
|
import androidx.loader.app.LoaderManager;
|
|
|
|
import androidx.loader.content.Loader;
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2020-08-31 23:16:15 +10:00
|
|
|
import androidx.viewpager.widget.ViewPager;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-09-23 21:30:00 -07:00
|
|
|
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
|
2020-08-31 23:16:15 +10:00
|
|
|
import com.google.android.material.tabs.TabLayout;
|
2017-09-23 21:30:00 -07:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2017-09-25 08:32:45 -07:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
2018-03-15 11:17:40 -07:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase;
|
2017-09-23 21:30:00 -07:00
|
|
|
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader.BucketedThreadMedia;
|
2017-09-25 08:32:45 -07:00
|
|
|
import org.thoughtcrime.securesms.database.loaders.ThreadMediaLoader;
|
2017-10-16 13:11:42 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2018-04-06 15:01:05 +02:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2018-03-15 11:17:40 -07:00
|
|
|
import org.thoughtcrime.securesms.util.AttachmentUtil;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
2018-04-06 15:01:05 +02:00
|
|
|
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
|
2017-09-25 08:32:45 -07:00
|
|
|
import org.thoughtcrime.securesms.util.StickyHeaderDecoration;
|
2018-12-01 10:00:35 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2017-09-23 21:30:00 -07:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2018-03-15 11:17:40 -07:00
|
|
|
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
2015-08-22 13:03:07 +02:00
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
import java.util.Collection;
|
2018-04-06 15:01:05 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-09-25 08:32:45 -07:00
|
|
|
import java.util.Locale;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2019-07-24 12:30:23 +10:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2015-01-18 16:11:30 -10:00
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
2018-09-27 09:47:08 -07:00
|
|
|
public class MediaOverviewActivity extends PassphraseRequiredActionBarActivity {
|
2017-12-05 12:07:37 -08:00
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
2015-01-18 16:11:30 -10:00
|
|
|
private final static String TAG = MediaOverviewActivity.class.getSimpleName();
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
public static final String ADDRESS_EXTRA = "address";
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
private Toolbar toolbar;
|
|
|
|
private TabLayout tabLayout;
|
|
|
|
private ViewPager viewPager;
|
|
|
|
private Recipient recipient;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 12:25:55 -08:00
|
|
|
protected void onPreCreate() {
|
2015-01-18 16:11:30 -10:00
|
|
|
dynamicLanguage.onCreate(this);
|
2014-12-15 12:25:55 -08:00
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2014-12-15 12:25:55 -08:00
|
|
|
@Override
|
2018-02-01 19:22:48 -08:00
|
|
|
protected void onCreate(Bundle bundle, boolean ready) {
|
2015-01-18 16:11:30 -10:00
|
|
|
setContentView(R.layout.media_overview_activity);
|
|
|
|
|
|
|
|
initializeResources();
|
2017-09-25 08:32:45 -07:00
|
|
|
initializeToolbar();
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
this.tabLayout.setupWithViewPager(viewPager);
|
|
|
|
this.viewPager.setAdapter(new MediaOverviewPagerAdapter(getSupportFragmentManager()));
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: finish(); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
2017-08-01 08:56:00 -07:00
|
|
|
Address address = getIntent().getParcelableExtra(ADDRESS_EXTRA);
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
this.viewPager = ViewUtil.findById(this, R.id.pager);
|
|
|
|
this.toolbar = ViewUtil.findById(this, R.id.toolbar);
|
|
|
|
this.tabLayout = ViewUtil.findById(this, R.id.tab_layout);
|
2017-09-23 21:30:00 -07:00
|
|
|
this.recipient = Recipient.from(this, address, true);
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
private void initializeToolbar() {
|
|
|
|
setSupportActionBar(this.toolbar);
|
|
|
|
getSupportActionBar().setTitle(recipient.toShortString());
|
2018-12-01 10:00:35 -08:00
|
|
|
this.recipient.addListener(recipient -> {
|
|
|
|
Util.runOnMain(() -> getSupportActionBar().setTitle(recipient.toShortString()));
|
|
|
|
});
|
2015-08-22 13:03:07 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 09:47:08 -07:00
|
|
|
public void onEnterMultiSelect() {
|
|
|
|
tabLayout.setEnabled(false);
|
|
|
|
viewPager.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onExitMultiSelect() {
|
|
|
|
tabLayout.setEnabled(true);
|
|
|
|
viewPager.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
private class MediaOverviewPagerAdapter extends FragmentStatePagerAdapter {
|
2015-08-22 13:03:07 +02:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
MediaOverviewPagerAdapter(FragmentManager fragmentManager) {
|
|
|
|
super(fragmentManager);
|
2015-08-22 13:03:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
|
|
|
Fragment fragment;
|
2015-08-22 13:03:07 +02:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
if (position == 0) fragment = new MediaOverviewGalleryFragment();
|
|
|
|
else if (position == 1) fragment = new MediaOverviewDocumentsFragment();
|
|
|
|
else throw new AssertionError();
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString(MediaOverviewGalleryFragment.ADDRESS_EXTRA, recipient.getAddress().serialize());
|
|
|
|
args.putSerializable(MediaOverviewGalleryFragment.LOCALE_EXTRA, dynamicLanguage.getCurrentLocale());
|
|
|
|
|
|
|
|
fragment.setArguments(args);
|
|
|
|
|
|
|
|
return fragment;
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence getPageTitle(int position) {
|
|
|
|
if (position == 0) return getString(R.string.MediaOverviewActivity_Media);
|
|
|
|
else if (position == 1) return getString(R.string.MediaOverviewActivity_Documents);
|
|
|
|
else throw new AssertionError();
|
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
public static abstract class MediaOverviewFragment<T> extends Fragment implements LoaderManager.LoaderCallbacks<T> {
|
|
|
|
|
2018-02-01 19:22:48 -08:00
|
|
|
public static final String ADDRESS_EXTRA = "address";
|
|
|
|
public static final String LOCALE_EXTRA = "locale_extra";
|
2017-09-25 08:32:45 -07:00
|
|
|
|
|
|
|
protected TextView noMedia;
|
|
|
|
protected Recipient recipient;
|
|
|
|
protected RecyclerView recyclerView;
|
|
|
|
protected Locale locale;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle bundle) {
|
|
|
|
super.onCreate(bundle);
|
|
|
|
|
|
|
|
String address = getArguments().getString(ADDRESS_EXTRA);
|
|
|
|
Locale locale = (Locale)getArguments().getSerializable(LOCALE_EXTRA);
|
|
|
|
|
|
|
|
if (address == null) throw new AssertionError();
|
|
|
|
if (locale == null) throw new AssertionError();
|
|
|
|
|
|
|
|
this.recipient = Recipient.from(getContext(), Address.fromSerialized(address), true);
|
|
|
|
this.locale = locale;
|
|
|
|
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
public static class MediaOverviewGalleryFragment
|
|
|
|
extends MediaOverviewFragment<BucketedThreadMedia>
|
|
|
|
implements MediaGalleryAdapter.ItemClickListener
|
|
|
|
{
|
2017-09-25 08:32:45 -07:00
|
|
|
|
|
|
|
private StickyHeaderGridLayoutManager gridManager;
|
2018-03-15 11:17:40 -07:00
|
|
|
private ActionMode actionMode;
|
|
|
|
private ActionModeCallback actionModeCallback = new ActionModeCallback();
|
2017-09-23 21:30:00 -07:00
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
@Override
|
2017-12-05 12:07:37 -08:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2017-09-25 08:32:45 -07:00
|
|
|
View view = inflater.inflate(R.layout.media_overview_gallery_fragment, container, false);
|
|
|
|
|
|
|
|
this.recyclerView = ViewUtil.findById(view, R.id.media_grid);
|
|
|
|
this.noMedia = ViewUtil.findById(view, R.id.no_images);
|
|
|
|
this.gridManager = new StickyHeaderGridLayoutManager(getResources().getInteger(R.integer.media_overview_cols));
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
this.recyclerView.setAdapter(new MediaGalleryAdapter(getContext(),
|
|
|
|
GlideApp.with(this),
|
|
|
|
new BucketedThreadMedia(getContext()),
|
|
|
|
locale,
|
|
|
|
this));
|
2017-09-25 08:32:45 -07:00
|
|
|
this.recyclerView.setLayoutManager(gridManager);
|
|
|
|
this.recyclerView.setHasFixedSize(true);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
if (gridManager != null) {
|
|
|
|
this.gridManager = new StickyHeaderGridLayoutManager(getResources().getInteger(R.integer.media_overview_cols));
|
|
|
|
this.recyclerView.setLayoutManager(gridManager);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public @NonNull Loader<BucketedThreadMedia> onCreateLoader(int i, Bundle bundle) {
|
2018-01-24 19:17:44 -08:00
|
|
|
return new BucketedThreadMediaLoader(getContext(), recipient.getAddress());
|
2017-09-25 08:32:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public void onLoadFinished(@NonNull Loader<BucketedThreadMedia> loader, BucketedThreadMedia bucketedThreadMedia) {
|
2017-09-25 08:32:45 -07:00
|
|
|
((MediaGalleryAdapter) recyclerView.getAdapter()).setMedia(bucketedThreadMedia);
|
|
|
|
((MediaGalleryAdapter) recyclerView.getAdapter()).notifyAllSectionsDataSetChanged();
|
|
|
|
|
|
|
|
noMedia.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public void onLoaderReset(@NonNull Loader<BucketedThreadMedia> cursorLoader) {
|
2017-09-25 08:32:45 -07:00
|
|
|
((MediaGalleryAdapter) recyclerView.getAdapter()).setMedia(new BucketedThreadMedia(getContext()));
|
|
|
|
}
|
2018-03-15 11:17:40 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMediaClicked(@NonNull MediaDatabase.MediaRecord mediaRecord) {
|
|
|
|
if (actionMode != null) {
|
|
|
|
handleMediaMultiSelectClick(mediaRecord);
|
|
|
|
} else {
|
|
|
|
handleMediaPreviewClick(mediaRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleMediaMultiSelectClick(@NonNull MediaDatabase.MediaRecord mediaRecord) {
|
|
|
|
MediaGalleryAdapter adapter = getListAdapter();
|
|
|
|
|
|
|
|
adapter.toggleSelection(mediaRecord);
|
|
|
|
if (adapter.getSelectedMediaCount() == 0) {
|
2018-10-01 23:38:07 -07:00
|
|
|
actionMode.finish();
|
2018-03-15 11:17:40 -07:00
|
|
|
} else {
|
|
|
|
actionMode.setTitle(String.valueOf(adapter.getSelectedMediaCount()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleMediaPreviewClick(@NonNull MediaDatabase.MediaRecord mediaRecord) {
|
|
|
|
if (mediaRecord.getAttachment().getDataUri() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context context = getContext();
|
|
|
|
if (context == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, MediaPreviewActivity.class);
|
|
|
|
intent.putExtra(MediaPreviewActivity.DATE_EXTRA, mediaRecord.getDate());
|
|
|
|
intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, mediaRecord.getAttachment().getSize());
|
|
|
|
intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, recipient.getAddress());
|
|
|
|
intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, mediaRecord.isOutgoing());
|
|
|
|
intent.putExtra(MediaPreviewActivity.LEFT_IS_RECENT_EXTRA, true);
|
|
|
|
|
|
|
|
intent.setDataAndType(mediaRecord.getAttachment().getDataUri(), mediaRecord.getContentType());
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMediaLongClicked(MediaDatabase.MediaRecord mediaRecord) {
|
|
|
|
if (actionMode == null) {
|
|
|
|
((MediaGalleryAdapter) recyclerView.getAdapter()).toggleSelection(mediaRecord);
|
|
|
|
recyclerView.getAdapter().notifyDataSetChanged();
|
|
|
|
|
2018-09-27 09:47:08 -07:00
|
|
|
enterMultiSelect();
|
2018-03-15 11:17:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:01:05 +02:00
|
|
|
@SuppressWarnings("CodeBlock2Expr")
|
2020-09-10 16:32:13 +10:00
|
|
|
@SuppressLint({"InlinedApi", "StaticFieldLeak"})
|
2018-04-06 15:01:05 +02:00
|
|
|
private void handleSaveMedia(@NonNull Collection<MediaDatabase.MediaRecord> mediaRecords) {
|
|
|
|
final Context context = getContext();
|
2020-09-10 16:32:13 +10:00
|
|
|
|
2018-04-06 15:01:05 +02:00
|
|
|
SaveAttachmentTask.showWarningDialog(context, (dialogInterface, which) -> {
|
|
|
|
Permissions.with(this)
|
2020-09-10 16:32:13 +10:00
|
|
|
.request(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
.maxSdkVersion(Build.VERSION_CODES.P)
|
|
|
|
.withPermanentDenialDialog(getString(R.string.MediaPreviewActivity_signal_needs_the_storage_permission_in_order_to_write_to_external_storage_but_it_has_been_permanently_denied))
|
|
|
|
.onAnyDenied(() -> Toast.makeText(getContext(), R.string.MediaPreviewActivity_unable_to_write_to_external_storage_without_permission, Toast.LENGTH_LONG).show())
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
new ProgressDialogAsyncTask<Void, Void, List<SaveAttachmentTask.Attachment>>(
|
|
|
|
context,
|
|
|
|
R.string.MediaOverviewActivity_collecting_attachments,
|
|
|
|
R.string.please_wait) {
|
|
|
|
@Override
|
|
|
|
protected List<SaveAttachmentTask.Attachment> doInBackground(Void... params) {
|
|
|
|
List<SaveAttachmentTask.Attachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
for (MediaDatabase.MediaRecord mediaRecord : mediaRecords) {
|
|
|
|
if (mediaRecord.getAttachment().getDataUri() != null) {
|
|
|
|
attachments.add(new SaveAttachmentTask.Attachment(mediaRecord.getAttachment().getDataUri(),
|
|
|
|
mediaRecord.getContentType(),
|
|
|
|
mediaRecord.getDate(),
|
|
|
|
mediaRecord.getAttachment().getFileName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<SaveAttachmentTask.Attachment> attachments) {
|
|
|
|
super.onPostExecute(attachments);
|
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(context, attachments.size());
|
|
|
|
saveTask.executeOnExecutor(THREAD_POOL_EXECUTOR,
|
|
|
|
attachments.toArray(new SaveAttachmentTask.Attachment[attachments.size()]));
|
|
|
|
actionMode.finish();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
})
|
|
|
|
.execute();
|
2018-04-06 15:01:05 +02:00
|
|
|
}, mediaRecords.size());
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
private void handleDeleteMedia(@NonNull Collection<MediaDatabase.MediaRecord> mediaRecords) {
|
|
|
|
int recordCount = mediaRecords.size();
|
|
|
|
Resources res = getContext().getResources();
|
|
|
|
String confirmTitle = res.getQuantityString(R.plurals.MediaOverviewActivity_Media_delete_confirm_title,
|
|
|
|
recordCount,
|
|
|
|
recordCount);
|
|
|
|
String confirmMessage = res.getQuantityString(R.plurals.MediaOverviewActivity_Media_delete_confirm_message,
|
|
|
|
recordCount,
|
|
|
|
recordCount);
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
|
|
|
builder.setIconAttribute(R.attr.dialog_alert_icon);
|
|
|
|
builder.setTitle(confirmTitle);
|
|
|
|
builder.setMessage(confirmMessage);
|
|
|
|
builder.setCancelable(true);
|
|
|
|
|
|
|
|
builder.setPositiveButton(R.string.delete, (dialogInterface, i) -> {
|
|
|
|
new ProgressDialogAsyncTask<MediaDatabase.MediaRecord, Void, Void>(getContext(),
|
|
|
|
R.string.MediaOverviewActivity_Media_delete_progress_title,
|
|
|
|
R.string.MediaOverviewActivity_Media_delete_progress_message)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(MediaDatabase.MediaRecord... records) {
|
|
|
|
if (records == null || records.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MediaDatabase.MediaRecord record : records) {
|
|
|
|
AttachmentUtil.deleteAttachment(getContext(), record.getAttachment());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}.execute(mediaRecords.toArray(new MediaDatabase.MediaRecord[mediaRecords.size()]));
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
2018-04-06 21:29:52 +02:00
|
|
|
private void handleSelectAllMedia() {
|
|
|
|
getListAdapter().selectAllMedia();
|
|
|
|
actionMode.setTitle(String.valueOf(getListAdapter().getSelectedMediaCount()));
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
private MediaGalleryAdapter getListAdapter() {
|
|
|
|
return (MediaGalleryAdapter) recyclerView.getAdapter();
|
|
|
|
}
|
|
|
|
|
2018-09-27 09:47:08 -07:00
|
|
|
private void enterMultiSelect() {
|
|
|
|
actionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(actionModeCallback);
|
|
|
|
((MediaOverviewActivity) getActivity()).onEnterMultiSelect();
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
private class ActionModeCallback implements ActionMode.Callback {
|
|
|
|
|
|
|
|
private int originalStatusBarColor;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
mode.getMenuInflater().inflate(R.menu.media_overview_context, menu);
|
|
|
|
mode.setTitle("1");
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
Window window = getActivity().getWindow();
|
|
|
|
originalStatusBarColor = window.getStatusBarColor();
|
|
|
|
window.setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem menuItem) {
|
|
|
|
switch (menuItem.getItemId()) {
|
2018-04-06 15:01:05 +02:00
|
|
|
case R.id.save:
|
|
|
|
handleSaveMedia(getListAdapter().getSelectedMedia());
|
|
|
|
return true;
|
2018-03-15 11:17:40 -07:00
|
|
|
case R.id.delete:
|
|
|
|
handleDeleteMedia(getListAdapter().getSelectedMedia());
|
2018-10-01 23:38:07 -07:00
|
|
|
actionMode.finish();
|
2018-04-06 21:29:52 +02:00
|
|
|
return true;
|
|
|
|
case R.id.select_all:
|
|
|
|
handleSelectAllMedia();
|
2018-03-15 11:17:40 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
actionMode = null;
|
|
|
|
getListAdapter().clearSelection();
|
2018-10-01 23:38:07 -07:00
|
|
|
((MediaOverviewActivity) getActivity()).onExitMultiSelect();
|
2018-03-15 11:17:40 -07:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
getActivity().getWindow().setStatusBarColor(originalStatusBarColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:45 -07:00
|
|
|
public static class MediaOverviewDocumentsFragment extends MediaOverviewFragment<Cursor> {
|
|
|
|
|
|
|
|
@Override
|
2017-12-05 12:07:37 -08:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2017-09-25 08:32:45 -07:00
|
|
|
View view = inflater.inflate(R.layout.media_overview_documents_fragment, container, false);
|
2018-01-24 19:17:44 -08:00
|
|
|
MediaDocumentsAdapter adapter = new MediaDocumentsAdapter(getContext(), null, locale);
|
2017-09-25 08:32:45 -07:00
|
|
|
|
|
|
|
this.recyclerView = ViewUtil.findById(view, R.id.recycler_view);
|
|
|
|
this.noMedia = ViewUtil.findById(view, R.id.no_documents);
|
|
|
|
|
|
|
|
this.recyclerView.setAdapter(adapter);
|
|
|
|
this.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
|
|
|
|
this.recyclerView.addItemDecoration(new StickyHeaderDecoration(adapter, false, true));
|
|
|
|
this.recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public @NonNull Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2018-02-01 19:22:48 -08:00
|
|
|
return new ThreadMediaLoader(getContext(), recipient.getAddress(), false);
|
2017-09-25 08:32:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
|
2017-09-25 08:32:45 -07:00
|
|
|
((CursorRecyclerViewAdapter)this.recyclerView.getAdapter()).changeCursor(data);
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
|
|
|
|
this.noMedia.setVisibility(data.getCount() > 0 ? View.GONE : View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
|
2017-09-25 08:32:45 -07:00
|
|
|
((CursorRecyclerViewAdapter)this.recyclerView.getAdapter()).changeCursor(null);
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
}
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
}
|