Revision 46be3ddf
Added by Leszek Koltunski over 2 years ago
src/main/java/org/distorted/network/RubikNetwork.java | ||
---|---|---|
46 | 46 |
|
47 | 47 |
public class RubikNetwork |
48 | 48 |
{ |
49 |
public interface Receiver |
|
49 |
public interface ScoresReceiver
|
|
50 | 50 |
{ |
51 | 51 |
void receive(String[][][] country, String[][][] name, float[][][] time); |
52 | 52 |
void message(String mess); |
53 | 53 |
void error(String error); |
54 | 54 |
} |
55 | 55 |
|
56 |
public interface Updatee
|
|
56 |
public interface IconReceiver
|
|
57 | 57 |
{ |
58 | 58 |
void iconDownloaded(int ordinal, Bitmap bitmap); |
59 |
} |
|
60 |
|
|
61 |
public interface Updatee |
|
62 |
{ |
|
59 | 63 |
void receiveUpdate(RubikUpdates update); |
60 | 64 |
void errorUpdate(); |
61 | 65 |
} |
62 | 66 |
|
67 |
public interface Downloadee |
|
68 |
{ |
|
69 |
void jsonDownloaded(); |
|
70 |
} |
|
71 |
|
|
63 | 72 |
public static final int MAX_PLACES = 10; |
64 | 73 |
|
65 | 74 |
private static final int REND_ADRENO= 0; |
... | ... | |
114 | 123 |
private static RubikNetwork mThis; |
115 | 124 |
private static String mScores = ""; |
116 | 125 |
private static boolean mRunning = false; |
117 |
private static Receiver mReceiver; |
|
118 | 126 |
private static Updatee mUpdatee; |
119 | 127 |
private static String mVersion; |
120 |
private static String mSuspicious; |
|
121 | 128 |
private static int mNumObjects; |
122 | 129 |
private static RubikUpdates mUpdates; |
123 | 130 |
private static int mDebugState; |
... | ... | |
168 | 175 |
|
169 | 176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
170 | 177 |
|
171 |
private boolean fillValuesNormal() |
|
178 |
private boolean fillValuesNormal(ScoresReceiver receiver)
|
|
172 | 179 |
{ |
173 | 180 |
int begin=-1 ,end, len = mScores.length(); |
174 | 181 |
String row; |
175 | 182 |
|
176 | 183 |
if( len==0 ) |
177 | 184 |
{ |
178 |
if( mReceiver!=null ) mReceiver.error("1");
|
|
185 |
receiver.error("1");
|
|
179 | 186 |
return false; |
180 | 187 |
} |
181 | 188 |
else if( len<=2 ) |
182 | 189 |
{ |
183 |
if( mReceiver!=null ) mReceiver.error(mScores);
|
|
190 |
receiver.error(mScores);
|
|
184 | 191 |
return false; |
185 | 192 |
} |
186 | 193 |
|
... | ... | |
376 | 383 |
|
377 | 384 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
378 | 385 |
|
379 |
private boolean network(String url) |
|
386 |
private boolean network(String url, ScoresReceiver receiver)
|
|
380 | 387 |
{ |
381 | 388 |
try |
382 | 389 |
{ |
... | ... | |
400 | 407 |
} |
401 | 408 |
|
402 | 409 |
mScores = total.toString(); |
410 |
conn.disconnect(); |
|
403 | 411 |
} |
404 | 412 |
catch( final UnknownHostException e ) |
405 | 413 |
{ |
406 |
if( mReceiver!=null ) mReceiver.message("No access to Internet");
|
|
414 |
receiver.message("No access to Internet");
|
|
407 | 415 |
return false; |
408 | 416 |
} |
409 | 417 |
catch( final SecurityException e ) |
410 | 418 |
{ |
411 |
if( mReceiver!=null ) mReceiver.message("Application not authorized to connect to the Internet");
|
|
419 |
receiver.message("Application not authorized to connect to the Internet");
|
|
412 | 420 |
return false; |
413 | 421 |
} |
414 | 422 |
catch( final Exception e ) |
415 | 423 |
{ |
416 |
if( mReceiver!=null ) mReceiver.message(e.getMessage());
|
|
424 |
receiver.message(e.getMessage());
|
|
417 | 425 |
return false; |
418 | 426 |
} |
419 | 427 |
|
420 | 428 |
if( mScores.length()==0 ) |
421 | 429 |
{ |
422 |
if( mReceiver!=null ) mReceiver.message("Failed to download scores");
|
|
430 |
receiver.message("Failed to download scores");
|
|
423 | 431 |
return false; |
424 | 432 |
} |
425 | 433 |
|
... | ... | |
428 | 436 |
|
429 | 437 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
430 | 438 |
|
431 |
private String constructSuspiciousURL() |
|
439 |
private String constructSuspiciousURL(String suspURL)
|
|
432 | 440 |
{ |
433 | 441 |
RubikScores scores = RubikScores.getInstance(); |
434 | 442 |
int deviceID= scores.getDeviceID(); |
435 |
String suspicious = URLencode(mSuspicious);
|
|
443 |
String suspicious = URLencode(suspURL);
|
|
436 | 444 |
|
437 | 445 |
String url="https://distorted.org/magic/cgi-bin/suspicious.cgi"; |
438 | 446 |
url += "?i="+deviceID+"&d="+suspicious; |
... | ... | |
534 | 542 |
|
535 | 543 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
536 | 544 |
|
537 |
private void downloadThread() |
|
545 |
private void downloadThread(ScoresReceiver receiver)
|
|
538 | 546 |
{ |
539 | 547 |
try |
540 | 548 |
{ |
541 | 549 |
if( gottaDownload() ) |
542 | 550 |
{ |
543 | 551 |
mRunning = true; |
544 |
boolean receiveValues = network(constructDownloadURL()); |
|
552 |
boolean receiveValues = network(constructDownloadURL(),receiver);
|
|
545 | 553 |
|
546 | 554 |
if( mRunning ) |
547 | 555 |
{ |
548 |
receiveValues = fillValuesNormal(); |
|
556 |
receiveValues = fillValuesNormal(receiver);
|
|
549 | 557 |
mRunning = false; |
550 | 558 |
} |
551 | 559 |
|
552 |
if( receiveValues && mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
|
|
560 |
if( receiveValues ) receiver.receive(mCountry, mName, mTime);
|
|
553 | 561 |
} |
554 | 562 |
} |
555 | 563 |
catch( Exception e ) |
556 | 564 |
{ |
557 |
if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
|
|
565 |
receiver.message("Exception downloading records: "+e.getMessage() );
|
|
558 | 566 |
} |
559 | 567 |
} |
560 | 568 |
|
561 | 569 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
562 | 570 |
|
563 |
private void submitThread() |
|
571 |
private void submitThread(ScoresReceiver receiver)
|
|
564 | 572 |
{ |
565 | 573 |
try |
566 | 574 |
{ |
... | ... | |
569 | 577 |
|
570 | 578 |
if( scores.thereAreUnsubmittedRecords() ) |
571 | 579 |
{ |
572 |
boolean receiveValues = network(constructSubmitURL()); |
|
580 |
boolean receiveValues = network(constructSubmitURL(),receiver);
|
|
573 | 581 |
|
574 | 582 |
if( mRunning ) |
575 | 583 |
{ |
576 |
receiveValues = fillValuesNormal(); |
|
584 |
receiveValues = fillValuesNormal(receiver);
|
|
577 | 585 |
mRunning = false; |
578 | 586 |
} |
579 | 587 |
|
580 | 588 |
if( receiveValues ) |
581 | 589 |
{ |
582 | 590 |
RubikScores.getInstance().successfulSubmit(); |
583 |
if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
|
|
591 |
receiver.receive(mCountry, mName, mTime);
|
|
584 | 592 |
} |
585 | 593 |
} |
586 | 594 |
} |
587 | 595 |
catch( Exception e ) |
588 | 596 |
{ |
589 |
if( mReceiver!=null ) mReceiver.message("Exception submitting records: "+e.getMessage() );
|
|
597 |
receiver.message("Exception submitting records: "+e.getMessage() );
|
|
590 | 598 |
} |
591 | 599 |
} |
592 | 600 |
|
... | ... | |
621 | 629 |
} |
622 | 630 |
|
623 | 631 |
String updates = answer.toString(); |
632 |
conn.disconnect(); |
|
624 | 633 |
mUpdates.parse(updates); |
625 | 634 |
|
626 | 635 |
if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates); |
... | ... | |
635 | 644 |
|
636 | 645 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
637 | 646 |
|
638 |
private void suspiciousThread() |
|
647 |
private void suspiciousThread(String suspURL)
|
|
639 | 648 |
{ |
640 |
String url = constructSuspiciousURL(); |
|
649 |
String url = constructSuspiciousURL(suspURL);
|
|
641 | 650 |
|
642 | 651 |
try |
643 | 652 |
{ |
... | ... | |
651 | 660 |
conn.connect(); |
652 | 661 |
conn.getOutputStream().flush(); |
653 | 662 |
conn.getInputStream(); |
663 |
conn.disconnect(); |
|
654 | 664 |
} |
655 | 665 |
catch( final Exception e ) |
656 | 666 |
{ |
... | ... | |
664 | 674 |
{ |
665 | 675 |
try |
666 | 676 |
{ |
667 |
android.util.Log.e("D", "downloading "+url); |
|
668 |
|
|
669 | 677 |
java.net.URL connectURL = new URL(url); |
670 | 678 |
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); |
671 | 679 |
conn.setDoInput(true); |
672 | 680 |
conn.connect(); |
673 | 681 |
InputStream input = conn.getInputStream(); |
674 |
return BitmapFactory.decodeStream(input); |
|
682 |
Bitmap icon = BitmapFactory.decodeStream(input); |
|
683 |
conn.disconnect(); |
|
684 |
return icon; |
|
675 | 685 |
} |
676 | 686 |
catch (IOException e) |
677 | 687 |
{ |
... | ... | |
683 | 693 |
|
684 | 694 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
685 | 695 |
|
686 |
private void iconThread() |
|
696 |
private void iconThread(IconReceiver receiver)
|
|
687 | 697 |
{ |
688 | 698 |
int numC = mUpdates.getCompletedNumber(); |
689 | 699 |
int numS = mUpdates.getStartedNumber(); |
... | ... | |
704 | 714 |
if( icon!=null ) |
705 | 715 |
{ |
706 | 716 |
mUpdates.setCompletedIcon(c,icon); |
707 |
mUpdatee.iconDownloaded(c,icon);
|
|
717 |
receiver.iconDownloaded(c,icon);
|
|
708 | 718 |
} |
709 | 719 |
} |
710 | 720 |
} |
... | ... | |
725 | 735 |
if( icon!=null ) |
726 | 736 |
{ |
727 | 737 |
mUpdates.setStartedIcon(s,icon); |
728 |
mUpdatee.iconDownloaded(numC+s,icon);
|
|
738 |
receiver.iconDownloaded(numC+s,icon);
|
|
729 | 739 |
} |
730 | 740 |
} |
731 | 741 |
} |
732 | 742 |
} |
733 | 743 |
|
744 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
745 |
|
|
746 |
private InputStream downloadJSON(String name) |
|
747 |
{ |
|
748 |
String url = mUpdates.getURL() + name; |
|
749 |
|
|
750 |
try |
|
751 |
{ |
|
752 |
java.net.URL connectURL = new URL(url); |
|
753 |
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); |
|
754 |
conn.setDoInput(true); |
|
755 |
conn.connect(); |
|
756 |
InputStream stream = conn.getInputStream(); |
|
757 |
conn.disconnect(); |
|
758 |
return stream; |
|
759 |
} |
|
760 |
catch (IOException e) |
|
761 |
{ |
|
762 |
android.util.Log.e("D", "Failed to download "+url); |
|
763 |
android.util.Log.e("D", e.getMessage() ); |
|
764 |
return null; |
|
765 |
} |
|
766 |
} |
|
767 |
|
|
768 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
769 |
|
|
770 |
private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee) |
|
771 |
{ |
|
772 |
if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json"); |
|
773 |
if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json"); |
|
774 |
|
|
775 |
downloadee.jsonDownloaded(); |
|
776 |
} |
|
777 |
|
|
734 | 778 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
735 | 779 |
|
736 | 780 |
private RubikNetwork() |
... | ... | |
761 | 805 |
|
762 | 806 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
763 | 807 |
|
764 |
public void download(Receiver receiver, FragmentActivity act)
|
|
808 |
public void download(final ScoresReceiver receiver, final FragmentActivity act)
|
|
765 | 809 |
{ |
766 | 810 |
initializeStatics(); |
767 |
mReceiver = receiver; |
|
768 | 811 |
figureOutVersion(act); |
769 | 812 |
|
770 | 813 |
Thread thread = new Thread() |
771 | 814 |
{ |
772 | 815 |
public void run() |
773 | 816 |
{ |
774 |
downloadThread(); |
|
817 |
downloadThread(receiver);
|
|
775 | 818 |
} |
776 | 819 |
}; |
777 | 820 |
|
... | ... | |
780 | 823 |
|
781 | 824 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
782 | 825 |
|
783 |
public void submit(Receiver receiver, FragmentActivity act)
|
|
826 |
public void submit(ScoresReceiver receiver, final FragmentActivity act)
|
|
784 | 827 |
{ |
785 | 828 |
initializeStatics(); |
786 |
mReceiver = receiver; |
|
787 | 829 |
figureOutVersion(act); |
788 | 830 |
|
789 | 831 |
Thread thread = new Thread() |
790 | 832 |
{ |
791 | 833 |
public void run() |
792 | 834 |
{ |
793 |
submitThread(); |
|
835 |
submitThread(receiver);
|
|
794 | 836 |
} |
795 | 837 |
}; |
796 | 838 |
|
... | ... | |
817 | 859 |
|
818 | 860 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
819 | 861 |
|
820 |
public void suspicious(String suspicious) |
|
862 |
public void suspicious(final String suspicious)
|
|
821 | 863 |
{ |
822 | 864 |
initializeStatics(); |
823 |
mSuspicious = suspicious; |
|
824 | 865 |
|
825 | 866 |
Thread thread = new Thread() |
826 | 867 |
{ |
827 | 868 |
public void run() |
828 | 869 |
{ |
829 |
suspiciousThread(); |
|
870 |
suspiciousThread(suspicious);
|
|
830 | 871 |
} |
831 | 872 |
}; |
832 | 873 |
|
... | ... | |
852 | 893 |
|
853 | 894 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
854 | 895 |
|
855 |
public void downloadIcons(Updatee updatee) |
|
896 |
public void downloadIcons(final IconReceiver receiver) |
|
897 |
{ |
|
898 |
initializeStatics(); |
|
899 |
|
|
900 |
Thread thread = new Thread() |
|
901 |
{ |
|
902 |
public void run() |
|
903 |
{ |
|
904 |
iconThread(receiver); |
|
905 |
} |
|
906 |
}; |
|
907 |
|
|
908 |
thread.start(); |
|
909 |
} |
|
910 |
|
|
911 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
912 |
|
|
913 |
public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee) |
|
856 | 914 |
{ |
857 | 915 |
initializeStatics(); |
858 |
mUpdatee = updatee; |
|
859 | 916 |
|
860 | 917 |
Thread thread = new Thread() |
861 | 918 |
{ |
862 | 919 |
public void run() |
863 | 920 |
{ |
864 |
iconThread();
|
|
921 |
jsonThread(info,downloadee);
|
|
865 | 922 |
} |
866 | 923 |
}; |
867 | 924 |
|
Also available in: Unified diff
progress with the Updates: download the jsons.