Project

General

Profile

« Previous | Next » 

Revision 4f9f99a2

Added by Leszek Koltunski almost 5 years ago

Separate the network package; rename RubikSize to RubikObject.

View differences:

src/main/java/org/distorted/dialog/RubikDialogScores.java
36 36
import android.widget.TextView;
37 37

  
38 38
import org.distorted.magic.R;
39
import org.distorted.magic.RubikSize;
39
import org.distorted.object.RubikObject;
40 40

  
41 41
///////////////////////////////////////////////////////////////////////////////////////////////////
42 42

  
......
104 104

  
105 105
    viewPager.setCurrentItem(curTab);
106 106

  
107
    for (int i = 0; i< RubikSize.LENGTH; i++)
107
    for (int i = 0; i< RubikObject.LENGTH; i++)
108 108
      {
109 109
      ImageView imageView = new ImageView(act);
110
      imageView.setImageResource(RubikSize.getSize(i).getIconID());
110
      imageView.setImageResource(RubikObject.getObject(i).getIconID());
111 111
      TabLayout.Tab tab = tabLayout.getTabAt(i);
112 112
      if(tab!=null) tab.setCustomView(imageView);
113 113
      }
src/main/java/org/distorted/dialog/RubikDialogScoresPagerAdapter.java
26 26
import android.view.View;
27 27
import android.view.ViewGroup;
28 28

  
29
import org.distorted.magic.RubikScoresDownloader;
30
import org.distorted.magic.RubikSize;
29
import org.distorted.network.RubikScoresDownloader;
30
import org.distorted.object.RubikObject;
31 31
import org.distorted.uistate.RubikStatePlay;
32 32

  
33 33
///////////////////////////////////////////////////////////////////////////////////////////////////
......
48 48

  
49 49
    addPage(mViews[c],country[c],name[c],time[c]);
50 50

  
51
    for(int i = 0; i< RubikSize.LENGTH; i++)
51
    for(int i = 0; i< RubikObject.LENGTH; i++)
