Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.external;
11

    
12
import java.io.InputStream;
13
import java.util.ArrayList;
14
import java.util.Locale;
15

    
16
import android.content.Context;
17
import android.graphics.Bitmap;
18
import org.distorted.objects.RubikObjectList;
19

    
20
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
public class RubikUpdates
25
{
26
  public static class UpdateInfo
27
    {
28
    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
    public final int mPercent;
34
    public final int mIconPresent;
35
    public final boolean mUpdateObject;
36
    public final boolean mUpdateExtras;
37

    
38
    public int mNumScrambles;
39
    public boolean mIsFree;
40
    public Bitmap mIcon;
41
    public InputStream mObjectStream;
42
    public InputStream mExtrasStream;
43

    
44
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
45
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
46
      {
47
      mObjectShortName    = shortName;
48
      mObjectLongName     = longName;
49
      mDescription        = description;
50
      mObjectMinorVersion = objectMinor;
51
      mExtrasMinorVersion = extrasMinor;
52
      mPercent            = percent;
53
      mIconPresent        = iconPresent;
54
      mUpdateObject       = updateO;
55
      mUpdateExtras       = updateE;
56

    
57
      mIcon = null;
58
      mNumScrambles = 0;
59
      }
60
    }
61

    
62
  private String mUrl;
63
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public RubikUpdates()
68
    {
69
    mCompleted = new ArrayList<>();
70
    mStarted   = new ArrayList<>();
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  private String debug(ArrayList<UpdateInfo> list)
76
    {
77
    String ret = "";
78

    
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
    return ret;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  private void parseLine(String[] elements)
91
    {
92
    String shortName   = elements[0].trim();
93
    String objMinor    = elements[1].trim();
94
    String extMinor    = elements[2].trim();
95
    String percent     = elements[3].trim();
96
    String iconPresent = elements[4].trim();
97
    String longName    = elements[5];
98
    String description = elements[6];
99
    int oMinor, eMinor, oPercent, oIcon;
100

    
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
    try { oPercent = Integer.parseInt(percent); }
106
    catch (NumberFormatException ex) { oPercent = -1; }
107
    try { oIcon = Integer.parseInt(iconPresent); }
108
    catch (NumberFormatException ex) { oIcon = 0; }
109

    
110
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
111
      {
112
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
113
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
114
      boolean updateO=true, updateE=true;
115

    
116
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
117

    
118
      if( objOrdinal>=0 )
119
        {
120
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
121
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
122
        updateO = localObjectMinor<oMinor;
123
        updateE = localExtrasMinor<eMinor;
124

    
125
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
126
        }
127
      if( updateO || updateE )
128
        {
129
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
130
        if(oPercent>=100)
131
          {
132
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
133
          mCompleted.add(info);
134
          }
135
        else
136
          {
137
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
138
          mStarted.add(info);
139
          }
140
        }
141
      }
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  void parse(String updates)
147
    {
148
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
149

    
150
    mCompleted.clear();
151
    mStarted.clear();
152

    
153
    String[] lines = updates.split("\n");
154
    int numLines = lines.length;
155

    
156
    if( numLines>=1 )
157
      {
158
      mUrl = lines[0];
159
      if( !mUrl.endsWith("/") ) mUrl += "/";
160

    
161
      for(int line=1; line<numLines; line++)
162
        {
163
        String[] elements = lines[line].split(",");
164
        if( elements.length>=7 ) parseLine(elements);
165
        }
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public void updateDone(String shortName)
172
    {
173
    for( UpdateInfo info : mCompleted )
174
      {
175
      if( info.mObjectShortName.equals(shortName) )
176
        {
177
        mCompleted.remove(info);
178
        return;
179
        }
180
      }
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  public UpdateInfo getCompletedUpdate(int ordinal)
186
    {
187
    return mCompleted.get(ordinal);
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public UpdateInfo getStartedUpdate(int ordinal)
193
    {
194
    return mStarted.get(ordinal);
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public int getCompletedNumber()
200
    {
201
    return mCompleted.size();
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public int getStartedNumber()
207
    {
208
    return mStarted.size();
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public Bitmap getCompletedIcon(Context context, int ordinal)
214
    {
215
    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
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public Bitmap getStartedIcon(Context context, int ordinal)
228
    {
229
    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
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public int getCompletedIconPresent(int ordinal)
242
    {
243
    try
244
      {
245
      return mCompleted.get(ordinal).mIconPresent;
246
      }
247
    catch(IndexOutOfBoundsException ie)
248
      {
249
      // ignore; the UpdateInfo must have been removed already
250
      // past a successful update; see updateDone()
251
      }
252
    return 0;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public int getStartedIconPresent(int ordinal)
258
    {
259
    return mStarted.get(ordinal).mIconPresent;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public String getURL()
281
    {
282
    return mUrl;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void setCompletedIcon(int ordinal, Bitmap icon)
288
    {
289
    try
290
      {
291
      UpdateInfo info = mCompleted.get(ordinal);
292
      info.mIcon = icon;
293
      }
294
    catch(IndexOutOfBoundsException ie)
295
      {
296
      // ignore; the UpdateInfo must have been removed already
297
      // past a successful update; see updateDone()
298
      }
299
    }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public void showDebug()
312
    {
313
    android.util.Log.e("D", "url: "+mUrl);
314
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
315
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
316
    }
317
}
(4-4/4)