Project

General

Profile

« Previous | Next » 

Revision 82ce8e64

Added by Leszek Koltunski about 4 years ago

Progress with getting/setting Country and DeviceID.

View differences:

src/main/java/org/distorted/scores/RubikScores.java
19 19

  
20 20
package org.distorted.scores;
21 21

  
22
import android.content.Context;
22 23
import android.content.SharedPreferences;
24
import android.telephony.TelephonyManager;
23 25

  
24
import org.distorted.magic.R;
25 26
import org.distorted.object.RubikObjectList;
26 27

  
28
import java.util.UUID;
29

  
27 30
import static org.distorted.object.RubikObjectList.MAX_SIZE;
28 31
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
29 32
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
30 33

  
31 34
///////////////////////////////////////////////////////////////////////////////////////////////////
32
// hold my own scores, some other statistics.
35
// hold my own scores, and some other statistics.
33 36

  
34 37
public class RubikScores
35 38
  {
......
39 42
  private long[][][] mRecords;
40 43
  private int [][][] mSubmitted;
41 44

  
42
  private String mName;
45
  private String mName, mCountry;
43 46
  private boolean mNameIsVerified;
44 47
  private int mNumRuns;
45 48
  private int mNumPlays;
46
  private int mCountryID;
49
  private int mDeviceID;
47 50

  
48 51
///////////////////////////////////////////////////////////////////////////////////////////////////
49 52

  
......
61 64
          }
62 65

  
63 66
    mName = "YOU";
67
    mCountry = "un";
68

  
64 69
    mNameIsVerified = false;
70

  
65 71
    mNumPlays= -1;
66 72
    mNumRuns = -1;
67
    mCountryID = R.drawable.un;
73
    mDeviceID= -1;
68 74
    }
69 75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/*
78
  private static int computeHash()
79
    {
80
    final int MODULO = 227;
81
    int ret = 0;
82

  
83
    for(int i=0; i<MAX_CUBE-1; i++)
84
      ret += (2*i+3)*passedlevel[i];
85

  
86
    for(int i=0; i<MAX_CUBE-1; i++)
87
      for(int j=0; j<MAX_LEVELS; j++)
88
        ret += (i*i+3*j)*records[i][j];
89

  
90
    int length = veriname==null ? 0 : veriname.length();
91

  
92
    for(int i=0;i<length;i++)
93
      ret += i*veriname.charAt(i);
94

  
95
    return (ret%=MODULO);
96
    }
97
*/
70 98
///////////////////////////////////////////////////////////////////////////////////////////////////
71 99

  
72 100
  public static RubikScores getInstance()
......
118 146
    editor.putBoolean("scores_isVerified", mNameIsVerified);
119 147
    editor.putInt("scores_numPlays", mNumPlays);
120 148
    editor.putInt("scores_numRuns" , mNumRuns);
149
    editor.putInt("scores_deviceid", mDeviceID);
121 150
    }
122 151

  
123 152
///////////////////////////////////////////////////////////////////////////////////////////////////
......
181 210
    mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
182 211
    mNumPlays       = preferences.getInt("scores_numPlays", 0);
183 212
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
213
    mDeviceID       = preferences.getInt("scores_deviceid",-1);
214

  
215
    if( mDeviceID==-1 ) mDeviceID = getDeviceID();
184 216
    }
185 217

  
186 218
///////////////////////////////////////////////////////////////////////////////////////////////////
......
232 264
///////////////////////////////////////////////////////////////////////////////////////////////////
233 265
// TODO
234 266

  
235
  public void setCountry(int country)
267
  public void setSubmitted(int object, int size, int scramble)
236 268
    {
237
    mCountryID = country;
269
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
270

  
271
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
272
      {
273
      mSubmitted[object][size][scramble] = 1;
274
      }
238 275
    }
239 276

  
240 277
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// TODO
242 278

  
243
  public void setSubmitted(int object, int size, int scramble)
279
  public void setCountry(Context context)
244 280
    {
245
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
281
    TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
246 282

  
247
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
283
    if( tM!=null )
248 284
      {
249
      mSubmitted[object][size][scramble] = 1;
285
      mCountry = tM.getSimCountryIso();
286

  
287
      if( mCountry==null || mCountry.length()<=1 )
288
        {
289
        mCountry = tM.getNetworkCountryIso();
290
        }
291
      }
292

  
293
    // Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here
294
    // because we later on map this to a resource name (the flag) and 'do' is a resevred Java keyword
295
    // and can't be a resource name.
296

  
297
    if( mCountry.equals("do") ) mCountry = "dm";
298
    }
299

  
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

  
302
  private int getDeviceID()
303
    {
304
    int id;
305

  
306
    try
307
      {
308
      String s = UUID.randomUUID().toString();
309
      id = (s!=null ? s.hashCode():0);
310
      }
311
    catch(Exception ex)
312
      {
313
      id = 0;
314
      android.util.Log.e("scores", "Exception in getDeviceID()");
250 315
      }
316

  
317
    return id<0 ? -id : id;
251 318
    }
252 319

  
320

  
253 321
///////////////////////////////////////////////////////////////////////////////////////////////////
254 322

  
255 323
  public long getRecord(int object, int size, int scramble)
......
310 378

  
311 379
///////////////////////////////////////////////////////////////////////////////////////////////////
312 380

  
313
  public int getCountryID()
381
  public String getCountry()
314 382
    {
315
    return mCountryID;
383
    return mCountry;
316 384
    }
317 385
  }

Also available in: Unified diff