1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*
* mygram-droid: Android TV client for Foldergram.
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program 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.
*/
package mygram.droid;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
/**
* Gate shown on every start and return to the app: the PIN prompt when a PIN
* is set, then a wait while Foldergram indexes, which makes it unusably slow.
*/
public final class SplashActivity extends Activity {
private static final long POLL_MS = 5000;
private final Handler handler = new Handler(Looper.getMainLooper());
private final Runnable poll = this::checkScan;
private LinearLayout box;
private TextView status;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
box = Ui.column(this);
ImageView logo = new ImageView(this);
logo.setImageResource(R.drawable.ic_launcher);
logo.setContentDescription("mygram");
int s = Ui.dp(this, 96);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(s, s);
lp.bottomMargin = Ui.dp(this, 24);
box.addView(logo, lp);
setContentView(box);
if (Prefs.hasPin())
askPin();
else
checkScan();
}
private void askPin() {
EditText pin = Ui.field(this, "PIN", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
box.addView(pin);
View ok = Ui.iconButton(this, R.drawable.ic_check, "Unlock", v -> {
if (Prefs.checkPin(pin.getText().toString())) {
box.removeViews(1, box.getChildCount() - 1);
checkScan();
} else {
pin.setText("");
Toast.makeText(this, "Wrong PIN", Toast.LENGTH_SHORT).show();
}
});
box.addView(ok);
// The keyboard's done key unlocks; the D-pad cannot leave the keyboard.
pin.setOnEditorActionListener((v, action, event) -> ok.performClick());
pin.requestFocus();
}
/** Waits while the library is being indexed; lets the user in once it is idle. */
private void checkScan() {
if (Prefs.server() == null) { // first run: setup comes next
done();
return;
}
Api.async(() -> Api.call("GET", "/scan-progress"), o -> {
if (isDestroyed())
return;
if (!o.optBoolean("isScanning")) {
done();
return;
}
showScan(o);
handler.postDelayed(poll, POLL_MS);
}, e -> {
if (isDestroyed())
return;
if (status == null)
done(); // unreachable at the first check: Home reports it with Retry
else
handler.postDelayed(poll, POLL_MS); // mid-scan the server is slow; keep waiting
});
}
private void showScan(JSONObject o) {
if (status == null) {
box.addView(new ProgressBar(this));
box.addView(Ui.text(this, "Library is indexing", 24));
status = Ui.text(this, "", 16);
box.addView(status);
}
String msg = Post.str(o, "currentPhaseMessage");
if (msg == null)
msg = o.optString("phase");
int done = o.optInt("processedDerivativeJobs"), total = o.optInt("queuedDerivativeJobs");
status.setText(total > 0 ? msg + " " + done + " / " + total : msg);
}
private void done() {
App.locked = false;
finish();
}
/** Back leaves the app instead of revealing the screen underneath. */
@Override
@SuppressWarnings("deprecation") // no predictive back on this TV; onBackPressed still fires
public void onBackPressed() {
finishAffinity();
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}
|