Project

General

Profile

« Previous | Next » 

Revision b8b38548

Added by Leszek Koltunski almost 5 years ago

Downloading High Scores

View differences:

src/main/java/org/distorted/magic/RubikScores.java
37 37

  
38 38
public class RubikScores extends AppCompatDialogFragment
39 39
  {
40
  interface ScoresReceiver
41
    {
42
    void receive(String scores);
43
    }
44

  
45 40
  RubikScoresViewPager mViewPager;
46 41

  
47 42
///////////////////////////////////////////////////////////////////////////////////////////////////
......
85 80
      if(tab!=null) tab.setCustomView(imageView);
86 81
      }
87 82

  
88
    RubikScoresDownloader.download(mViewPager);
89

  
90 83
    return builder.create();
91 84
    }
92 85
  }
src/main/java/org/distorted/magic/RubikScoresDownloader.java
21 21

  
22 22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 23

  
24
class RubikScoresDownloader extends Thread
24
class RubikScoresDownloader implements Runnable
25 25
  {
26
  interface Receiver
27
    {
28
    void receive(String scores);
29
    }
30

  
31
  private static final int DOWNLOAD   = 0;
32
  private static final int SUBMIT     = 1;
33
  private static final int IDLE       = 2;
34

  
35
  private static final String URL  ="http://koltunski.pl/rubik/cgi-bin";
36

  
37
  private static boolean mRunning = false;
38
  private static Thread mNetworkThrd = null;
39
  private static int mMode = IDLE;
40
  private static Receiver mReceiver;
41

  
26 42
  private static String mScores = "";
27 43

  
28 44
///////////////////////////////////////////////////////////////////////////////////////////////////
29 45

  
30 46
  static void onPause()
31 47
    {
48
    mRunning = false;
32 49
    mScores = "";
33 50
    }
34 51

  
35 52
///////////////////////////////////////////////////////////////////////////////////////////////////
36 53

  
37
  static boolean gottaDownload()
54
  private boolean gottaDownload()
38 55
    {
39
    return mScores.length() == 0;
56
    return ((mScores.length()==0) && !mRunning);
40 57
    }
41 58

  
42 59
///////////////////////////////////////////////////////////////////////////////////////////////////
43 60

  
44
  static void download(RubikScores.ScoresReceiver receiver)
61
  void abortNetworkTransaction()
45 62
    {
46
    if( gottaDownload() )
47
      {
48
      doDownload();
49
      }
63
    mRunning = false;
64
    }
50 65

  
51
    receiver.receive(mScores);
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

  
68
  void download(Receiver receiver)
69
    {
70
    mReceiver = receiver;
71
    mMode = DOWNLOAD;
72
    mNetworkThrd = new Thread(this);
73
    mNetworkThrd.start();
52 74
    }
53 75

  
54 76
///////////////////////////////////////////////////////////////////////////////////////////////////
55 77

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

  
86
        Thread.sleep(2000);
87

  
58 88
mScores =
59 89

  
60 90
"0 0 0 INEED7X7X7 1 rus" + "\n" +
......
77 107
"0 3 2 MDR04 9 ita" + "\n" +
78 108
"0 3 3 ROBERT04 9 unk" + "\n" +
79 109
"0 3 4 DULCE 10 mex" + "\n" +
80
"0 4 0 THISNAVIS 11 usa" + "\n" +
81
"0 4 1 Tilly 12 uk" + "\n" +
82
"0 4 2 OHIOCUBER 14 usa" + "\n" +
83
"0 4 3 GRV 15 mex" + "\n" +
84
"0 4 4 INEED7X7X7 16 rus" + "\n" +
85
"0 5 0 KADE 6 unk" + "\n" +
86
"0 5 1 FREDRIK 11 den" + "\n" +
87
"0 5 2 THISNAVIS 11 usa" + "\n" +
88
"0 5 3 Tilly 14 uk" + "\n" +
89
"0 5 4 A9QSKPJJDM 16 bra" + "\n" +
90
"0 6 0 LoiraH 1 bra" + "\n" +
91
"0 6 1 KAN 5 unk" + "\n" +
92
"0 6 2 Tilly 7 uk" + "\n" +
93
"0 6 3 FRITZ 17 ita" + "\n" +
94
"0 6 4 JONIWWE 17 ina" + "\n" +
95
"0 7 0 SULTANOUU 30 rus" + "\n" +
96
"0 7 1 LOLO766 33 fra" + "\n" +
97
"0 7 2 ElyKho 37 mly" + "\n" +
98
"0 7 3 FREDRIK 37 den" + "\n" +
99
"0 7 4 ILXOM707 37 uzb" + "\n" +
100
"0 8 0 pardal 16 bra" + "\n" +
101
"0 8 1 ILXOM707 32 uzb" + "\n" +
102
"0 8 2 LOLO766 43 fra" + "\n" +
103
"0 8 3 NIKODZ 44 geo" + "\n" +
104
"0 8 4 SHAFFAF 48 ind" + "\n" +
105 110
"1 0 0 INEED7X7X7 1 rus" + "\n" +
106 111
"1 0 1 Tilly 1 uk" + "\n" +
107 112
"1 0 2 REBECCA 1 fra" + "\n" +
......
122 127
"1 3 2 NIKITOS 21 rus" + "\n" +
123 128
"1 3 3 PYCUK1707 21 rus" + "\n" +
124 129
"1 3 4 KEDAR 22 ind" + "\n" +
125
"1 4 0 Kinkz 7 ned" + "\n" +
126
"1 4 1 UTEK 14 ina" + "\n" +
127
"1 4 2 SJNT 23 ind" + "\n" +
128
"1 4 3 ABCDEF89 24 vie" + "\n" +
129
"1 4 4 SIJOYSIJ 27 ind" + "\n" +
130
"1 5 0 SIJOYSIJ 40 ind" + "\n" +
131
"1 5 1 UTEK 46 ina" + "\n" +
132
"1 5 2 SJNT 54 ind" + "\n" +
133
"1 5 3 FREDRIK 59 den" + "\n" +
134
"1 5 4 YANY 64 arg" + "\n" +
135
"1 6 0 UTEK 26 ina" + "\n" +
136
"1 6 1 FLAVINHA 57 bra" + "\n" +
137
"1 6 2 MILKMAN 57 fin" + "\n" +
138
"1 6 3 MARIOEAGLE 59 ita" + "\n" +
139
"1 6 4 LOLO766 85 fra" + "\n" +
140
"1 7 0 UTEK 45 ina" + "\n" +
141
"1 7 1 SEUA 157 vie" + "\n" +
142
"1 7 2 FREDRIK 201 den" + "\n" +
143
"1 7 3 ARMIN 205 rus" + "\n" +
144
"1 7 4 ILXOM7 215 uzb" + "\n" +
145
"1 8 0 SJNT 73 ind" + "\n" +
146
"1 8 1 MILKOMANN 219 fin" + "\n" +
147
"1 8 2 DEMONAIRE 288 uk" + "\n" +
148
"1 8 3 SIJ 311 ind" + "\n" +
149
"1 8 4 SIJOYSIJ 316 ind" + "\n" +
150 130
"2 0 0 INEED7X7X7 1 rus" + "\n" +
151 131
"2 0 1 HOMER0815 1 ger" + "\n" +
152 132
"2 0 2 EIP 1 usa" + "\n" +
......
167 147
"2 3 2 NONAME 24 fin" + "\n" +
168 148
"2 3 3 ABB0 26 ind" + "\n" +
169 149
"2 3 4 NOYS 27 mex" + "\n" +
170
"2 4 0 NIKITOS 37 rus" + "\n" +
171
"2 4 1 INEED7X7X7 45 rus" + "\n" +
172
"2 4 2 ILXOM7 50 uzb" + "\n" +
173
"2 4 3 NOYS 52 mex" + "\n" +
174
"2 4 4 MikeK 57 ger" + "\n" +
175
"2 5 0 NIKITOS 62 rus" + "\n" +
176
"2 5 1 LOLO766 70 fra" + "\n" +
177
"2 5 2 HUBBUB 87 swe" + "\n" +
178
"2 5 3 TETRAULT 90 usa" + "\n" +
179
"2 5 4 NOYS 115 mex" + "\n" +
180
"2 6 0 LOLO766 128 fra" + "\n" +
181
"2 6 1 KAN 193 unk" + "\n" +
182
"2 6 2 MEECMEEC 195 uk" + "\n" +
183
"2 6 3 HUBBUB 197 swe" + "\n" +
184
"2 6 4 PTOR 238 kyr" + "\n" +
185
"2 7 0 CHRISTA 268 za" + "\n" +
186
"2 7 1 MUZAFFER 288 tur" + "\n" +
187
"2 7 2 INEED7X7X7 323 rus" + "\n" +
188
"2 7 3 HECTOR58 360 mex" + "\n" +
189
"2 7 4 CABC 391 rus" + "\n" +
190
"2 8 0 CABC 245 rus" + "\n" +
191
"2 8 1 SIRWALTR 289 arg" + "\n" +
192
"2 8 2 NAMIT 558 ind" + "\n" +
193
"2 8 3 HDH580702 602 mex" + "\n" +
194
"2 8 4 HECTOR58 959 mex" + "\n" +
195 150
"3 0 0 JARO 1 ger" + "\n" +
196 151
"3 0 1 INEED7X7X7 1 rus" + "\n" +
197 152
"3 0 2 MDR04 1 ita" + "\n" +
......
211 166
"3 3 1 THISNAVIS 23 usa" + "\n" +
212 167
"3 3 2 INEED7X7X7 24 rus" + "\n" +
213 168
"3 3 3 RUBIK123 30 lit" + "\n" +
214
"3 3 4 SKY16 31 usa" + "\n" +
215
"3 4 0 NIKITOS 37 rus" + "\n" +
216
"3 4 1 6969696 43 usa" + "\n" +
217
"3 4 2 ALFREDOPVV 45 bra" + "\n" +
218
"3 4 3 HIEU 48 vie" + "\n" +
219
"3 4 4 INEED7X7X7 52 rus" + "\n" +
220
"3 5 0 ILXOM707 63 uzb" + "\n" +
221
"3 5 1 PHOENIX 77 fin" + "\n" +
222
"3 5 2 Nargiza 82 unk" + "\n" +
223
"3 5 3 NIKITOS 83 rus" + "\n" +
224
"3 5 4 ILXOM7 83 uzb" + "\n" +
225
"3 6 0 ILXOM7 81 uzb" + "\n" +
226
"3 6 1 ILXOM707 140 uzb" + "\n" +
227
"3 6 2 INEED7X7X7 143 rus" + "\n" +
228
"3 6 3 KAN 143 unk" + "\n" +
229
"3 6 4 RUBIK123 177 lit" + "\n" +
230
"3 7 0 ILXOM707 238 uzb" + "\n" +
231
"3 7 1 LOLO766 283 fra" + "\n" +
232
"3 7 2 INEED7X7X7 293 rus" + "\n" +
233
"3 7 3 RUBIK123 313 lit" + "\n" +
234
"3 7 4 SIRWALTR 343 arg" + "\n" +
235
"3 8 0 CABC 312 rus" + "\n" +
236
"3 8 1 HDH580702 411 mex" + "\n" +
237
"3 8 2 RUBIK123 558 lit" + "\n" +
238
"3 8 3 MUZAFFER 568 tur" + "\n" +
239
"3 8 4 NOYS 649 mex";
169
"3 3 4 SKY16 31 usa";
170
      }
