Skip to content

Commit

Permalink
lower memory consumption from previews
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
mcginty committed Feb 11, 2015
1 parent 3a9d521 commit ac4db41
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
<activity android:name=".MediaPreviewActivity"
android:label="@string/AndroidManifest__media_preview"
android:windowSoftInputMode="stateHidden"
android:launchMode="singleTask"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>

<activity android:name=".DummyActivity"
Expand Down
100 changes: 63 additions & 37 deletions src/org/thoughtcrime/securesms/MediaPreviewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.opengl.GLES20;
Expand All @@ -36,7 +37,6 @@
import android.widget.Toast;

import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
Expand All @@ -48,14 +48,13 @@
import org.thoughtcrime.securesms.util.SaveAttachmentTask.Attachment;

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 PassphraseRequiredActionBarActivity {
public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity implements RecipientModifiedListener {
private final static String TAG = MediaPreviewActivity.class.getSimpleName();

public final static String MASTER_SECRET_EXTRA = "master_secret";
Expand All @@ -68,6 +67,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity {

private View loadingView;
private TextView errorText;
private Bitmap bitmap;
private ImageView image;
private PhotoViewAttacher imageAttacher;
private Uri mediaUri;
Expand All @@ -88,7 +88,9 @@ protected void onCreate(Bundle bundle) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.media_preview_activity);

initializeViews();
initializeResources();
initializeActionBar();
}

@TargetApi(VERSION_CODES.JELLY_BEAN)
Expand All @@ -98,32 +100,73 @@ private void setFullscreenIfPossible() {
}
}

@Override
public void onModified(Recipient recipient) {
initializeActionBar();
}

private void initializeActionBar() {
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);
}

@Override
public void onResume() {
super.onResume();
dynamicLanguage.onResume(this);
if (recipient != null) recipient.addListener(this);
initializeMedia();
}

@Override
public void onPause() {
super.onPause();
if (recipient != null) recipient.removeListener(this);
cleanupMedia();
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (recipient != null) recipient.removeListener(this);
setIntent(intent);
initializeResources();
initializeActionBar();
initializeMedia();
}

private void initializeViews() {
loadingView = findViewById(R.id.loading_indicator);
errorText = (TextView) findViewById(R.id.error);
image = (ImageView) findViewById(R.id.image);
imageAttacher = new PhotoViewAttacher(image);
}

private void initializeResources() {
final long recipientId = getIntent().getLongExtra(RECIPIENT_EXTRA, -1);

masterSecret = getIntent().getParcelableExtra(MASTER_SECRET_EXTRA);
mediaUri = getIntent().getData();
mediaType = getIntent().getType();
date = getIntent().getLongExtra(DATE_EXTRA, -1);
mediaUri = getIntent().getData();
mediaType = getIntent().getType();
date = getIntent().getLongExtra(DATE_EXTRA, -1);

if (recipientId > -1) {
recipient = RecipientFactory.getRecipientForId(this, recipientId, true);
recipient.addListener(new RecipientModifiedListener() {
@Override
public void onModified(Recipient recipient) {
initializeActionBar();
}
});
recipient.addListener(this);
} else {
recipient = null;
}
}

initializeActionBar();

private void initializeMedia() {
if (!isContentTypeSupported(mediaType)) {
Log.w(TAG, "Unsupported media type sent to MediaPreviewActivity, finishing.");
Toast.makeText(getApplicationContext(), R.string.MediaPreviewActivity_unssuported_media_type, Toast.LENGTH_LONG).show();
Expand All @@ -137,32 +180,14 @@ public void onModified(Recipient recipient) {
}
}

private void initializeActionBar() {
final CharSequence relativeTimeSpan;
if (date > 0) {
relativeTimeSpan = DateUtils.getRelativeTimeSpanString(date,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS);
} else {
relativeTimeSpan = null;
private void cleanupMedia() {
image.setImageDrawable(null);
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
getSupportActionBar().setTitle(recipient == null ? getString(R.string.MediaPreviewActivity_you) : recipient.getName());
getSupportActionBar().setSubtitle(relativeTimeSpan);

}

@Override
public void onPause() {
super.onPause();
}

private void initializeResources() {
loadingView = findViewById(R.id.loading_indicator);
errorText = (TextView) findViewById(R.id.error);
image = (ImageView) findViewById(R.id.image);
imageAttacher = new PhotoViewAttacher(image);
}

private void displayImage() {
new AsyncTask<Void,Void,Bitmap>() {
@Override
Expand Down Expand Up @@ -192,6 +217,7 @@ protected void onPostExecute(Bitmap bitmap) {
errorText.setText(R.string.MediaPreviewActivity_cant_display);
errorText.setVisibility(View.VISIBLE);
} else {
MediaPreviewActivity.this.bitmap = bitmap;
image.setImageBitmap(bitmap);
image.setVisibility(View.VISIBLE);
imageAttacher.update();
Expand Down

0 comments on commit ac4db41

Please sign in to comment.