From 784b0e0c23085a2a73fd9a273e4412863c7493c6 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sat, 3 Aug 2024 18:43:10 +0200 Subject: [PATCH] Adding classes for server instance and server db search. --- .../me/sergiotarxz/bedrockstation/DB.java | 7 ++ .../bedrockstation/DBContract.java | 8 ++ .../bedrockstation/MainActivity.java | 61 ++++++++++-- .../me/sergiotarxz/bedrockstation/Server.java | 48 ++++++++++ .../sergiotarxz/bedrockstation/Servers.java | 95 +++++++++++++++++++ 5 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/me/sergiotarxz/bedrockstation/Server.java create mode 100644 app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java diff --git a/app/src/main/java/me/sergiotarxz/bedrockstation/DB.java b/app/src/main/java/me/sergiotarxz/bedrockstation/DB.java index d214728..71159db 100644 --- a/app/src/main/java/me/sergiotarxz/bedrockstation/DB.java +++ b/app/src/main/java/me/sergiotarxz/bedrockstation/DB.java @@ -15,6 +15,13 @@ public class DB extends SQLiteOpenHelper { + ");", "INSERT OR IGNORE INTO options(key, value) VALUES(\"host_cache\", \"192.168.2.1\");", "INSERT OR IGNORE INTO options(key, value) VALUES(\"port_cache\", \"19132\");", + "CREATE TABLE servers (\n" + + "id INTEGER PRIMARY KEY,\n" + + "title TEXT NOT NULL,\n" + + "host TEXT NOT NULL,\n" + + "port INTEGER NOT NULL\n" + + ");", + }; public DB(Context context) { diff --git a/app/src/main/java/me/sergiotarxz/bedrockstation/DBContract.java b/app/src/main/java/me/sergiotarxz/bedrockstation/DBContract.java index 4ef1a4a..6a7accb 100644 --- a/app/src/main/java/me/sergiotarxz/bedrockstation/DBContract.java +++ b/app/src/main/java/me/sergiotarxz/bedrockstation/DBContract.java @@ -10,4 +10,12 @@ public final class DBContract { public static final String COLUMN_NAME_KEY = "key"; public static final String COLUMN_NAME_VALUE = "value"; } + + public static class Servers implements BaseColumns { + public static final String TABLE_NAME = "servers"; + public static final String COLUMN_NAME_ID = "id"; + public static final String COLUMN_NAME_TITLE = "title"; + public static final String COLUMN_NAME_HOST = "host"; + public static final String COLUMN_NAME_PORT = "port"; + } } diff --git a/app/src/main/java/me/sergiotarxz/bedrockstation/MainActivity.java b/app/src/main/java/me/sergiotarxz/bedrockstation/MainActivity.java index be932d8..d4b01ab 100644 --- a/app/src/main/java/me/sergiotarxz/bedrockstation/MainActivity.java +++ b/app/src/main/java/me/sergiotarxz/bedrockstation/MainActivity.java @@ -29,6 +29,9 @@ import android.content.ServiceConnection; import android.content.Context; import android.content.ComponentName; import android.os.IBinder; +import android.util.TypedValue; +import java.util.List; +import me.sergiotarxz.bedrockstation.Servers; public class MainActivity extends AppCompatActivity { interface Lambda { @@ -37,7 +40,7 @@ public class MainActivity extends AppCompatActivity { ProxyService proxyService = null; boolean mBound = false; - private Button button; + private Button directConnectionButton; boolean serverStarted = false; private EditText hostEditText = null; private EditText portEditText = null; @@ -104,12 +107,12 @@ public class MainActivity extends AppCompatActivity { } public void onStartProxyService() { - button.setText("Finish Proxy"); + directConnectionButton.setText("Finish Proxy"); serverStarted = true; } public void onFinishProxyService() { - button.setText("Start Proxy"); + directConnectionButton.setText("Direct connection"); serverStarted = false; } @@ -126,8 +129,15 @@ public class MainActivity extends AppCompatActivity { layout.setId(1); SQLiteDatabase db = new DB(this).getInstance(); Options options = new Options(db); - button = new Button(this); - button.setOnClickListener( (View view) -> { + Servers servers = new Servers(db); + addDirectConnection(layout, options); + addServerList(layout, servers); + setContentView(layout); + } + + private void addDirectConnection(LinearLayout layout, Options options) { + directConnectionButton = new Button(this); + directConnectionButton.setOnClickListener( (View view) -> { if(!serverStarted) { if (!getSystemService(NotificationManager.class) .areNotificationsEnabled()) { @@ -140,9 +150,9 @@ public class MainActivity extends AppCompatActivity { finishServiceProxy(); }); if (serverStarted) { - button.setText("Finish Proxy"); + directConnectionButton.setText("Finish Proxy"); } else { - button.setText("Start Proxy"); + directConnectionButton.setText("Start Proxy"); } LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, @@ -151,7 +161,7 @@ public class MainActivity extends AppCompatActivity { LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); editTextLayoutParams.width = 500; - button.setLayoutParams(buttonLayoutParams); + directConnectionButton.setLayoutParams(buttonLayoutParams); LinearLayout hostLayout = new LinearLayout(this); hostLayout.setGravity(Gravity.CENTER); TextView hostIndicator = new TextView(this); @@ -180,8 +190,39 @@ public class MainActivity extends AppCompatActivity { portLayout.addView(portEditText); layout.addView(hostLayout); layout.addView(portLayout); - layout.addView(button); - setContentView(layout); + layout.addView(directConnectionButton); + } + + private void addServerList(LinearLayout layout, Servers servers) { + LinearLayout serverListLayout = new LinearLayout(this); + serverListLayout.setOrientation(LinearLayout.VERTICAL); + center(serverListLayout); + TextView labelServerList = new TextView(this); + center(labelServerList); + labelServerList.setText("List of added servers"); + labelServerList.setTextSize(TypedValue.COMPLEX_UNIT_PX, 70); + serverListLayout.addView(labelServerList); + fillServerList(serverListLayout, servers); + layout.addView(serverListLayout); + } + + public void fillServerList(LinearLayout serverListLayout, Servers servers) { + List serverList = servers.all(); + if (serverList.size() < 1) { + TextView nothingHereYet = new TextView(this); + nothingHereYet.setText("You did not add any servers yet."); + center(nothingHereYet); + serverListLayout.addView(nothingHereYet); + return; + } + } + + private void center(TextView view) { + view.setGravity(Gravity.CENTER); + } + + private void center(LinearLayout view) { + view.setGravity(Gravity.CENTER); } private ActivityResultLauncher requestPermissionLauncher = diff --git a/app/src/main/java/me/sergiotarxz/bedrockstation/Server.java b/app/src/main/java/me/sergiotarxz/bedrockstation/Server.java new file mode 100644 index 0000000..3893f07 --- /dev/null +++ b/app/src/main/java/me/sergiotarxz/bedrockstation/Server.java @@ -0,0 +1,48 @@ +package me.sergiotarxz.bedrockstation; +public class Server { + private Integer id = null; + private String title = null; + private String host = null; + private int port; + + public Server(int id, String title, String host, int port) { + this.id = id; + this.title = title; + this.host = host; + this.port = port; + } + + public Server(String title, String host, int port) { + this.title = title; + this.host = host; + this.port = port; + } + + public Integer getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java b/app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java new file mode 100644 index 0000000..56c4d5c --- /dev/null +++ b/app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java @@ -0,0 +1,95 @@ +package me.sergiotarxz.bedrockstation; + +import android.database.sqlite.SQLiteDatabase; +import me.sergiotarxz.bedrockstation.DBContract; +import me.sergiotarxz.bedrockstation.Server; +import android.database.Cursor; +import java.util.List; +import java.util.ArrayList; + +public class Servers { + private SQLiteDatabase db = null; + + public Servers(SQLiteDatabase db) { + this.db = db; + } + + public List all() { + String[] projection = { + DBContract.Servers.COLUMN_NAME_ID, + DBContract.Servers.COLUMN_NAME_TITLE, + DBContract.Servers.COLUMN_NAME_HOST, + DBContract.Servers.COLUMN_NAME_PORT + }; + String selection = ""; + String[] selectionArgs = {}; + Cursor cursor = db.query( + DBContract.Servers.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + "" + ); + ArrayList array = new ArrayList(); + while (cursor.moveToNext()) { + int id = cursor.getInt( + columnName(cursor, DBContract.Servers.COLUMN_NAME_ID) + ); + String title = cursor.getString( + columnName(cursor, DBContract.Servers.COLUMN_NAME_TITLE) + ); + String host = cursor.getString( + columnName(cursor, DBContract.Servers.COLUMN_NAME_HOST) + ); + int port = cursor.getInt( + columnName(cursor, DBContract.Servers.COLUMN_NAME_PORT) + ); + array.add(new Server(id, title, host, port)); + } + cursor.close(); + return array; + } + + public Server get(int id) { + String[] projection = { + DBContract.Servers.COLUMN_NAME_ID, + DBContract.Servers.COLUMN_NAME_TITLE, + DBContract.Servers.COLUMN_NAME_HOST, + DBContract.Servers.COLUMN_NAME_PORT + }; + String selection = DBContract.Servers.COLUMN_NAME_ID + " = ?"; + String[] selectionArgs = { id+"" }; + Cursor cursor = db.query( + DBContract.Servers.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + "" + ); + if (!cursor.moveToNext()) { + return null; + } + id = cursor.getInt( + columnName(cursor, DBContract.Servers.COLUMN_NAME_ID) + ); + String title = cursor.getString( + columnName(cursor, DBContract.Servers.COLUMN_NAME_TITLE) + ); + String host = cursor.getString( + columnName(cursor, DBContract.Servers.COLUMN_NAME_HOST) + ); + int port = cursor.getInt( + columnName(cursor, DBContract.Servers.COLUMN_NAME_PORT) + ); + cursor.close(); + return new Server(id, title, host, port); + } + + public int columnName(Cursor cursor, String columnName) { + return cursor.getColumnIndexOrThrow(columnName); + } +}