Commit a1e0d136 authored by Thomas's avatar Thomas
Browse files

Improve media activity

parent e755dc79
package app.fedilab.android.activities;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.ActionMode;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import com.franmontiel.localechanger.LocaleChanger;
import com.google.android.material.snackbar.Snackbar;
import com.jaredrummler.cyanea.app.CyaneaAppCompatActivity;
import com.vanniktech.emoji.EmojiManager;
import com.vanniktech.emoji.one.EmojiOneProvider;
import org.jetbrains.annotations.NotNull;
import java.util.Timer;
import app.fedilab.android.BuildConfig;
import app.fedilab.android.helper.Helper;
import es.dmoral.toasty.Toasty;
/**
* Created by Thomas on 12/12/2017.
* Base activity which updates security provider
* Created by Thomas on 16/05/2020.
* Fragment base activity which updates security provider
*/
@SuppressLint("Registered")
public class BaseFragmentActivity extends CyaneaAppCompatActivity {
public class BaseFragmentActivity extends AppCompatActivity {
public static final int READ_WRITE_STORAGE = 52;
public static Timer timer;
public static boolean canShowActionMode = true;
static {
Helper.installProvider();
EmojiManager.install(new EmojiOneProvider());
}
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectAll()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.detectActivityLeaks()
.penaltyLog()
// .penaltyDeath()
.build());
}
canShowActionMode = true;
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleChanger.onConfigurationChanged();
}
@Override
protected void attachBaseContext(@NotNull Context newBase) {
newBase = LocaleChanger.configureBaseContext(newBase);
super.attachBaseContext(newBase);
}
public boolean requestPermission(String permission) {
boolean isGranted = ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
if (!isGranted) {
ActivityCompat.requestPermissions(
this,
new String[]{permission},
READ_WRITE_STORAGE);
}
return isGranted;
}
public void isPermissionGranted(boolean isGranted, String permission) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == READ_WRITE_STORAGE) {
isPermissionGranted(grantResults[0] == PackageManager.PERMISSION_GRANTED, permissions[0]);
}
}
protected void showLoading(@NonNull String message) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(message);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
protected void hideLoading() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
protected void showSnackbar(@NonNull String message) {
View view = findViewById(android.R.id.content);
if (view != null) {
Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
} else {
Toasty.info(BaseFragmentActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActionModeStarted(ActionMode mode) {
if (!canShowActionMode) {
mode.finish();
}
super.onActionModeStarted(mode);
}
}
......@@ -411,52 +411,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
}
private static class commentTask extends AsyncTask<Void, Void, app.fedilab.android.client.Entities.Status>{
private WeakReference<Context> contextReference;
private Peertube peertube;
commentTask(WeakReference<Context> contextReference, Peertube peertube){
this.peertube = peertube;
this.contextReference = contextReference;
}
@Override
protected app.fedilab.android.client.Entities.Status doInBackground(Void... voids) {
List<app.fedilab.android.client.Entities.Status> remoteStatuses = null;
if (peertube != null) {
APIResponse search = new API(contextReference.get()).search("https://" + peertube.getAccount().getHost() + "/videos/watch/" + peertube.getUuid());
if (search != null && search.getResults() != null) {
remoteStatuses = search.getResults().getStatuses();
}
}
if( remoteStatuses != null && remoteStatuses.size() > 0) {
return remoteStatuses.get(0);
}else {
return null;
}
}
@Override
protected void onPostExecute(app.fedilab.android.client.Entities.Status remoteStatuses) {
Intent intent = new Intent(contextReference.get(), TootActivity.class);
Bundle b = new Bundle();
if (remoteStatuses == null) {
Toasty.error(contextReference.get(), contextReference.get().getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
return;
}
if (remoteStatuses.getReblog() != null) {
b.putParcelable("tootReply", remoteStatuses.getReblog());
} else {
b.putParcelable("tootReply", remoteStatuses);
}
intent.putExtras(b); //Put your id to your next Intent
contextReference.get().startActivity(intent);
}
}
public FullScreenMediaController.fullscreen getFullscreen() {
return fullscreen;
}
......@@ -601,12 +555,12 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
String userAgent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT);
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
ProgressiveMediaSource videoSource;
if( video_cache == 0) {
if (video_cache == 0) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this,
Util.getUserAgent(PeertubeActivity.this, userAgent), null);
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService())));
}else{
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this, video_cache * 1024 * 1024, 5 * 1024 * 1024);
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService())));
......@@ -684,7 +638,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
});
}
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
......@@ -767,7 +720,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
}
@Override
public void onResume() {
super.onResume();
......@@ -811,12 +763,12 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
String userAgent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT);
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
ProgressiveMediaSource videoSource;
if( video_cache == 0) {
if (video_cache == 0) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this,
Util.getUserAgent(PeertubeActivity.this, userAgent), null);
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(peertube.getFileUrl(res, peertube.isStreamService())));
}else{
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this, video_cache * 1024 * 1024, 5 * 1024 * 1024);
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(peertube.getFileUrl(res, peertube.isStreamService())));
......@@ -839,7 +791,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
private void initFullscreenDialog() {
fullScreenDialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
......@@ -860,7 +811,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
fullScreenDialog.show();
}
private void closeFullscreenDialog() {
((ViewGroup) playerView.getParent()).removeView(playerView);
......@@ -890,7 +840,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
resolution.setOnClickListener(v -> displayResolution());
}
private void changeColor() {
if (peertube.getMyRating() != null && peertube.getMyRating().equals("like")) {
changeDrawableColor(PeertubeActivity.this, R.drawable.ic_thumb_up_peertube, R.color.positive_thumbs);
......@@ -912,4 +861,49 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
playlists = apiResponse.getPlaylists();
}
}
private static class commentTask extends AsyncTask<Void, Void, app.fedilab.android.client.Entities.Status> {
private WeakReference<Context> contextReference;
private Peertube peertube;
commentTask(WeakReference<Context> contextReference, Peertube peertube) {
this.peertube = peertube;
this.contextReference = contextReference;
}
@Override
protected app.fedilab.android.client.Entities.Status doInBackground(Void... voids) {
List<app.fedilab.android.client.Entities.Status> remoteStatuses = null;
if (peertube != null) {
APIResponse search = new API(contextReference.get()).search("https://" + peertube.getAccount().getHost() + "/videos/watch/" + peertube.getUuid());
if (search != null && search.getResults() != null) {
remoteStatuses = search.getResults().getStatuses();
}
}
if (remoteStatuses != null && remoteStatuses.size() > 0) {
return remoteStatuses.get(0);
} else {
return null;
}
}
@Override
protected void onPostExecute(app.fedilab.android.client.Entities.Status remoteStatuses) {
Intent intent = new Intent(contextReference.get(), TootActivity.class);
Bundle b = new Bundle();
if (remoteStatuses == null) {
Toasty.error(contextReference.get(), contextReference.get().getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
return;
}
if (remoteStatuses.getReblog() != null) {
b.putParcelable("tootReply", remoteStatuses.getReblog());
} else {
b.putParcelable("tootReply", remoteStatuses);
}
intent.putExtras(b); //Put your id to your next Intent
contextReference.get().startActivity(intent);
}
}
}
......@@ -737,6 +737,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
attachments.add(attachment);
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("position", 1);
b.putInt("bgcolor", getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
startActivity(intent);
});
......
......@@ -24,7 +24,6 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
......@@ -49,11 +48,6 @@ import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrInterface;
import com.r0adkll.slidr.model.SlidrPosition;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
......@@ -72,7 +66,7 @@ import es.dmoral.toasty.Toasty;
* Media Activity
*/
public class SlideMediaActivity extends BaseActivity implements OnDownloadInterface {
public class SlideMediaActivity extends BaseFragmentActivity implements OnDownloadInterface {
int flags;
private ArrayList<Attachment> attachments;
......@@ -80,13 +74,13 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
private ViewPager mPager;
private long downloadID;
private boolean fullscreen;
private SlidrInterface slidrInterface;
private TextView media_description;
private Handler handler;
private boolean swipeEnabled;
private int minTouch, maxTouch;
private float startX;
private float startY;
private int bgColor;
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
......@@ -131,21 +125,24 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
media_description = findViewById(R.id.media_description);
final int med_desc_timeout = sharedpreferences.getInt(Helper.SET_MED_DESC_TIMEOUT, 3) * 1000;
flags = getWindow().getDecorView().getSystemUiVisibility();
if (getIntent().getExtras() != null) {
mediaPosition = getIntent().getExtras().getInt("position", 1);
bgColor = getIntent().getExtras().getInt("bgcolor", -1);
}
swipeEnabled = true;
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//actionBar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(SlideMediaActivity.this, R.color.cyanea_primary)));
actionBar.setBackgroundDrawable(new ColorDrawable(bgColor));
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
assert inflater != null;
View view = inflater.inflate(R.layout.media_action_bar, new LinearLayout(SlideMediaActivity.this), false);
view.setBackground(new ColorDrawable(ContextCompat.getColor(SlideMediaActivity.this, R.color.cyanea_primary)));
view.setBackgroundColor(bgColor);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
ImageView media_save = getSupportActionBar().getCustomView().findViewById(R.id.media_save);
ImageView media_share = getSupportActionBar().getCustomView().findViewById(R.id.media_share);
ImageView media_save = actionBar.getCustomView().findViewById(R.id.media_save);
ImageView media_share = actionBar.getCustomView().findViewById(R.id.media_share);
toolbar_close.setOnClickListener(v -> finish());
media_save.setOnClickListener(view12 -> {
int position = mPager.getCurrentItem();
......@@ -198,8 +195,7 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
}
attachments = getIntent().getParcelableArrayListExtra("mediaArray");
if (getIntent().getExtras() != null)
mediaPosition = getIntent().getExtras().getInt("position", 1);
if (attachments == null || attachments.size() == 0)
finish();
......@@ -246,19 +242,7 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
}
});
SlidrConfig config = new SlidrConfig.Builder()
.sensitivity(1f)
.scrimColor(Color.BLACK)
.scrimStartAlpha(0.8f)
.scrimEndAlpha(0f)
.position(SlidrPosition.VERTICAL)
.velocityThreshold(2400)
.distanceThreshold(0.25f)
.edgeSize(0.18f)
.build();
slidrInterface = Slidr.attach(SlideMediaActivity.this, config);
setFullscreen(true);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
......@@ -352,15 +336,6 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
super.onPostResume();
}
public void enableSliding(boolean enable) {
if (enable && !swipeEnabled) {
slidrInterface.unlock();
swipeEnabled = true;
} else if (!enable && swipeEnabled) {
slidrInterface.lock();
swipeEnabled = false;
}
}
public boolean getFullScreen() {
return this.fullscreen;
......@@ -419,6 +394,7 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf
MediaSliderFragment mediaSliderFragment = new MediaSliderFragment();
bundle.putInt("position", position);
bundle.putParcelable("attachment", attachments.get(position));
bundle.putInt("bgcolor", bgColor);
mediaSliderFragment.setArguments(bundle);
return mediaSliderFragment;
}
......
......@@ -3708,7 +3708,8 @@ public class API {
newStatus.setArt_attachment(attachment);
apiResponseReply.getStatuses().add(newStatus);
}
}catch (Exception ignored){}
} catch (Exception ignored) {
}
}
}
}
......
......@@ -173,6 +173,7 @@ public class ArtListAdapter extends RecyclerView.Adapter implements OnPostAction
else if (status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0)
attachments.add(status.getMedia_attachments().get(0));
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
b.putInt("position", 1);
intent.putExtras(b);
context.startActivity(intent);
......
......@@ -84,6 +84,7 @@ public class ImageAdapter extends RecyclerView.Adapter {
}
intent.putParcelableArrayListExtra("mediaArray", attachmentsTmp);
b.putInt("position", (viewHolder.getAdapterPosition() + 1));
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
context.startActivity(intent);
});
......
......@@ -1184,6 +1184,7 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
Bundle b = new Bundle();
intent.putParcelableArrayListExtra("mediaArray", notification.getStatus().getMedia_attachments());
b.putInt("position", finalPosition);
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
context.startActivity(intent);
});
......
......@@ -397,6 +397,7 @@ public class PixelfedListAdapter extends RecyclerView.Adapter implements OnPostA
Bundle b = new Bundle();
intent.putParcelableArrayListExtra("mediaArray", status.getMedia_attachments());
b.putInt("position", 1);
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
context.startActivity(intent);
});
......
......@@ -134,6 +134,7 @@ public class PixelfedStoriesListAdapter extends RecyclerView.Adapter {
Bundle b = new Bundle();
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("position", 1);
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
context.startActivity(intent);
});
......
......@@ -107,6 +107,7 @@ public class SliderAdapter extends SliderViewAdapter<SliderAdapter.SliderAdapter
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("position", (position + 1));
b.putInt("bgcolor", contextWeakReference.get().getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
contextWeakReference.get().startActivity(intent);
});
......
......@@ -3404,6 +3404,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
Intent intent = new Intent(context, SlideMediaActivity.class);
Bundle b = new Bundle();
intent.putParcelableArrayListExtra("mediaArray", attachmentArrayList);
b.putInt("bgcolor", context.getResources().getColor(R.color.cyanea_primary_dark));
b.putInt("position", finalPosition);
intent.putExtras(b);
context.startActivity(intent);
......
......@@ -21,7 +21,6 @@ import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
......@@ -470,7 +469,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
Button update_tracking_domains = rootView.findViewById(R.id.update_tracking_domains);
update_tracking_domains.setOnClickListener(v ->
new DownloadTrackingDomainsAsyncTask(Objects.requireNonNull(getActivity()), update_tracking_domains).execute()
new DownloadTrackingDomainsAsyncTask(Objects.requireNonNull(getActivity()), update_tracking_domains).execute()
);
......@@ -1028,7 +1027,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
int video_cache_size = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
SeekBar set_video_cache_size = rootView.findViewById(R.id.set_video_cache_size);
set_video_cache_size.setMax(5*Helper.DEFAULT_VIDEO_CACHE_MB);
set_video_cache_size.setMax(5 * Helper.DEFAULT_VIDEO_CACHE_MB);
set_video_cache_size.setProgress(video_cache_size);
TextView set_video_cache = rootView.findViewById(R.id.set_video_cache);
set_video_cache.setText(String.valueOf(video_cache_size));
......@@ -1051,7 +1050,6 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
});
int truncate_toots_size = sharedpreferences.getInt(Helper.SET_TRUNCATE_TOOTS_SIZE, 0);
SeekBar set_truncate_size = rootView.findViewById(R.id.set_truncate_size);