Fix for videos

This commit is contained in:
Thomas 2020-05-17 17:47:35 +02:00
parent 129621c73c
commit a0563ce4c0
4 changed files with 201 additions and 203 deletions

View File

@ -559,7 +559,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService())));
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this, video_cache * 1024 * 1024, 5 * 1024 * 1024);
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this);
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService())));
}
@ -767,7 +767,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(peertube.getFileUrl(res, peertube.isStreamService())));
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this, video_cache * 1024 * 1024, 5 * 1024 * 1024);
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this);
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(peertube.getFileUrl(res, peertube.isStreamService())));
}

View File

@ -263,7 +263,7 @@ public class MediaSliderFragment extends Fragment implements MediaPlayer.OnCompl
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(context, video_cache * 1024 * 1024, 5 * 1024 * 1024);
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(context);
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(uri);
}

View File

@ -18,15 +18,15 @@ import java.io.File;
public class CacheDataSourceFactory implements DataSource.Factory {
private static SimpleCache sDownloadCache;
private final Context context;
private final DefaultDataSourceFactory defaultDatasourceFactory;
private final long maxFileSize, maxCacheSize;
private final long maxFileSize;
public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) {
public CacheDataSourceFactory(Context context) {
super();
this.context = context;
this.maxCacheSize = maxCacheSize;
this.maxFileSize = maxFileSize;
this.maxFileSize = 5 * 1024 * 1024;
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userAgent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT);
DefaultBandwidthMeter.Builder bandwidthMeterBuilder = new DefaultBandwidthMeter.Builder(context);
@ -36,11 +36,19 @@ public class CacheDataSourceFactory implements DataSource.Factory {
new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter));
}
public static SimpleCache getInstance(Context context) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(video_cache * 1024 * 1024);
ExoDatabaseProvider exoDatabaseProvider = new ExoDatabaseProvider(context);
if (sDownloadCache == null)
sDownloadCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor, exoDatabaseProvider);
return sDownloadCache;
}
@Override
public DataSource createDataSource() {
LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
ExoDatabaseProvider exoDatabaseProvider = new ExoDatabaseProvider(context);
SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor, exoDatabaseProvider);
SimpleCache simpleCache = getInstance(context);
return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(),
new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);

View File

