Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 5e048300

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.external;
21

    
22
import java.io.InputStream;
23
import java.util.ArrayList;
24
import java.util.Locale;
25

    
26
import android.content.Context;
27
import android.graphics.Bitmap;
28
import org.distorted.objects.RubikObjectList;
29

    
30
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class RubikUpdates
35
{
36
  public static class UpdateInfo
37
    {
38
    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
    public final int mPercent;
44
    public final int mIconPresent;
45
    public final boolean mUpdateObject;
46
    public final boolean mUpdateExtras;
47

    
48
    public int mNumScrambles;
49
    public Bitmap mIcon;
50
    public InputStream mObjectStream;
51
    public InputStream mExtrasStream;
52

    
53
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
54
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
55
      {
56
      mObjectShortName    = shortName;
57
      mObjectLongName     = longName;
58
      mDescription        = description;
59
      mObjectMinorVersion = objectMinor;
60
      mExtrasMinorVersion = extrasMinor;
61
      mPercent            = percent;
62
      mIconPresent        = iconPresent;
63
      mUpdateObject       = updateO;
64
      mUpdateExtras       = updateE;
65

    
66
      mIcon = null;
67
      mNumScrambles = 0;
68
      }
69
    }
70

    
71
  private String mUrl;
72
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public RubikUpdates()
77
    {
78
    mCompleted = new ArrayList<>();
79
    mStarted   = new ArrayList<>();
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  private String debug(ArrayList<UpdateInfo> list)
85
    {
86
    String ret = "";
87

    
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
    return ret;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private void parseLine(String[] elements)
100
    {
101
    String shortName   = elements[0].trim();
102
    String objMinor    = elements[1].trim();
103
    String extMinor    = elements[2].trim();
104
    String percent     = elements[3].trim();
105
    String iconPresent = elements[4].trim();
106
    String longName    = elements[5];
107
    String description = elements[6];
108
    int oMinor, eMinor, oPercent, oIcon;
109

    
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
    try { oPercent = Integer.parseInt(percent); }
115
    catch (NumberFormatException ex) { oPercent = -1; }
116
    try { oIcon = Integer.parseInt(iconPresent); }
117
    catch (NumberFormatException ex) { oIcon = 0; }
118

    
119
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
120
      {
121
      String upperName = shortName.toUpperCase(Locale.ENGLISH);
122
      int objOrdinal = RubikObjectList.getOrdinal(upperName);
123
      boolean updateO=true, updateE=true;
124

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

    
127
      if( objOrdinal>=0 )
128
        {
129
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
130
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
131
        updateO = localObjectMinor<oMinor;
132
        updateE = localExtrasMinor<eMinor;
133

    
134
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
135
        }
136
      if( updateO || updateE )
137
        {
138
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
139
        if(oPercent>=100)
140
          {
141
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to completed");
142
          mCompleted.add(info);
143
          }
144
        else
145
          {
146
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "object added to started");
147
          mStarted.add(info);
148
          }
149
        }
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  void parse(String updates)
156
    {
157
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", updates);
158

    
159
    mCompleted.clear();
160
    mStarted.clear();
161

    
162
    String[] lines = updates.split("\n");
163
    int numLines = lines.length;
164

    
165
    if( numLines>=1 )
166
      {
167
      mUrl = lines[0];
168
      if( !mUrl.endsWith("/") ) mUrl += "/";
169

    
170
      for(int line=1; line<numLines; line++)
171
        {
172
        String[] elements = lines[line].split(",");
173
        if( elements.length>=7 ) parseLine(elements);
174
        }
175
      }
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public void updateDone(String shortName)
181
    {
182
    for( UpdateInfo info : mCompleted )
183
      {
184
      if( info.mObjectShortName.equals(shortName) )
185
        {
186
        mCompleted.remove(info);
187
        return;
188
        }
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public UpdateInfo getCompletedUpdate(int ordinal)
195
    {
196
    return mCompleted.get(ordinal);
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public UpdateInfo getStartedUpdate(int ordinal)
202
    {
203
    return mStarted.get(ordinal);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public int getCompletedNumber()
209
    {
210
    return mCompleted.size();
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public int getStartedNumber()
216
    {
217
    return mStarted.size();
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public Bitmap getCompletedIcon(Context context, int ordinal)
223
    {
224
    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
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public Bitmap getStartedIcon(Context context, int ordinal)
237
    {
238
    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
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public int getCompletedIconPresent(int ordinal)
251
    {
252
    try
253
      {
254
      return mCompleted.get(ordinal).mIconPresent;
255
      }
256
    catch(IndexOutOfBoundsException ie)
257
      {
258
      // ignore; the UpdateInfo must have been removed already
259
      // past a successful update; see updateDone()
260
      }
261
    return 0;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public int getStartedIconPresent(int ordinal)
267
    {
268
    return mStarted.get(ordinal).mIconPresent;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public String getURL()
290
    {
291
    return mUrl;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  public void setCompletedIcon(int ordinal, Bitmap icon)
297
    {
298
    try
299
      {
300
      UpdateInfo info = mCompleted.get(ordinal);
301
      info.mIcon = icon;
302
      }
303
    catch(IndexOutOfBoundsException ie)
304
      {
305
      // ignore; the UpdateInfo must have been removed already
306
      // past a successful update; see updateDone()
307
      }
308
    }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public void showDebug()
321
    {
322
    android.util.Log.e("D", "url: "+mUrl);
323
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
324
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
325
    }
326
}
(4-4/4)