Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 83018ac4

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 b88cdd91 Leszek Koltunski
    public Bitmap mIcon;
40 46be3ddf Leszek Koltunski
    public InputStream mObjectStream;
41
    public InputStream mExtrasStream;
42 c99db493 Leszek Koltunski
43 2c9ab085 Leszek Koltunski
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
44 b92ad5cd Leszek Koltunski
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
45 fcf7320f Leszek Koltunski
      {
46 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
47
      mObjectLongName     = longName;
48
      mDescription        = description;
49
      mObjectMinorVersion = objectMinor;
50
      mExtrasMinorVersion = extrasMinor;
51 2c9ab085 Leszek Koltunski
      mPercent            = percent;
52 b92ad5cd Leszek Koltunski
      mIconPresent        = iconPresent;
53 c99db493 Leszek Koltunski
      mUpdateObject       = updateO;
54
      mUpdateExtras       = updateE;
55 b88cdd91 Leszek Koltunski
56
      mIcon = null;
57 84d746d7 Leszek Koltunski
      mNumScrambles = 0;
58 fcf7320f Leszek Koltunski
      }
59
    }
60
61
  private String mUrl;
62 2c9ab085 Leszek Koltunski
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
63 fcf7320f Leszek Koltunski
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
  public RubikUpdates()
67
    {
68 2c9ab085 Leszek Koltunski
    mCompleted = new ArrayList<>();
69
    mStarted   = new ArrayList<>();
70 fcf7320f Leszek Koltunski
    }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
  private String debug(ArrayList<UpdateInfo> list)
