diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-05-02 17:18:27 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-05-02 17:18:27 +0300 |
| commit | 93c300c43ee9978c16edddf6ec06965e4fe91b6a (patch) | |
| tree | c1ab450d019a63837222da0749af05b7f56f3a6f /Software/Android_Studio | |
| parent | 702be0f8634c3012a1a5e833eeadd37bc7d06181 (diff) | |
| download | Tango-93c300c43ee9978c16edddf6ec06965e4fe91b6a.tar.gz Tango-93c300c43ee9978c16edddf6ec06965e4fe91b6a.zip | |
Working on TCC...
Diffstat (limited to 'Software/Android_Studio')
12 files changed, 231 insertions, 100 deletions
diff --git a/Software/Android_Studio/ColorCapture/app/build.gradle b/Software/Android_Studio/ColorCapture/app/build.gradle index be070a573..7b8adc999 100644 --- a/Software/Android_Studio/ColorCapture/app/build.gradle +++ b/Software/Android_Studio/ColorCapture/app/build.gradle @@ -23,7 +23,7 @@ android { buildTypes { debug { - buildConfigField "String", "WEB_SERVICE_ADDRESS", "\"http://10.100.102.46:45455/api/\"" + buildConfigField "String", "WEB_SERVICE_ADDRESS", "\"http://192.168.1.86:45455/api/\"" buildConfigField "String", "WEB_SERVICE_APP_ID", "\"Tdf793i4ughsiduf8749509237885ehgfdlkghlT\"" } diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/integration/ExternalBridgeClient.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/integration/ExternalBridgeClient.java index 3d4d80ccf..b2ccc16dc 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/integration/ExternalBridgeClient.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/integration/ExternalBridgeClient.java @@ -3,6 +3,7 @@ package com.twine.colorcapture.integration; import com.google.protobuf.ByteString; import com.twine.colorcapture.core.IAction; import com.twine.colorcapture.core.IAction1; +import com.twine.colorcapture.core.Task; import com.twine.colorcapture.web.messages.DetectionColor; import com.twine.tango.pmr.common.MessageContainerOuterClass.MessageContainer; import com.twine.tango.pmr.common.MessageTypeOuterClass; @@ -25,79 +26,87 @@ public class ExternalBridgeClient public void sendColorProfile(ExternalBridgeMachine machine, DetectionColor detectionColor, IAction success, IAction1<Exception> error) { - try + Thread t = new Thread(() -> { - Socket tcpClient = new Socket(); - tcpClient.connect(new InetSocketAddress(machine.getIpAddress(), EXTERNAL_BRIDGE_PORT), 2000); + try + { + Socket tcpClient = new Socket(); + tcpClient.connect(new InetSocketAddress(machine.getIpAddress(), EXTERNAL_BRIDGE_PORT), 2000); - //Create detection color - DetectionColorOuterClass.DetectionColor.Builder color = DetectionColorOuterClass.DetectionColor.newBuilder(); - color.setR(detectionColor.getR()); - color.setG(detectionColor.getG()); - color.setB(detectionColor.getB()); + //Create detection color + DetectionColorOuterClass.DetectionColor.Builder color = DetectionColorOuterClass.DetectionColor.newBuilder(); + color.setR(detectionColor.getR()); + color.setG(detectionColor.getG()); + color.setB(detectionColor.getB()); - //Create Message - ColorProfileRequest.Builder message = ColorProfileRequest.newBuilder(); - message.setAppID("SnapMatch"); - message.setDetectionColor(color); + //Create Message + ColorProfileRequest.Builder message = ColorProfileRequest.newBuilder(); + message.setAppID("SnapMatch"); + message.setDetectionColor(color); - //Create container - MessageContainer.Builder container = MessageContainer.newBuilder(); - container.setToken(UUID.randomUUID().toString()); - container.setType(MessageTypeOuterClass.MessageType.ColorProfileRequest); - container.setData(ByteString.copyFrom(message.build().toByteArray())); + //Create container + MessageContainer.Builder container = MessageContainer.newBuilder(); + container.setToken(UUID.randomUUID().toString()); + container.setType(MessageTypeOuterClass.MessageType.ColorProfileRequest); + container.setData(ByteString.copyFrom(message.build().toByteArray())); - //Create data - byte[] data = container.build().toByteArray(); - byte[] size = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data.length).array(); - byte[] packet = concatByteArrays(size, data); + //Create data + byte[] data = container.build().toByteArray(); + byte[] size = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data.length).array(); + byte[] packet = concatByteArrays(size, data); - //Write data - tcpClient.getOutputStream().write(packet); + //Write data + tcpClient.getOutputStream().write(packet); - //Wait for response... - InputStream stream = tcpClient.getInputStream(); + //Wait for response... + InputStream stream = tcpClient.getInputStream(); - //Initializing expected message size. - size = new byte[4]; + //Initializing expected message size. + size = new byte[4]; - //Block reading from socket. - int read = stream.read(size, 0, size.length); - if (read == -1) - { - throw new SocketException("Error reading from TCP adapter."); - } + //Block reading from socket. + int read = stream.read(size, 0, size.length); + if (read == -1) + { + throw new SocketException("Error reading from TCP adapter."); + } - //Get expected size. - int expectedSize = ByteBuffer.wrap(size).order(ByteOrder.LITTLE_ENDIAN).getInt(); + //Get expected size. + int expectedSize = ByteBuffer.wrap(size).order(ByteOrder.LITTLE_ENDIAN).getInt(); - //Init response message size. - data = new byte[expectedSize]; - read = 0; + //Init response message size. + data = new byte[expectedSize]; + read = 0; - //Read the whole message. - while (read < expectedSize) - { - read += stream.read(data, read, Math.min(stream.available(), expectedSize - read)); - } + //Read the whole message. + while (read < expectedSize) + { + read += stream.read(data, read, Math.min(stream.available(), expectedSize - read)); + } - //Init response container and message. - MessageContainer responseContainer = MessageContainer.parseFrom(data); - ColorProfileResponse response = ColorProfileResponse.parseFrom(responseContainer.getData()); + //Init response container and message. + MessageContainer responseContainer = MessageContainer.parseFrom(data); + ColorProfileResponse response = ColorProfileResponse.parseFrom(responseContainer.getData()); - if (response.getApproved()) + tcpClient.close(); + + if (response.getApproved()) + { + //Approved !!! + success.invoke(); + } + else + { + //Declined + error.invoke(new Exception("The machine owner has declined the request.")); + } + } + catch (Exception ex) { - //Approved !!! + error.invoke(ex); } - - tcpClient.close(); - - success.invoke(); - } - catch (Exception ex) - { - error.invoke(ex); - } + }); + t.start(); } private byte[] concatByteArrays(byte[] first, byte[] second) diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/mvvm/ViewModelBase.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/mvvm/ViewModelBase.java index 0f426c912..a9f0550c0 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/mvvm/ViewModelBase.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/mvvm/ViewModelBase.java @@ -1,7 +1,11 @@ package com.twine.colorcapture.mvvm; +import android.os.Handler; +import android.os.Looper; import android.util.Log; +import com.twine.colorcapture.core.IAction; + import java.lang.reflect.Field; /** @@ -55,6 +59,23 @@ public abstract class ViewModelBase<T extends IView> extends ExtendedObject } } + protected void invokeUI(IAction action) + { + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + public void run() { + try + { + action.invoke(); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + }); + } + protected void onNavigatedTo() { diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/navigation/AndroidNavigationProvider.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/navigation/AndroidNavigationProvider.java index 923dc41b2..5924c17d9 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/navigation/AndroidNavigationProvider.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/navigation/AndroidNavigationProvider.java @@ -224,27 +224,32 @@ public class AndroidNavigationProvider extends ExtendedObject implements INaviga final FragmentBase fragmentTo = fragment; final FragmentBase fragmentFrom = currentFragment; - activity.runOnUiThread(() -> + fragment.setOnCreateListener((f) -> { - if (fragmentTo.getVM() instanceof INavigationObjectReceiver) + activity.runOnUiThread(() -> { - ((INavigationObjectReceiver)fragmentTo.getVM()).onNavigationObjectReceived(navigationObject); - } + if (fragmentTo.getVM() instanceof INavigationObjectReceiver && navigationObject != null) + { + ((INavigationObjectReceiver)fragmentTo.getVM()).onNavigationObjectReceived(navigationObject); + } - new Handler().postDelayed(() -> - { + new Handler().postDelayed(() -> + { - fragmentTo.getVM().notifyNavigatedTo(); + fragmentTo.getVM().notifyNavigatedTo(); - if (fragmentFrom != null && !fragmentTo.getClass().getSimpleName().equals(fragmentFrom.getClass().getSimpleName())) - { - fragmentFrom.getVM().notifyNavigatedFrom(); - } + if (fragmentFrom != null && !fragmentTo.getClass().getSimpleName().equals(fragmentFrom.getClass().getSimpleName())) + { + fragmentFrom.getVM().notifyNavigatedFrom(); + } - }, 300); + }, 300); + }); }); + + // try // { // //noinspection ConstantConditions diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragment.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragment.java index 4da73e988..fa8c1cde5 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragment.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragment.java @@ -272,17 +272,17 @@ public class CaptureFragment extends FragmentBase<FragmentCaptureBinding, Captur parameters = camera.getParameters(); - List<Size> sizes = parameters.getSupportedPreviewSizes(); - - for (Camera.Size size : parameters.getSupportedPreviewSizes()) - { - if (size.width >= 1200 & size.width <= 1280) - { - parameters.setPreviewSize(1280, 720); - parameters.setPictureSize(1280, 720); - break; - } - } +// List<Size> sizes = parameters.getSupportedPreviewSizes(); +// +// for (Camera.Size size : parameters.getSupportedPreviewSizes()) +// { +// if (size.width >= 1200 & size.width <= 1280) +// { +// parameters.setPreviewSize(1280, 720); +// parameters.setPictureSize(1280, 720); +// break; +// } +// } imageFormat = parameters.getPreviewFormat(); diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragmentVM.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragmentVM.java index e9c6c3878..b043bef9f 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragmentVM.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/capture/CaptureFragmentVM.java @@ -155,7 +155,7 @@ public class CaptureFragmentVM extends ViewModelBase<ICaptureFragment> implement DetectionResponse response = tccService.detect(sampleBitmap, barcode); - ThreadingUtils.sleep(5000); + ThreadingUtils.sleep(1000); isDetecting.set(false); @@ -166,6 +166,7 @@ public class CaptureFragmentVM extends ViewModelBase<ICaptureFragment> implement } else { + isCardDetected.set(false); onNavigatedTo(); } } diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/sendtomachine/SendToMachineFragmentVM.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/sendtomachine/SendToMachineFragmentVM.java index b89b3cdb9..0d415b498 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/sendtomachine/SendToMachineFragmentVM.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/views/sendtomachine/SendToMachineFragmentVM.java @@ -9,6 +9,7 @@ import android.widget.AdapterView; import com.twine.colorcapture.BR; import com.twine.colorcapture.R; +import com.twine.colorcapture.integration.ExternalBridgeClient; import com.twine.colorcapture.integration.ExternalBridgeMachine; import com.twine.colorcapture.integration.ExternalBridgeScanner; import com.twine.colorcapture.mvvm.ViewModelBase; @@ -17,6 +18,8 @@ import com.twine.colorcapture.navigation.INavigationProvider; import com.twine.colorcapture.notification.INotificationProvider; import com.twine.colorcapture.web.messages.DetectionColor; +import java.util.concurrent.atomic.AtomicBoolean; + import javax.inject.Inject; import me.tatarka.bindingcollectionadapter2.ItemBinding; @@ -24,6 +27,9 @@ import me.tatarka.bindingcollectionadapter2.ItemBinding; public class SendToMachineFragmentVM extends ViewModelBase<ISendToMachineFragment> implements INavigationObjectReceiver<DetectionColor> { private ExternalBridgeScanner scanner; + private DetectionColor detectionColor; + private INotificationProvider notificationProvider; + private INavigationProvider navigationProvider; public ObservableList<ExternalBridgeMachine> machines; public ItemBinding<String> machineBinding; @@ -31,6 +37,9 @@ public class SendToMachineFragmentVM extends ViewModelBase<ISendToMachineFragmen @Inject public SendToMachineFragmentVM(INotificationProvider notificationProvider, INavigationProvider navigationProvider) { + this.notificationProvider = notificationProvider; + this.navigationProvider = navigationProvider; + machines = new ObservableArrayList<>(); scanner = new ExternalBridgeScanner(); machineBinding = ItemBinding.of(BR.machine, R.layout.external_machine_item); @@ -41,21 +50,17 @@ public class SendToMachineFragmentVM extends ViewModelBase<ISendToMachineFragmen private void onMachineDisconnected(Object o, ExternalBridgeMachine machine) { - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - public void run() { - machines.remove(machine); - } + invokeUI(() -> + { + machines.remove(machine); }); } private void onMachineDiscovered(Object o, ExternalBridgeMachine machine) { - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - public void run() { - machines.add(0, machine); - } + invokeUI(() -> + { + machines.add(0, machine); }); } @@ -78,12 +83,42 @@ public class SendToMachineFragmentVM extends ViewModelBase<ISendToMachineFragmen @Override public void onNavigationObjectReceived(DetectionColor detectionColor) { - DetectionColor color = detectionColor; + this.detectionColor = detectionColor; } public void onMachineSelected(AdapterView<?> adapterView, View view, int i, long l) { ExternalBridgeMachine machine = (ExternalBridgeMachine) adapterView.getAdapter().getItem(i); - ExternalBridgeMachine m = machine; + + AtomicBoolean canceled = new AtomicBoolean(false); + + notificationProvider.showProgress("Send To Machine", "Sending your color...", (vm) -> + { + canceled.set(true); + }); + + ExternalBridgeClient client = new ExternalBridgeClient(); + client.sendColorProfile(machine, detectionColor, () -> + { + if (!canceled.get()) + { + invokeUI(() -> + { + notificationProvider.showSuccess("Sending To Machine", "Color sent successfully.", null); + navigationProvider.navigateBack(); + }); + } + + }, (ex) -> + { + if (!canceled.get()) + { + invokeUI(() -> + { + notificationProvider.showError("Sending To Machine", ex.getMessage(), null); + navigationProvider.navigateBack(); + }); + } + }); } } diff --git a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/web/TCCService.java b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/web/TCCService.java index 6ecb815b7..6e03755b8 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/web/TCCService.java +++ b/Software/Android_Studio/ColorCapture/app/src/main/java/com/twine/colorcapture/web/TCCService.java @@ -1,6 +1,9 @@ package com.twine.colorcapture.web; +import android.content.SharedPreferences; import android.graphics.Bitmap; +import android.os.Build; +import android.provider.Settings; import android.util.Base64; import com.twine.colorcapture.BuildConfig; @@ -15,17 +18,22 @@ import com.twine.colorcapture.web.messages.MachineRegistrationRequest; import com.twine.colorcapture.web.messages.MachineRegistrationResponse; import java.io.IOException; +import java.util.UUID; import javax.inject.Inject; import retrofit2.HttpException; import retrofit2.Response; +import static android.content.Context.MODE_PRIVATE; +import static com.twine.colorcapture.App.getContext; + public class TCCService implements ITCCService { private DefinitionResponse definition; private IWebServiceAPI webAPI; private LoginResponse loginResponse; + private static final String DEVICE_ID_PREF_NAME = "DeviceIdFile"; @Inject public TCCService(IWebServiceAPI webServiceAPI) @@ -109,7 +117,23 @@ public class TCCService implements ITCCService { LoginRequest request = new LoginRequest(); request.setAppId(BuildConfig.WEB_SERVICE_APP_ID); - request.setDeviceId("1234"); + + String deviceId; + + SharedPreferences prefs = getContext().getSharedPreferences(DEVICE_ID_PREF_NAME, MODE_PRIVATE); + deviceId = prefs.getString("DeviceId", null); + + if (deviceId == null) + { + deviceId = UUID.randomUUID().toString(); + SharedPreferences.Editor editor = getContext().getSharedPreferences(DEVICE_ID_PREF_NAME, MODE_PRIVATE).edit(); + editor.putString("DeviceId", deviceId); + editor.apply(); + } + + request.setDeviceId(deviceId); + request.setDevice(Build.MANUFACTURER + " " + Build.MODEL); + request.setOsVersion(Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName()); Response<LoginResponse> response = webAPI.login(request).execute(); diff --git a/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/border_primary_background.xml b/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/border_primary_background.xml index 51861f9fe..a69dfcd5e 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/border_primary_background.xml +++ b/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/border_primary_background.xml @@ -4,6 +4,7 @@ <layer-list> <item android:bottom="5dp"> <shape android:shape="rectangle"> + <stroke android:color="#979797" android:width="1dp" /> <solid android:color="@color/colorPrimaryBackground"/> <corners android:radius="5dp" /> </shape> diff --git a/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/gradient_progress.xml b/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/gradient_progress.xml new file mode 100644 index 000000000..b942480f0 --- /dev/null +++ b/Software/Android_Studio/ColorCapture/app/src/main/res/drawable/gradient_progress.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + + <item android:id="@android:id/background"> + <shape> + <corners android:radius="5dp"/> + <solid android:color="#fff"/> + </shape> + </item> + + <item android:id="@android:id/progress"> + <clip> + <shape> + <corners android:radius="5dp"/> + <gradient + android:endColor="@color/colorGradientAccent1" + android:startColor="@color/colorGradientAccent2" + /> + </shape> + </clip> + </item> + +</layer-list>
\ No newline at end of file diff --git a/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_capture.xml b/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_capture.xml index a6ea50d88..a4c4affc8 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_capture.xml +++ b/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_capture.xml @@ -88,8 +88,8 @@ android:layout_width="match_parent" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" - android:layout_height="5dp" - android:background="@color/colorPrimary" + android:layout_height="2dp" + android:background="@color/colorGradientAccent2" android:visibility="@{vm.isCardDetected ? View.GONE : View.VISIBLE}"/> diff --git a/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_send_to_machine.xml b/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_send_to_machine.xml index 0e9fba276..6881b50ff 100644 --- a/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_send_to_machine.xml +++ b/Software/Android_Studio/ColorCapture/app/src/main/res/layout/fragment_send_to_machine.xml @@ -36,16 +36,28 @@ android:layout_height="wrap_content" android:layout_below="@id/txtTitle" android:layout_gravity="center" + android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:fontFamily="@font/flexo_light" android:letterSpacing="0.07" - android:text="Scanning for machines on your local network..." - android:textSize="17.3sp" /> + android:text="Scanning your local network..." + android:textSize="15.3sp" /> + + <ProgressBar + android:layout_below="@id/txtScanning" + style="?android:attr/progressBarStyleHorizontal" + android:id="@+id/scanProgress" + android:layout_width="match_parent" + android:layout_height="4dp" + android:layout_marginTop="5dp" + android:progressDrawable="@drawable/gradient_progress" + android:indeterminate="true" + tools:progress="100"/> <ListView android:id="@+id/listResults" android:layout_marginTop="10dp" - android:layout_below="@id/txtScanning" + android:layout_below="@id/scanProgress" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="none" |
