Project

General

Profile

« Previous | Next » 

Revision d8aa4ba8

Added by Leszek Koltunski almost 5 years ago

Downloading High Scores: optimizations

View differences:

src/main/java/org/distorted/magic/RubikScoresDownloader.java
25 25
  {
26 26
  interface Receiver
27 27
    {
28
    void receive(String scores);
28
    void receive(String[][][][] scores);
29 29
    }
30 30

  
31
  static final int MAX_PLACES = 10;
32

  
31 33
  private static final int DOWNLOAD   = 0;
32 34
  private static final int SUBMIT     = 1;
33 35
  private static final int IDLE       = 2;
......
40 42
  private static Receiver mReceiver;
41 43

  
42 44
  private static String mScores = "";
45
  private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3];
43 46

  
44 47
///////////////////////////////////////////////////////////////////////////////////////////////////
45 48

  
46
  static void onPause()
49
  private void fillValues()
47 50
    {
48
    mRunning = false;
49
    mScores = "";
50
    }
51
    int begin=-1 ,end, len = mScores.length();
51 52

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

  
54
  private boolean gottaDownload()
55
    {
56
    return ((mScores.length()==0) && !mRunning);
53
    while( begin<len )
54
      {
55
      end = mScores.indexOf('\n', begin+1);
56
      if( end<0 ) end = len;
57
      fillRow(mScores.substring(begin+1,end));
58
      begin = end;
59
      }
57 60
    }
58 61

  
59 62
///////////////////////////////////////////////////////////////////////////////////////////////////
60 63

  
61
  void abortNetworkTransaction()
64
  private void fillRow(String row)
62 65
    {
63
    mRunning = false;
64
    }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
66
    int s1 = row.indexOf(' ');
67
    int s2 = row.indexOf(' ',s1+1);
68
    int s3 = row.indexOf(' ',s2+1);
69
    int s4 = row.indexOf(' ',s3+1);
70
    int s5 = row.indexOf(' ',s4+1);
71
    int s6 = row.length();
72

  
73
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
74
      {
75
      int size = Integer.valueOf( row.substring(0,s1) );
67 76

  
68
  void download(Receiver receiver)
69
    {
70
    mReceiver = receiver;
71
    mMode = DOWNLOAD;
72
    mNetworkThrd = new Thread(this);
73
    mNetworkThrd.start();
77
      if( size>=0 && size<RubikSize.LENGTH )
78
        {
79
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
80
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
81
        String name    = row.substring(s3+1, s4);
82
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
83
        String country = row.substring(s5+1, s6);
84
        String realTime= String.valueOf(time/10.0f);
85

  
86
        if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
87
          {
88
          mValues[size][level][place][0] = country;
89
          mValues[size][level][place][1] = name;
90
          mValues[size][level][place][2] = realTime;
91
          }
92
        else
93
          {
94
          android.util.Log.e("fillRow", "row invalid: level or place invalid: "+row);
95
          }
96
        }
97
      else
98
        {
99
        android.util.Log.e("fillRow", "row invalid: size invalid: "+row);
100
        }
101
      }
102
    else
103
      {
104
      android.util.Log.e("fillRow", "row invalid: "+row);
105
      }
74 106
    }
75 107

  
76 108
///////////////////////////////////////////////////////////////////////////////////////////////////
77 109

  
78
  public void run()
110
  private void doDownload()
79 111
    {
80
    try
81
      {
82
      if( gottaDownload() )
83
        {
84
        mRunning = true;
85

  
86
        Thread.sleep(2000);
87

  
88 112
mScores =
89 113

  
90 114
"0 0 0 INEED7X7X7 1 rus" + "\n" +
......
167 191
"3 3 2 INEED7X7X7 24 rus" + "\n" +
168 192
"3 3 3 RUBIK123 30 lit" + "\n" +
169 193
"3 3 4 SKY16 31 usa";
170
      }
171 194
    }
172
  catch( Exception e )
195

  
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

  
198
  private boolean gottaDownload()
173 199
    {
174
    android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
200
    return ((mScores.length()==0) && !mRunning);
175 201
    }
176 202

  
177
  mRunning = false;
178
  mReceiver.receive(mScores);
179
  }
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

  
205
  static void onPause()
206
    {
207
    mRunning = false;
208
    mScores = "";
209
    }
210

  
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

  
213
  void abortNetworkTransaction()
214
    {
215
    mRunning = false;
216
    }
217

  
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

  
220
  void download(Receiver receiver)
221
    {
222
    mReceiver = receiver;
223
    mMode = DOWNLOAD;
224
    mNetworkThrd = new Thread(this);
225
    mNetworkThrd.start();
226
    }
227

  
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

  
230
  public void run()
231
    {
232
    try
233
      {
234
      if( gottaDownload() )
235
        {
236
        mRunning = true;
237
        doDownload();
238
        fillValues();
239
        }
240
      }
241
    catch( Exception e )
242
      {
243
      android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
244
      }
245

  
246
    mRunning = false;
247
    mReceiver.receive(mValues);
248
    }
180 249
}
src/main/java/org/distorted/magic/RubikScoresView.java
67 67

  
68 68
///////////////////////////////////////////////////////////////////////////////////////////////////
69 69

  
70
  private int getLevelLayoutID(int level)
71
    {
72
    return 100+level;
73
    }
74

  
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  public void prepareView(FragmentActivity act)
70
  void prepareView(FragmentActivity act, final String[][][] values)
