Skip to content

Commit

Permalink
brought back gif support
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Apr 14, 2015
1 parent 88cc7a8 commit 765d143
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@

import java.io.File;

import pl.droidsonroids.gif.GifTextureView;
import pl.droidsonroids.gif.InputSource.FileSource;


public final class MediaViewerActivity extends ThemedActionBarActivity implements Constants, OnPageChangeListener {

Expand Down Expand Up @@ -368,15 +371,19 @@ public static final class ImagePageFragment extends BaseSupportFragment
implements DownloadListener, LoaderCallbacks<Result>, OnLayoutChangeListener, OnClickListener {

private SubsamplingScaleImageView mImageView;
private GifTextureView mGifImageView;
private ProgressWheel mProgressBar;
private boolean mLoaderInitialized;
private float mContentLength;
private SaveImageTask mSaveImageTask;

private File mImageFile;

@Override
public void onBaseViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onBaseViewCreated(view, savedInstanceState);
mImageView = (SubsamplingScaleImageView) view.findViewById(R.id.image_view);
mGifImageView = (GifTextureView) view.findViewById(R.id.gif_image_view);
mProgressBar = (ProgressWheel) view.findViewById(R.id.progress);
}

Expand Down Expand Up @@ -405,29 +412,27 @@ private ParcelableMedia getMedia() {
@Override
public void onLoadFinished(final Loader<TileImageLoader.Result> loader, final TileImageLoader.Result data) {
if (data.hasData()) {
mImageView.setVisibility(View.VISIBLE);
mImageView.setTag(data.file);
mImageFile = data.file;
if (data.useDecoder) {
mGifImageView.setVisibility(View.GONE);
mImageView.setVisibility(View.VISIBLE);
mImageView.setImage(ImageSource.uri(Uri.fromFile(data.file)));
} else if ("image/gif".equals(data.options.outMimeType)) {
// try {
// final FileDescriptor fd = new RandomAccessFile(data.file, "r").getFD();
// mImageView.setImageDrawable(new GifDrawable(fd));
// } catch (IOException e) {
// mImageView.setImage(null);
// mImageView.setTag(null);
// mImageView.setVisibility(View.GONE);
// Utils.showErrorMessage(getActivity(), null, e, true);
// }
mGifImageView.setVisibility(View.VISIBLE);
mImageView.setVisibility(View.GONE);
mGifImageView.setInputSource(new FileSource(data.file));
updateScaleLimit();
} else {
mGifImageView.setVisibility(View.GONE);
mImageView.setVisibility(View.VISIBLE);
mImageView.setImage(ImageSource.bitmap(data.bitmap));
updateScaleLimit();
}
} else {
mImageView.recycle();
mImageView.setTag(null);
mImageFile = null;
mImageView.setVisibility(View.GONE);
mGifImageView.setVisibility(View.GONE);
Utils.showErrorMessage(getActivity(), null, data.exception, true);
}
mProgressBar.setVisibility(View.GONE);
Expand All @@ -451,28 +456,26 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
final Object imageTag = mImageView.getTag();
final File file = mImageFile;
final boolean isLoading = getLoaderManager().hasRunningLoaders();
final boolean hasImage = imageTag instanceof File;
final boolean hasImage = file != null && file.exists();
MenuUtils.setMenuItemAvailability(menu, R.id.refresh, !hasImage && !isLoading);
MenuUtils.setMenuItemAvailability(menu, R.id.share, hasImage && !isLoading);
MenuUtils.setMenuItemAvailability(menu, R.id.save, hasImage && !isLoading);
if (hasImage) {
final MenuItem shareItem = menu.findItem(R.id.share);
final ShareActionProvider shareProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
final File file = (File) imageTag;
final Intent intent = new Intent(Intent.ACTION_SEND);
final Uri fileUri = Uri.fromFile(file);
intent.setDataAndType(fileUri, Utils.getImageMimeType(file));
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
final MediaViewerActivity activity = (MediaViewerActivity) getActivity();
if (activity.hasStatus()) {
final ParcelableStatus status = activity.getStatus();
intent.putExtra(Intent.EXTRA_TEXT, Utils.getStatusShareText(activity, status));
intent.putExtra(Intent.EXTRA_SUBJECT, Utils.getStatusShareSubject(activity, status));
}
shareProvider.setShareIntent(intent);
if (!hasImage) return;
final MenuItem shareItem = menu.findItem(R.id.share);
final ShareActionProvider shareProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
final Intent intent = new Intent(Intent.ACTION_SEND);
final Uri fileUri = Uri.fromFile(file);
intent.setDataAndType(fileUri, Utils.getImageMimeType(file));
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
final MediaViewerActivity activity = (MediaViewerActivity) getActivity();
if (activity.hasStatus()) {
final ParcelableStatus status = activity.getStatus();
intent.putExtra(Intent.EXTRA_TEXT, Utils.getStatusShareText(activity, status));
intent.putExtra(Intent.EXTRA_SUBJECT, Utils.getStatusShareSubject(activity, status));
}
shareProvider.setShareIntent(intent);
}

@Override
Expand Down Expand Up @@ -501,14 +504,11 @@ public boolean onOptionsItemSelected(MenuItem item) {

private void saveToGallery() {
if (mSaveImageTask != null && mSaveImageTask.getStatus() == Status.RUNNING) return;
final Object imageTag = mImageView.getTag();
final boolean hasImage = imageTag instanceof File;
if (hasImage) {
mSaveImageTask = new SaveImageTask(getActivity(), (File) imageTag);
mSaveImageTask.execute();
} else {

}
final File file = mImageFile;
final boolean hasImage = file != null && file.exists();
if (!hasImage) return;
mSaveImageTask = new SaveImageTask(getActivity(), file);
mSaveImageTask.execute();
}

private void openInBrowser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected void onStartLoading() {


private void dump(final InputStream is, final OutputStream os) throws IOException {
final byte buffer[] = new byte[1024];
final byte buffer[] = new byte[128];
int rc = is.read(buffer, 0, buffer.length);
long downloaded = 0;
while (rc > 0) {
Expand Down
6 changes: 6 additions & 0 deletions twidere/src/main/res/layout/fragment_media_page_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<pl.droidsonroids.gif.GifTextureView
android:id="@+id/gif_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>

<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
Expand Down

0 comments on commit 765d143

Please sign in to comment.