Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 84d746d7

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 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
30 fcf7320f Leszek Koltunski
31
public class RubikUpdates
32
{
33
  public static class UpdateInfo
34
    {
35 c99db493 Leszek Koltunski
    public final String mObjectShortName;
36
    public final String mObjectLongName;
37
    public final String mDescription;
38
    public final int mObjectMinorVersion;
39
    public final int mExtrasMinorVersion;
40 2c9ab085 Leszek Koltunski
    public final int mPercent;
41 b92ad5cd Leszek Koltunski
    public final int mIconPresent;
42 c99db493 Leszek Koltunski
    public final boolean mUpdateObject;
43
    public final boolean mUpdateExtras;
44 84d746d7 Leszek Koltunski
45
    public int mNumScrambles;
46 b88cdd91 Leszek Koltunski
    public Bitmap mIcon;
47 46be3ddf Leszek Koltunski
    public InputStream mObjectStream;
48
    public InputStream mExtrasStream;
49 c99db493 Leszek Koltunski
50 2c9ab085 Leszek Koltunski
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
51 b92ad5cd Leszek Koltunski
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
52 fcf7320f Leszek Koltunski
      {
53 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
54
      mObjectLongName     = longName;
55
      mDescription        = description;
56
      mObjectMinorVersion = objectMinor;
57
      mExtrasMinorVersion = extrasMinor;
58 2c9ab085 Leszek Koltunski
      mPercent            = percent;
59 b92ad5cd Leszek Koltunski
      mIconPresent        = iconPresent;
60 c99db493 Leszek Koltunski
      mUpdateObject       = updateO;
61
      mUpdateExtras       = updateE;
62 b88cdd91 Leszek Koltunski
63
      mIcon = null;
64 84d746d7 Leszek Koltunski
      mNumScrambles = 0;
65 fcf7320f Leszek Koltunski
      }
66
    }
67
68
  private String mUrl;
69 2c9ab085 Leszek Koltunski
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
70 fcf7320f Leszek Koltunski
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  public RubikUpdates()
74
    {
75 2c9ab085 Leszek Koltunski
    mCompleted = new ArrayList<>();
76
    mStarted   = new ArrayList<>();
77 fcf7320f Leszek Koltunski
    }
78
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81
  private String debug(ArrayList<UpdateInfo> list)
82
    {
83
    String ret = "";
84 c99db493 Leszek Koltunski
85
    for( UpdateInfo info : list)
86
      {
87
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
88
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
89
      }
90
91 fcf7320f Leszek Koltunski
    return ret;
92
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
  private void parseLine(String[] elements)
97
    {
98 2c9ab085 Leszek Koltunski
    String shortName   = elements[0].trim();
99
    String objMinor    = elements[1].trim();
100
    String extMinor    = elements[2].trim();
101
    String percent     = elements[3].trim();
102 b92ad5cd Leszek Koltunski
    String iconPresent = elements[4].trim();
103
    String longName    = elements[5];
104
    String description = elements[6];
105
    int oMinor, eMinor, oPercent, oIcon;
106 fcf7320f Leszek Koltunski
107
    try { oMinor = Integer.parseInt(objMinor); }
108
    catch (NumberFormatException ex) { oMinor = -1; }
109
    try { eMinor = Integer.parseInt(extMinor); }
110
    catch (NumberFormatException ex) { eMinor = -1; }
111 2c9ab085 Leszek Koltunski
    try { oPercent = Integer.parseInt(percent); }
112
    catch (NumberFormatException ex) { oPercent = -1; }
113 b92ad5cd Leszek Koltunski
    try { oIcon = Integer.parseInt(iconPresent); }
114
    catch (NumberFormatException ex) { oIcon = 0; }
115 fcf7320f Leszek Koltunski
116 2c9ab085 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
117 fcf7320f Leszek Koltunski
      {
118 c99db493 Leszek Koltunski
      int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
119 2c9ab085 Leszek Koltunski
      boolean updateO=true, updateE=true;
120 fcf7320f Leszek Koltunski
121 84d746d7 Leszek Koltunski
android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
122
123 fcf7320f Leszek Koltunski
      if( objOrdinal>=0 )
124
        {
125 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
126 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
127 2c9ab085 Leszek Koltunski
        updateO = localObjectMinor<oMinor;
128
        updateE = localExtrasMinor<eMinor;
129 84d746d7 Leszek Koltunski
130
131
android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
132
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
android.util.Log.e("D", "object added to completed");
140
141
          mCompleted.add(info);
142
          }
143
        else
144
          {
145
android.util.Log.e("D", "object added to started");
146
147
          mStarted.add(info);
148
          }
149 fcf7320f Leszek Koltunski
        }
150
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
  void parse(String updates)
156
    {
157
    android.util.Log.e("D", updates);
158
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
    return mCompleted.get(ordinal).mIconPresent;
253
    }
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
    UpdateInfo info = mCompleted.get(ordinal);
290
    info.mIcon = icon;
291
    }
292
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
295
  public void setStartedIcon(int ordinal, Bitmap icon)
296
    {
297
    UpdateInfo info = mStarted.get(ordinal);
298
    info.mIcon = icon;
299
    }
300
301 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302 fcf7320f Leszek Koltunski
303 63dd19c4 Leszek Koltunski
  public void showDebug()
304
    {
305 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
306 2c9ab085 Leszek Koltunski
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
307
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
308 fcf7320f Leszek Koltunski
    }
309
}