-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
894 lines (799 loc) · 36.9 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* Copyright (c) 2023 IMAGE Project, Shared Reality Lab, McGill University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* and our Additional Terms along with this program.
* If not, see <https://github.com/Shared-Reality-Lab/IMAGE-Monarch/LICENSE>.
*/
package ca.mcgill.a11y.image;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.hardware.input.InputManager;
import android.media.MediaPlayer;
import android.os.BrailleDisplay;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Base64;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import ca.mcgill.a11y.image.R;
import com.scand.svg.SVGHelper;
import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MediaPlayer.OnCompletionListener {
static final String TAG = MainActivity.class.getSimpleName();
private BrailleDisplay brailleServiceObj = null;
//final private Handler mHandler = new Handler();
private byte[][] data = null; // byte array used to reset pins
//variable to change whether TTS tags fill the object or are assigned only to the raised edges.
boolean labelFill=true, ttsEnabled=true;
int layercount; // number of layers found in svg
String image;// used to store svg in string format
//presentLayer: The layer to be displayed when pins are raised;
//fileSelected: file index from the list of files in specified target directory.
int presentLayer=0, fileSelected=0;
// keyCode of confirm button as per current standard
int confirmButton = 504;
// short and long descriptions of objects in the present layer
private ArrayList<String[][]> tags;
//private ArrayList<byte[][]> dataLayers;
static TextToSpeech tts;
private GestureDetectorCompat mDetector;
// ongoing server request
private Call<ResponseFormat> ongoingCall;
@SuppressLint("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
InputManager im = (InputManager) getSystemService(INPUT_SERVICE);
brailleServiceObj = (BrailleDisplay) getSystemService(BrailleDisplay.BRAILLE_DISPLAY_SERVICE);
// Arrays for pin down and TTS tags
data = new byte[brailleServiceObj.getDotLineCount()][];
for (int i = 0; i < data.length; ++i) {
data[i] = new byte[brailleServiceObj.getDotPerLineCount()];
Arrays.fill(data[i], (byte) 0x00);
}
// Initializing separate 2D arrays for short and long descriptions associated with the pins
tags = new ArrayList<>();
tags.add(new String [brailleServiceObj.getDotLineCount()][brailleServiceObj.getDotPerLineCount()]);
tags.add(new String [brailleServiceObj.getDotLineCount()][brailleServiceObj.getDotPerLineCount()]);
int[] ids = im.getInputDeviceIds();
for(int i = 0; i < ids.length;++i) {
Log.d(TAG, "id: " + ids[i] + ":" + im.getInputDevice(ids[i]).getName());
}
// Opening first file in the directory to be read by default
try {
image=getFile(0)[0];
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
// Setting up TTS
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.SUCCESS){
Log.e("error", "Initialization Failed!"+status);
}
else {
tts.setLanguage(Locale.getDefault());
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
}
@Override
public void onDone(String s) {
//Log.d("CHECKING!", s);
// plays ping when TTS readout is completed based on utteranceId
if (s.equals("ping")){
pingsPlayer(R.raw.blip);
}
}
@Override
public void onError(String s) {
}
});
}
}});
brailleServiceObj.registerMotionEventHandler(new BrailleDisplay.MotionEventHandler() {
@Override
public boolean handleMotionEvent(MotionEvent e) {
/*// Observed limits of IR outputs for the pin array. Might need tweaking for more accurate mapping of finger location to pin...
float xMin= 0;
float xMax= 1920;
float yMin=23;
float yMax=1080;
// Calculating pin based on position
int pinX= (int) (Math.ceil((e.getX()-xMin+0.000001)/((xMax-xMin)/brailleServiceObj.getDotPerLineCount()))-1);
int pinY= (int) Math.ceil((e.getY()-yMin+0.000001)/((yMax-yMin)/brailleServiceObj.getDotLineCount()))-1;
//Log.d(TAG, String.valueOf(e.getX())+","+String.valueOf(e.getY())+ " ; "+ pinX+","+pinY);
*/
if(ttsEnabled){
try{
// This works! Gesture control can now be used along with the handler.
onTouchEvent(e);
}
catch(RuntimeException ex){
Log.d(TAG, String.valueOf(ex));
}}
return false;
//return true;
}
});
((Button) findViewById(R.id.zeros)).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (((Button) findViewById(R.id.zeros)).hasFocus() &&
keyEvent.getKeyCode()==confirmButton &&
keyEvent.getAction()== KeyEvent.ACTION_DOWN){
ttsEnabled=false;
brailleServiceObj.display(data);
}
return false;
}
});
((Button) findViewById(R.id.ones)).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (((Button) findViewById(R.id.ones)).hasFocus() &&
keyEvent.getKeyCode()== confirmButton &&
keyEvent.getAction()== KeyEvent.ACTION_DOWN){
try {
// Display current layer
ttsEnabled=true;
brailleServiceObj.display(getBitmaps(getfreshDoc(), presentLayer++));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
//Log.d("LAYER!", String.valueOf(presentLayer));
if (presentLayer==layercount+1)
presentLayer=0;
}
return false;
}
});
((Button) findViewById(R.id.getMap)).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (((Button) findViewById(R.id.getMap)).hasFocus() &&
keyEvent.getKeyCode()== confirmButton &&
keyEvent.getAction()== KeyEvent.ACTION_DOWN){
try {
Double latitude= Double.parseDouble(((EditText) findViewById(R.id.latitude)).getText().toString());
Double longitude= Double.parseDouble(((EditText) findViewById(R.id.longitude)).getText().toString());
getMap(latitude, longitude);
} catch (JSONException e) {
throw new RuntimeException(e);
}
catch (NumberFormatException e){
speaker("Invalid coordinates");
}
}
return false;
}
});
Switch debugSwitch = (Switch) findViewById(R.id.debugViewSwitch);
debugSwitch.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (debugSwitch.hasFocus() &&
keyEvent.getKeyCode()== confirmButton &&
keyEvent.getAction()== KeyEvent.ACTION_DOWN){
if (debugSwitch.isChecked()){
debugSwitch.setChecked(false);
}
else{
debugSwitch.setChecked(true);
}
brailleServiceObj.setDebugView(debugSwitch.isChecked());
//audioPlayer("/sdcard/IMAGE/", "audio.mp3");
}
return false;
}
});
debugSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
debugSwitch.setChecked(!debugSwitch.isChecked());
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
try {
Map<Integer, String> keyMapping = new HashMap<Integer, String>() {{
put(421, "UP");
put(420, "DOWN");
}};
switch (keyMapping.getOrDefault(keyCode, "default")) {
// Navigating between files
case "UP":
Log.d(TAG, event.toString());
changeFile(++fileSelected);
return true;
// Navigating between files
case "DOWN":
Log.d(TAG, event.toString());
changeFile(--fileSelected);
return true;
default:
Log.d(TAG, event.toString());
return false;
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
// function to pad the bitmap to match the pin array aspect ratio
public Bitmap padBitmap(Bitmap bitmap, int padX, int padY)
{
Bitmap paddedBitmap = Bitmap.createBitmap(
bitmap.getWidth() + padX,
bitmap.getHeight() + padY,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(paddedBitmap);
canvas.drawARGB(0x00, 0xFF, 0xFF, 0xFF);
canvas.drawBitmap(
bitmap,
padX / 2,
padY / 2,
new Paint(Paint.FILTER_BITMAP_FLAG));
return paddedBitmap;
}
// convert the modified doc to a string
public String getStringFromDocument(Document doc)
{
try
{
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex)
{
Log.d("ERROR", "Write Failed");
return null;
}
}
// get present layer and the description tags from the doc
public byte[][] getBitmaps(Document doc, int presentLayer) throws IOException, XPathExpressionException {
String caption = null;
XPath xPath = XPathFactory.newInstance().newXPath();
// get the caption from the title node
Element title = ((Element) ((NodeList) xPath.evaluate("//title", doc, XPathConstants.NODESET)).item(0));
if (title!=null){
caption = title.getTextContent();
}
// get list of layers; Uses default ordering which is expected to be 'document order' but the return type is node-set which is unordered!
NodeList nodeslist = (NodeList)xPath.evaluate("//*[@data-image-layer]", doc, XPathConstants.NODESET);
//Log.d("XPATH", String.valueOf(nodeslist.getLength()));
layercount=nodeslist.getLength();
for(int i = 0 ; i < nodeslist.getLength() ; i ++) {
Node node = nodeslist.item(i);
// hide layers which are not the present layer
if (i!= presentLayer && presentLayer!=layercount) {
((Element)node).setAttribute("display","none");
}
// TTS output of layer description
if (i==presentLayer){
//Log.d("GETTING TAGS", String.valueOf(nodeslist.getLength()));
String tag;
//Log.d("GETTING TAGS", node.getNodeName());
if (((Element)node).hasAttribute("aria-labelledby")) {
tag= doc.getElementById(((Element) node).getAttribute("aria-labelledby")).getTextContent();
//Log.d("GETTING TAGS", (doc.getElementById(((Element) node).getAttribute("aria-describedby")).getTextContent()));
}
else{
tag=((Element)node).getAttribute("aria-label");
//Log.d("GETTING TAGS", "Otherwise here!");
}
if(i==0 && caption!=null){
// Read the caption along with layer tag for the first layer
speaker(caption + ". Layer: " + tag);
}
else{
speaker("Layer: "+tag);
}
}
}
//If there is no tag as a layer, hide elements unless the full image is to be shown
if (presentLayer!=layercount){
nodeslist=(NodeList)xPath.evaluate("//*[not(ancestor-or-self::*[@data-image-layer]) and not(descendant::*[@data-image-layer])] ", doc, XPathConstants.NODESET);
for(int i = 0 ; i < nodeslist.getLength() ; i ++) {
Node node = nodeslist.item(i);
((Element)node).setAttribute("display","none");
}
}
else{
speaker("All layers");
}
// fetch TTS tags for elements within present layer
//getDescriptions(doc);
// get bitmap of present layer
byte[] byteArray= docToBitmap(doc);
//Log.d("BITMAP", Arrays.toString(byteArray));
// the TTS tags are fetched in a separate thread.
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
try {
getDescriptions(doc);
//Log.d("DESCRIPTIONS", "Description loaded");
// This ping plays when the descriptions (i.e. TTS labels) are laoded.
// Generally occurs with a little delay following the tactile rendering
pingsPlayer(R.raw.ping);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
// reshape byte array into 2D array to match pin array dimensions
byte[][] dataRead = new byte[brailleServiceObj.getDotLineCount()][brailleServiceObj.getDotPerLineCount()];
for (int i = 0; i < data.length; ++i) {
dataRead[i]=Arrays.copyOfRange(byteArray, i*brailleServiceObj.getDotPerLineCount(), (i+1)*brailleServiceObj.getDotPerLineCount());
}
return dataRead;
}
// get basic and detailed descriptions
public void getDescriptions(Document doc) throws XPathExpressionException, IOException {
//Log.d("GETTING TAGS", "Here!");
XPath xPath = XPathFactory.newInstance().newXPath();
// query elements that are in the present layer AND have element level descriptions (NOT layer level descriptions)
// Assuming that only elements with short description can have a long description here. Is this assumption safe?!
NodeList nodeslist=(NodeList)xPath.evaluate("//*[not(ancestor-or-self::*[@display]) and not(descendant::*[@display]) and (not(self::*[@data-image-layer]) or not(child::*)) and (self::*[@aria-labelledby] or self::*[@aria-label])]", doc, XPathConstants.NODESET); // temporary var for objects tags
String[] layerTags=new String[brailleServiceObj.getDotPerLineCount()*brailleServiceObj.getDotLineCount()];
// temporary var for objects long descriptions
String[] layerDesc=new String[brailleServiceObj.getDotPerLineCount()*brailleServiceObj.getDotLineCount()];
//Log.d("GETTING TAGS", String.valueOf(nodeslist.getLength()));
// initially hiding all elements filtered in the previous stage
for(int i = 0 ; i < nodeslist.getLength() ; i ++) {
Node node = nodeslist.item(i);
((Element)node).setAttribute("display", "none");
}
for(int i = 0 ; i < nodeslist.getLength() ; i ++) {
String tag, detailTag = null;
Node node = nodeslist.item(i);
// fetching the tag for each element
if (((Element)node).hasAttribute("aria-labelledby")) {
tag= doc.getElementById(((Element) node).getAttribute("aria-labelledby")).getTextContent();
}
else{
tag=((Element)node).getAttribute("aria-label");
}
if (((Element)node).hasAttribute("aria-describedby")) {
detailTag= doc.getElementById(((Element) node).getAttribute("aria-describedby")).getTextContent();
}
else{
// this returns an empty string even if the attribute doesn't exist i.e. if there is no long description
detailTag=((Element)node).getAttribute("aria-description");
}
if (labelFill) {
((Element) node).setAttribute("fill", "black");
}
// showing the element whose tag is stored to obtain its bitmap mapping
((Element)node).removeAttribute("display");
byte[] byteArray= docToBitmap(doc);
// using a 'for' loop to map since there are now 2 kinds of tags: label and detailed. Could possibly find a prettier way to do this Java objects
for (int j=0; j<layerTags.length; j++){
if (byteArray[j]!=0){
if (layerTags[j]==null){
layerTags[j]=tag;
}
else {
layerTags[j]= layerTags[j] + ", " + tag;
}
//check if detailTag is not blank string.
if (!detailTag.equalsIgnoreCase("")){
if (layerDesc[j]==null){
layerDesc[j]=detailTag;
}
else {
layerDesc[j]= layerDesc[j] + ", " + detailTag;
}
}
}
}
/*String[] finalLayerTags = layerTags;
// mapping pins corresponding to the selected element to its description tag. Unable to directly convert Object array (which is the return type of mapToObj) to String array but can use the funny copy hack to do it!
layerTags= Arrays.copyOf((IntStream.range(0,layerTags.length).mapToObj(k-> {
if (byteArray[k]!=0){
if (finalLayerTags[k]==null){
return tag;
}
else {
return finalLayerTags[k] + ", " + tag;
}
}
else{
return finalLayerTags[k];
}
}).collect(Collectors.toList())).toArray(), layerTags.length, String[].class);*/
// hiding element again so we can move on to the next element
((Element)node).setAttribute("display", "none");
}
// converting string array into 2D array that maps to the pins
for (int i = 0; i < data.length; ++i) {
tags.get(0)[i]=Arrays.copyOfRange(layerTags, i*brailleServiceObj.getDotPerLineCount(), (i+1)*brailleServiceObj.getDotPerLineCount());
tags.get(1)[i]=Arrays.copyOfRange(layerDesc, i*brailleServiceObj.getDotPerLineCount(), (i+1)*brailleServiceObj.getDotPerLineCount());
}
return;
}
// converts the xml doc to bitmap
public byte[] docToBitmap(Document doc) throws IOException {
String img= getStringFromDocument(doc).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ", "");
//Log.d("SVG",img);
Bitmap svg = SVGHelper.noContext().open(img).setRequestBounds(brailleServiceObj.getDotPerLineCount(), brailleServiceObj.getDotLineCount()).getBitmap();
int x = svg.getWidth();
int y = svg.getHeight();
int[] intArray = new int[brailleServiceObj.getDotPerLineCount() * brailleServiceObj.getDotLineCount()];
// padding bitmap to fit to pin array size
Bitmap svgScaled=padBitmap(svg, brailleServiceObj.getDotPerLineCount()-x, brailleServiceObj.getDotLineCount()-y);
svg.recycle();
// extracting only the alpha value of bitmap to convert it into a byte array
Bitmap alphas=svgScaled.extractAlpha();
int size = alphas.getRowBytes() * alphas.getHeight();
ByteBuffer byteBuffer = ByteBuffer.allocate(size);
alphas.copyPixelsToBuffer(byteBuffer);
byte[] byteArray = byteBuffer.array();
return byteArray;
}
// fetching the file to read from; returns file contents as String and also the file name
public String[] getFile(int fileNumber) throws IOException, JSONException {
String folderName= "/sdcard/IMAGE/client/";
File directory = new File(folderName);
File[] files = directory.listFiles();
int filecount=files.length;
if (fileNumber>= filecount)
fileSelected=0;
else if (fileNumber<0)
fileSelected=filecount-1;
Bitmap bitmap = BitmapFactory.decodeFile(folderName+files[fileSelected].getName());
byte[] imageBytes = Files.readAllBytes(Paths.get(folderName + files[fileSelected].getName()));
/*
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//BitmapFactory.Options options = new BitmapFactory.Options();
//options.inJustDecodeBounds = true;
Log.d("FILES", files[fileSelected].getName());
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/IMAGE/client/"+files[fileSelected].getName());
// This works for other file types (png, avif) as well despite being specified as jpeg
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
*/
String base64 = "data:"+ getMimeType(files[fileSelected].getName())+";base64,"+ Base64.encodeToString(imageBytes, Base64.NO_WRAP);
Integer[] dims= new Integer[] {bitmap.getWidth(), bitmap.getHeight()};
PhotoRequestFormat req= new PhotoRequestFormat();
req.setValues(base64, dims);
// Uncomment the following lines for logging http requests
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
//Need next 2 lines when server response is slow
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS);
//httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
//.baseUrl("https://unicorn.cim.mcgill.ca/image/")
.baseUrl("https://image.a11y.mcgill.ca/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
MakeRequest makereq= retrofit.create(MakeRequest.class);
Call<ResponseFormat> call= makereq.makePhotoRequest(req);
image= makeServerCall(call);
// The regex expression in replaceFirst removes everything following the '.' i.e. .jpg, .png etc.
return new String[]{image, files[fileSelected].getName().replaceFirst("\\.[^.]*$", "")};
}
public String getMap(Double lat, Double lon) throws JSONException {
presentLayer=0;
MapRequestFormat req= new MapRequestFormat();
req.setValues(lat, lon);
// Uncomment the following lines for logging http requests
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
//httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
//.baseUrl("https://unicorn.cim.mcgill.ca/image/")
.baseUrl("https://image.a11y.mcgill.ca/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
MakeRequest makereq= retrofit.create(MakeRequest.class);
Call<ResponseFormat> call= makereq.makeMapRequest(req);
//speaker("Opening map");
brailleServiceObj.display(data);
image= makeServerCall(call);
return image;
}
public String makeServerCall(Call<ResponseFormat> call){
// Cancelling any ongoing requests that haven't been completed
if (ongoingCall!=null){
ongoingCall.cancel();
}
pingsPlayer(R.raw.image_request_sent);
// Disabling the up button when request is in progress to prevent catastrophic fails
findViewById(R.id.ones).setEnabled(false);
call.enqueue(new Callback<ResponseFormat>() {
@Override
public void onResponse(Call<ResponseFormat> call, Response<ResponseFormat> response) {
try {
ResponseFormat resource= response.body();
ResponseFormat.Rendering[] renderings = resource.renderings;
image= (renderings[0].data.graphic).replaceFirst("data:.+,", "");
//Log.d("RESPONSE", image);
byte[] data = new byte[0];
data = image.getBytes("UTF-8");
data = Base64.decode(data, Base64.DEFAULT);
image = new String(data, "UTF-8");
pingsPlayer(R.raw.image_results_arrived);
// Enabling the up button again when the response has been received.
findViewById(R.id.ones).setEnabled(true);
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// This occurs when there is no rendering returned
catch (ArrayIndexOutOfBoundsException| NullPointerException e){
pingsPlayer(R.raw.image_error);
}
}
//onFailure is called both when a request is cancelled (i.e. interrupted with another request)
// AND when it fails to give a valid response
@Override
public void onFailure(Call<ResponseFormat> call, Throwable t) {
// Ensure that a request was cancelled before reading out 'Request failed' TTS
// This text is not read out when a request is cancelled as there is expected to be
// an ongoing request and can be confused as a result of that request.
// Causes interrupted requests to die silently!
if (!call.isCanceled()){
pingsPlayer(R.raw.image_error);
}
}
});
// Saving the in-progress call to allow interruption if needed
ongoingCall=call;
return image;
}
//similar to getFile. To be used when TTS read out of file name is required. Could possibly replace getFile entirely
public void changeFile(int fileNumber) throws JSONException, IOException {
presentLayer=0;
brailleServiceObj.display(data);
String[] output=getFile(fileNumber);
image=output[0];
//speaker("Opening file "+ output[1]);
//Log.d("FILENAME", output[1]);
return;
}
// get file mime type for photos
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
// get fresh copy of the file void of previously made changes
public Document getfreshDoc() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(image));
//Log.d("STRING", image);
Document doc = builder.parse(is);
return doc;
}
// find which pin the finger position corresponds to
public Integer[] pinCheck(float x, float y){
float xMin= 0;
float xMax= 1920;
float yMin=23;
float yMax=1080;
double epsilon= 0.000001;
// Calculating pin based on position
int pinX= (int) (Math.ceil((x-xMin+epsilon)/((xMax-xMin)/brailleServiceObj.getDotPerLineCount()))-1);
int pinY= (int) Math.ceil((y-yMin+epsilon)/((yMax-yMin)/brailleServiceObj.getDotLineCount()))-1;
return new Integer[] {pinX, pinY};
}
// TTS speaker. Probably needs a little more work on flushing and/or selecting whether to continue playing
public void speaker(String text, String... utterId){
tts.speak (text, TextToSpeech.QUEUE_FLUSH, null, utterId.length > 0 ? utterId[0] : "000000");
return;
}
@Override
public boolean onTouchEvent(MotionEvent event){
if (this.mDetector.onTouchEvent(event)) {
int action = event.getActionMasked();
if (action==MotionEvent.ACTION_UP)
{
Integer [] pins=pinCheck(event.getX(), event.getY());
try{
// Speak out label tags based on finger location and ping when detailed description is available
if ((tags.get(1)[pins[1]][pins[0]]!=null) && (tags.get(1)[pins[1]][pins[0]].trim().length() > 0))
{
//Log.d("CHECKING!", tags.get(1)[pins[1]][pins[0]]);
speaker(tags.get(0)[pins[1]][pins[0]], "ping");
}
else{
speaker(tags.get(0)[pins[1]][pins[0]]);
}
}
catch(RuntimeException ex){
Log.d(TAG, String.valueOf(ex));
}
}
return true;
}
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent event) {
Log.d("GESTURE!","onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("GESTURE!", "onFling: " + event1.toString() + event2.toString());
return true;
}
@Override
public void onLongPress(MotionEvent event) {
Integer [] pins=pinCheck(event.getX(), event.getY());
try{
// Speak out detailed description based on finger location
speaker(tags.get(1)[pins[1]][pins[0]]);
}
catch(RuntimeException ex){
Log.d(TAG, String.valueOf(ex));
}
Log.d("GESTURE!", "onLongPress: " + event.toString());
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX,
float distanceY) {
Log.d("GESTURE!", "onScroll: " + event1.toString() + event2.toString());
return true;
}
@Override
public void onShowPress(MotionEvent event) {
Log.d("GESTURE!", "onShowPress: " + event.toString());
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d("GESTURE!", "onSingleTapUp: " + event.toString());
/*Integer [] pins=pinCheck(event.getX(), event.getY());
try{
// Speak out label tags based on finger location
speaker(tags.get(0)[pins[1]][pins[0]]);
}
catch(RuntimeException ex){
Log.d(TAG, String.valueOf(ex));
}*/
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d("GESTURE!", "onDoubleTap: " + event.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d("GESTURE!", "onDoubleTapEvent: " + event.toString());
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d("GESTURE!", "onSingleTapConfirmed: " + event.toString());
return true;
}
// plays audio from resource file. Has provision for other playing indicator tones if required
public void pingsPlayer(int file){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp=MediaPlayer.create(getApplicationContext(), file);
mp.start();
} catch (Exception e) {
Log.d("ERROR", e.toString());
}
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
/*
//Audio player for media files. Not being used currently
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.d("ERROR", e.toString());
}
}
*/
}