Adding functional server list.
This commit is contained in:
parent
784b0e0c23
commit
3bf07ba4c5
@ -32,12 +32,16 @@ import android.os.IBinder;
|
||||
import android.util.TypedValue;
|
||||
import java.util.List;
|
||||
import me.sergiotarxz.bedrockstation.Servers;
|
||||
import me.sergiotarxz.bedrockstation.Server;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
interface Lambda {
|
||||
void l();
|
||||
};
|
||||
|
||||
private Options options;
|
||||
|
||||
private LinearLayout serverListInnerLayout = null;
|
||||
ProxyService proxyService = null;
|
||||
boolean mBound = false;
|
||||
private Button directConnectionButton;
|
||||
@ -85,6 +89,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
Intent intent = new Intent(this, ProxyService.class);
|
||||
intent.setAction(ProxyService.Action.END);
|
||||
this.startForegroundService(intent);
|
||||
try {
|
||||
Thread.sleep(250);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
private void startServiceProxy() {
|
||||
@ -122,20 +130,26 @@ public class MainActivity extends AppCompatActivity {
|
||||
EdgeToEdge.enable(this);
|
||||
LinearLayout layout = new LinearLayout(this);
|
||||
layout.setOrientation(LinearLayout.VERTICAL);
|
||||
layout.setLayoutParams(new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
|
||||
LayoutParams.MATCH_PARENT,
|
||||
LayoutParams.MATCH_PARENT));
|
||||
layout.setGravity(Gravity.CENTER);
|
||||
LayoutParams.MATCH_PARENT);
|
||||
layoutParams.setMargins(100, 100, 100, 100);
|
||||
layout.setLayoutParams(layoutParams);
|
||||
// layout.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
layout.setId(1);
|
||||
SQLiteDatabase db = new DB(this).getInstance();
|
||||
Options options = new Options(db);
|
||||
Servers servers = new Servers(db);
|
||||
addDirectConnection(layout, options);
|
||||
options = new Options(db);
|
||||
Servers servers = Servers.instance(db);
|
||||
addDirectConnection(layout);
|
||||
addServerList(layout, servers);
|
||||
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.setOnClickListener( (View view) -> {
|
||||
if(!serverStarted) {
|
||||
@ -144,6 +158,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS);
|
||||
return;
|
||||
}
|
||||
options.set(Options.HOST_SELECTED, options.get(Options.HOST_CACHE));
|
||||
options.set(Options.PORT_SELECTED, options.get(Options.PORT_CACHE));
|
||||
startServiceProxy();
|
||||
return;
|
||||
}
|
||||
@ -163,7 +179,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
editTextLayoutParams.width = 500;
|
||||
directConnectionButton.setLayoutParams(buttonLayoutParams);
|
||||
LinearLayout hostLayout = new LinearLayout(this);
|
||||
hostLayout.setGravity(Gravity.CENTER);
|
||||
// hostLayout.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
TextView hostIndicator = new TextView(this);
|
||||
hostIndicator.setText("IP: ");
|
||||
hostEditText = new EditText(this);
|
||||
@ -176,7 +192,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
hostLayout.addView(hostIndicator);
|
||||
hostLayout.addView(hostEditText);
|
||||
LinearLayout portLayout = new LinearLayout(this);
|
||||
portLayout.setGravity(Gravity.CENTER);
|
||||
// portLayout.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
TextView portIndicator = new TextView(this);
|
||||
portEditText = new EditText(this);
|
||||
onChangeEditText(portEditText, () -> {
|
||||
@ -188,6 +204,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
portEditText.setText(options.get(Options.PORT_CACHE));
|
||||
portLayout.addView(portIndicator);
|
||||
portLayout.addView(portEditText);
|
||||
center(header);
|
||||
layout.addView(header);
|
||||
layout.addView(hostLayout);
|
||||
layout.addView(portLayout);
|
||||
layout.addView(directConnectionButton);
|
||||
@ -204,25 +222,143 @@ public class MainActivity extends AppCompatActivity {
|
||||
serverListLayout.addView(labelServerList);
|
||||
fillServerList(serverListLayout, servers);
|
||||
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) {
|
||||
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) {
|
||||
TextView nothingHereYet = new TextView(this);
|
||||
nothingHereYet.setText("You did not add any servers yet.");
|
||||
center(nothingHereYet);
|
||||
serverListLayout.addView(nothingHereYet);
|
||||
return;
|
||||
serverListInnerLayout.addView(nothingHereYet);
|
||||
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) {
|
||||
view.setGravity(Gravity.CENTER);
|
||||
// view.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
}
|
||||
|
||||
private void center(LinearLayout view) {
|
||||
view.setGravity(Gravity.CENTER);
|
||||
// view.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
}
|
||||
|
||||
private ActivityResultLauncher<String> requestPermissionLauncher =
|
||||
|
@ -8,6 +8,8 @@ import android.content.ContentValues;
|
||||
public class Options {
|
||||
public static final String PORT_CACHE = "port_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;
|
||||
public Options(SQLiteDatabase db) {
|
||||
|
@ -135,11 +135,11 @@ public class ProxyService extends Service {
|
||||
public void startServer() {
|
||||
if (!isServerStarted()) {
|
||||
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;
|
||||
try {
|
||||
port = Integer.parseInt(options.get(Options.PORT_CACHE));
|
||||
port = Integer.parseInt(options.get(Options.PORT_SELECTED));
|
||||
} catch (Exception e) {
|
||||
Log.e("bedrockstation", Log.getStackTraceString(e));
|
||||
Toast.makeText(this, "Port is not a number", Toast.LENGTH_LONG).show();
|
||||
|
@ -45,4 +45,8 @@ public class Server {
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
@ -4,16 +4,70 @@ import android.database.sqlite.SQLiteDatabase;
|
||||
import me.sergiotarxz.bedrockstation.DBContract;
|
||||
import me.sergiotarxz.bedrockstation.Server;
|
||||
import android.database.Cursor;
|
||||
import android.content.ContentValues;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Servers {
|
||||
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) {
|
||||
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() {
|
||||
String[] projection = {
|
||||
DBContract.Servers.COLUMN_NAME_ID,
|
||||
|
Loading…
Reference in New Issue
Block a user