2015-01-19 02:11:30 +00: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.annotation.TargetApi;
|
|
|
|
import android.content.Context;
|
2015-08-22 11:03:07 +00:00
|
|
|
import android.content.DialogInterface;
|
2015-01-19 02:11:30 +00:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
|
|
|
import android.os.Bundle;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-19 02:11:30 +00:00
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.Log;
|
2015-08-22 11:03:07 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2015-01-19 02:11:30 +00:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-02-01 02:49:19 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase.MediaRecord;
|
2017-09-21 01:14:28 +00:00
|
|
|
import org.thoughtcrime.securesms.database.loaders.ThreadMediaLoader;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
2015-08-22 11:03:07 +00:00
|
|
|
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
|
|
|
|
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
|
|
|
public class MediaOverviewActivity extends PassphraseRequiredActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> {
|
|
|
|
private final static String TAG = MediaOverviewActivity.class.getSimpleName();
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public static final String ADDRESS_EXTRA = "address";
|
2015-01-19 02:11:30 +00:00
|
|
|
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
|
|
|
private RecyclerView gridView;
|
|
|
|
private GridLayoutManager gridManager;
|
|
|
|
private TextView noImages;
|
2017-08-01 15:56:00 +00:00
|
|
|
private Recipient recipient;
|
2015-01-19 02:11:30 +00:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 20:25:55 +00:00
|
|
|
protected void onPreCreate() {
|
2015-01-19 02:11:30 +00:00
|
|
|
this.setTheme(R.style.TextSecure_DarkTheme);
|
|
|
|
dynamicLanguage.onCreate(this);
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|
2015-01-19 02:11:30 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle bundle, @NonNull MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
2015-01-19 02:11:30 +00:00
|
|
|
setFullscreenIfPossible();
|
|
|
|
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
setContentView(R.layout.media_overview_activity);
|
|
|
|
|
|
|
|
initializeResources();
|
|
|
|
initializeActionBar();
|
|
|
|
getSupportLoaderManager().initLoader(0, null, MediaOverviewActivity.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
if (gridManager != null) gridManager.setSpanCount(getResources().getInteger(R.integer.media_overview_cols));
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(VERSION_CODES.JELLY_BEAN)
|
|
|
|
private void setFullscreenIfPossible() {
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
|
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
|
|
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
|
|
|
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeActionBar() {
|
2017-08-01 15:56:00 +00:00
|
|
|
getSupportActionBar().setTitle(recipient == null
|
2017-02-01 02:49:19 +00:00
|
|
|
? getString(R.string.AndroidManifest__all_media)
|
2017-08-01 15:56:00 +00:00
|
|
|
: getString(R.string.AndroidManifest__all_media_named, recipient.toShortString()));
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
|
|
|
noImages = (TextView ) findViewById(R.id.no_images );
|
|
|
|
gridView = (RecyclerView) findViewById(R.id.media_grid);
|
|
|
|
gridManager = new GridLayoutManager(this, getResources().getInteger(R.integer.media_overview_cols));
|
|
|
|
gridView.setLayoutManager(gridManager);
|
|
|
|
gridView.setHasFixedSize(true);
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
Address address = getIntent().getParcelableExtra(ADDRESS_EXTRA);
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (address != null) {
|
2017-08-22 01:32:38 +00:00
|
|
|
recipient = Recipient.from(this, address, true);
|
2016-09-20 16:53:44 +00:00
|
|
|
} else {
|
2017-08-01 15:56:00 +00:00
|
|
|
recipient = null;
|
2016-09-20 16:53:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (recipient != null) {
|
|
|
|
recipient.addListener(new RecipientModifiedListener() {
|
2015-01-19 02:11:30 +00:00
|
|
|
@Override
|
2017-08-01 15:56:00 +00:00
|
|
|
public void onModified(Recipient recipients) {
|
2015-01-19 02:11:30 +00:00
|
|
|
initializeActionBar();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 11:03:07 +00: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-21 01:14:28 +00:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(c).getThreadIdFor(recipient);
|
2017-02-01 02:49:19 +00:00
|
|
|
Cursor cursor = DatabaseFactory.getMediaDatabase(c).getMediaForThread(threadId);
|
2015-08-22 11:03:07 +00:00
|
|
|
List<SaveAttachmentTask.Attachment> attachments = new ArrayList<>(cursor.getCount());
|
|
|
|
|
2017-09-21 01:14:28 +00:00
|
|
|
while (cursor.moveToNext()) {
|
2017-03-28 19:05:30 +00:00
|
|
|
MediaRecord record = MediaRecord.from(c, masterSecret, cursor);
|
2015-08-22 11:03:07 +00:00
|
|
|
attachments.add(new SaveAttachmentTask.Attachment(record.getAttachment().getDataUri(),
|
|
|
|
record.getContentType(),
|
2017-03-28 19:05:30 +00:00
|
|
|
record.getDate(),
|
|
|
|
null));
|
2015-08-22 11:03:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 01:14:28 +00:00
|
|
|
cursor.close();
|
|
|
|
|
2015-08-22 11:03:07 +00:00
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<SaveAttachmentTask.Attachment> attachments) {
|
|
|
|
super.onPostExecute(attachments);
|
|
|
|
|
2017-03-28 19:05:30 +00:00
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(c, masterSecret, gridView, attachments.size());
|
2015-08-22 11:03:07 +00: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-19 02:11:30 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
2015-08-22 11:03:07 +00:00
|
|
|
case R.id.save: saveToDisk(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
|
2017-09-21 01:14:28 +00:00
|
|
|
return new ThreadMediaLoader(this, masterSecret, recipient.getAddress());
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
|
|
|
Log.w(TAG, "onLoadFinished()");
|
2017-09-21 01:14:28 +00:00
|
|
|
gridView.setAdapter(new MediaAdapter(this, masterSecret, cursor, recipient.getAddress()));
|
2015-01-19 02:11:30 +00:00
|
|
|
noImages.setVisibility(gridView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
|
2015-08-22 11:03:07 +00:00
|
|
|
invalidateOptionsMenu();
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> cursorLoader) {
|
|
|
|
((CursorRecyclerViewAdapter)gridView.getAdapter()).changeCursor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|