171
    }
172
  catch( Exception e )
173
    {
174
    android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
240 175
    }
176

  
177
  mRunning = false;
178
  mReceiver.receive(mScores);
241 179
  }
180
}
src/main/java/org/distorted/magic/RubikScoresView.java
20 20
package org.distorted.magic;
21 21

  
22 22
import android.content.Context;
23
import android.support.v4.app.FragmentActivity;
23 24
import android.util.AttributeSet;
24 25
import android.view.View;
25 26
import android.widget.FrameLayout;
27
import android.widget.LinearLayout;
28
import android.widget.TextView;
26 29

  
27 30
///////////////////////////////////////////////////////////////////////////////////////////////////
28 31

  
......
58 61
    super(context);
59 62

  
60 63
    mPosition = position;
64
    View view = inflate(context, R.layout.scores_downloading, null);
65
    addView(view);
61 66
    }
62 67

  
63 68
///////////////////////////////////////////////////////////////////////////////////////////////////
64 69

  
65
  @Override
66
  public void onAttachedToWindow()
70
  private int getLevelLayoutID(int level)
67 71
    {
68
    super.onAttachedToWindow();
72
    return 100+level;
73
    }
69 74

  
70
    Context context = getContext();
71
    View view = inflate(context, R.layout.scores_downloading, null);
