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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-08-22 13:03:07 +02:00
|
|
|
import android.content.DialogInterface;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.Bundle;
|
2014-12-15 12:25:55 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2015-08-22 13:03:07 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2015-01-18 16:11:30 -10:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2017-09-23 21:30:00 -07:00
|
|
|
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
|
|
|
|
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-02-01 03:49:19 +01:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase.MediaRecord;
|
2017-09-23 21:30:00 -07:00
|
|
|
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader.BucketedThreadMedia;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-01-18 16:11:30 -10:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
2017-09-23 21:30:00 -07:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2015-08-22 13:03:07 +02:00
|
|
|
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
|
2017-09-23 21:30:00 -07:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2015-08-22 13:03:07 +02:00
|
|
|
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
2017-09-23 21:30:00 -07:00
|
|
|
public class MediaOverviewActivity extends PassphraseRequiredActionBarActivity implements LoaderManager.LoaderCallbacks<BucketedThreadMedia> {
|
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
|
|
|
|
2017-09-23 21:30:00 -07:00
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
2015-01-18 16:11:30 -10:00
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
|
|
|
private RecyclerView gridView;
|
2017-09-23 21:30:00 -07:00
|
|
|
private StickyHeaderGridLayoutManager gridManager;
|
2015-01-18 16:11:30 -10:00
|
|
|
private TextView noImages;
|
2017-08-01 08:56:00 -07:00
|
|
|
private Recipient recipient;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 12:25:55 -08:00
|
|
|
protected void onPreCreate() {
|
2017-09-23 21:30:00 -07:00
|
|
|
dynamicTheme.onCreate(this);
|
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
|
|
|
|
protected void onCreate(Bundle bundle, @NonNull MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
2015-01-18 16:11:30 -10:00
|
|
|
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
setContentView(R.layout.media_overview_activity);
|
|
|
|
|
|
|
|
initializeResources();
|
|
|
|
initializeActionBar();
|
2017-09-23 21:30:00 -07:00
|
|
|
|
2015-01-18 16:11:30 -10:00
|
|
|
getSupportLoaderManager().initLoader(0, null, MediaOverviewActivity.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
2017-09-23 21:30:00 -07:00
|
|
|
if (gridManager != null) {
|
|
|
|
this.gridManager = new StickyHeaderGridLayoutManager(getResources().getInteger(R.integer.media_overview_cols));
|
|
|
|
this.gridView.setLayoutManager(gridManager);
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2017-09-23 21:30:00 -07:00
|
|
|
dynamicTheme.onResume(this);
|
2015-01-18 16:11:30 -10:00
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeActionBar() {
|
2017-09-23 21:30:00 -07:00
|
|
|
getSupportActionBar().setTitle(recipient.toShortString());
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
2017-09-23 21:30:00 -07:00
|
|
|
this.noImages = ViewUtil.findById(this, R.id.no_images);
|
|
|
|
this.gridView = ViewUtil.findById(this, R.id.media_grid);
|
|
|
|
this.gridManager = new StickyHeaderGridLayoutManager(getResources().getInteger(R.integer.media_overview_cols));
|
2015-01-18 16:11:30 -10:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
Address address = getIntent().getParcelableExtra(ADDRESS_EXTRA);
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2017-09-23 21:30:00 -07:00
|
|
|
this.recipient = Recipient.from(this, address, true);
|
|
|
|
this.recipient.addListener(recipient -> initializeActionBar());
|
2016-09-20 18:53:44 +02:00
|
|
|
|
2017-09-23 21:30:00 -07:00
|
|
|
this.gridView.setAdapter(new MediaAdapter(this, masterSecret, new BucketedThreadMedia(this), dynamicLanguage.getCurrentLocale(), address));
|
|
|
|
this.gridView.setLayoutManager(gridManager);
|
|
|
|
this.gridView.setHasFixedSize(true);
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
2015-08-22 13:03:07 +02:00
|
|
|
private void saveToDisk() {
|
|
|
|
final Context c = this;
|
|
|
|
|
|
|
|
SaveAttachmentTask.showWarningDialog(this, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
new ProgressDialogAsyncTask<Void, Void, List<SaveAttachmentTask.Attachment>>(c,
|
|
|
|
R.string.ConversationFragment_collecting_attahments,
|
|
|
|
R.string.please_wait) {
|
|
|
|
@Override
|
|
|
|
protected List<SaveAttachmentTask.Attachment> doInBackground(Void... params) {
|
2017-09-20 18:14:28 -07:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(c).getThreadIdFor(recipient);
|
2017-02-01 03:49:19 +01:00
|
|
|
Cursor cursor = DatabaseFactory.getMediaDatabase(c).getMediaForThread(threadId);
|
2015-08-22 13:03:07 +02:00
|
|
|
List<SaveAttachmentTask.Attachment> attachments = new ArrayList<>(cursor.getCount());
|
|
|
|
|
2017-09-20 18:14:28 -07:00
|
|
|
while (cursor.moveToNext()) {
|
2017-03-28 12:05:30 -07:00
|
|
|
MediaRecord record = MediaRecord.from(c, masterSecret, cursor);
|
2015-08-22 13:03:07 +02:00
|
|
|
attachments.add(new SaveAttachmentTask.Attachment(record.getAttachment().getDataUri(),
|
|
|
|
record.getContentType(),
|
2017-03-28 12:05:30 -07:00
|
|
|
record.getDate(),
|
|
|
|
null));
|
2015-08-22 13:03:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 18:14:28 -07:00
|
|
|
cursor.close();
|
|
|
|
|
2015-08-22 13:03:07 +02:00
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<SaveAttachmentTask.Attachment> attachments) {
|
|
|
|
super.onPostExecute(attachments);
|
|
|
|
|
2017-03-28 12:05:30 -07:00
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(c, masterSecret, gridView, attachments.size());
|
2015-08-22 13:03:07 +02:00
|
|
|
saveTask.execute(attachments.toArray(new SaveAttachmentTask.Attachment[attachments.size()]));
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
}, gridView.getAdapter().getItemCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
|
|
|
|
menu.clear();
|
|
|
|
if (gridView.getAdapter() != null && gridView.getAdapter().getItemCount() > 0) {
|
|
|
|
MenuInflater inflater = this.getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.media_overview, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-18 16:11:30 -10:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
2015-08-22 13:03:07 +02:00
|
|
|
case R.id.save: saveToDisk(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-23 21:30:00 -07:00
|
|
|
public Loader<BucketedThreadMedia> onCreateLoader(int i, Bundle bundle) {
|
|
|
|
return new BucketedThreadMediaLoader(this, masterSecret, recipient.getAddress());
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-23 21:30:00 -07:00
|
|
|
public void onLoadFinished(Loader<BucketedThreadMedia> loader, BucketedThreadMedia bucketedThreadMedia) {
|
|
|
|
((MediaAdapter)gridView.getAdapter()).setMedia(bucketedThreadMedia);
|
|
|
|
((MediaAdapter)gridView.getAdapter()).notifyAllSectionsDataSetChanged();
|
|
|
|
|
2015-01-18 16:11:30 -10:00
|
|
|
noImages.setVisibility(gridView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
|
2015-08-22 13:03:07 +02:00
|
|
|
invalidateOptionsMenu();
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-23 21:30:00 -07:00
|
|
|
public void onLoaderReset(Loader<BucketedThreadMedia> cursorLoader) {
|
|
|
|
((MediaAdapter)gridView.getAdapter()).setMedia(new BucketedThreadMedia(this));
|
2015-01-18 16:11:30 -10:00
|
|
|
}
|
|
|
|
}
|