feat: added the ability for the user to add a local server address and use that address when available

This commit is contained in:
CappielloAntonio 2024-06-01 15:23:40 +02:00
parent aa5290c7ee
commit f6b176a357
11 changed files with 1179 additions and 36 deletions

View file

@ -6,6 +6,7 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
@ -18,6 +19,7 @@ import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import com.cappielloantonio.tempo.App;
import com.cappielloantonio.tempo.BuildConfig;
import com.cappielloantonio.tempo.R;
import com.cappielloantonio.tempo.broadcast.receiver.ConnectivityStatusBroadcastReceiver;
@ -42,7 +44,7 @@ import java.util.concurrent.ExecutionException;
@UnstableApi
public class MainActivity extends BaseActivity {
private static final String TAG = "MainActivity";
private static final String TAG = "MainActivityLogs";
public ActivityMainBinding bind;
private MainViewModel mainViewModel;
@ -305,10 +307,11 @@ public class MainActivity extends BaseActivity {
Preferences.setToken(null);
Preferences.setPassword(null);
Preferences.setServer(null);
Preferences.setLocalAddress(null);
Preferences.setUser(null);
// TODO Enter all settings to be reset
Preferences.setServerId(null);
Preferences.setOpenSubsonic(false);
Preferences.setPlaybackSpeed(Constants.MEDIA_PLAYBACK_SPEED_100);
Preferences.setSkipSilenceMode(false);
Preferences.setDataSavingMode(false);
@ -338,17 +341,37 @@ public class MainActivity extends BaseActivity {
}
private void pingServer() {
if (Preferences.getToken() != null) {
mainViewModel.ping().observe(this, subsonicResponse -> {
if (subsonicResponse == null && Preferences.showServerUnreachableDialog()) {
ServerUnreachableDialog dialog = new ServerUnreachableDialog();
dialog.show(getSupportFragmentManager(), null);
}
if (Preferences.getToken() == null) return;
if (subsonicResponse != null) {
if (Preferences.isInUseServerAddressLocal()) {
mainViewModel.ping().observe(this, subsonicResponse -> {
if (subsonicResponse == null) {
Preferences.setServerSwitchableTimer();
Preferences.switchInUseServerAddress();
App.refreshSubsonicClient();
pingServer();
} else {
Preferences.setOpenSubsonic(subsonicResponse.getOpenSubsonic() != null && subsonicResponse.getOpenSubsonic());
}
});
} else {
if (Preferences.isServerSwitchable()) {
Preferences.setServerSwitchableTimer();
Preferences.switchInUseServerAddress();
App.refreshSubsonicClient();
pingServer();
} else {
mainViewModel.ping().observe(this, subsonicResponse -> {
if (subsonicResponse == null) {
if (Preferences.showServerUnreachableDialog()) {
ServerUnreachableDialog dialog = new ServerUnreachableDialog();
dialog.show(getSupportFragmentManager(), null);
}
} else {
Preferences.setOpenSubsonic(subsonicResponse.getOpenSubsonic() != null && subsonicResponse.getOpenSubsonic());
}
});
}
}
}

View file

@ -28,6 +28,7 @@ public class ServerSignupDialog extends DialogFragment {
private String username;
private String password;
private String server;
private String localAddress;
private boolean lowSecurity = false;
@NonNull
@ -69,6 +70,7 @@ public class ServerSignupDialog extends DialogFragment {
bind.usernameTextView.setText(loginViewModel.getServerToEdit().getUsername());
bind.passwordTextView.setText("");
bind.serverTextView.setText(loginViewModel.getServerToEdit().getAddress());
bind.localAddressTextView.setText(loginViewModel.getServerToEdit().getLocalAddress());
bind.lowSecurityCheckbox.setChecked(loginViewModel.getServerToEdit().isLowSecurity());
}
} else {
@ -97,6 +99,7 @@ public class ServerSignupDialog extends DialogFragment {
username = Objects.requireNonNull(bind.usernameTextView.getText()).toString().trim();
password = bind.lowSecurityCheckbox.isChecked() ? MusicUtil.passwordHexEncoding(Objects.requireNonNull(bind.passwordTextView.getText()).toString()) : Objects.requireNonNull(bind.passwordTextView.getText()).toString();
server = Objects.requireNonNull(bind.serverTextView.getText()).toString().trim();
localAddress = Objects.requireNonNull(bind.localAddressTextView.getText()).toString().trim();
lowSecurity = bind.lowSecurityCheckbox.isChecked();
if (TextUtils.isEmpty(serverName)) {
@ -114,6 +117,11 @@ public class ServerSignupDialog extends DialogFragment {
return false;
}
if (!TextUtils.isEmpty(localAddress) && !localAddress.matches("^https?://(.*)")) {
bind.localAddressTextView.setError(getString(R.string.error_server_prefix));
return false;
}
if (!server.matches("^https?://(.*)")) {
bind.serverTextView.setError(getString(R.string.error_server_prefix));
return false;
@ -124,6 +132,6 @@ public class ServerSignupDialog extends DialogFragment {
private void saveServerPreference() {
String serverID = loginViewModel.getServerToEdit() != null ? loginViewModel.getServerToEdit().getServerId() : UUID.randomUUID().toString();
loginViewModel.addServer(new Server(serverID, this.serverName, this.username, this.password, this.server, System.currentTimeMillis(), this.lowSecurity));
loginViewModel.addServer(new Server(serverID, this.serverName, this.username, this.password, this.server, this.localAddress, System.currentTimeMillis(), this.lowSecurity));
}
}

View file

@ -117,7 +117,7 @@ public class LoginFragment extends Fragment implements ClickCallback {
@Override
public void onServerClick(Bundle bundle) {
Server server = bundle.getParcelable("server_object");
saveServerPreference(server.getServerId(), server.getAddress(), server.getUsername(), server.getPassword(), server.isLowSecurity());
saveServerPreference(server.getServerId(), server.getAddress(), server.getLocalAddress(), server.getUsername(), server.getPassword(), server.isLowSecurity());
SystemRepository systemRepository = new SystemRepository();
systemRepository.checkUserCredential(new SystemCallback() {
@ -141,9 +141,10 @@ public class LoginFragment extends Fragment implements ClickCallback {
dialog.show(activity.getSupportFragmentManager(), null);
}
private void saveServerPreference(String serverId, String server, String user, String password, boolean isLowSecurity) {
private void saveServerPreference(String serverId, String server, String localAddress, String user, String password, boolean isLowSecurity) {
Preferences.setServerId(serverId);
Preferences.setServer(server);
Preferences.setLocalAddress(localAddress);
Preferences.setUser(user);
Preferences.setPassword(password);
Preferences.setLowSecurity(isLowSecurity);