fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/MediaActivity.java

454 lines
18 KiB
Java
Raw Normal View History

/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
*
* 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.
*
2017-07-10 10:33:24 +02:00
* Mastalab 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.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
2017-06-30 17:09:07 +02:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
2018-02-09 14:56:57 +01:00
import android.graphics.RectF;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
2018-02-09 14:56:57 +01:00
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
2017-11-07 11:09:09 +01:00
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
2017-12-23 19:31:12 +01:00
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.OnMatrixChangedListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.gw.swipeback.SwipeBackLayout;
import java.io.File;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
2017-12-23 19:31:12 +01:00
import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
2017-11-18 12:22:41 +01:00
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
import fr.gouv.etalab.mastodon.helper.Helper;
2017-11-18 12:22:41 +01:00
import fr.gouv.etalab.mastodon.interfaces.OnDownloadInterface;
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
2017-07-10 09:24:58 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
/**
* Created by Thomas on 25/06/2017.
* Media Activity
*/
2017-12-12 18:17:01 +01:00
public class MediaActivity extends BaseActivity implements OnDownloadInterface {
private RelativeLayout loader;
private ArrayList<Attachment> attachments;
private PhotoView imageView;
private VideoView videoView;
2017-07-10 09:24:58 +02:00
private float downX;
private int mediaPosition;
MediaActivity.actionSwipe currentAction;
static final int MIN_DISTANCE = 100;
private String finalUrlDownload;
private String preview_url;
private ImageView prev, next;
private boolean isHiding;
private Bitmap downloadedImage;
private File fileVideo;
private TextView progress;
2017-12-23 19:31:12 +01:00
private ProgressBar pbar_inf;
private TextView message_ready;
2018-02-09 14:56:57 +01:00
private boolean canSwipe;
2017-11-18 12:22:41 +01:00
private enum actionSwipe{
RIGHT_TO_LEFT,
LEFT_TO_RIGHT,
POP
}
2018-02-09 14:56:57 +01:00
private FloatingActionButton media_save, media_close;
private boolean scheduleHidden;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
setContentView(R.layout.activity_media);
2018-04-18 16:01:07 +02:00
SwipeBackLayout mSwipeBackLayout = new SwipeBackLayout(MediaActivity.this);
mSwipeBackLayout.setDirectionMode(SwipeBackLayout.FROM_BOTTOM);
mSwipeBackLayout.setMaskAlpha(125);
mSwipeBackLayout.setSwipeBackFactor(0.5f);
mSwipeBackLayout.setSwipeBackListener(new SwipeBackLayout.OnSwipeBackListener() {
@Override
public void onViewPositionChanged(View mView, float swipeBackFraction, float SWIPE_BACK_FACTOR) {
2018-04-18 16:01:07 +02:00
canSwipe = swipeBackFraction<0.1;
}
@Override
public void onViewSwipeFinished(View mView, boolean isEnd) {
if(!isEnd)
canSwipe = true;
else
finish();
}
});
mSwipeBackLayout.attachToActivity(this);
attachments = getIntent().getParcelableArrayListExtra("mediaArray");
2017-10-27 15:34:53 +02:00
if( getIntent().getExtras() != null)
mediaPosition = getIntent().getExtras().getInt("position", 1);
if( attachments == null || attachments.size() == 0)
finish();
2017-11-06 19:38:04 +01:00
2017-10-27 15:34:53 +02:00
RelativeLayout main_container_media = findViewById(R.id.main_container_media);
2017-07-01 15:24:28 +02:00
if( theme == Helper.THEME_LIGHT){
main_container_media.setBackgroundResource(R.color.mastodonC2);
2017-07-01 15:24:28 +02:00
}else {
2017-11-07 19:04:02 +01:00
main_container_media.setBackgroundResource(R.color.mastodonC1_);
2017-07-01 15:24:28 +02:00
}
message_ready = findViewById(R.id.message_ready);
2017-11-07 11:09:09 +01:00
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
2018-02-09 14:56:57 +01:00
media_save = findViewById(R.id.media_save);
media_close = findViewById(R.id.media_close);
media_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Build.VERSION.SDK_INT >= 23 ){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MediaActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
} else {
Helper.manageMoveFileDownload(MediaActivity.this, preview_url, finalUrlDownload, downloadedImage, fileVideo);
2017-11-07 11:09:09 +01:00
}
2018-02-09 14:56:57 +01:00
}else{
Helper.manageMoveFileDownload(MediaActivity.this, preview_url, finalUrlDownload, downloadedImage, fileVideo);
2017-11-07 11:09:09 +01:00
}
2018-02-09 14:56:57 +01:00
}
});
media_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
Handler h = new Handler();
scheduleHidden = true;
h.postDelayed(new Runnable() {
2018-02-09 14:56:57 +01:00
@Override
public void run() {
// DO DELAYED STUFF
media_close.setVisibility(View.GONE);
media_save.setVisibility(View.GONE);
scheduleHidden = false;
}
}, 2000);
canSwipe = true;
2017-10-27 15:34:53 +02:00
loader = findViewById(R.id.loader);
imageView = findViewById(R.id.media_picture);
videoView = findViewById(R.id.media_video);
prev = findViewById(R.id.media_prev);
next = findViewById(R.id.media_next);
changeDrawableColor(getApplicationContext(), prev,R.color.mastodonC4);
changeDrawableColor(getApplicationContext(), next,R.color.mastodonC4);
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPosition--;
displayMediaAtPosition(actionSwipe.POP);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPosition++;
displayMediaAtPosition(actionSwipe.POP);
}
});
2017-10-27 15:34:53 +02:00
progress = findViewById(R.id.loader_progress);
imageView.setOnMatrixChangeListener(new OnMatrixChangedListener() {
@Override
public void onMatrixChanged(RectF rect) {
canSwipe = (imageView.getScale() == 1 );
}
});
2017-12-23 19:31:12 +01:00
pbar_inf = findViewById(R.id.pbar_inf);
setTitle("");
isHiding = false;
setTitle("");
displayMediaAtPosition(actionSwipe.POP);
}
/**
* Manage touch event
* Allows to swipe from timelines
* @param event MotionEvent
* @return boolean
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
2018-02-09 14:56:57 +01:00
if( event.getAction() == MotionEvent.ACTION_DOWN && !scheduleHidden){
scheduleHidden = true;
media_close.setVisibility(View.VISIBLE);
media_save.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
media_close.setVisibility(View.GONE);
media_save.setVisibility(View.GONE);
scheduleHidden = false;
}
}, 2000);
2017-11-06 19:38:04 +01:00
}
if( !canSwipe || mediaPosition > attachments.size() || mediaPosition < 1 || attachments.size() <= 1)
return super.dispatchTouchEvent(event);
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
//Displays navigation left/right buttons
if( attachments != null && attachments.size() > 1 && !isHiding){
prev.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
isHiding = true;
new Handler().postDelayed(new Runnable(){
public void run() {
prev.setVisibility(View.GONE);
next.setVisibility(View.GONE);
isHiding = false;
}
}, 2000);
}
return super.dispatchTouchEvent(event);
}
case MotionEvent.ACTION_UP: {
float upX = event.getX();
float deltaX = downX - upX;
// swipe horizontal
2017-07-10 09:24:58 +02:00
if( downX > MIN_DISTANCE & (Math.abs(deltaX) > MIN_DISTANCE ) ){
if(deltaX < 0) { switchOnSwipe(MediaActivity.actionSwipe.LEFT_TO_RIGHT); return true; }
if(deltaX > 0) { switchOnSwipe(MediaActivity.actionSwipe.RIGHT_TO_LEFT); return true; }
}else{
currentAction = MediaActivity.actionSwipe.POP;
}
}
}
return super.dispatchTouchEvent(event);
}
private void switchOnSwipe(actionSwipe action){
loader.setVisibility(View.VISIBLE);
mediaPosition = (action == actionSwipe.LEFT_TO_RIGHT)?mediaPosition-1:mediaPosition+1;
displayMediaAtPosition(action);
}
private void displayMediaAtPosition(actionSwipe action){
if( mediaPosition > attachments.size() )
mediaPosition = 1;
if( mediaPosition < 1)
mediaPosition = attachments.size();
currentAction = action;
final Attachment attachment = attachments.get(mediaPosition-1);
String type = attachment.getType();
String url = attachment.getUrl();
finalUrlDownload = url;
videoView.setVisibility(View.GONE);
if( videoView.isPlaying()) {
videoView.stopPlayback();
}
imageView.setVisibility(View.GONE);
preview_url = attachment.getPreview_url();
2017-10-18 17:07:50 +02:00
if( type.equals("unknown")){
preview_url = attachment.getRemote_url();
if( preview_url.endsWith(".png") || preview_url.endsWith(".jpg")|| preview_url.endsWith(".jpeg")) {
2017-10-18 17:07:50 +02:00
type = "image";
}else if( preview_url.endsWith(".mp4")) {
2017-10-18 17:07:50 +02:00
type = "video";
}
url = attachment.getRemote_url();
attachment.setType(type);
2017-10-18 17:07:50 +02:00
}
2017-11-18 12:22:41 +01:00
final String finalUrl = url;
switch (type){
case "image":
pbar_inf.setScaleY(1f);
2017-12-23 19:31:12 +01:00
imageView.setVisibility(View.VISIBLE);
fileVideo = null;
pbar_inf.setIndeterminate(true);
loader.setVisibility(View.VISIBLE);
fileVideo = null;
2017-12-23 19:31:12 +01:00
Glide.with(getApplicationContext())
.asBitmap()
.load(preview_url).into(
new SimpleTarget<Bitmap>() {
@Override
2018-02-09 14:56:57 +01:00
public void onResourceReady(@NonNull final Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
Glide.with(getApplicationContext())
.asBitmap()
.load(finalUrl).into(
new SimpleTarget<Bitmap>() {
@Override
2018-02-09 14:56:57 +01:00
public void onResourceReady(@NonNull final Bitmap resource, Transition<? super Bitmap> transition) {
loader.setVisibility(View.GONE);
if( imageView.getScale() < 1.1) {
downloadedImage = resource;
imageView.setImageBitmap(resource);
}else{
message_ready.setVisibility(View.VISIBLE);
}
message_ready.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadedImage = resource;
imageView.setImageBitmap(resource);
message_ready.setVisibility(View.GONE);
}
});
}
}
);
}
}
);
break;
case "video":
case "gifv":
2017-12-23 19:31:12 +01:00
pbar_inf.setIndeterminate(false);
pbar_inf.setScaleY(3f);
2017-11-18 12:22:41 +01:00
File file = new File(getCacheDir() + "/" + Helper.md5(url)+".mp4");
if(file.exists()) {
Uri uri = Uri.parse(file.getAbsolutePath());
videoView.setVisibility(View.VISIBLE);
try {
HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
2017-11-18 12:22:41 +01:00
videoView.setVideoURI(uri);
videoView.start();
MediaController mc = new MediaController(MediaActivity.this);
videoView.setMediaController(mc);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
loader.setVisibility(View.GONE);
mp.start();
mp.setLooping(true);
}
});
fileVideo = file;
downloadedImage = null;
}else{
2017-12-24 08:56:08 +01:00
videoView.setVisibility(View.VISIBLE);
Uri uri = Uri.parse(url);
videoView.setVideoURI(uri);
videoView.start();
MediaController mc = new MediaController(MediaActivity.this);
mc.setAnchorView(videoView);
videoView.setMediaController(mc);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
}
});
videoView.start();
progress.setText("0 %");
progress.setVisibility(View.VISIBLE);
new HttpsConnection(MediaActivity.this).download(finalUrl, MediaActivity.this );
}
break;
}
}
2017-11-18 12:22:41 +01:00
@Override
2017-12-07 18:48:18 +01:00
public void onDownloaded(String path, String originUrl, Error error) {
File response = new File(path);
File dir = getCacheDir();
File from = new File(dir, response.getName());
File to = new File(dir, Helper.md5(originUrl) + ".mp4");
if (from.exists())
//noinspection ResultOfMethodCallIgnored
from.renameTo(to);
fileVideo = to;
downloadedImage = null;
if( progress != null)
progress.setVisibility(View.GONE);
if( loader != null)
loader.setVisibility(View.GONE);
2017-11-18 12:22:41 +01:00
}
2018-04-23 15:16:26 +02:00
@Override
public void onResume(){
super.onResume();
FullScreencall();
}
public void FullScreencall() {
if(Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if(Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
2017-11-18 12:36:10 +01:00
2017-11-18 12:22:41 +01:00
@Override
public void onUpdateProgress(int progressPercentage) {
progress.setText(String.format("%s%%",String.valueOf(progressPercentage)));
pbar_inf.setProgress(progressPercentage);
2017-11-18 12:22:41 +01:00
}
}