72
    addView(view);
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  public void prepareView(FragmentActivity act)
78
    {
79
    removeAllViews();
80

  
81
    View tab = inflate(act, R.layout.scores_tab, null);
82
    LinearLayout layout = tab.findViewById(R.id.tabLayout);
83
    addView(tab);
84

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

  
92
      layout.addView(level);
93
      }
94
    }
95

  
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);
104

  
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);
108

  
109
      float real_time = time/10.0f;
110

  
111
      textName.setText(name);
112
      textTime.setText( String.valueOf(real_time) );
113
      textCountry.setText(country);
114

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

  
29 29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 30

  
31
class RubikScoresViewPager extends PagerAdapter implements RubikScores.ScoresReceiver
31
class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader.Receiver
32 32
  {
33 33
  private FragmentActivity mAct;
34
  private RubikScoresView[] mViews;
34 35

  
35 36
///////////////////////////////////////////////////////////////////////////////////////////////////
36 37

  
37 38
  RubikScoresViewPager(FragmentActivity act, ViewPager viewPager)
38 39
    {
39 40
    mAct = act;
41
    mViews = new RubikScoresView[getCount()];
40 42
    viewPager.setAdapter(this);
41 43
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
42 44
    }
43 45

  
44 46
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// TODO
46 47

  
47
  public void receive(String scores)
48
  public void receive(final String scores)
48 49
    {
50
    mAct.runOnUiThread(new Runnable()
51
      {
52
      @Override
53
      public void run()
54
        {
55
        for(int i=0; i<RubikSize.LENGTH; i++)
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;
68
          }
69
        }
70
      });
71
    }
