Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 298f3977

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