mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 10:32:39 +00:00
in-app image media preview
// FREEBIE
This commit is contained in:
199
src/org/thoughtcrime/securesms/MediaPreviewActivity.java
Normal file
199
src/org/thoughtcrime/securesms/MediaPreviewActivity.java
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* Copyright (C) 2014 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.ContentUris;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.providers.PartProvider;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.DateUtils;
|
||||
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
|
||||
import org.thoughtcrime.securesms.util.SaveAttachmentTask.Attachment;
|
||||
import org.whispersystems.textsecure.crypto.MasterSecret;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import uk.co.senab.photoview.PhotoViewAttacher;
|
||||
|
||||
/**
|
||||
* Activity for displaying media attachments in-app
|
||||
*/
|
||||
public class MediaPreviewActivity extends PassphraseRequiredSherlockActivity {
|
||||
private final static String TAG = MediaPreviewActivity.class.getSimpleName();
|
||||
|
||||
public final static String MASTER_SECRET_EXTRA = "master_secret";
|
||||
public final static String RECIPIENT_EXTRA = "recipient";
|
||||
public final static String DATE_EXTRA = "date";
|
||||
|
||||
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
||||
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
||||
|
||||
private MasterSecret masterSecret;
|
||||
|
||||
private ImageView image;
|
||||
private PhotoViewAttacher imageAttacher;
|
||||
private Uri mediaUri;
|
||||
private String mediaType;
|
||||
private Recipient recipient;
|
||||
private long date;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
setFullscreenIfPossible();
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
dynamicTheme.onCreate(this);
|
||||
dynamicLanguage.onCreate(this);
|
||||
super.onCreate(bundle);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
setContentView(R.layout.media_preview_activity);
|
||||
initializeResources();
|
||||
}
|
||||
|
||||
@TargetApi(VERSION_CODES.HONEYCOMB)
|
||||
private void setFullscreenIfPossible() {
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
dynamicTheme.onResume(this);
|
||||
dynamicLanguage.onResume(this);
|
||||
|
||||
masterSecret = getIntent().getParcelableExtra(MASTER_SECRET_EXTRA);
|
||||
|
||||
mediaUri = getIntent().getData();
|
||||
mediaType = getIntent().getType();
|
||||
recipient = getIntent().getParcelableExtra(RECIPIENT_EXTRA);
|
||||
date = getIntent().getLongExtra(DATE_EXTRA, -1);
|
||||
|
||||
final CharSequence relativeTimeSpan;
|
||||
if (date > 0) {
|
||||
relativeTimeSpan = DateUtils.getRelativeTimeSpanString(date,
|
||||
System.currentTimeMillis(),
|
||||
DateUtils.MINUTE_IN_MILLIS);
|
||||
} else {
|
||||
relativeTimeSpan = null;
|
||||
}
|
||||
getSupportActionBar().setTitle(recipient == null ? getString(R.string.MediaPreviewActivity_you) : recipient.getName());
|
||||
getSupportActionBar().setSubtitle(relativeTimeSpan);
|
||||
|
||||
if (!isContentTypeSupported(mediaType)) {
|
||||
Log.w(TAG, "Unsupported media type sent to MediaPreviewActivity, finishing.");
|
||||
Toast.makeText(getApplicationContext(), "Unsupported media type", Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
|
||||
try {
|
||||
Log.w(TAG, "Loading Part URI: " + mediaUri);
|
||||
|
||||
final InputStream is = getInputStream(mediaUri, masterSecret);
|
||||
|
||||
if (mediaType != null && mediaType.startsWith("image/")) {
|
||||
displayImage(is);
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
Log.w(TAG, ioe);
|
||||
Toast.makeText(getApplicationContext(), "Could not read the media", Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream getInputStream(Uri uri, MasterSecret masterSecret) throws IOException {
|
||||
if (PartProvider.isAuthority(uri)) {
|
||||
return DatabaseFactory.getEncryptingPartDatabase(this, masterSecret).getPartStream(ContentUris.parseId(uri));
|
||||
} else {
|
||||
throw new AssertionError("Given a URI that is not handled by our app.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void initializeResources() {
|
||||
image = (ImageView) findViewById(R.id.image);
|
||||
imageAttacher = new PhotoViewAttacher(image);
|
||||
}
|
||||
|
||||
private void displayImage(final InputStream is) {
|
||||
image.setImageBitmap(BitmapFactory.decodeStream(is));
|
||||
image.setVisibility(View.VISIBLE);
|
||||
imageAttacher.update();
|
||||
}
|
||||
|
||||
private void saveToDisk() {
|
||||
SaveAttachmentTask.showWarningDialog(this, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
SaveAttachmentTask saveTask = new SaveAttachmentTask(MediaPreviewActivity.this, masterSecret);
|
||||
saveTask.execute(new Attachment(mediaUri, mediaType, date));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
|
||||
menu.clear();
|
||||
MenuInflater inflater = this.getSupportMenuInflater();
|
||||
inflater.inflate(R.menu.media_preview, menu);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
super.onOptionsItemSelected(item);
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.save: saveToDisk(); return true;
|
||||
case android.R.id.home: finish(); return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isContentTypeSupported(final String contentType) {
|
||||
return contentType != null && contentType.startsWith("image/");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user