72

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
49 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
      }
50 106
    }
51 107

  
52 108
///////////////////////////////////////////////////////////////////////////////////////////////////
......
55 111
  @NonNull
56 112
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
57 113
    {
58
    RubikScoresView view = new RubikScoresView(mAct,position);
59
    collection.addView(view);
60

  
61
    return view;
114
    mViews[position] = new RubikScoresView(mAct,position);
115
    collection.addView(mViews[position]);
116

  
117
    boolean allCreated = true;
118

  
119
    for(int i=0; i<RubikSize.LENGTH; i++)
120
      {
121
      if( mViews[i]==null )
122
        {
123
        allCreated = false;
124
        break;
125
        }
126
      }
127

  
128
    if( allCreated )
129
      {
130
      RubikScoresDownloader downloader = new RubikScoresDownloader();
131
      downloader.download(this);
132
      }
133

  
134
    return mViews[position];
62 135
    }
63 136

  
64 137
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/magic/RubikSettings.java
23 23
import android.os.Bundle;
24 24
import android.support.annotation.NonNull;
25 25
import android.support.v4.app.FragmentActivity;
26
import android.support.v4.content.ContextCompat;
26 27
import android.support.v7.app.AlertDialog;
27 28
import android.support.v7.app.AppCompatDialogFragment;
28 29
import android.util.DisplayMetrics;
......
69 70
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
70 71

  
71 72
    int margin = (int)(scale*10 + 0.5f);
72
    int color  = act.getResources().getColor(R.color.grey);
73
    int color  = ContextCompat.getColor(act, R.color.grey);
73 74
    LinearLayout outerLayout = new LinearLayout(act);
74 75
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
75 76
    outerLayoutParams.topMargin    = margin;
src/main/res/layout/level_records.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:id="@+id/levelLayout"
4
    android:layout_width="match_parent"
5
    android:layout_height="wrap_content"
6
    android:background="@color/grey"
7

  
8
    android:paddingBottom="0dp"
9
    android:paddingTop="0dp"
10
    android:paddingLeft="5dp"
11
    android:paddingRight="5dp"
12
    android:orientation="vertical" >
13

  
14
    <TextView
15
        android:id="@+id/levelTitle"
16
        android:layout_width="match_parent"
17
        android:layout_height="40dp"
18
        android:textSize="26sp"
19
        android:gravity="center"
20
        android:background="@color/grey"
21
        android:paddingBottom="5dp"
22
        android:paddingTop="0dp"
23
        android:paddingLeft="5dp"
24
        android:paddingRight="5dp"/>
25

  
26
</LinearLayout>
src/main/res/layout/level_row.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="32dp"
5
    android:orientation="horizontal" >
6

  
7
    <TextView
8
        android:id="@+id/level_row_country"
9
        android:layout_width="match_parent"
10
        android:layout_height="match_parent"
11
        android:layout_weight="1.5"
12
        android:textSize="20sp"
13
        android:gravity="left"
14
        android:background="@color/black"
15
        android:paddingLeft="5dp"/>
16

  
17
    <TextView
18
        android:id="@+id/level_row_name"
19
        android:layout_width="match_parent"
20
        android:layout_height="match_parent"
21
        android:layout_weight="1"
22
        android:textSize="20sp"
23
        android:gravity="left"
24
        android:background="@color/black"
25
        />
26

  
27
    <TextView
28
        android:id="@+id/level_row_time"
29
        android:layout_width="match_parent"
30
        android:layout_height="match_parent"
31
        android:layout_weight="1.5"
32
        android:textSize="20sp"
33
        android:gravity="right"
34
        android:background="@color/black"
35
        android:paddingRight="5dp"
36
        />
37
</LinearLayout>
src/main/res/layout/scores_tab.xml
14 14
             android:id="@+id/tabLayout"
15 15
             android:layout_width="match_parent"
16 16
             android:layout_height="wrap_content"
17
             android:background="@color/grey"
17 18
             android:orientation="vertical" >
18 19
         </LinearLayout>
19 20

  
src/main/res/values/colors.xml
5 5
    <color name="colorAccent">#D81B60</color>
6 6
    <color name="red">#ffff0000</color>
7 7
    <color name="grey">#ff333333</color>
8
    <color name="black">#ff010101</color>
8 9
</resources>
src/main/res/values/strings.xml
21 21
    <string name="credits2">Code, tutorials, learn how to write your own graphics effect: <a href="http://www.distorted.org/cube">Distorted.org</a></string>
22 22

  
23 23
    <string name="ms_placeholder">%1$d ms</string>
24
    <string name="sc_placeholder">Scramble %1$d</string>
24 25
</resources>

Also available in: Unified diff