78 71
    {
79 72
    removeAllViews();
80 73

  
......
82 75
    LinearLayout layout = tab.findViewById(R.id.tabLayout);
83 76
    addView(tab);
84 77

  
85
    for(int i=1; i<=RubikActivity.MAX_SCRAMBLE; i++)
78
    for(int i=0; i<RubikActivity.MAX_SCRAMBLE; i++)
86 79
      {
87
      View level = inflate(act, R.layout.level_records, null);
88
      level.setId(getLevelLayoutID(i));
80
      LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
89 81
      TextView text = level.findViewById(R.id.levelTitle);
90
      text.setText(act.getString(R.string.sc_placeholder,i));
82
      text.setText(act.getString(R.string.sc_placeholder,(i+1)));
91 83

  
92
      layout.addView(level);
93
      }
94
    }
84
      for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
85
        {
86
        String coun = values[i][j][0];
87
        String name = values[i][j][1];
88
        String time = values[i][j][2];
95 89

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
  public void fillRow(FragmentActivity act, int level, String place, int time, String name, String country)
99
    {
100
    if( level>=0 && level<RubikActivity.MAX_SCRAMBLE )
101
      {
102
      LinearLayout layout = findViewById(getLevelLayoutID(level+1));
103
      View rowLay = inflate(act, R.layout.level_row, null);
90
        if( name!=null && name.length()>0 )
91
          {
92
          View row = inflate(act, R.layout.level_row, null);
104 93

  
105
      TextView textName    = rowLay.findViewById(R.id.level_row_name);
106
      TextView textTime    = rowLay.findViewById(R.id.level_row_time);
107
      TextView textCountry = rowLay.findViewById(R.id.level_row_country);
94
          TextView textCoun = row.findViewById(R.id.level_row_country);
95
          TextView textName = row.findViewById(R.id.level_row_name);
96
          TextView textTime = row.findViewById(R.id.level_row_time);
108 97

  
109
      float real_time = time/10.0f;
98
          textCoun.setText(coun);
99
          textName.setText(name);
100
          textTime.setText(time);
110 101

  
111
      textName.setText(name);
112
      textTime.setText( String.valueOf(real_time) );
113
      textCountry.setText(country);
102
          level.addView(row);
103
          }
104
        }
114 105

  
115
      layout.addView(rowLay);
116
      }
117
    else
118
      {
119
      android.util.Log.e("receive", "level invalid: "+level);
106
      layout.addView(level);
120 107
      }
121 108
    }
122 109
  }
src/main/java/org/distorted/magic/RubikScoresViewPager.java
45 45

  
46 46
///////////////////////////////////////////////////////////////////////////////////////////////////
47 47

  
48
  public void receive(final String scores)
48
  public void receive(final String[][][][] scores)
49 49
    {
50 50
    mAct.runOnUiThread(new Runnable()
51 51
      {
......
54 54
        {
55 55
        for(int i=0; i<RubikSize.LENGTH; i++)
56 56
          {
57
          mViews[i].prepareView(mAct);
58
          }
59

  
60
        int begin=-1 ,end, len = scores.length();
61

  
62
        while( begin<len )
63
          {
64
          end = scores.indexOf('\n', begin+1);
65
          if( end<0 ) end = len;
66
          sendRow(scores.substring(begin+1,end));
67
          begin = end;
57
          mViews[i].prepareView(mAct,scores[i]);
68 58
          }
69 59
        }
70 60
      });
71 61
    }
72 62

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

  
75
  private void sendRow(String row)
76
    {
77
    int s1 = row.indexOf(' ');
78
    int s2 = row.indexOf(' ',s1+1);
79
    int s3 = row.indexOf(' ',s2+1);
80
    int s4 = row.indexOf(' ',s3+1);
81
    int s5 = row.indexOf(' ',s4+1);
82
    int s6 = row.length();
83

  
84
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
85
      {
86
      int size       = Integer.valueOf( row.substring(   0,s1) );
87
      int levl       = Integer.valueOf( row.substring(s1+1,s2) );
88
      String place   = row.substring(s2+1, s3);
89
      String name    = row.substring(s3+1, s4);
90
      int time       = Integer.valueOf( row.substring(s4+1,s5) );
91
      String country = row.substring(s5+1, s6);
92

  
93
      if( size>=0 && size< RubikSize.LENGTH )
94
        {
95
        mViews[size].fillRow(mAct, levl, place, time, name, country);
96
        }
97
      else
98
        {
99
        android.util.Log.e("receive", "row invalid: size invalid: "+row);
100
        }
101
      }
102
    else
103
      {
104
      android.util.Log.e("receive", "row invalid: "+row);
105
      }
106
    }
107

  
108 63
///////////////////////////////////////////////////////////////////////////////////////////////////
109 64

  
110 65
  @Override
src/main/res/layout/level_records.xml
14 14
    <TextView
15 15
        android:id="@+id/levelTitle"
16 16
        android:layout_width="match_parent"
17
        android:layout_height="40dp"
18
        android:textSize="26sp"
17
        android:layout_height="32dp"
18
        android:textSize="24sp"
19 19
        android:gravity="center"
20 20
        android:background="@color/grey"
21
        android:paddingBottom="5dp"
21
        android:paddingBottom="0dp"
22 22
        android:paddingTop="0dp"
23 23
        android:paddingLeft="5dp"
24 24
        android:paddingRight="5dp"/>

Also available in: Unified diff