Project

General

Profile

« Previous | Next » 

Revision b88cdd91

Added by Leszek Koltunski over 2 years ago

Download Update icons.

View differences:

src/main/java/org/distorted/network/RubikNetwork.java
20 20
package org.distorted.network;
21 21

  
22 22
import java.io.BufferedReader;
23
import java.io.IOException;
23 24
import java.io.InputStream;
24 25
import java.io.InputStreamReader;
25 26
import java.net.HttpURLConnection;
......
30 31

  
31 32
import android.content.pm.PackageInfo;
32 33
import android.content.pm.PackageManager;
34
import android.graphics.Bitmap;
35
import android.graphics.BitmapFactory;
33 36

  
34 37
import androidx.fragment.app.FragmentActivity;
35 38

  
......
52 55

  
53 56
  public interface Updatee
54 57
    {
55
    void iconDownloaded();
58
    void iconDownloaded(int ordinal, Bitmap bitmap);
56 59
    void receiveUpdate(RubikUpdates update);
57 60
    void errorUpdate();
58 61
    }
......
655 658
      }
656 659
    }
657 660

  
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662

  
663
  private Bitmap downloadIcon(String url)
664
    {
665
    try
666
      {
667
      java.net.URL connectURL = new URL(url);
668
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
669
      conn.setDoInput(true);
670
      conn.connect();
671
      InputStream input = conn.getInputStream();
672
      return BitmapFactory.decodeStream(input);
673
      }
674
    catch (IOException e)
675
      {
676
      android.util.Log.e("D", "Failed to download "+url);
677
      android.util.Log.e("D", e.getMessage() );
678
      return null;
679
      }
680
    }
681

  
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683

  
684
  private void iconThread()
685
    {
686
    int numC = mUpdates.getCompletedNumber();
687
    int numS = mUpdates.getStartedNumber();
688

  
689
    for(int c=0; c<numC; c++)
690
      {
691
      Bitmap icon = mUpdates.getCompletedIcon(c);
692

  
693
      if( icon==null )
694
        {
695
        String url = mUpdates.getCompletedURL(c);
696
        icon = downloadIcon(url);
697
        }
698
      if( icon!=null )
699
        {
700
        mUpdates.setCompletedIcon(c,icon);
701
        mUpdatee.iconDownloaded(c,icon);
702
        }
703
      }
704

  
705
    for(int s=0; s<numS; s++)
706
      {
707
      Bitmap icon = mUpdates.getStartedIcon(s);
708

  
709
      if( icon==null )
710
        {
711
        String url = mUpdates.getStartedURL(s);
712
        icon = downloadIcon(url);
713
        }
714
      if( icon!=null )
715
        {
716
        mUpdates.setStartedIcon(s,icon);
717
        mUpdatee.iconDownloaded(numC+s,icon);
718
        }
719
      }
720
    }
721

  
658 722
///////////////////////////////////////////////////////////////////////////////////////////////////
659 723

  
660 724
  private RubikNetwork()
......
778 842

  
779 843
  public void downloadIcons(Updatee updatee)
780 844
    {
845
    initializeStatics();
846
    mUpdatee = updatee;
847

  
848
    Thread thread = new Thread()
849
      {
850
      public void run()
851
        {
852
        iconThread();
853
        }
854
      };
781 855

  
856
    thread.start();
782 857
    }
783 858

  
784 859
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff