Project

General

Profile

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

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

1 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 1c327853 Leszek Koltunski
// 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 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 acabdd83 Leszek Koltunski
package org.distorted.external;
11 fcf7320f Leszek Koltunski
12 46be3ddf Leszek Koltunski
import java.io.InputStream;
13 b88cdd91 Leszek Koltunski
import java.util.ArrayList;
14 5e048300 Leszek Koltunski
import java.util.Locale;
15 7fe62d1f Leszek Koltunski
16
import android.content.Context;
17 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
18 fcf7320f Leszek Koltunski
import org.distorted.objects.RubikObjectList;
19
20 e847c553 Leszek Koltunski
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
21
22 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
23 fcf7320f Leszek Koltunski
24
public class RubikUpdates
25
{
26
  public static class UpdateInfo
27
    {
28 c99db493 Leszek Koltunski
    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 2c9ab085 Leszek Koltunski
    public final int mPercent;
34 b92ad5cd Leszek Koltunski
    public final int mIconPresent;
35 c99db493 Leszek Koltunski
    public final boolean mUpdateObject;
36
    public final boolean mUpdateExtras;
37 84d746d7 Leszek Koltunski
38
    public int mNumScrambles;
39 0b5e585c Leszek Koltunski
    public int mDifficulty;
40 b88cdd91 Leszek Koltunski
    public Bitmap mIcon;
41 46be3ddf Leszek Koltunski
    public InputStream mObjectStream;
42
    public InputStream mExtrasStream;
43 c99db493 Leszek Koltunski
44 2c9ab085 Leszek Koltunski
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
45 b92ad5cd Leszek Koltunski
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
46 fcf7320f Leszek Koltunski
      {
47 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
48
      mObjectLongName     = longName;
49
      mDescription        = description;
50
      mObjectMinorVersion = objectMinor;
51
      mExtrasMinorVersion = extrasMinor;
52 2c9ab085 Leszek Koltunski
      mPercent            = percent;
53 b92ad5cd Leszek Koltunski
      mIconPresent        = iconPresent;
54 c99db493 Leszek Koltunski
      mUpdateObject       = updateO;
55
      mUpdateExtras       = updateE;
56 b88cdd91 Leszek Koltunski
57
      mIcon = null;
58 84d746d7 Leszek Koltunski
      mNumScrambles = 0;
59 0b5e585c Leszek Koltunski
      mDifficulty   = 0;
60 fcf7320f Leszek Koltunski
      }
61
    }
62
63
  private String mUrl;
64 2c9ab085 Leszek Koltunski
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
65 fcf7320f Leszek Koltunski
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
  public RubikUpdates()
69
    {
70 2c9ab085 Leszek Koltunski
    mCompleted = new ArrayList<>();
71
    mStarted   = new ArrayList<>();
72 fcf7320f Leszek Koltunski
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
  private String debug(ArrayList<UpdateInfo> list)
77
    {
78
    String ret = "";
79 c99db493 Leszek Koltunski
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 fcf7320f Leszek Koltunski
    return ret;
87
    }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
  private void parseLine(String[] elements)
92
    {
93 2c9ab085 Leszek Koltunski
    String shortName   = elements[0].trim();
94
    String objMinor    = elements[1].trim();
95
    String extMinor    = elements[2].trim();
96
    String percent     = elements[3].trim();
97 b92ad5cd Leszek Koltunski
    String iconPresent = elements[4].trim();
98
    String longName    = elements[5];
99
    String description = elements[6];
100
    int oMinor, eMinor, oPercent, oIcon;
101 fcf7320f Leszek Koltunski
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 2c9ab085 Leszek Koltunski
    try { oPercent = Integer.parseInt(percent); }
107
    catch (NumberFormatException ex) { oPercent = -1; }
108 b92ad5cd Leszek Koltunski
    try { oIcon = Integer.parseInt(iconPresent); }
109
    catch (NumberFormatException ex) { oIcon = 0; }
110 fcf7320f Leszek Koltunski
111 2c9ab085 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
112 fcf7320f Leszek Koltunski
      {
113 5e048300 Leszek Koltunski
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
114
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
115 2c9ab085 Leszek Koltunski
      boolean updateO=true, updateE=true;
116 fcf7320f Leszek Koltunski
117 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
118 84d746d7 Leszek Koltunski
119 fcf7320f Leszek Koltunski
      if( objOrdinal>=0 )
120
        {
121 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
122 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
123 2c9ab085 Leszek Koltunski
        updateO = localObjectMinor<oMinor;
124
        updateE = localExtrasMinor<eMinor;
125 84d746d7 Leszek Koltunski
126 e847c553 Leszek Koltunski
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
127 fcf7320f Leszek Koltunski
        }
128 2c9ab085 Leszek Koltunski
      if( updateO || updateE )
129 fcf7320f Leszek Koltunski
        {
130 b92ad5cd Leszek Koltunski
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
131 84d746d7 Leszek Koltunski
        if(oPercent>=100)
132
          {
133 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
134 84d746d7 Leszek Koltunski
          mCompleted.add(info);
135
          }
136
        else
137
          {
138 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
139 84d746d7 Leszek Koltunski
          mStarted.add(info);
140
          }
141 fcf7320f Leszek Koltunski
        }
142
      }
143
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
  void parse(String updates)
148
    {
149 e847c553 Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
150 fcf7320f Leszek Koltunski
151 2c9ab085 Leszek Koltunski
    mCompleted.clear();
152
    mStarted.clear();
153 fcf7320f Leszek Koltunski
154
    String[] lines = updates.split("\n");
155
    int numLines = lines.length;
156
157
    if( numLines>=1 )
158
      {
159
      mUrl = lines[0];
160 b88cdd91 Leszek Koltunski
      if( !mUrl.endsWith("/") ) mUrl += "/";
161
162 fcf7320f Leszek Koltunski
      for(int line=1; line<numLines; line++)
163
        {
164 c99db493 Leszek Koltunski
        String[] elements = lines[line].split(",");
165 b92ad5cd Leszek Koltunski
        if( elements.length>=7 ) parseLine(elements);
166 fcf7320f Leszek Koltunski
        }
167
      }
168 63dd19c4 Leszek Koltunski
    }
169
170 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  public void updateDone(String shortName)
173
    {
174 7fe62d1f Leszek Koltunski
    for( UpdateInfo info : mCompleted )
175 903c7bbc Leszek Koltunski
      {
176
      if( info.mObjectShortName.equals(shortName) )
177
        {
178 2c9ab085 Leszek Koltunski
        mCompleted.remove(info);
179 903c7bbc Leszek Koltunski
        return;
180
        }
181
      }
182
    }
183
184 10373dc7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186 2c9ab085 Leszek Koltunski
  public UpdateInfo getCompletedUpdate(int ordinal)
187 10373dc7 Leszek Koltunski
    {
188 2c9ab085 Leszek Koltunski
    return mCompleted.get(ordinal);
189 9c39179e Leszek Koltunski
    }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 2c9ab085 Leszek Koltunski
  public UpdateInfo getStartedUpdate(int ordinal)
194 9c39179e Leszek Koltunski
    {
195 2c9ab085 Leszek Koltunski
    return mStarted.get(ordinal);
196 9c39179e Leszek Koltunski
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200 2c9ab085 Leszek Koltunski
  public int getCompletedNumber()
201 9c39179e Leszek Koltunski
    {
202 2c9ab085 Leszek Koltunski
    return mCompleted.size();
203 9c39179e Leszek Koltunski
    }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207 2c9ab085 Leszek Koltunski
  public int getStartedNumber()
208 9c39179e Leszek Koltunski
    {
209 2c9ab085 Leszek Koltunski
    return mStarted.size();
210 10373dc7 Leszek Koltunski
    }
211
212 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214 7fe62d1f Leszek Koltunski
  public Bitmap getCompletedIcon(Context context, int ordinal)
215 b88cdd91 Leszek Koltunski
    {
216 7fe62d1f Leszek Koltunski
    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 b88cdd91 Leszek Koltunski
    }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 7fe62d1f Leszek Koltunski
  public Bitmap getStartedIcon(Context context, int ordinal)
229 b88cdd91 Leszek Koltunski
    {
230 7fe62d1f Leszek Koltunski
    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 b88cdd91 Leszek Koltunski
    }
239
240 b92ad5cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242
  public int getCompletedIconPresent(int ordinal)
243
    {
244 7e950e32 Leszek Koltunski
    try
245
      {
246
      return mCompleted.get(ordinal).mIconPresent;
247
      }
248
    catch(IndexOutOfBoundsException ie)
249
      {
250 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
251 7e950e32 Leszek Koltunski
      // past a successful update; see updateDone()
252
      }
253
    return 0;
254 b92ad5cd Leszek Koltunski
    }
255
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258
  public int getStartedIconPresent(int ordinal)
259
    {
260
    return mStarted.get(ordinal).mIconPresent;
261
    }
262
263 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
  public String getURL()
282
    {
283
    return mUrl;
284
    }
285
286 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
  public void setCompletedIcon(int ordinal, Bitmap icon)
289
    {
290 37b1e723 Leszek Koltunski
    try
291
      {
292
      UpdateInfo info = mCompleted.get(ordinal);
293
      info.mIcon = icon;
294
      }
295
    catch(IndexOutOfBoundsException ie)
296
      {
297 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
298 37b1e723 Leszek Koltunski
      // past a successful update; see updateDone()
299
      }
300 b88cdd91 Leszek Koltunski
    }
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 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
311 fcf7320f Leszek Koltunski
312 63dd19c4 Leszek Koltunski
  public void showDebug()
313
    {
314 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
315 2c9ab085 Leszek Koltunski
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
316
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
317 fcf7320f Leszek Koltunski
    }
318
}