|
1 | | -package com.teamopensmartglasses.smartglassesmanager; |
| 1 | +package com.augmentos.smartglassesmanager; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
4 | 4 |
|
|
12 | 12 | //custom, our code |
13 | 13 | import androidx.lifecycle.LifecycleOwner; |
14 | 14 |
|
15 | | -import com.teamopensmartglasses.augmentoslib.events.AudioChunkNewEvent; |
16 | | -import com.teamopensmartglasses.smartglassesmanager.eventbusmessages.DisableBleScoAudioEvent; |
17 | | -import com.teamopensmartglasses.augmentoslib.events.DisplayCustomContentRequestEvent; |
18 | | -import com.teamopensmartglasses.augmentoslib.events.DoubleTextWallViewRequestEvent; |
19 | | -import com.teamopensmartglasses.augmentoslib.events.HomeScreenEvent; |
20 | | -import com.teamopensmartglasses.augmentoslib.events.SendBitmapViewRequestEvent; |
21 | | -import com.teamopensmartglasses.smartglassesmanager.eventbusmessages.SetFontSizeEvent; |
22 | | -import com.teamopensmartglasses.augmentoslib.events.TextWallViewRequestEvent; |
23 | | -import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.AudioWearableSGC; |
24 | | -import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.EvenRealitiesG1SGC; |
25 | | -import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.UltraliteSGC; |
26 | | -import com.teamopensmartglasses.augmentoslib.events.BulletPointListViewRequestEvent; |
27 | | -import com.teamopensmartglasses.augmentoslib.events.CenteredTextViewRequestEvent; |
28 | | -import com.teamopensmartglasses.augmentoslib.events.FinalScrollingTextRequestEvent; |
29 | | -import com.teamopensmartglasses.augmentoslib.events.IntermediateScrollingTextRequestEvent; |
30 | | -import com.teamopensmartglasses.augmentoslib.events.ReferenceCardImageViewRequestEvent; |
31 | | -import com.teamopensmartglasses.augmentoslib.events.ReferenceCardSimpleViewRequestEvent; |
32 | | -import com.teamopensmartglasses.augmentoslib.events.RowsCardViewRequestEvent; |
33 | | -import com.teamopensmartglasses.augmentoslib.events.PromptViewRequestEvent; |
34 | | -import com.teamopensmartglasses.augmentoslib.events.ScrollingTextViewStartRequestEvent; |
35 | | -import com.teamopensmartglasses.augmentoslib.events.ScrollingTextViewStopRequestEvent; |
36 | | -import com.teamopensmartglasses.smartglassesmanager.hci.AudioChunkCallback; |
37 | | -import com.teamopensmartglasses.smartglassesmanager.hci.MicrophoneLocalAndBluetooth; |
38 | | -//import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.ActiveLookSGC; |
39 | | -import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.AndroidSGC; |
40 | | -import com.teamopensmartglasses.smartglassesmanager.smartglassescommunicators.SmartGlassesCommunicator; |
41 | | -import com.teamopensmartglasses.smartglassesmanager.supportedglasses.SmartGlassesDevice; |
42 | | -import com.teamopensmartglasses.augmentoslib.events.TextLineViewRequestEvent; |
| 15 | +import com.augmentos.augmentoslib.events.AudioChunkNewEvent; |
| 16 | +import com.augmentos.smartglassesmanager.eventbusmessages.DisableBleScoAudioEvent; |
| 17 | +import com.augmentos.augmentoslib.events.DisplayCustomContentRequestEvent; |
| 18 | +import com.augmentos.augmentoslib.events.DoubleTextWallViewRequestEvent; |
| 19 | +import com.augmentos.augmentoslib.events.HomeScreenEvent; |
| 20 | +import com.augmentos.augmentoslib.events.SendBitmapViewRequestEvent; |
| 21 | +import com.augmentos.smartglassesmanager.eventbusmessages.SetFontSizeEvent; |
| 22 | +import com.augmentos.augmentoslib.events.TextWallViewRequestEvent; |
| 23 | +import com.augmentos.smartglassesmanager.smartglassescommunicators.AudioWearableSGC; |
| 24 | +import com.augmentos.smartglassesmanager.smartglassescommunicators.EvenRealitiesG1SGC; |
| 25 | +import com.augmentos.smartglassesmanager.smartglassescommunicators.UltraliteSGC; |
| 26 | +import com.augmentos.augmentoslib.events.BulletPointListViewRequestEvent; |
| 27 | +import com.augmentos.augmentoslib.events.CenteredTextViewRequestEvent; |
| 28 | +import com.augmentos.augmentoslib.events.FinalScrollingTextRequestEvent; |
| 29 | +import com.augmentos.augmentoslib.events.IntermediateScrollingTextRequestEvent; |
| 30 | +import com.augmentos.augmentoslib.events.ReferenceCardImageViewRequestEvent; |
| 31 | +import com.augmentos.augmentoslib.events.ReferenceCardSimpleViewRequestEvent; |
| 32 | +import com.augmentos.augmentoslib.events.RowsCardViewRequestEvent; |
| 33 | +import com.augmentos.augmentoslib.events.PromptViewRequestEvent; |
| 34 | +import com.augmentos.augmentoslib.events.ScrollingTextViewStartRequestEvent; |
| 35 | +import com.augmentos.augmentoslib.events.ScrollingTextViewStopRequestEvent; |
| 36 | +import com.augmentos.smartglassesmanager.hci.AudioChunkCallback; |
| 37 | +import com.augmentos.smartglassesmanager.hci.MicrophoneLocalAndBluetooth; |
| 38 | +//import com.augmentos.smartglassesmanager.smartglassescommunicators.ActiveLookSGC; |
| 39 | +import com.augmentos.smartglassesmanager.smartglassescommunicators.AndroidSGC; |
| 40 | +import com.augmentos.smartglassesmanager.smartglassescommunicators.SmartGlassesCommunicator; |
| 41 | +import com.augmentos.smartglassesmanager.supportedglasses.SmartGlassesDevice; |
| 42 | +import com.augmentos.augmentoslib.events.TextLineViewRequestEvent; |
| 43 | +import com.augmentos.smartglassesmanager.utils.SmartGlassesConnectionState; |
43 | 44 |
|
44 | 45 | //rxjava |
45 | 46 | import java.nio.ByteBuffer; |
46 | | -import java.util.List; |
47 | 47 |
|
48 | 48 | import io.reactivex.rxjava3.subjects.PublishSubject; |
49 | 49 |
|
@@ -84,51 +84,67 @@ class SmartGlassesRepresentative { |
84 | 84 | } |
85 | 85 |
|
86 | 86 | public void findCompatibleDeviceNames(){ |
87 | | - SmartGlassesCommunicator temporarySmartGlassesCommunicator = null; |
88 | | - switch (smartGlassesDevice.getGlassesOs()){ |
89 | | - case ANDROID_OS_GLASSES: |
90 | | - temporarySmartGlassesCommunicator = new AndroidSGC(context, smartGlassesDevice, dataObservable); |
91 | | - break; |
92 | | - case AUDIO_WEARABLE_GLASSES: |
93 | | - temporarySmartGlassesCommunicator = new AudioWearableSGC(context, smartGlassesDevice); |
94 | | - break; |
95 | | - case ULTRALITE_MCU_OS_GLASSES: |
96 | | - temporarySmartGlassesCommunicator = new UltraliteSGC(context, smartGlassesDevice, lifecycleOwner); |
97 | | - break; |
98 | | - case EVEN_REALITIES_G1_MCU_OS_GLASSES: |
99 | | - temporarySmartGlassesCommunicator = new EvenRealitiesG1SGC(context, smartGlassesDevice); |
100 | | - break; |
| 87 | + // If we have not created a communicator yet (or the device changed), create it once |
| 88 | + //if (smartGlassesCommunicator == null || !isSameDevice(smartGlassesDevice, smartGlassesCommunicator)) { |
| 89 | + if (smartGlassesCommunicator == null) { |
| 90 | + smartGlassesCommunicator = createCommunicator(); |
101 | 91 | } |
102 | 92 |
|
103 | | - temporarySmartGlassesCommunicator.findCompatibleDeviceNames(); |
| 93 | + if (smartGlassesCommunicator != null) { |
| 94 | + smartGlassesCommunicator.findCompatibleDeviceNames(); |
| 95 | + } else { |
| 96 | + Log.d(TAG, "SmartGlassesCommunicator is NULL, something truly awful must have transpired"); |
| 97 | + } |
104 | 98 | } |
105 | 99 |
|
106 | 100 | public void connectToSmartGlasses(){ |
107 | | - switch (smartGlassesDevice.getGlassesOs()){ |
108 | | - case ANDROID_OS_GLASSES: |
109 | | - smartGlassesCommunicator = new AndroidSGC(context, smartGlassesDevice, dataObservable); |
110 | | - break; |
111 | | - case AUDIO_WEARABLE_GLASSES: |
112 | | - smartGlassesCommunicator = new AudioWearableSGC(context, smartGlassesDevice); |
113 | | - break; |
114 | | - case ULTRALITE_MCU_OS_GLASSES: |
115 | | - smartGlassesCommunicator = new UltraliteSGC(context, smartGlassesDevice, lifecycleOwner); |
116 | | - break; |
117 | | - case EVEN_REALITIES_G1_MCU_OS_GLASSES: |
118 | | - smartGlassesCommunicator = new EvenRealitiesG1SGC(context, smartGlassesDevice); |
119 | | - break; |
| 101 | + // Same approach: if the communicator is null, create it |
| 102 | + //if (smartGlassesCommunicator == null || !isSameDevice(smartGlassesDevice, smartGlassesCommunicator)) { |
| 103 | + if (smartGlassesCommunicator == null) { |
| 104 | + smartGlassesCommunicator = createCommunicator(); |
120 | 105 | } |
121 | 106 |
|
122 | | - smartGlassesCommunicator.connectToSmartGlasses(); |
| 107 | + if (smartGlassesCommunicator != null) { |
| 108 | + smartGlassesCommunicator.connectToSmartGlasses(); |
| 109 | + } else { |
| 110 | + Log.d(TAG, "SmartGlassesCommunicator is NULL, something truly awful must have transpired"); |
| 111 | + } |
123 | 112 |
|
124 | | - //if the glasses don't support a microphone, this Representative handles local microphone |
| 113 | + // If the glasses don't support a microphone, handle local microphone |
125 | 114 | if (smartGlassesDevice.useScoMic) { |
126 | 115 | connectAndStreamLocalMicrophone(true); |
127 | 116 | } else if (!smartGlassesDevice.getHasInMic() && !smartGlassesDevice.getHasOutMic()) { |
128 | 117 | connectAndStreamLocalMicrophone(false); |
129 | 118 | } |
130 | 119 | } |
131 | 120 |
|
| 121 | + /** |
| 122 | + * Helper to create the appropriate communicator once. |
| 123 | + */ |
| 124 | + private SmartGlassesCommunicator createCommunicator() { |
| 125 | + switch (smartGlassesDevice.getGlassesOs()) { |
| 126 | + case ANDROID_OS_GLASSES: |
| 127 | + return new AndroidSGC(context, smartGlassesDevice, dataObservable); |
| 128 | + case AUDIO_WEARABLE_GLASSES: |
| 129 | + return new AudioWearableSGC(context, smartGlassesDevice); |
| 130 | + case ULTRALITE_MCU_OS_GLASSES: |
| 131 | + return new UltraliteSGC(context, smartGlassesDevice, lifecycleOwner); |
| 132 | + case EVEN_REALITIES_G1_MCU_OS_GLASSES: |
| 133 | + return new EvenRealitiesG1SGC(context, smartGlassesDevice); |
| 134 | + default: |
| 135 | + return null; // or throw an exception |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Optional helper to check if the communicator is for the same device. |
| 141 | + * Some communicator classes might have a method or field to check device identity. |
| 142 | + */ |
| 143 | + //private boolean isSameDevice(SmartGlassesDevice device, SmartGlassesCommunicator comm) { |
| 144 | + // return comm != null && comm. != null |
| 145 | + // && comm.getDevice().equals(device); |
| 146 | + //} |
| 147 | + |
132 | 148 | public void updateGlassesBrightness(int brightness) { |
133 | 149 | if (smartGlassesCommunicator != null) { |
134 | 150 | smartGlassesCommunicator.updateGlassesBrightness(brightness); |
@@ -187,13 +203,17 @@ public void destroy(){ |
187 | 203 | smartGlassesCommunicator = null; |
188 | 204 | } |
189 | 205 |
|
| 206 | + if (uiHandler != null) { |
| 207 | + uiHandler.removeCallbacksAndMessages(null); |
| 208 | + } |
| 209 | + |
190 | 210 | Log.d(TAG, "SG rep destroy complete"); |
191 | 211 | } |
192 | 212 |
|
193 | 213 | //are our smart glasses currently connected? |
194 | | - public int getConnectionState(){ |
| 214 | + public SmartGlassesConnectionState getConnectionState(){ |
195 | 215 | if (smartGlassesCommunicator == null){ |
196 | | - return 0; |
| 216 | + return SmartGlassesConnectionState.DISCONNECTED; |
197 | 217 | } else { |
198 | 218 | return smartGlassesCommunicator.getConnectionState(); |
199 | 219 | } |
|
0 commit comments