@ -172,6 +172,8 @@ public class LiveNotificationService extends Service implements NetworkStateRece
public int onStartCommand(Intent intent, int flags, int startId) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
if (!notify || intent == null || intent.getBooleanExtra("stop", false)) {
totalAccount = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -298,213 +300,202 @@ public class LiveNotificationService extends Service implements NetworkStateRece
if (response == null)
return;
final Notification notification;
String dataId;
Bundle b = new Bundle();
boolean canSendBroadCast = true;
Helper.EventStreaming event;
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
try {
switch (response.get("event").toString()) {
case "notification":
eventsCount++;
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.top_notification))
.setSmallIcon(getNotificationIcon(LiveNotificationService.this))
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
if ("notification".equals(response.get("event").toString())) {
eventsCount++;
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel(CHANNEL_ID,
"Live notifications",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.top_notification))
.setSmallIcon(getNotificationIcon(LiveNotificationService.this))
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
startForeground(1, notificationChannel);
}
startForeground(1, notificationChannel);
}
event = Helper.EventStreaming.NOTIFICATION;
notification = API.parseNotificationResponse(LiveNotificationService.this, new JSONObject(response.get("payload").toString()));
b.putParcelable("data", notification);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean canNotify = Helper.canNotify(LiveNotificationService.this);
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
boolean activityRunning = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isMainActivityRunning", false);
String key = account.getAcct() + "@" + account.getInstance();
if (lastNotification.containsKey(key) && notification.getId().compareTo(Objects.requireNonNull(lastNotification.get(key))) <= 0) {
canNotify = false;
}
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (notification.getId().compareTo(Objects.requireNonNull(lastNotif)) <= 0) {
canNotify = false;
}
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (!allowStream) {
canNotify = false;
}
event = Helper.EventStreaming.NOTIFICATION;
notification = API.parseNotificationResponse(LiveNotificationService.this, new JSONObject(response.get("payload").toString()));
b.putParcelable("data", notification);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
boolean canNotify = Helper.canNotify(LiveNotificationService.this);
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
boolean activityRunning = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isMainActivityRunning", false);
String key = account.getAcct() + "@" + account.getInstance();
if (lastNotification.containsKey(key) && notification.getId().compareTo(Objects.requireNonNull(lastNotification.get(key))) <= 0) {
canNotify = false;
}
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (notification.getId().compareTo(Objects.requireNonNull(lastNotif)) <= 0) {
canNotify = false;
}
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (!allowStream) {
canNotify = false;
}
if ((userId == null || !userId.equals(account.getId()) || !activityRunning) && liveNotifications && canNotify && notify) {
lastNotification.put(key, notification.getId());
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
boolean somethingToPush = (notif_follow || notif_add || notif_mention || notif_share || notif_poll);
String message = null;
if (somethingToPush) {
switch (notification.getType()) {
case "mention":
notifType = Helper.NotifType.MENTION;
if (notif_mention) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_mention));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_mention));
if (notification.getStatus() != null) {
if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text(), FROM_HTML_MODE_LEGACY));
else
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text()));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent(), FROM_HTML_MODE_LEGACY));
else
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent()));
}
if ((userId == null || !userId.equals(account.getId()) || !activityRunning) && liveNotifications && canNotify && notify) {
lastNotification.put(key, notification.getId());
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
boolean somethingToPush = (notif_follow || notif_add || notif_mention || notif_share || notif_poll);
String message = null;
if (somethingToPush) {
switch (notification.getType()) {
case "mention":
notifType = Helper.NotifType.MENTION;
if (notif_mention) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_mention));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_mention));
if (notification.getStatus() != null) {
if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text(), FROM_HTML_MODE_LEGACY));
else
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text()));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent(), FROM_HTML_MODE_LEGACY));
else
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent()));
}
} else {
canSendBroadCast = false;
}
break;
case "reblog":
notifType = Helper.NotifType.BOOST;
if (notif_share) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_reblog));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_reblog));
} else {
canSendBroadCast = false;
}
break;
case "favourite":
notifType = Helper.NotifType.FAV;
if (notif_add) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_favourite));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_favourite));
} else {
canSendBroadCast = false;
}
break;
case "follow_request":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_follow_request));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_follow_request));
targeted_account = notification.getAccount().getId();
} else {
canSendBroadCast = false;
}
break;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_follow));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_follow));
targeted_account = notification.getAccount().getId();
} else {
canSendBroadCast = false;
}
break;
case "poll":
notifType = Helper.NotifType.POLL;
if (notif_poll) {
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
message = getString(R.string.notif_poll_self);
else
message = getString(R.string.notif_poll);
} else {
canSendBroadCast = false;
}
break;
default:
}
//Some others notification
final Intent intent = new Intent(LiveNotificationService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT);
intent.putExtra(Helper.PREF_KEY_ID, account.getId());
intent.putExtra(Helper.PREF_INSTANCE, account.getInstance());
if (targeted_account != null) {
intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
}
final String finalMessage = message;
Handler mainHandler = new Handler(Looper.getMainLooper());
Helper.NotifType finalNotifType = notifType;
Runnable myRunnable = () -> {
if (finalMessage != null) {
Glide.with(LiveNotificationService.this)
.asBitmap()
.load(notification.getAccount().getAvatar())
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
assert e != null;
Helper.notify_user(LiveNotificationService.this, account, intent, BitmapFactory.decodeResource(getResources(),
getMainLogo(LiveNotificationService.this)), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
return false;
}
})
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
Helper.notify_user(LiveNotificationService.this, account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
} else {
canSendBroadCast = false;
}
};
mainHandler.post(myRunnable);
break;
case "reblog":
notifType = Helper.NotifType.BOOST;
if (notif_share) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_reblog));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_reblog));
} else {
canSendBroadCast = false;
}
break;
case "favourite":
notifType = Helper.NotifType.FAV;
if (notif_add) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_favourite));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_favourite));
} else {
canSendBroadCast = false;
}
break;
case "follow_request":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_follow_request));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_follow_request));
targeted_account = notification.getAccount().getId();
} else {
canSendBroadCast = false;
}
break;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), getString(R.string.notif_follow));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_follow));
targeted_account = notification.getAccount().getId();
} else {
canSendBroadCast = false;
}
break;
case "poll":
notifType = Helper.NotifType.POLL;
if (notif_poll) {
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
message = getString(R.string.notif_poll_self);
else
message = getString(R.string.notif_poll);
} else {
canSendBroadCast = false;
}
break;
default:
}
}
//Some others notification
final Intent intent = new Intent(LiveNotificationService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT);
intent.putExtra(Helper.PREF_KEY_ID, account.getId());
intent.putExtra(Helper.PREF_INSTANCE, account.getInstance());
if (targeted_account != null) {
intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
}
final String finalMessage = message;
Handler mainHandler = new Handler(Looper.getMainLooper());
Helper.NotifType finalNotifType = notifType;
Runnable myRunnable = () -> {
if (finalMessage != null) {
Glide.with(LiveNotificationService.this)
.asBitmap()
.load(notification.getAccount().getAvatar())
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
if (canSendBroadCast) {
b.putString("userIdService", account.getId());
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
intentBC.putExtras(b);
b.putParcelable("data", notification);
LocalBroadcastManager.getInstance(LiveNotificationService.this).sendBroadcast(intentBC);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notification.getId());
editor.apply();
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
assert e != null;
Helper.notify_user(LiveNotificationService.this, account, intent, BitmapFactory.decodeResource(getResources(),
getMainLogo(LiveNotificationService.this)), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
return false;
}
})
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
Helper.notify_user(LiveNotificationService.this, account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
};
mainHandler.post(myRunnable);
}
break;
case "delete":
event = Helper.EventStreaming.DELETE;
try {
dataId = response.getString("id");
b.putString("dataId", dataId);
} catch (JSONException ignored) {
}
break;
}
if (canSendBroadCast) {
b.putString("userIdService", account.getId());
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
intentBC.putExtra("eventStreaming", event);
intentBC.putExtras(b);
b.putParcelable("data", notification);
LocalBroadcastManager.getInstance(LiveNotificationService.this).sendBroadcast(intentBC);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notification.getId());
editor.apply();
}
}
} catch (Exception ignored) {
}
@ -520,7 +511,6 @@ public class LiveNotificationService extends Service implements NetworkStateRece
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t.getName().startsWith("notif_live_")) {
t.interrupt();
t = null;
}
}
Thread.currentThread().interrupt();