Adding functional server list.

This commit is contained in:
sergiotarxz 2024-08-05 00:19:14 +02:00
parent 784b0e0c23
commit 3bf07ba4c5
5 changed files with 215 additions and 19 deletions

View File

@ -32,12 +32,16 @@ import android.os.IBinder;
import android.util.TypedValue; import android.util.TypedValue;
import java.util.List; import java.util.List;
import me.sergiotarxz.bedrockstation.Servers; import me.sergiotarxz.bedrockstation.Servers;
import me.sergiotarxz.bedrockstation.Server;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
interface Lambda { interface Lambda {
void l(); void l();
}; };
private Options options;
private LinearLayout serverListInnerLayout = null;
ProxyService proxyService = null; ProxyService proxyService = null;
boolean mBound = false; boolean mBound = false;
private Button directConnectionButton; private Button directConnectionButton;
@ -85,6 +89,10 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, ProxyService.class); Intent intent = new Intent(this, ProxyService.class);
intent.setAction(ProxyService.Action.END); intent.setAction(ProxyService.Action.END);
this.startForegroundService(intent); this.startForegroundService(intent);
try {
Thread.sleep(250);
} catch (Exception e) {
}
} }
private void startServiceProxy() { private void startServiceProxy() {
@ -122,20 +130,26 @@ public class MainActivity extends AppCompatActivity {
EdgeToEdge.enable(this); EdgeToEdge.enable(this);
LinearLayout layout = new LinearLayout(this); LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT)); LayoutParams.MATCH_PARENT);
layout.setGravity(Gravity.CENTER); layoutParams.setMargins(100, 100, 100, 100);
layout.setLayoutParams(layoutParams);
// layout.setGravity(Gravity.CENTER_HORIZONTAL);
layout.setId(1); layout.setId(1);
SQLiteDatabase db = new DB(this).getInstance(); SQLiteDatabase db = new DB(this).getInstance();
Options options = new Options(db); options = new Options(db);
Servers servers = new Servers(db); Servers servers = Servers.instance(db);
addDirectConnection(layout, options); addDirectConnection(layout);
addServerList(layout, servers); addServerList(layout, servers);
setContentView(layout); setContentView(layout);
} }
private void addDirectConnection(LinearLayout layout, Options options) { private void addDirectConnection(LinearLayout layout) {
TextView header = new TextView(this);
header.setTextSize(TypedValue.COMPLEX_UNIT_PX, 70);
header.setText("Connect without saving");
directConnectionButton = new Button(this); directConnectionButton = new Button(this);
directConnectionButton.setOnClickListener( (View view) -> { directConnectionButton.setOnClickListener( (View view) -> {
if(!serverStarted) { if(!serverStarted) {
@ -144,6 +158,8 @@ public class MainActivity extends AppCompatActivity {
requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS); requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS);
return; return;
} }
options.set(Options.HOST_SELECTED, options.get(Options.HOST_CACHE));
options.set(Options.PORT_SELECTED, options.get(Options.PORT_CACHE));
startServiceProxy(); startServiceProxy();
return; return;
} }
@ -163,7 +179,7 @@ public class MainActivity extends AppCompatActivity {
editTextLayoutParams.width = 500; editTextLayoutParams.width = 500;
directConnectionButton.setLayoutParams(buttonLayoutParams); directConnectionButton.setLayoutParams(buttonLayoutParams);
LinearLayout hostLayout = new LinearLayout(this); LinearLayout hostLayout = new LinearLayout(this);
hostLayout.setGravity(Gravity.CENTER); // hostLayout.setGravity(Gravity.CENTER_HORIZONTAL);
TextView hostIndicator = new TextView(this); TextView hostIndicator = new TextView(this);
hostIndicator.setText("IP: "); hostIndicator.setText("IP: ");
hostEditText = new EditText(this); hostEditText = new EditText(this);
@ -176,7 +192,7 @@ public class MainActivity extends AppCompatActivity {
hostLayout.addView(hostIndicator); hostLayout.addView(hostIndicator);
hostLayout.addView(hostEditText); hostLayout.addView(hostEditText);
LinearLayout portLayout = new LinearLayout(this); LinearLayout portLayout = new LinearLayout(this);
portLayout.setGravity(Gravity.CENTER); // portLayout.setGravity(Gravity.CENTER_HORIZONTAL);
TextView portIndicator = new TextView(this); TextView portIndicator = new TextView(this);
portEditText = new EditText(this); portEditText = new EditText(this);
onChangeEditText(portEditText, () -> { onChangeEditText(portEditText, () -> {
@ -188,6 +204,8 @@ public class MainActivity extends AppCompatActivity {
portEditText.setText(options.get(Options.PORT_CACHE)); portEditText.setText(options.get(Options.PORT_CACHE));
portLayout.addView(portIndicator); portLayout.addView(portIndicator);
portLayout.addView(portEditText); portLayout.addView(portEditText);
center(header);
layout.addView(header);
layout.addView(hostLayout); layout.addView(hostLayout);
layout.addView(portLayout); layout.addView(portLayout);
layout.addView(directConnectionButton); layout.addView(directConnectionButton);
@ -204,25 +222,143 @@ public class MainActivity extends AppCompatActivity {
serverListLayout.addView(labelServerList); serverListLayout.addView(labelServerList);
fillServerList(serverListLayout, servers); fillServerList(serverListLayout, servers);
layout.addView(serverListLayout); layout.addView(serverListLayout);
addCreateNewServerForm(layout, servers);
}
private void addCreateNewServerForm(LinearLayout layout, Servers servers) {
LinearLayout form = new LinearLayout(this);
center(form);
form.setOrientation(LinearLayout.VERTICAL);
TextView header = new TextView(this);
header.setText("Create a new server");
header.setTextSize(TypedValue.COMPLEX_UNIT_PX, 70);
center(header);
LayoutParams editTextLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
editTextLayoutParams.width = 500;
LinearLayout titleLayout = new LinearLayout(this);
TextView titleLabel = new TextView(this);
EditText titleEdit = new EditText(this);
titleEdit.setLayoutParams(editTextLayoutParams);
titleLabel.setText("Server name: ");
titleLayout.addView(titleLabel);
titleLayout.addView(titleEdit);
center(titleLayout);
LinearLayout hostLayout = new LinearLayout(this);
TextView hostLabel = new TextView(this);
EditText hostEdit = new EditText(this);
hostEdit.setLayoutParams(editTextLayoutParams);
hostLabel.setText("Host: ");
hostLayout.addView(hostLabel);
hostLayout.addView(hostEdit);
center(hostLayout);
LinearLayout portLayout = new LinearLayout(this);
TextView portLabel = new TextView(this);
EditText portEdit = new EditText(this);
portEdit.setLayoutParams(editTextLayoutParams);
portLabel.setText("Port: ");
portLayout.addView(portLabel);
portLayout.addView(portEdit);
center(portLayout);
Button create = new Button(this);
create.setOnClickListener( (View view) -> {
int port;
String title;
String host;
try {
port = Integer.parseInt(portEdit.getText().toString());
} catch (Exception e) {
Toast.makeText(this, "Port must be a number", Toast.LENGTH_LONG).show();
return;
}
title = titleEdit.getText().toString();
host = hostEdit.getText().toString();
if (host.indexOf("\n") != -1) {
Toast.makeText(this, "Invalid host", Toast.LENGTH_LONG).show();
return;
}
if (title.indexOf("\n") != -1) {
Toast.makeText(this, "Invalid title", Toast.LENGTH_LONG).show();
return;
}
servers.add(new Server(title, host, port));
titleEdit.setText("");
hostEdit.setText("");
portEdit.setText("");
fillServerList(null, servers);
});
create.setText("Add this server");
form.addView(header);
form.addView(titleLayout);
form.addView(hostLayout);
form.addView(portLayout);
form.addView(create);
layout.addView(form);
} }
public void fillServerList(LinearLayout serverListLayout, Servers servers) { public void fillServerList(LinearLayout serverListLayout, Servers servers) {
List<Server> serverList = servers.all(); boolean firstSetup = false;
if (serverListInnerLayout == null) {
firstSetup = true;
serverListInnerLayout = new LinearLayout(this);
serverListInnerLayout.setOrientation(LinearLayout.VERTICAL);
}
do {
List<Server> serverList = servers.all();
serverListInnerLayout.removeViews(0, serverListInnerLayout.getChildCount());
if (serverList.size() < 1) { if (serverList.size() < 1) {
TextView nothingHereYet = new TextView(this); TextView nothingHereYet = new TextView(this);
nothingHereYet.setText("You did not add any servers yet."); nothingHereYet.setText("You did not add any servers yet.");
center(nothingHereYet); center(nothingHereYet);
serverListLayout.addView(nothingHereYet); serverListInnerLayout.addView(nothingHereYet);
return; break;
}
for (Server server : serverList) {
TextView title = new TextView(this);
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);
title.setText(server.getTitle());
TextView host = new TextView(this);
host.setText(server.getHost() + ":" + server.getPort());
serverListInnerLayout.addView(title);
serverListInnerLayout.addView(host);
LinearLayout buttons = new LinearLayout(this);
Button connect = new Button(this);
connect.setText("Connect");
Button delete = new Button(this);
delete.setText("Delete");
buttons.addView(connect);
connect.setOnClickListener( (View view) -> {
if (serverStarted) {
finishServiceProxy();
}
options.set(Options.HOST_SELECTED, server.getHost());
options.set(Options.PORT_SELECTED, ""+server.getPort());
startServiceProxy();
});
delete.setOnClickListener( (View view) -> {
servers.delete(server);
fillServerList(null, servers);
});
buttons.addView(delete);
serverListInnerLayout.addView(buttons);
}
} while (false);
if (firstSetup) {
serverListLayout.addView(serverListInnerLayout);
} }
} }
private void center(TextView view) { private void center(TextView view) {
view.setGravity(Gravity.CENTER); // view.setGravity(Gravity.CENTER_HORIZONTAL);
} }
private void center(LinearLayout view) { private void center(LinearLayout view) {
view.setGravity(Gravity.CENTER); // view.setGravity(Gravity.CENTER_HORIZONTAL);
} }
private ActivityResultLauncher<String> requestPermissionLauncher = private ActivityResultLauncher<String> requestPermissionLauncher =

View File

@ -8,6 +8,8 @@ import android.content.ContentValues;
public class Options { public class Options {
public static final String PORT_CACHE = "port_cache"; public static final String PORT_CACHE = "port_cache";
public static final String HOST_CACHE = "host_cache"; public static final String HOST_CACHE = "host_cache";
public static final String PORT_SELECTED = "port_selected";
public static final String HOST_SELECTED = "host_selected";
private SQLiteDatabase db = null; private SQLiteDatabase db = null;
public Options(SQLiteDatabase db) { public Options(SQLiteDatabase db) {

View File

@ -135,11 +135,11 @@ public class ProxyService extends Service {
public void startServer() { public void startServer() {
if (!isServerStarted()) { if (!isServerStarted()) {
Options options = new Options(new DB(this).getInstance()); Options options = new Options(new DB(this).getInstance());
String host = options.get(Options.HOST_CACHE); String host = options.get(Options.HOST_SELECTED);
int port = 0; int port = 0;
try { try {
port = Integer.parseInt(options.get(Options.PORT_CACHE)); port = Integer.parseInt(options.get(Options.PORT_SELECTED));
} catch (Exception e) { } catch (Exception e) {
Log.e("bedrockstation", Log.getStackTraceString(e)); Log.e("bedrockstation", Log.getStackTraceString(e));
Toast.makeText(this, "Port is not a number", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Port is not a number", Toast.LENGTH_LONG).show();

View File

@ -45,4 +45,8 @@ public class Server {
public void setPort(int port) { public void setPort(int port) {
this.port = port; this.port = port;
} }
public void setId(int id) {
this.id = id;
}
} }

View File

@ -4,16 +4,70 @@ import android.database.sqlite.SQLiteDatabase;
import me.sergiotarxz.bedrockstation.DBContract; import me.sergiotarxz.bedrockstation.DBContract;
import me.sergiotarxz.bedrockstation.Server; import me.sergiotarxz.bedrockstation.Server;
import android.database.Cursor; import android.database.Cursor;
import android.content.ContentValues;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
public class Servers { public class Servers {
private SQLiteDatabase db = null; private SQLiteDatabase db = null;
private static Servers servers = null;
public static Servers instance(SQLiteDatabase db) {
if (servers != null) {
return servers;
}
servers = new Servers(db);
return servers;
}
public Servers(SQLiteDatabase db) { public Servers(SQLiteDatabase db) {
this.db = db; this.db = db;
} }
public void delete(Server server) {
String selection = DBContract.Servers.COLUMN_NAME_ID + " = ?";
String[] selectionArgs = { server.getId()+"" };
db.delete(
DBContract.Servers.TABLE_NAME,
selection,
selectionArgs
);
}
public void update_or_add(Server server) {
if (server.getId() == null) {
add(server);
return;
}
update(server);
return;
}
public void add(Server server) {
ContentValues values = new ContentValues();
values.put(DBContract.Servers.COLUMN_NAME_TITLE, server.getTitle());
values.put(DBContract.Servers.COLUMN_NAME_HOST, server.getHost());
values.put(DBContract.Servers.COLUMN_NAME_PORT, server.getPort());
server.setId((int) db.insert(DBContract.Servers.TABLE_NAME, null, values));
}
public void update(Server server) {
ContentValues values = new ContentValues();
values.put(DBContract.Servers.COLUMN_NAME_TITLE, server.getTitle());
values.put(DBContract.Servers.COLUMN_NAME_HOST, server.getHost());
values.put(DBContract.Servers.COLUMN_NAME_PORT, server.getPort());
String selection = DBContract.Servers.COLUMN_NAME_ID + " = ?";
String[] selectionArgs = { server.getId()+"" };
db.update(
DBContract.Servers.TABLE_NAME,
values,
selection,
selectionArgs
);
}
public List<Server> all() { public List<Server> all() {
String[] projection = { String[] projection = {
DBContract.Servers.COLUMN_NAME_ID, DBContract.Servers.COLUMN_NAME_ID,