Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 1ba56d95

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 Bitmap mIcon;
40
    public InputStream mObjectStream;
41
    public InputStream mExtrasStream;
42

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

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

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
78
    for( UpdateInfo info : list)
79
      {
80
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
81
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
82
      }
83

    
84
    return ret;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

    
100
    try { oMinor = Integer.parseInt(objMinor); }
101
    catch (NumberFormatException ex) { oMinor = -1; }
102
    try { eMinor = Integer.parseInt(extMinor); }
103
    catch (NumberFormatException ex) { eMinor = -1; }
104
    try { oPercent = Integer.parseInt(percent); }
105
    catch (NumberFormatException ex) { oPercent = -1; }
106
    try { oIcon = Integer.parseInt(iconPresent); }
107
    catch (NumberFormatException ex) { oIcon = 0; }
108

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

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

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

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

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

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

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

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public Bitmap getCompletedIcon(Context context, int ordinal)
213
    {
214
    UpdateInfo info = mCompleted.get(ordinal);
215
    Bitmap bmp = info.mIcon;
216
    if( bmp!=null ) return bmp;
217

    
218
    RubikFiles files = RubikFiles.getInstance();
219
    bmp = files.getIcon(context,info.mObjectShortName+".png");
220
    info.mIcon = bmp;
221
    return bmp;
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  public Bitmap getStartedIcon(Context context, int ordinal)
227
    {
228
    UpdateInfo info = mStarted.get(ordinal);
229
    Bitmap bmp = info.mIcon;
230
    if( bmp!=null ) return bmp;
231

    
232
    RubikFiles files = RubikFiles.getInstance();
233
    bmp = files.getIcon(context,info.mObjectShortName+".png");
234
    info.mIcon = bmp;
235
    return bmp;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public String getCompletedURL(int ordinal)
264
    {
265
    UpdateInfo info = mCompleted.get(ordinal);
266
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  public String getStartedURL(int ordinal)
272
    {
273
    UpdateInfo info = mStarted.get(ordinal);
274
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

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

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  public void setStartedIcon(int ordinal, Bitmap icon)
303
    {
304
    UpdateInfo info = mStarted.get(ordinal);
305
    info.mIcon = icon;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

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