Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 0b5e585c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.external;
11

    
12
import java.io.InputStream;
13
import java.util.ArrayList;
14
import java.util.Locale;
15

    
16
import android.content.Context;
17
import android.graphics.Bitmap;
18
import org.distorted.objects.RubikObjectList;
19

    
20
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
21

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

    
24
public class RubikUpdates
25
{
26
  public static class UpdateInfo
27
    {
28
    public final String mObjectShortName;
29
    public final String mObjectLongName;
30
    public final String mDescription;
31
    public final int mObjectMinorVersion;
32
    public final int mExtrasMinorVersion;
33
    public final int mPercent;
34
    public final int mIconPresent;
35
    public final boolean mUpdateObject;
36
    public final boolean mUpdateExtras;
37

    
38
    public int mNumScrambles;
39
    public int mDifficulty;
40
    public Bitmap mIcon;
41
    public InputStream mObjectStream;
42
    public InputStream mExtrasStream;
43

    
44
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
45
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
46
      {
47
      mObjectShortName    = shortName;
48
      mObjectLongName     = longName;
49
      mDescription        = description;
50
      mObjectMinorVersion = objectMinor;
51
      mExtrasMinorVersion = extrasMinor;
52
      mPercent            = percent;
53
      mIconPresent        = iconPresent;
54
      mUpdateObject       = updateO;
55
      mUpdateExtras       = updateE;
56

    
57
      mIcon = null;
58
      mNumScrambles = 0;
59
      mDifficulty   = 0;
60
      }
61
    }
62

    
63
  private String mUrl;
64
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public RubikUpdates()
69
    {
70
    mCompleted = new ArrayList<>();
71
    mStarted   = new ArrayList<>();
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  private String debug(ArrayList<UpdateInfo> list)
77
    {
78
    String ret = "";
79

    
80
    for( UpdateInfo info : list)
81
      {
82
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
83
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
84
      }
85

    
86
    return ret;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private void parseLine(String[] elements)
92
    {
93
    String shortName   = elements[0].trim();
94
    String objMinor    = elements[1].trim();
95
    String extMinor    = elements[2].trim();
96
    String percent     = elements[3].trim();
97
    String iconPresent = elements[4].trim();
98
    String longName    = elements[5];
99
    String description = elements[6];
100
    int oMinor, eMinor, oPercent, oIcon;
101

    
102
    try { oMinor = Integer.parseInt(objMinor); }
103
    catch (NumberFormatException ex) { oMinor = -1; }
104
    try { eMinor = Integer.parseInt(extMinor); }
105
    catch (NumberFormatException ex) { eMinor = -1; }
106
    try { oPercent = Integer.parseInt(percent); }
107
    catch (NumberFormatException ex) { oPercent = -1; }
108
    try { oIcon = Integer.parseInt(iconPresent); }
109
    catch (NumberFormatException ex) { oIcon = 0; }
110

    
111
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
112
      {
113
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
114
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
115
      boolean updateO=true, updateE=true;
116

    
117
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
118

    
119
      if( objOrdinal>=0 )
120
        {
121
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
122
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
123
        updateO = localObjectMinor<oMinor;
124
        updateE = localExtrasMinor<eMinor;
125

    
126
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
127
        }
128
      if( updateO || updateE )
129
        {
130
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
131
        if(oPercent>=100)
132
          {
133
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
134
          mCompleted.add(info);
135
          }
136
        else
137
          {
138
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
139
          mStarted.add(info);
140
          }
141
        }
142
      }
143
    }
144

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

    
147
  void parse(String updates)
148
    {
149
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
150

    
151
    mCompleted.clear();
152
    mStarted.clear();
153

    
154
    String[] lines = updates.split("\n");
155
    int numLines = lines.length;
156

    
157
    if( numLines>=1 )
158
      {
159
      mUrl = lines[0];
160
      if( !mUrl.endsWith("/") ) mUrl += "/";
161

    
162
      for(int line=1; line<numLines; line++)
163
        {
164
        String[] elements = lines[line].split(",");
165
        if( elements.length>=7 ) parseLine(elements);
166
        }
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public void updateDone(String shortName)
173
    {
174
    for( UpdateInfo info : mCompleted )
175
      {
176
      if( info.mObjectShortName.equals(shortName) )
177
        {
178
        mCompleted.remove(info);
179
        return;
180
        }
181
      }
182
    }
183

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

    
186
  public UpdateInfo getCompletedUpdate(int ordinal)
187
    {
188
    return mCompleted.get(ordinal);
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public UpdateInfo getStartedUpdate(int ordinal)
194
    {
195
    return mStarted.get(ordinal);
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public int getCompletedNumber()
201
    {
202
    return mCompleted.size();
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public int getStartedNumber()
208
    {
209
    return mStarted.size();
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public Bitmap getCompletedIcon(Context context, int ordinal)
215
    {
216
    UpdateInfo info = mCompleted.get(ordinal);
217
    Bitmap bmp = info.mIcon;
218
    if( bmp!=null ) return bmp;
219

    
220
    RubikFiles files = RubikFiles.getInstance();
221
    bmp = files.getIcon(context,info.mObjectShortName+".png");
222
    info.mIcon = bmp;
223
    return bmp;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public Bitmap getStartedIcon(Context context, int ordinal)
229
    {
230
    UpdateInfo info = mStarted.get(ordinal);
231
    Bitmap bmp = info.mIcon;
232
    if( bmp!=null ) return bmp;
233

    
234
    RubikFiles files = RubikFiles.getInstance();
235
    bmp = files.getIcon(context,info.mObjectShortName+".png");
236
    info.mIcon = bmp;
237
    return bmp;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public int getCompletedIconPresent(int ordinal)
243
    {
244
    try
245
      {
246
      return mCompleted.get(ordinal).mIconPresent;
247
      }
248
    catch(IndexOutOfBoundsException ie)
249
      {
250
      // ignore; the UpdateInfo must have been removed already
251
      // past a successful update; see updateDone()
252
      }
253
    return 0;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public int getStartedIconPresent(int ordinal)
259
    {
260
    return mStarted.get(ordinal).mIconPresent;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public String getCompletedURL(int ordinal)
266
    {
267
    UpdateInfo info = mCompleted.get(ordinal);
268
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public String getStartedURL(int ordinal)
274
    {
275
    UpdateInfo info = mStarted.get(ordinal);
276
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public String getURL()
282
    {
283
    return mUrl;
284
    }
285

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

    
288
  public void setCompletedIcon(int ordinal, Bitmap icon)
289
    {
290
    try
291
      {
292
      UpdateInfo info = mCompleted.get(ordinal);
293
      info.mIcon = icon;
294
      }
295
    catch(IndexOutOfBoundsException ie)
296
      {
297
      // ignore; the UpdateInfo must have been removed already
298
      // past a successful update; see updateDone()
299
      }
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public void setStartedIcon(int ordinal, Bitmap icon)
305
    {
306
    UpdateInfo info = mStarted.get(ordinal);
307
    info.mIcon = icon;
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  public void showDebug()
313
    {
314
    android.util.Log.e("D", "url: "+mUrl);
315
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
316
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
317
    }
318
}
(4-4/4)