52 52
      {
53 53
      if( i==c ) continue;
54 54

  
......
65 65
      @Override
66 66
      public void run()
67 67
        {
68
        for(int i=0; i<RubikSize.LENGTH; i++)
68
        for(int i = 0; i< RubikObject.LENGTH; i++)
69 69
          {
70 70
          mViews[i].prepareView(mAct);
71 71
          }
......
121 121
      @Override
122 122
      public void run()
123 123
        {
124
        for(int i=0; i<RubikSize.LENGTH; i++)
124
        for(int i = 0; i< RubikObject.LENGTH; i++)
125 125
          {
126 126
          mViews[i].exception(exce);
127 127
          }
......
134 134
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
135 135
    {
136 136
    mAct = act;
137
    mViews = new RubikDialogScoresView[RubikSize.LENGTH];
137
    mViews = new RubikDialogScoresView[RubikObject.LENGTH];
138 138
    mViewPager = viewPager;
139 139

  
140 140
    viewPager.setAdapter(this);
141
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
141
    viewPager.setOffscreenPageLimit( RubikObject.LENGTH-1 );
142 142
    }
143 143

  
144 144
///////////////////////////////////////////////////////////////////////////////////////////////////
......
152 152

  
153 153
    boolean allCreated = true;
154 154

  
155
    for(int i=0; i<RubikSize.LENGTH; i++)
155
    for(int i = 0; i< RubikObject.LENGTH; i++)
156 156
      {
157 157
      if( mViews[i]==null )
158 158
        {
......
183 183
  @Override
184 184
  public int getCount()
185 185
    {
186
    return RubikSize.LENGTH;
186
    return RubikObject.LENGTH;
187 187
    }
188 188

  
189 189
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/dialog/RubikDialogScoresView.java
29 29
import android.widget.TextView;
30 30

  
31 31
import org.distorted.magic.R;
32
import org.distorted.magic.RubikScoresDownloader;
32
import org.distorted.network.RubikScoresDownloader;
33 33

  
34 34
///////////////////////////////////////////////////////////////////////////////////////////////////
35 35

  
src/main/java/org/distorted/magic/RubikActivity.java
31 31
import org.distorted.effect.BaseEffect;
32 32
import org.distorted.library.main.DistortedLibrary;
33 33

  
34
import org.distorted.network.RubikScoresDownloader;
35
import org.distorted.object.RubikObject;
34 36
import org.distorted.uistate.RubikState;
35 37
import org.distorted.uistate.RubikStateAbstract;
36 38
import org.distorted.uistate.RubikStatePlay;
......
88 90
      {
89 91
      int id = v.getId();
90 92

  
91
      if( id>=0 && id<RubikSize.LENGTH )
93
      if( id>=0 && id< RubikObject.LENGTH )
92 94
        {
93
        int size = RubikSize.getSize(id).getCubeSize();
95
        int size = RubikObject.getObject(id).getObjectSize();
94 96

  
95 97
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
96 98
        boolean success = view.getRenderer().createCube(size);
src/main/java/org/distorted/magic/RubikRenderer.java
30 30
import org.distorted.library.mesh.MeshFlat;
31 31
import org.distorted.library.message.EffectListener;
32 32
import org.distorted.object.RubikCube;
33
import org.distorted.object.RubikObject;
33 34
import org.distorted.uistate.RubikState;
34 35
import org.distorted.uistate.RubikStatePlay;
35 36

  
......
87 88

  
88 89
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
89 90
      int size = play.getButton();
90
      mNextCubeSize = RubikSize.getSize(size).getCubeSize();
91
      mNextCubeSize = RubikObject.getObject(size).getObjectSize();
91 92
      }
92 93

  
93 94
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/magic/RubikScoresDownloader.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.magic;
21

  
22
import android.content.res.Resources;
23

  
24
import java.io.InputStream;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27
import java.net.UnknownHostException;
28

  
29
import org.distorted.uistate.RubikStatePlay;
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
public class RubikScoresDownloader implements Runnable
34
  {
35
  public interface Receiver
36
    {
37
    void receive(int[][][] country, String[][][] name, String[][][] time);
38
    void exception(String exception);
39
    }
40

  
41
  public static final int MAX_PLACES = 12;
42

  
43
  private static final int DOWNLOAD   = 0;
44
  private static final int SUBMIT     = 1;
45
  private static final int IDLE       = 2;
46

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

  
49
  private final String[] hex = {
50
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
51
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
52
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
53
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
54
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
55
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
56
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
57
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
58
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
59
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
60
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
61
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
62
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
63
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
64
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
65
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
66
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
67
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
68
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
69
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
70
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
71
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
72
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
73
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
74
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
75
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
76
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
77
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
78
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
79
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
80
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
81
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
82
    };
83

  
84
  private static boolean mRunning = false;
85
  private static int mMode = IDLE;
86
  private static Receiver mReceiver;
87
  private static Resources mResources;
88
  private static String mPackageName;
89

  
90
  private static String mScores = "";
91
  private static int[][][] mCountry = new int   [RubikSize.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
92
  private static String[][][] mName = new String[RubikSize.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
93
  private static String[][][] mTime = new String[RubikSize.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  private void fillValues()
98
    {
99
    int begin=-1 ,end, len = mScores.length();
100

  
101
    while( begin<len )
102
      {
103
      end = mScores.indexOf('\n', begin+1);
104
      if( end<0 ) end = len;
105
      fillRow(mScores.substring(begin+1,end));
106
      begin = end;
107
      }
108
    }
109

  
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

  
112
  private void fillRow(String row)
113
    {
114
    int s1 = row.indexOf(' ');
115
    int s2 = row.indexOf(' ',s1+1);
116
    int s3 = row.indexOf(' ',s2+1);
117
    int s4 = row.indexOf(' ',s3+1);
118
    int s5 = row.indexOf(' ',s4+1);
119
    int s6 = row.length();
120

  
121
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
122
      {
123
      int size = Integer.valueOf( row.substring(0,s1) );
124

  
125
      if( size>=0 && size<RubikSize.LENGTH )
126
        {
127
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
128
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
129
        String name    = row.substring(s3+1, s4);
130
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
131
        String country = row.substring(s5+1, s6);
132
        String realTime= String.valueOf(time/10.0f);
133

  
134
        if(level>=0 && level<RubikStatePlay.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
135
          {
136
          int resID = mResources.getIdentifier( country, "drawable", mPackageName);
137
          mCountry[size][level][place] = resID!=0 ? resID:R.drawable.unk;
138
          mName[size][level][place]    = name;
139
          mTime[size][level][place]    = realTime;
140
          }
141
        }
142
      }
143
    }
144

  
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

  
147
  private String URLencode(String s)
148
    {
149
    StringBuilder sbuf = new StringBuilder();
150
    int len = s.length();
151

  
152
    for (int i = 0; i < len; i++)
153
      {
154
      int ch = s.charAt(i);
155

  
156
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
157
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
158
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
159
      else if (ch == ' '             ) sbuf.append('+');
160
      else if (ch == '-' || ch == '_'
161
            || ch == '.' || ch == '!'
162
            || ch == '~' || ch == '*'
163
            || ch == '\'' || ch == '('
164
            || ch == ')'             ) sbuf.append((char)ch);
165
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
166
      else if (ch <= 0x07FF)
167
        {
168
        sbuf.append(hex[0xc0 | (ch >> 6)]);
169
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
170
        }
171
      else
172
        {
173
        sbuf.append(hex[0xe0 | (ch >> 12)]);
174
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
175
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
176
        }
177
      }
178

  
179
    return sbuf.toString();
180
    }
181

  
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

  
184
  private boolean doDownload()
185
    {
186
    String veri = "distored";
187
    String name = URLencode(veri);
188
    int numRuns = 1;
189
    String version = R.string.app_version+"d";
190
    String message=URL+"/download.cgi?n="+name+"&r="+numRuns+"&e="+version;
191

  
192
    try
193
      {
194
      java.net.URL connectURL = new URL(message);
195
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
196

  
197
      conn.setDoInput(true);
198
      conn.setDoOutput(true);
199
      conn.setUseCaches(false);
200
      conn.setRequestMethod("GET");
201
      conn.connect();
202
      conn.getOutputStream().flush();
203

  
204
      try( InputStream is = conn.getInputStream() )
205
        {
206
        int ch;
207
        StringBuilder sb = new StringBuilder();
208
        while( ( ch = is.read() ) != -1 )
209
          {
210
          sb.append( (char)ch );
211
          }
212
        mScores = sb.toString();
213
        }
214
      catch( final Exception e)
215
        {
216
        mReceiver.exception("Failed to get an answer from the High Scores server");
217
        return false;
218
        }
219
      }
220
    catch( final UnknownHostException e )
221
      {
222
      mReceiver.exception("No access to Internet");
223
      return false;
224
      }
225
    catch( final SecurityException e )
226
      {
227
      mReceiver.exception("Application not authorized to connect to the Internet");
228
      return false;
229
      }
230
    catch( final Exception e )
231
      {
232
      mReceiver.exception(e.getMessage());
233
      return false;
234
      }
235

  
236
    return true;
237
    }
238

  
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

  
241
  private boolean gottaDownload()
242
    {
243
    return ((mScores.length()==0) && !mRunning);
244
    }
245

  
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

  
248
  static void onPause()
249
    {
250
    mRunning = false;
251
    }
252

  
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

  
255
  public void download(Receiver receiver, Resources resources, String packageName)
256
    {
257
    mReceiver = receiver;
258
    mResources= resources;
259
    mPackageName = packageName;
260
    mMode = DOWNLOAD;
261

  
262
    Thread networkThrd = new Thread(this);
263
    networkThrd.start();
264
    }
265

  
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

  
268
  public void run()
269
    {
270
    boolean success=true;
271

  
272
    try
273
      {
274
      if( gottaDownload() )
275
        {
276
        mRunning = true;
277
        success = doDownload();
278
        fillValues();
279
        }
280
      }
281
    catch( Exception e )
282
      {
283
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
284
      }
285

  
286
    mRunning = false;
287

  
288
    if( success )
289
      {
290
      mReceiver.receive(mCountry, mName, mTime);
291
      }
292
    }
293
}
src/main/java/org/distorted/magic/RubikSize.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.magic;
21

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

  
24
public enum RubikSize
25
  {
26
  SIZE2 ( 2, R.drawable.button2 ),
27
  SIZE3 ( 3, R.drawable.button3 ),
28
  SIZE4 ( 4, R.drawable.button4 ),
29
  SIZE5 ( 5, R.drawable.button5 ),
30
  ;
31

  
32
  public static final int LENGTH = values().length;
33
  private final int mCubeSize, mIconID;
34
  private static final RubikSize[] sizes;
35

  
36
  static
37
    {
38
    int i = 0;
39
    sizes = new RubikSize[LENGTH];
40

  
41
    for(RubikSize size: RubikSize.values())
42
      {
43
      sizes[i] = size;
44
      i++;
45
      }
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  public static RubikSize getSize(int ordinal)
51
    {
52
    return sizes[ordinal];
53
    }
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
  RubikSize(int size, int iconID)
58
    {
59
    mCubeSize = size;
60
    mIconID   = iconID;
61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  public int getIconID()
66
    {
67
    return mIconID;
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  public int getCubeSize()
73
    {
74
    return mCubeSize;
75
    }
76
  }
src/main/java/org/distorted/network/RubikScoresDownloader.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.network;
21

  
22
import android.content.res.Resources;
23

  
24
import java.io.InputStream;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27
import java.net.UnknownHostException;
28

  
29
import org.distorted.magic.R;
30
import org.distorted.object.RubikObject;
31
import org.distorted.uistate.RubikStatePlay;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
public class RubikScoresDownloader implements Runnable
36
  {
37
  public interface Receiver
38
    {
39
    void receive(int[][][] country, String[][][] name, String[][][] time);
40
    void exception(String exception);
41
    }
42

  
43
  public static final int MAX_PLACES = 12;
44

  
45
  private static final int DOWNLOAD   = 0;
46
  private static final int SUBMIT     = 1;
47
  private static final int IDLE       = 2;
48

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

  
51
  private final String[] hex = {
52
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
53
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
54
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
55
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
56
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
57
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
58
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
59
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
60
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
61
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
62
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
63
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
64
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
65
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
66
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
67
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
68
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
69
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
70
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
71
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
72
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
73
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
74
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
75
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
76
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
77
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
78
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
79
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
80
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
81
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
82
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
83
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
84
    };
85

  
86
  private static boolean mRunning = false;
87
  private static int mMode = IDLE;
88
  private static Receiver mReceiver;
89
  private static Resources mResources;
90
  private static String mPackageName;
91

  
92
  private static String mScores = "";
93
  private static int[][][] mCountry = new int   [RubikObject.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
94
  private static String[][][] mName = new String[RubikObject.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
95
  private static String[][][] mTime = new String[RubikObject.LENGTH][RubikStatePlay.MAX_SCRAMBLE][MAX_PLACES];
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
  private void fillValues()
100
    {
101
    int begin=-1 ,end, len = mScores.length();
102

  
103
    while( begin<len )
104
      {
105
      end = mScores.indexOf('\n', begin+1);
106
      if( end<0 ) end = len;
107
      fillRow(mScores.substring(begin+1,end));
108
      begin = end;
109
      }
110
    }
111

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

  
114
  private void fillRow(String row)
115
    {
116
    int s1 = row.indexOf(' ');
117
    int s2 = row.indexOf(' ',s1+1);
118
    int s3 = row.indexOf(' ',s2+1);
119
    int s4 = row.indexOf(' ',s3+1);
120
    int s5 = row.indexOf(' ',s4+1);
121
    int s6 = row.length();
122

  
123
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
124
      {
125
      int size = Integer.valueOf( row.substring(0,s1) );
126

  
127
      if( size>=0 && size< RubikObject.LENGTH )
128
        {
129
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
130
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
131
        String name    = row.substring(s3+1, s4);
132
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
133
        String country = row.substring(s5+1, s6);
134
        String realTime= String.valueOf(time/10.0f);
135

  
136
        if(level>=0 && level<RubikStatePlay.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
137
          {
138
          int resID = mResources.getIdentifier( country, "drawable", mPackageName);
139
          mCountry[size][level][place] = resID!=0 ? resID: R.drawable.unk;
140
          mName[size][level][place]    = name;
141
          mTime[size][level][place]    = realTime;
142
          }
143
        }
144
      }
145
    }
146

  
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

  
149
  private String URLencode(String s)
150
    {
151
    StringBuilder sbuf = new StringBuilder();
152
    int len = s.length();
153

  
154
    for (int i = 0; i < len; i++)
155
      {
156
      int ch = s.charAt(i);
157

  
158
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
159
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
160
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
161
      else if (ch == ' '             ) sbuf.append('+');
162
      else if (ch == '-' || ch == '_'
163
            || ch == '.' || ch == '!'
164
            || ch == '~' || ch == '*'
165
            || ch == '\'' || ch == '('
166
            || ch == ')'             ) sbuf.append((char)ch);
167
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
168
      else if (ch <= 0x07FF)
169
        {
170
        sbuf.append(hex[0xc0 | (ch >> 6)]);
171
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
172
        }
173
      else
174
        {
175
        sbuf.append(hex[0xe0 | (ch >> 12)]);
176
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
177
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
178
        }
179
      }
180

  
181
    return sbuf.toString();
182
    }
183

  
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

  
186
  private boolean doDownload()
187
    {
188
    String veri = "distored";
189
    String name = URLencode(veri);
190
    int numRuns = 1;
191
    String version = R.string.app_version+"d";
192
    String message=URL+"/download.cgi?n="+name+"&r="+numRuns+"&e="+version;
193

  
194
    try
195
      {
196
      java.net.URL connectURL = new URL(message);
197
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
198

  
199
      conn.setDoInput(true);
200
      conn.setDoOutput(true);
201
      conn.setUseCaches(false);
202
      conn.setRequestMethod("GET");
203
      conn.connect();
204
      conn.getOutputStream().flush();
205

  
206
      try( InputStream is = conn.getInputStream() )
207
        {
208
        int ch;
209
        StringBuilder sb = new StringBuilder();
210
        while( ( ch = is.read() ) != -1 )
211
          {
212
          sb.append( (char)ch );
213
          }
214
        mScores = sb.toString();
215
        }
216
      catch( final Exception e)
217
        {
218
        mReceiver.exception("Failed to get an answer from the High Scores server");
219
        return false;
220
        }
221
      }
222
    catch( final UnknownHostException e )
223
      {
224
      mReceiver.exception("No access to Internet");
225
      return false;
226
      }
227
    catch( final SecurityException e )
228
      {
229
      mReceiver.exception("Application not authorized to connect to the Internet");
230
      return false;
231
      }
232
    catch( final Exception e )
233
      {
234
      mReceiver.exception(e.getMessage());
235
      return false;
236
      }
237

  
238
    return true;
239
    }
240

  
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

  
243
  private boolean gottaDownload()
244
    {
245
    return ((mScores.length()==0) && !mRunning);
246
    }
247

  
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

  
250
  @Override
251
  public void run()
252
    {
253
    boolean success=true;
254

  
255
    try
256
      {
257
      if( gottaDownload() )
258
        {
259
        mRunning = true;
260
        success = doDownload();
261
        fillValues();
262
        }
263
      }
264
    catch( Exception e )
265
      {
266
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
267
      }
268

  
269
    mRunning = false;
270

  
271
    if( success )
272
      {
273
      mReceiver.receive(mCountry, mName, mTime);
274
      }
275
    }
276

  
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// PUBLIC API
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

  
281
  public static void onPause()
282
    {
283
    mRunning = false;
284
    }
285

  
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

  
288
  public void download(Receiver receiver, Resources resources, String packageName)
289
    {
290
    mReceiver = receiver;
291
    mResources= resources;
292
    mPackageName = packageName;
293
    mMode = DOWNLOAD;
294

  
295
    Thread networkThrd = new Thread(this);
296
    networkThrd.start();
297
    }
298
}
src/main/java/org/distorted/object/RubikObject.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.object;
21

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

  
24
import org.distorted.magic.R;
25

  
26
public enum RubikObject
27
  {
28
  CUBE2 ( 2, R.drawable.button2 ),
29
  CUBE3 ( 3, R.drawable.button3 ),
30
  CUBE4 ( 4, R.drawable.button4 ),
31
  CUBE5 ( 5, R.drawable.button5 ),
32
  ;
33

  
34
  public static final int LENGTH = values().length;
35
  private final int mObjectSize, mIconID;
36
  private static final RubikObject[] objects;
37

  
38
  static
39
    {
40
    int i = 0;
41
    objects = new RubikObject[LENGTH];
42

  
43
    for(RubikObject size: RubikObject.values())
44
      {
45
      objects[i] = size;
46
      i++;
47
      }
48
    }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
  public static RubikObject getObject(int ordinal)
53
    {
54
    return objects[ordinal];
55
    }
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
  RubikObject(int size, int iconID)
60
    {
61
    mObjectSize = size;
62
    mIconID     = iconID;
63
    }
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

  
67
  public int getIconID()
68
    {
69
    return mIconID;
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
  public int getObjectSize()
75
    {
76
    return mObjectSize;
77
    }
78
  }
src/main/java/org/distorted/uistate/RubikStatePlay.java
34 34
import org.distorted.component.HorizontalNumberPicker;
35 35
import org.distorted.magic.R;
36 36
import org.distorted.magic.RubikActivity;
37
import org.distorted.magic.RubikSize;
37
import org.distorted.object.RubikObject;
38 38

  
39 39
///////////////////////////////////////////////////////////////////////////////////////////////////
40 40

  
......
46 46

  
47 47
  private HorizontalNumberPicker mPicker;
48 48
  private int mPickerValue;
49
  private int mButton = RubikSize.SIZE3.ordinal();
49
  private int mButton = RubikObject.CUBE3.ordinal();
50 50

  
51 51
///////////////////////////////////////////////////////////////////////////////////////////////////
52 52

  
......
78 78
    int padding = (int)(3*scale + 0.5f);
79 79
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
80 80

  
81
    for(int i = 0; i< RubikSize.LENGTH; i++)
81
    for(int i = 0; i< RubikObject.LENGTH; i++)
82 82
      {
83 83
      ImageButton button = new ImageButton(act);
84 84
      button.setLayoutParams(params);
85 85
      button.setId(i);
86 86
      button.setPadding(padding,0,padding,0);
87
      int iconID = RubikSize.getSize(i).getIconID();
87
      int iconID = RubikObject.getObject(i).getIconID();
88 88
      button.setImageResource(iconID);
89 89
      button.setOnClickListener(act);
90 90
      layoutBot.addView(button);
......
149 149
    {
150 150
    mButton = button;
151 151

  
152
    for(int b=0; b<RubikSize.LENGTH; b++)
152
    for(int b = 0; b< RubikObject.LENGTH; b++)
153 153
      {
154 154
      Drawable d = act.findViewById(b).getBackground();
155 155

  

Also available in: Unified diff