adding setup workflow

This commit is contained in:
2025-04-15 09:57:51 -04:00
parent e36d0616d5
commit 0c04500fc2
7 changed files with 340 additions and 6 deletions

View File

@@ -66,6 +66,23 @@
android:value=".view.MainActivity" />
</activity>
<!-- Welcome Activity -->
<activity
android:name=".view.WelcomeActivity"
android:label="Welcome"
android:exported="false"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<!-- Server Setup Activity -->
<activity
android:name=".view.ServerSetupActivity"
android:label="Server Setup"
android:exported="false">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.WelcomeActivity" />
</activity>
<!-- File Upload Foreground Service -->
<service
android:name=".service.FileUploadForegroundService"

View File

@@ -13,11 +13,13 @@ public class PreferencesUtil {
public static final String KEY_SERVER_ADDRESS = "server_address";
public static final String KEY_SERVER_PORT = "server_port";
public static final String KEY_CLIENT_KEY = "client_key";
public static final String KEY_SETUP_COMPLETED = "setup_completed";
// Default values
private static final String DEFAULT_SERVER_ADDRESS = "172.16.100.167";
private static final int DEFAULT_SERVER_PORT = 7777;
private static final String DEFAULT_CLIENT_KEY = "default-secret-key";
private static final String DEFAULT_SERVER_ADDRESS = "";
private static final int DEFAULT_SERVER_PORT = 0;
private static final String DEFAULT_CLIENT_KEY = "";
private static final boolean DEFAULT_SETUP_COMPLETED = false;
/**
* Get the shared preferences instance
@@ -111,6 +113,42 @@ public class PreferencesUtil {
editor.putString(KEY_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
editor.putInt(KEY_SERVER_PORT, DEFAULT_SERVER_PORT);
editor.putString(KEY_CLIENT_KEY, DEFAULT_CLIENT_KEY);
editor.putBoolean(KEY_SETUP_COMPLETED, DEFAULT_SETUP_COMPLETED);
editor.apply();
}
/**
* Check if setup has been completed
*
* @param context The application context
* @return True if setup has been completed, false otherwise
*/
public static boolean isSetupCompleted(Context context) {
return getPreferences(context).getBoolean(KEY_SETUP_COMPLETED, DEFAULT_SETUP_COMPLETED);
}
/**
* Set the setup completed status
*
* @param context The application context
* @param completed True if setup has been completed, false otherwise
*/
public static void setSetupCompleted(Context context, boolean completed) {
getPreferences(context).edit().putBoolean(KEY_SETUP_COMPLETED, completed).apply();
}
/**
* Check if all required server settings are configured
*
* @param context The application context
* @return True if all required settings are configured, false otherwise
*/
public static boolean areServerSettingsConfigured(Context context) {
SharedPreferences prefs = getPreferences(context);
String address = prefs.getString(KEY_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
int port = prefs.getInt(KEY_SERVER_PORT, DEFAULT_SERVER_PORT);
String clientKey = prefs.getString(KEY_CLIENT_KEY, DEFAULT_CLIENT_KEY);
return !address.isEmpty() && port > 0 && !clientKey.isEmpty();
}
}

View File

@@ -25,6 +25,7 @@ import com.mattintech.simplelogupload.controller.ShareIntentHandler;
import com.mattintech.simplelogupload.controller.UploadController;
import com.mattintech.simplelogupload.service.FileUploadForegroundService;
import com.mattintech.simplelogupload.util.PermissionUtil;
import com.mattintech.simplelogupload.util.PreferencesUtil;
import java.io.File;
import java.util.List;
@@ -48,6 +49,16 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if setup is required
if (!PreferencesUtil.isSetupCompleted(this) || !PreferencesUtil.areServerSettingsConfigured(this)) {
// Navigate to welcome activity
Intent intent = new Intent(this, WelcomeActivity.class);
startActivity(intent);
finish();
return;
}
setContentView(R.layout.activity_main);
uploadController = new UploadController(this);

View File

@@ -0,0 +1,112 @@
package com.mattintech.simplelogupload.view;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import com.mattintech.simplelogupload.R;
import com.mattintech.simplelogupload.constant.AppConstants;
import com.mattintech.simplelogupload.util.PreferencesUtil;
/**
* Activity for server setup
*/
public class ServerSetupActivity extends AppCompatActivity {
private static final String TAG = AppConstants.APP_TAG + "ServerSetupActivity";
private TextInputEditText serverAddressInput;
private TextInputEditText serverPortInput;
private TextInputEditText clientKeyInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_setup);
// Initialize UI components
serverAddressInput = findViewById(R.id.serverAddressInput);
serverPortInput = findViewById(R.id.serverPortInput);
clientKeyInput = findViewById(R.id.clientKeyInput);
Button finishSetupButton = findViewById(R.id.finishSetupButton);
// Set up action bar
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("Server Setup");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// Set up button click listener
finishSetupButton.setOnClickListener(v -> saveSettings());
}
/**
* Save settings to SharedPreferences
*/
private void saveSettings() {
try {
// Get values from input fields
String serverAddress = serverAddressInput.getText().toString().trim();
String serverPortStr = serverPortInput.getText().toString().trim();
String clientKey = clientKeyInput.getText().toString().trim();
// Validate input
if (TextUtils.isEmpty(serverAddress)) {
serverAddressInput.setError("Server address is required");
return;
}
if (TextUtils.isEmpty(serverPortStr)) {
serverPortInput.setError("Server port is required");
return;
}
int serverPort;
try {
serverPort = Integer.parseInt(serverPortStr);
if (serverPort <= 0 || serverPort > 65535) {
serverPortInput.setError("Port must be between 1 and 65535");
return;
}
} catch (NumberFormatException e) {
serverPortInput.setError("Invalid port number");
return;
}
if (TextUtils.isEmpty(clientKey)) {
clientKeyInput.setError("Client key is required");
return;
}
// Save settings
PreferencesUtil.setServerAddress(this, serverAddress);
PreferencesUtil.setServerPort(this, serverPort);
PreferencesUtil.setClientKey(this, clientKey);
PreferencesUtil.setSetupCompleted(this, true);
Log.d(TAG, "Server setup completed successfully");
Toast.makeText(this, "Setup completed successfully!", Toast.LENGTH_SHORT).show();
// Navigate to main activity
Intent intent = new Intent(ServerSetupActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} catch (Exception e) {
Log.e(TAG, "Error saving settings", e);
Toast.makeText(this, "Error saving settings: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}

View File

@@ -53,9 +53,17 @@ public class SettingsActivity extends AppCompatActivity {
* Load current settings from SharedPreferences
*/
private void loadSettings() {
serverAddressInput.setText(PreferencesUtil.getServerAddress(this));
serverPortInput.setText(String.valueOf(PreferencesUtil.getServerPort(this)));
clientKeyInput.setText(PreferencesUtil.getClientKey(this));
String serverAddress = PreferencesUtil.getServerAddress(this);
int serverPort = PreferencesUtil.getServerPort(this);
String clientKey = PreferencesUtil.getClientKey(this);
serverAddressInput.setText(serverAddress);
if (serverPort > 0) {
serverPortInput.setText(String.valueOf(serverPort));
} else {
serverPortInput.setText("");
}
clientKeyInput.setText(clientKey);
}
/**

View File

@@ -0,0 +1,35 @@
package com.mattintech.simplelogupload.view;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.mattintech.simplelogupload.R;
/**
* Welcome activity for first-time setup
*/
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
// Hide action bar for cleaner look
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
// Set up next button
Button nextButton = findViewById(R.id.nextButton);
nextButton.setOnClickListener(v -> {
// Proceed to server setup activity
Intent intent = new Intent(WelcomeActivity.this, ServerSetupActivity.class);
startActivity(intent);
// No finish() here to allow back navigation
});
}
}

View File

@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<TextView
android:id="@+id/setupTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Server Configuration"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/setupDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Please enter your server details below. These are required to connect to your SimpleLogUpload server."
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/setupTitle" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/finishSetupButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/setupDescription">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/serverAddressLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Server Address (IP or hostname)">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/serverAddressInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/serverPortLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Server Port">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/serverPortInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/clientKeyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Client Key">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/clientKeyInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/testConnectionInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="You can test your connection after saving these settings."
android:textAlignment="center"
android:textStyle="italic" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/finishSetupButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save and Continue"
android:padding="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>