|
| 1 | +package org.openbot.env; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.util.Log; |
| 6 | +import androidx.preference.PreferenceManager; |
| 7 | +import org.openbot.utils.ConnectionUtils; |
| 8 | + |
| 9 | +public class ControllerConfig { |
| 10 | + private static ControllerConfig _controllerConfig; |
| 11 | + SharedPreferences preferences; |
| 12 | + |
| 13 | + enum VIDEO_SERVER_TYPE { |
| 14 | + WEBRTC, |
| 15 | + RTSP |
| 16 | + } |
| 17 | + |
| 18 | + private final boolean isMirrored = true; |
| 19 | + private final boolean mute = true; |
| 20 | + |
| 21 | + private String currentServerType; |
| 22 | + |
| 23 | + public static ControllerConfig getInstance() { |
| 24 | + if (_controllerConfig == null) { |
| 25 | + synchronized (PhoneController.class) { |
| 26 | + if (_controllerConfig == null) _controllerConfig = new ControllerConfig(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return _controllerConfig; |
| 31 | + } |
| 32 | + |
| 33 | + void init(Context context) { |
| 34 | + preferences = PreferenceManager.getDefaultSharedPreferences(context); |
| 35 | + currentServerType = get("video_server", "WEBRTC"); |
| 36 | + monitorSettingUpdates(); |
| 37 | + } |
| 38 | + |
| 39 | + private void set(String name, String value) { |
| 40 | + SharedPreferences.Editor editor = preferences.edit(); |
| 41 | + editor.putString(name, value); |
| 42 | + editor.apply(); |
| 43 | + } |
| 44 | + |
| 45 | + private String get(String name, String defaultValue) { |
| 46 | + try { |
| 47 | + return preferences.getString(name, defaultValue); |
| 48 | + } catch (ClassCastException e) { |
| 49 | + return defaultValue; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private Boolean getBoolean(String name, Boolean defaultValue) { |
| 54 | + try { |
| 55 | + return preferences.getBoolean(name, defaultValue); |
| 56 | + } catch (ClassCastException e) { |
| 57 | + return defaultValue; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void setBoolean(String name, boolean value) { |
| 62 | + SharedPreferences.Editor editor = preferences.edit(); |
| 63 | + editor.putBoolean(name, value); |
| 64 | + editor.apply(); |
| 65 | + } |
| 66 | + |
| 67 | + // specific settings |
| 68 | + public boolean isMirrored() { |
| 69 | + return getBoolean("MIRRORED", true); |
| 70 | + } |
| 71 | + |
| 72 | + public void setMirrored(boolean mirrored) { |
| 73 | + setBoolean("MIRRORED", mirrored); |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isMute() { |
| 77 | + return getBoolean("MUTE", true); |
| 78 | + } |
| 79 | + |
| 80 | + public void setMute(boolean mute) { |
| 81 | + setBoolean("MUTE", mute); |
| 82 | + } |
| 83 | + |
| 84 | + public String getVideoServerType() { |
| 85 | + return get("video_server", "WEBRTC"); |
| 86 | + } |
| 87 | + |
| 88 | + public void setVideoServerType(String type) { |
| 89 | + set("video_server", type); |
| 90 | + } |
| 91 | + |
| 92 | + private void monitorSettingUpdates() { |
| 93 | + ControllerToBotEventBus.subscribe( |
| 94 | + this.getClass().getSimpleName(), |
| 95 | + event -> { |
| 96 | + String commandType = event.getString("command"); |
| 97 | + |
| 98 | + switch (commandType) { |
| 99 | + case "TOGGLE_MIRROR": |
| 100 | + setMirrored(!isMirrored()); |
| 101 | + |
| 102 | + // inform the controller of current state |
| 103 | + BotToControllerEventBus.emitEvent( |
| 104 | + ConnectionUtils.createStatus("TOGGLE_MIRROR", isMirrored())); |
| 105 | + break; |
| 106 | + |
| 107 | + case "TOGGLE_SOUND": |
| 108 | + setMute(!isMute()); |
| 109 | + |
| 110 | + // inform the controller of current state |
| 111 | + BotToControllerEventBus.emitEvent( |
| 112 | + ConnectionUtils.createStatus("TOGGLE_SOUND", isMute())); |
| 113 | + break; |
| 114 | + } |
| 115 | + }, |
| 116 | + error -> { |
| 117 | + Log.d(null, "Error occurred in monitorConnection: " + error); |
| 118 | + }, |
| 119 | + event -> |
| 120 | + event.has("command") |
| 121 | + && (event.getString("command").contains("TOGGLE_MIRROR") |
| 122 | + || event.getString("command").contains("TOGGLE_SOUND"))); |
| 123 | + } |
| 124 | +} |
0 commit comments