240 lines
8.7 KiB
Java
240 lines
8.7 KiB
Java
package tv.luma.launcher;
|
|
|
|
import android.content.*;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Color;
|
|
import android.graphics.drawable.GradientDrawable;
|
|
import android.net.Uri;
|
|
import android.os.*;
|
|
import android.view.*;
|
|
import android.widget.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
|
|
/** Runs only while displayed. Two sampled images maximum, or one platform video decoder. */
|
|
final class SaverView extends FrameLayout {
|
|
private final SharedPreferences prefs;
|
|
private final SaverMedia media;
|
|
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
private final ExecutorService worker = Executors.newSingleThreadExecutor();
|
|
private final SaverPictureView picture;
|
|
private VideoView video;
|
|
private TextView title, credit, clock;
|
|
private List<SaverMedia.Entry> entries;
|
|
private Bitmap preloaded;
|
|
private int nextIndex = 0, generation = 1, failed = 0;
|
|
private boolean running = true, transitioning = false;
|
|
private final Runnable next =
|
|
() -> {
|
|
if (running) advance();
|
|
};
|
|
private final Runnable tick =
|
|
new Runnable() {
|
|
public void run() {
|
|
if (!running) return;
|
|
clock.setText(
|
|
android.text.format.DateFormat.getTimeFormat(getContext()).format(new Date()));
|
|
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) clock.getLayoutParams();
|
|
lp.bottomMargin = Ui.dp(getContext(), 28 + (System.currentTimeMillis() / 60000 % 3) * 5);
|
|
clock.setLayoutParams(lp);
|
|
handler.postDelayed(this, 60000 - System.currentTimeMillis() % 60000);
|
|
}
|
|
};
|
|
|
|
SaverView(Context context) {
|
|
super(context);
|
|
prefs = context.getSharedPreferences("luma", Context.MODE_PRIVATE);
|
|
media = new SaverMedia(context);
|
|
String mode = prefs.getString("saverMode", "scenic");
|
|
setBackgroundColor(Color.BLACK);
|
|
setContentDescription("Luma screensaver");
|
|
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
|
|
setFocusableInTouchMode(true);
|
|
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
|
|
String framing = prefs.getString("saverFraming", "auto");
|
|
picture =
|
|
new SaverPictureView(
|
|
context, framing.equals("fit") || framing.equals("auto") && mode.equals("art"));
|
|
addView(picture, new LayoutParams(-1, -1));
|
|
View dim = new View(context);
|
|
dim.setBackgroundColor(Color.BLACK);
|
|
dim.setAlpha(prefs.getInt("saverDim", 0) / 100f);
|
|
addView(dim, new LayoutParams(-1, -1));
|
|
View scrim = new View(context);
|
|
scrim.setBackground(
|
|
new GradientDrawable(
|
|
GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0x00000000, 0xD9000000}));
|
|
addView(scrim, new LayoutParams(-1, Ui.dp(context, 170), Gravity.BOTTOM));
|
|
LinearLayout captions = new LinearLayout(context);
|
|
captions.setOrientation(LinearLayout.VERTICAL);
|
|
title = Ui.text(context, "", 17, Ui.WHITE);
|
|
title.setMaxLines(2);
|
|
credit = Ui.text(context, "", 10, 0xFFDEE3EB);
|
|
credit.setMaxLines(2);
|
|
credit.setPadding(0, Ui.dp(context, 7), 0, 0);
|
|
title.setShadowLayer(Ui.dp(context, 3), 0, Ui.dp(context, 1), Color.BLACK);
|
|
credit.setShadowLayer(Ui.dp(context, 2), 0, Ui.dp(context, 1), Color.BLACK);
|
|
captions.addView(title);
|
|
captions.addView(credit);
|
|
LayoutParams captionLp =
|
|
new LayoutParams(Ui.dp(context, 650), -2, Gravity.BOTTOM | Gravity.LEFT);
|
|
captionLp.leftMargin = Ui.dp(context, 32);
|
|
captionLp.bottomMargin = Ui.dp(context, 30);
|
|
addView(captions, captionLp);
|
|
captions.setVisibility(prefs.getBoolean("saverCaptions", true) ? VISIBLE : GONE);
|
|
clock = Ui.text(context, "", 27, Ui.WHITE);
|
|
clock.setShadowLayer(Ui.dp(context, 3), 0, Ui.dp(context, 1), Color.BLACK);
|
|
LayoutParams clockLp = new LayoutParams(-2, -2, Gravity.BOTTOM | Gravity.RIGHT);
|
|
clockLp.rightMargin = Ui.dp(context, 32);
|
|
clockLp.bottomMargin = Ui.dp(context, 28);
|
|
addView(clock, clockLp);
|
|
clock.setVisibility(prefs.getBoolean("saverClock", false) ? VISIBLE : GONE);
|
|
if (clock.getVisibility() == VISIBLE) handler.post(tick);
|
|
if (!prefs.getBoolean("saverCaptions", true) && !prefs.getBoolean("saverClock", false))
|
|
scrim.setVisibility(GONE);
|
|
if (mode.equals("video") && startVideo()) return;
|
|
startPhotos(mode.equals("art") ? "art" : "scenic");
|
|
}
|
|
|
|
private void startPhotos(String album) {
|
|
entries = media.entries(album);
|
|
if (entries.isEmpty()) entries = media.entries("scenic");
|
|
if (prefs.getBoolean("saverShuffle", true)) Collections.shuffle(entries);
|
|
loadInitial();
|
|
}
|
|
|
|
private void loadInitial() {
|
|
if (entries.isEmpty()) return;
|
|
int g = generation;
|
|
worker.execute(
|
|
() -> {
|
|
for (int i = 0; i < entries.size(); i++) {
|
|
Bitmap bitmap = media.bitmap(entries.get(i));
|
|
if (bitmap == null) continue;
|
|
final int index = i;
|
|
handler.post(
|
|
() -> {
|
|
if (!running || g != generation) return;
|
|
picture.display(bitmap);
|
|
caption(entries.get(index));
|
|
nextIndex = (index + 1) % entries.size();
|
|
preload();
|
|
});
|
|
return;
|
|
}
|
|
handler.post(
|
|
() -> {
|
|
if (running && g == generation) {
|
|
title.setText("Artwork unavailable");
|
|
credit.setText("Choose another collection in screensaver settings.");
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
private void caption(SaverMedia.Entry e) {
|
|
title.setText(e.title);
|
|
credit.setText(e.credit);
|
|
}
|
|
|
|
private void preload() {
|
|
if (!running || entries.isEmpty()) return;
|
|
int g = generation, index = nextIndex;
|
|
worker.execute(
|
|
() -> {
|
|
Bitmap b = media.bitmap(entries.get(index));
|
|
handler.post(
|
|
() -> {
|
|
if (!running || g != generation) return;
|
|
if (b == null) {
|
|
nextIndex = (index + 1) % entries.size();
|
|
if (++failed < entries.size()) preload();
|
|
return;
|
|
}
|
|
failed = 0;
|
|
preloaded = b;
|
|
handler.removeCallbacks(next);
|
|
handler.postDelayed(next, prefs.getInt("saverInterval", 30) * 1000L);
|
|
});
|
|
});
|
|
}
|
|
|
|
private void advance() {
|
|
if (transitioning || preloaded == null) return;
|
|
transitioning = true;
|
|
Bitmap ready = preloaded;
|
|
preloaded = null;
|
|
SaverMedia.Entry entry = entries.get(nextIndex);
|
|
nextIndex = (nextIndex + 1) % entries.size();
|
|
picture.transition(
|
|
ready,
|
|
prefs.getBoolean("motion", true),
|
|
() -> {
|
|
if (!running) return;
|
|
caption(entry);
|
|
transitioning = false;
|
|
preload();
|
|
});
|
|
}
|
|
|
|
private boolean startVideo() {
|
|
Uri uri = null;
|
|
String source = prefs.getString("saverVideoSource", "local");
|
|
if (source.equals("local") && media.videoFile().isFile()) uri = Uri.fromFile(media.videoFile());
|
|
else if (source.equals("document")) uri = Uri.parse(prefs.getString("saverVideoUri", ""));
|
|
else if (source.equals("stream")) uri = Uri.parse(prefs.getString("saverVideoUrl", ""));
|
|
if (uri == null || uri.toString().isEmpty()) return false;
|
|
video = new VideoView(getContext());
|
|
video.setAudioFocusRequest(
|
|
prefs.getBoolean("saverVideoAudio", false)
|
|
? android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
|
|
: android.media.AudioManager.AUDIOFOCUS_NONE);
|
|
addView(video, 0, new LayoutParams(-1, -1, Gravity.CENTER));
|
|
picture.setVisibility(GONE);
|
|
title.setText("Nature loop");
|
|
credit.setText(
|
|
source.equals("local")
|
|
? prefs.getString("saverVideoCredit", "Local video")
|
|
: "Your video source");
|
|
video.setOnPreparedListener(
|
|
player -> {
|
|
if (!running || video == null) return;
|
|
player.setLooping(true);
|
|
boolean audio = prefs.getBoolean("saverVideoAudio", false);
|
|
player.setVolume(audio ? 1 : 0, audio ? 1 : 0);
|
|
video.start();
|
|
});
|
|
video.setOnErrorListener(
|
|
(player, what, extra) -> {
|
|
if (!running || video == null) return true;
|
|
video.stopPlayback();
|
|
removeView(video);
|
|
video = null;
|
|
picture.setVisibility(VISIBLE);
|
|
startPhotos("scenic");
|
|
return true;
|
|
});
|
|
video.setVideoURI(uri);
|
|
return true;
|
|
}
|
|
|
|
void stop() {
|
|
if (!running) return;
|
|
running = false;
|
|
generation++;
|
|
handler.removeCallbacksAndMessages(null);
|
|
picture.release();
|
|
preloaded = null;
|
|
if (video != null) {
|
|
video.stopPlayback();
|
|
video = null;
|
|
}
|
|
worker.shutdownNow();
|
|
}
|
|
|
|
@Override
|
|
protected void onDetachedFromWindow() {
|
|
stop();
|
|
super.onDetachedFromWindow();
|
|
}
|
|
}
|