mirror of
https://github.com/TimeCrafters/TimeCraftersConfigurationTool.git
synced 2025-12-15 05:02:33 +00:00
Fixed active config not updated when renamed, TACNET Connection 'works' (send/receive heartbeats to/from server), added functions for playing error sound to Backend
This commit is contained in:
@@ -46,6 +46,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
new Backend();
|
||||
}
|
||||
}
|
||||
|
||||
Backend.instance().applicationContext = getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.backend;
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.SoundPool;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Action;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Configuration;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Group;
|
||||
@@ -47,12 +55,15 @@ public class Backend {
|
||||
private static final String TAG = "Backend";
|
||||
static private HashMap<String, Object> storage = new HashMap<>();
|
||||
static private Backend instance;
|
||||
public Context applicationContext;
|
||||
private TACNET tacnet;
|
||||
private Server server;
|
||||
private Exception lastServerError;
|
||||
private Config config;
|
||||
private Settings settings;
|
||||
private boolean configChanged, settingsChanged;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private SoundPool soundPool;
|
||||
|
||||
public static HashMap<String, Object> getStorage() {
|
||||
return storage;
|
||||
@@ -349,4 +360,42 @@ public class Backend {
|
||||
configsPath.mkdir();
|
||||
}
|
||||
}
|
||||
|
||||
public void startErrorSound(Context context) {
|
||||
if (isPlayingErrorSound()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mediaPlayer = new MediaPlayer();
|
||||
try {
|
||||
mediaPlayer.setDataSource(context, Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.error_alarm));
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build());
|
||||
} else {
|
||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
|
||||
}
|
||||
|
||||
mediaPlayer.prepare();
|
||||
mediaPlayer.start();
|
||||
mediaPlayer.setLooping(true);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPlayingErrorSound() {
|
||||
return mediaPlayer != null && mediaPlayer.isPlaying();
|
||||
}
|
||||
|
||||
public void stopErrorSound() {
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.stop();
|
||||
mediaPlayer.release();
|
||||
mediaPlayer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.backend;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.Client;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.tacnet.Connection;
|
||||
@@ -8,6 +9,7 @@ import org.timecrafters.TimeCraftersConfigurationTool.tacnet.Connection;
|
||||
import java.io.IOException;
|
||||
|
||||
public class TACNET {
|
||||
private final static String TAG = "TACNET|TACNET";
|
||||
public static final String DEFAULT_HOSTNAME = "192.168.49.1";
|
||||
public static final int DEFAULT_PORT = 8962;
|
||||
|
||||
@@ -29,7 +31,20 @@ public class TACNET {
|
||||
}
|
||||
|
||||
connection = new Connection(hostname, port);
|
||||
connection.connect(null);
|
||||
Backend.instance().stopErrorSound();
|
||||
|
||||
connection.connect(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d(TAG, "run: " + connection.lastError());
|
||||
Backend.instance().startErrorSound(Backend.instance().applicationContext);
|
||||
try {
|
||||
connection.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Status status() {
|
||||
|
||||
@@ -81,6 +81,14 @@ public class ConfigurationDialog extends TimeCraftersDialog {
|
||||
if (validated(newConfigName)) {
|
||||
if (configName != null) {
|
||||
Backend.instance().moveConfig(configName, newConfigName);
|
||||
|
||||
// If config being renamed is the active config then update Backend to use
|
||||
// the correct config name/file, and save settings.
|
||||
if (Backend.instance().getSettings().config.equals(configName)) {
|
||||
Backend.instance().loadConfig(newConfigName);
|
||||
Backend.instance().getSettings().config = newConfigName;
|
||||
Backend.instance().saveSettings();
|
||||
}
|
||||
} else {
|
||||
Backend.instance().writeNewConfig(newConfigName);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class Connection {
|
||||
};
|
||||
}
|
||||
|
||||
public void connect(final Runnable callback) {
|
||||
public void connect(final Runnable errorCallback) {
|
||||
if (client != null) {
|
||||
return;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class Connection {
|
||||
socketError = true;
|
||||
lastSocketError = e.getMessage();
|
||||
|
||||
callback.run();
|
||||
errorCallback.run();
|
||||
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
|
||||
@@ -72,6 +72,8 @@ public class TACNETFragment extends TimeCraftersFragment {
|
||||
// ConnectDialog dialog = new ConnectDialog();
|
||||
// dialog.show(getFragmentManager(), null);
|
||||
Backend.instance().saveSettings();
|
||||
|
||||
Backend.instance().tacnet().connect(hostname.getText().toString(), Integer.parseInt(port.getText().toString()));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user