75
    {
76
    String ret = "";
77 c99db493 Leszek Koltunski
78
    for( UpdateInfo info : list)
79
      {
80
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
81
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
82
      }
83
84 fcf7320f Leszek Koltunski
    return ret;
85
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  private void parseLine(String[] elements)
90
    {
91 2c9ab085 Leszek Koltunski
    String shortName   = elements[0].trim();
92
    String objMinor    = elements[1].trim();
93
    String extMinor    = elements[2].trim();
94
    String percent     = elements[3].trim();
95 b92ad5cd Leszek Koltunski
    String iconPresent = elements[4].trim();
96
    String longName    = elements[5];
97
    String description = elements[6];
98
    int oMinor, eMinor, oPercent, oIcon;
99 fcf7320f Leszek Koltunski
100
    try { oMinor = Integer.parseInt(objMinor); }
101
    catch (NumberFormatException ex) { oMinor = -1; }
102
    try { eMinor = Integer.parseInt(extMinor); }
103
    catch (NumberFormatException ex) { eMinor = -1; }
104 2c9ab085 Leszek Koltunski
    try { oPercent = Integer.parseInt(percent); }
105
    catch (NumberFormatException ex) { oPercent = -1; }
106 b92ad5cd Leszek Koltunski
    try { oIcon = Integer.parseInt(iconPresent); }
107
    catch (NumberFormatException ex) { oIcon = 0; }
108 fcf7320f Leszek Koltunski
109 2c9ab085 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
110 fcf7320f Leszek Koltunski
      {
111 5e048300 Leszek Koltunski
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
112
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
113 2c9ab085 Leszek Koltunski
      boolean updateO=true, updateE=true;
114 fcf7320f Leszek Koltunski
115 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
116 84d746d7 Leszek Koltunski
117 fcf7320f Leszek Koltunski
      if( objOrdinal>=0 )
118
        {
119 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
120 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
121 2c9ab085 Leszek Koltunski
        updateO = localObjectMinor<oMinor;
122
        updateE = localExtrasMinor<eMinor;
123 84d746d7 Leszek Koltunski
124 e847c553 Leszek Koltunski
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
125 fcf7320f Leszek Koltunski
        }
126 2c9ab085 Leszek Koltunski
      if( updateO || updateE )
127 fcf7320f Leszek Koltunski
        {
128 b92ad5cd Leszek Koltunski
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
129 84d746d7 Leszek Koltunski
        if(oPercent>=100)
130
          {
131 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
132 84d746d7 Leszek Koltunski
          mCompleted.add(info);
133
          }
134
        else
135
          {
136 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
137 84d746d7 Leszek Koltunski
          mStarted.add(info);
138
          }
139 fcf7320f Leszek Koltunski
        }
140
      }
141
    }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
  void parse(String updates)
146
    {
147 e847c553 Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
148 fcf7320f Leszek Koltunski
149 2c9ab085 Leszek Koltunski
    mCompleted.clear();
150
    mStarted.clear();
151 fcf7320f Leszek Koltunski
152
    String[] lines = updates.split("\n");
153
    int numLines = lines.length;
154
155
    if( numLines>=1 )
156
      {
157
      mUrl = lines[0];
158 b88cdd91 Leszek Koltunski
      if( !mUrl.endsWith("/") ) mUrl += "/";
159
160 fcf7320f Leszek Koltunski
      for(int line=1; line<numLines; line++)
161
        {
162 c99db493 Leszek Koltunski
        String[] elements = lines[line].split(",");
163 b92ad5cd Leszek Koltunski
        if( elements.length>=7 ) parseLine(elements);
164 fcf7320f Leszek Koltunski
        }
165
      }
166 63dd19c4 Leszek Koltunski
    }
167
168 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
  public void updateDone(String shortName)
171
    {
172 7fe62d1f Leszek Koltunski
    for( UpdateInfo info : mCompleted )
173 903c7bbc Leszek Koltunski
      {
174
      if( info.mObjectShortName.equals(shortName) )
175
        {
176 2c9ab085 Leszek Koltunski
        mCompleted.remove(info);
177 903c7bbc Leszek Koltunski
        return;
178
        }
179
      }
180
    }
181
182 10373dc7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 2c9ab085 Leszek Koltunski
  public UpdateInfo getCompletedUpdate(int ordinal)
185 10373dc7 Leszek Koltunski
    {
186 2c9ab085 Leszek Koltunski
    return mCompleted.get(ordinal);
187 9c39179e Leszek Koltunski
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191 2c9ab085 Leszek Koltunski
  public UpdateInfo getStartedUpdate(int ordinal)
192 9c39179e Leszek Koltunski
    {
193 2c9ab085 Leszek Koltunski
    return mStarted.get(ordinal);
194 9c39179e Leszek Koltunski
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 2c9ab085 Leszek Koltunski
  public int getCompletedNumber()
199 9c39179e Leszek Koltunski
    {
200 2c9ab085 Leszek Koltunski
    return mCompleted.size();
201 9c39179e Leszek Koltunski
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 2c9ab085 Leszek Koltunski
  public int getStartedNumber()
206 9c39179e Leszek Koltunski
    {
207 2c9ab085 Leszek Koltunski
    return mStarted.size();
208 10373dc7 Leszek Koltunski
    }
209
210 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 7fe62d1f Leszek Koltunski
  public Bitmap getCompletedIcon(Context context, int ordinal)
213 b88cdd91 Leszek Koltunski
    {
214 7fe62d1f Leszek Koltunski
    UpdateInfo info = mCompleted.get(ordinal);
215
    Bitmap bmp = info.mIcon;
216
    if( bmp!=null ) return bmp;
217
218
    RubikFiles files = RubikFiles.getInstance();
219
    bmp = files.getIcon(context,info.mObjectShortName+".png");
220
    info.mIcon = bmp;
221
    return bmp;
222 b88cdd91 Leszek Koltunski
    }
223
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226 7fe62d1f Leszek Koltunski
  public Bitmap getStartedIcon(Context context, int ordinal)
227 b88cdd91 Leszek Koltunski
    {
228 7fe62d1f Leszek Koltunski
    UpdateInfo info = mStarted.get(ordinal);
229
    Bitmap bmp = info.mIcon;
230
    if( bmp!=null ) return bmp;
231
232
    RubikFiles files = RubikFiles.getInstance();
233
    bmp = files.getIcon(context,info.mObjectShortName+".png");
234
    info.mIcon = bmp;
235
    return bmp;
236 b88cdd91 Leszek Koltunski
    }
237
238 b92ad5cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
  public int getCompletedIconPresent(int ordinal)
241
    {
242 7e950e32 Leszek Koltunski
    try
243
      {
244
      return mCompleted.get(ordinal).mIconPresent;
245
      }
246
    catch(IndexOutOfBoundsException ie)
247
      {
248 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
249 7e950e32 Leszek Koltunski
      // past a successful update; see updateDone()
250
      }
251
    return 0;
252 b92ad5cd Leszek Koltunski
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  public int getStartedIconPresent(int ordinal)
257
    {
258
    return mStarted.get(ordinal).mIconPresent;
259
    }
260
261 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  public String getCompletedURL(int ordinal)
264
    {
265
    UpdateInfo info = mCompleted.get(ordinal);
266
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
267
    }
268
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271
  public String getStartedURL(int ordinal)
272
    {
273
    UpdateInfo info = mStarted.get(ordinal);
274
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
275
    }
276
277 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279
  public String getURL()
280
    {
281
    return mUrl;
282
    }
283
284 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286
  public void setCompletedIcon(int ordinal, Bitmap icon)
287
    {
288 37b1e723 Leszek Koltunski
    try
289
      {
290
      UpdateInfo info = mCompleted.get(ordinal);
291
      info.mIcon = icon;
292
      }
293
    catch(IndexOutOfBoundsException ie)
294
      {
295 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
296 37b1e723 Leszek Koltunski
      // past a successful update; see updateDone()
297
      }
298 b88cdd91 Leszek Koltunski
    }
299
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
302
  public void setStartedIcon(int ordinal, Bitmap icon)
303
    {
304
    UpdateInfo info = mStarted.get(ordinal);
305
    info.mIcon = icon;
306
    }
307
308 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
309 fcf7320f Leszek Koltunski
310 63dd19c4 Leszek Koltunski
  public void showDebug()
311
    {
312 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
313 2c9ab085 Leszek Koltunski
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
314
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
315 fcf7320f Leszek Koltunski
    }
316
}