Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 109a2b68

1 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 acabdd83 Leszek Koltunski
package org.distorted.external;
21 fcf7320f Leszek Koltunski
22 46be3ddf Leszek Koltunski
import java.io.InputStream;
23 b88cdd91 Leszek Koltunski
import java.util.ArrayList;
24 5e048300 Leszek Koltunski
import java.util.Locale;
25 7fe62d1f Leszek Koltunski
26
import android.content.Context;
27 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
28 fcf7320f Leszek Koltunski
import org.distorted.objects.RubikObjectList;
29
30 e847c553 Leszek Koltunski
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
31
32 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
33 fcf7320f Leszek Koltunski
34
public class RubikUpdates
35
{
36
  public static class UpdateInfo
37
    {
38 c99db493 Leszek Koltunski
    public final String mObjectShortName;
39
    public final String mObjectLongName;
40
    public final String mDescription;
41
    public final int mObjectMinorVersion;
42
    public final int mExtrasMinorVersion;
43 2c9ab085 Leszek Koltunski
    public final int mPercent;
44 b92ad5cd Leszek Koltunski
    public final int mIconPresent;
45 c99db493 Leszek Koltunski
    public final boolean mUpdateObject;
46
    public final boolean mUpdateExtras;
47 84d746d7 Leszek Koltunski
48
    public int mNumScrambles;
49 b88cdd91 Leszek Koltunski
    public Bitmap mIcon;
50 46be3ddf Leszek Koltunski
    public InputStream mObjectStream;
51
    public InputStream mExtrasStream;
52 c99db493 Leszek Koltunski
53 2c9ab085 Leszek Koltunski
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
54 b92ad5cd Leszek Koltunski
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
55 fcf7320f Leszek Koltunski
      {
56 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
57
      mObjectLongName     = longName;
58
      mDescription        = description;
59
      mObjectMinorVersion = objectMinor;
60
      mExtrasMinorVersion = extrasMinor;
61 2c9ab085 Leszek Koltunski
      mPercent            = percent;
62 b92ad5cd Leszek Koltunski
      mIconPresent        = iconPresent;
63 c99db493 Leszek Koltunski
      mUpdateObject       = updateO;
64
      mUpdateExtras       = updateE;
65 b88cdd91 Leszek Koltunski
66
      mIcon = null;
67 84d746d7 Leszek Koltunski
      mNumScrambles = 0;
68 fcf7320f Leszek Koltunski
      }
69
    }
70
71
  private String mUrl;
72 2c9ab085 Leszek Koltunski
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
73 fcf7320f Leszek Koltunski
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
  public RubikUpdates()
77
    {
78 2c9ab085 Leszek Koltunski
    mCompleted = new ArrayList<>();
79
    mStarted   = new ArrayList<>();
80 fcf7320f Leszek Koltunski
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84
  private String debug(ArrayList<UpdateInfo> list)
85
    {
86
    String ret = "";
87 c99db493 Leszek Koltunski
88
    for( UpdateInfo info : list)
89
      {
90
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
91
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
92
      }
93
94 fcf7320f Leszek Koltunski
    return ret;
95
    }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99
  private void parseLine(String[] elements)
100
    {
101 2c9ab085 Leszek Koltunski
    String shortName   = elements[0].trim();
102
    String objMinor    = elements[1].trim();
103
    String extMinor    = elements[2].trim();
104
    String percent     = elements[3].trim();
105 b92ad5cd Leszek Koltunski
    String iconPresent = elements[4].trim();
106
    String longName    = elements[5];
107
    String description = elements[6];
108
    int oMinor, eMinor, oPercent, oIcon;
109 fcf7320f Leszek Koltunski
110
    try { oMinor = Integer.parseInt(objMinor); }
111
    catch (NumberFormatException ex) { oMinor = -1; }
112
    try { eMinor = Integer.parseInt(extMinor); }
113
    catch (NumberFormatException ex) { eMinor = -1; }
114 2c9ab085 Leszek Koltunski
    try { oPercent = Integer.parseInt(percent); }
115
    catch (NumberFormatException ex) { oPercent = -1; }
116 b92ad5cd Leszek Koltunski
    try { oIcon = Integer.parseInt(iconPresent); }
117
    catch (NumberFormatException ex) { oIcon = 0; }
118 fcf7320f Leszek Koltunski
119 2c9ab085 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
120 fcf7320f Leszek Koltunski
      {
121 5e048300 Leszek Koltunski
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
122
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
123 2c9ab085 Leszek Koltunski
      boolean updateO=true, updateE=true;
124 fcf7320f Leszek Koltunski
125 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
126 84d746d7 Leszek Koltunski
127 fcf7320f Leszek Koltunski
      if( objOrdinal>=0 )
128
        {
129 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
130 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
131 2c9ab085 Leszek Koltunski
        updateO = localObjectMinor<oMinor;
132
        updateE = localExtrasMinor<eMinor;
133 84d746d7 Leszek Koltunski
134 e847c553 Leszek Koltunski
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
135 fcf7320f Leszek Koltunski
        }
136 2c9ab085 Leszek Koltunski
      if( updateO || updateE )
137 fcf7320f Leszek Koltunski
        {
138 b92ad5cd Leszek Koltunski
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
139 84d746d7 Leszek Koltunski
        if(oPercent>=100)
140
          {
141 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
142 84d746d7 Leszek Koltunski
          mCompleted.add(info);
143
          }
144
        else
145
          {
146 e847c553 Leszek Koltunski
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
147 84d746d7 Leszek Koltunski
          mStarted.add(info);
148
          }
149 fcf7320f Leszek Koltunski
        }
150
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
  void parse(String updates)
156
    {
157 e847c553 Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
158 fcf7320f Leszek Koltunski
159 2c9ab085 Leszek Koltunski
    mCompleted.clear();
160
    mStarted.clear();
161 fcf7320f Leszek Koltunski
162
    String[] lines = updates.split("\n");
163
    int numLines = lines.length;
164
165
    if( numLines>=1 )
166
      {
167
      mUrl = lines[0];
168 b88cdd91 Leszek Koltunski
      if( !mUrl.endsWith("/") ) mUrl += "/";
169
170 fcf7320f Leszek Koltunski
      for(int line=1; line<numLines; line++)
171
        {
172 c99db493 Leszek Koltunski
        String[] elements = lines[line].split(",");
173 b92ad5cd Leszek Koltunski
        if( elements.length>=7 ) parseLine(elements);
174 fcf7320f Leszek Koltunski
        }
175
      }
176 63dd19c4 Leszek Koltunski
    }
177
178 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180
  public void updateDone(String shortName)
181
    {
182 7fe62d1f Leszek Koltunski
    for( UpdateInfo info : mCompleted )
183 903c7bbc Leszek Koltunski
      {
184
      if( info.mObjectShortName.equals(shortName) )
185
        {
186 2c9ab085 Leszek Koltunski
        mCompleted.remove(info);
187 903c7bbc Leszek Koltunski
        return;
188
        }
189
      }
190
    }
191
192 10373dc7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194 2c9ab085 Leszek Koltunski
  public UpdateInfo getCompletedUpdate(int ordinal)
195 10373dc7 Leszek Koltunski
    {
196 2c9ab085 Leszek Koltunski
    return mCompleted.get(ordinal);
197 9c39179e Leszek Koltunski
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 2c9ab085 Leszek Koltunski
  public UpdateInfo getStartedUpdate(int ordinal)
202 9c39179e Leszek Koltunski
    {
203 2c9ab085 Leszek Koltunski
    return mStarted.get(ordinal);
204 9c39179e Leszek Koltunski
    }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 2c9ab085 Leszek Koltunski
  public int getCompletedNumber()
209 9c39179e Leszek Koltunski
    {
210 2c9ab085 Leszek Koltunski
    return mCompleted.size();
211 9c39179e Leszek Koltunski
    }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215 2c9ab085 Leszek Koltunski
  public int getStartedNumber()
216 9c39179e Leszek Koltunski
    {
217 2c9ab085 Leszek Koltunski
    return mStarted.size();
218 10373dc7 Leszek Koltunski
    }
219
220 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222 7fe62d1f Leszek Koltunski
  public Bitmap getCompletedIcon(Context context, int ordinal)
223 b88cdd91 Leszek Koltunski
    {
224 7fe62d1f Leszek Koltunski
    UpdateInfo info = mCompleted.get(ordinal);
225
    Bitmap bmp = info.mIcon;
226
    if( bmp!=null ) return bmp;
227
228
    RubikFiles files = RubikFiles.getInstance();
229
    bmp = files.getIcon(context,info.mObjectShortName+".png");
230
    info.mIcon = bmp;
231
    return bmp;
232 b88cdd91 Leszek Koltunski
    }
233
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 7fe62d1f Leszek Koltunski
  public Bitmap getStartedIcon(Context context, int ordinal)
237 b88cdd91 Leszek Koltunski
    {
238 7fe62d1f Leszek Koltunski
    UpdateInfo info = mStarted.get(ordinal);
239
    Bitmap bmp = info.mIcon;
240
    if( bmp!=null ) return bmp;
241
242
    RubikFiles files = RubikFiles.getInstance();
243
    bmp = files.getIcon(context,info.mObjectShortName+".png");
244
    info.mIcon = bmp;
245
    return bmp;
246 b88cdd91 Leszek Koltunski
    }
247
248 b92ad5cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
  public int getCompletedIconPresent(int ordinal)
251
    {
252 7e950e32 Leszek Koltunski
    try
253
      {
254
      return mCompleted.get(ordinal).mIconPresent;
255
      }
256
    catch(IndexOutOfBoundsException ie)
257
      {
258 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
259 7e950e32 Leszek Koltunski
      // past a successful update; see updateDone()
260
      }
261
    return 0;
262 b92ad5cd Leszek Koltunski
    }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
  public int getStartedIconPresent(int ordinal)
267
    {
268
    return mStarted.get(ordinal).mIconPresent;
269
    }
270
271 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
  public String getCompletedURL(int ordinal)
274
    {
275
    UpdateInfo info = mCompleted.get(ordinal);
276
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
277
    }
278
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
  public String getStartedURL(int ordinal)
282
    {
283
    UpdateInfo info = mStarted.get(ordinal);
284
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
285
    }
286
287 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
  public String getURL()
290
    {
291
    return mUrl;
292
    }
293
294 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
  public void setCompletedIcon(int ordinal, Bitmap icon)
297
    {
298 37b1e723 Leszek Koltunski
    try
299
      {
300
      UpdateInfo info = mCompleted.get(ordinal);
301
      info.mIcon = icon;
302
      }
303
    catch(IndexOutOfBoundsException ie)
304
      {
305 10194caa Leszek Koltunski
      // ignore; the UpdateInfo must have been removed already
306 37b1e723 Leszek Koltunski
      // past a successful update; see updateDone()
307
      }
308 b88cdd91 Leszek Koltunski
    }
309
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312
  public void setStartedIcon(int ordinal, Bitmap icon)
313
    {
314
    UpdateInfo info = mStarted.get(ordinal);
315
    info.mIcon = icon;
316
    }
317
318 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
319 fcf7320f Leszek Koltunski
320 63dd19c4 Leszek Koltunski
  public void showDebug()
321
    {
322 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
323 2c9ab085 Leszek Koltunski
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
324
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
325 fcf7320f Leszek Koltunski
    }
326
}