Adding classes for server instance and server db search.
This commit is contained in:
parent
16299d6509
commit
784b0e0c23
@ -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) {
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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<Server> 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<String> requestPermissionLauncher =
|
||||
|
48
app/src/main/java/me/sergiotarxz/bedrockstation/Server.java
Normal file
48
app/src/main/java/me/sergiotarxz/bedrockstation/Server.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
95
app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java
Normal file
95
app/src/main/java/me/sergiotarxz/bedrockstation/Servers.java
Normal file
@ -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<Server> 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<Server> array = new ArrayList<Server>();
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user