Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ e48ad1af

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