aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/SettingsActivity.java
blob: a8839bbe1dce9e1f03d275f8b23c44f72648f82d (plain)
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
/*
 * 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.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

/** Viewer password, photo and background durations, change instance. */
public final class SettingsActivity extends Activity {
	@Override
	protected void onCreate(Bundle state) {
		super.onCreate(state);
		LinearLayout box = Ui.column(this);
		box.addView(Ui.text(this, Prefs.server(), 18));
		box.addView(Ui.text(this, "Viewer password", 14));
		EditText pw = Ui.field(this, "Password", Ui.password());
		pw.setText(Prefs.password());
		box.addView(pw);
		box.addView(Ui.text(this, "Photo duration, seconds", 14));
		EditText photo = Ui.field(this, "8", InputType.TYPE_CLASS_NUMBER);
		photo.setText(String.valueOf(Prefs.photoSeconds()));
		box.addView(photo);
		box.addView(Ui.text(this, "Background change, seconds", 14));
		EditText bg = Ui.field(this, "15", InputType.TYPE_CLASS_NUMBER);
		bg.setText(String.valueOf(Prefs.backgroundSeconds()));
		box.addView(bg);
		LinearLayout row = Ui.row(this);
		row.addView(Ui.iconButton(this, R.drawable.ic_check, "Save", v -> save(pw.getText().toString(),
				photo.getText().toString(), bg.getText().toString())));
		row.addView(Ui.iconButton(this, R.drawable.ic_swap, "Change instance",
				v -> startActivity(new Intent(this, SetupActivity.class))));
		box.addView(row);
		setContentView(box);
		row.getChildAt(0).requestFocus();
	}

	private void save(String password, String photo, String background) {
		int p = seconds(photo), b = seconds(background);
		if (p < 1 || b < 1) {
			Toast.makeText(this, "Durations must be 1 to 600 seconds", Toast.LENGTH_LONG).show();
			return;
		}
		Prefs.setTimes(p, b);
		if (password.equals(Prefs.password())) {
			finish();
			return;
		}
		Api.async(() -> {
			Api.login(Prefs.server(), password);
			return true;
		}, ok -> {
			Prefs.setPassword(password);
			finish();
		}, e -> Toast.makeText(this, "Login failed: " + e.getMessage(), Toast.LENGTH_LONG).show());
	}

	private static int seconds(String s) {
		try {
			int v = Integer.parseInt(s.trim());
			return v <= 600 ? v : -1;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
}