Project

General

Profile

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

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

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

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

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

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

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

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

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

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

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

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

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

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

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

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

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

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

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
157
    mCompleted.clear();
158
    mStarted.clear();
159

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

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

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

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

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

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

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

    
206
  public int getCompletedNumber()
207
    {
208
    return mCompleted.size();
209
    }
210

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

    
213
  public int getStartedNumber()
214
    {
215
    return mStarted.size();
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public Bitmap getCompletedIcon(Context context, int ordinal)
221
    {
222
    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
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public Bitmap getStartedIcon(Context context, int ordinal)
235
    {
236
    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
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public int getCompletedIconPresent(int ordinal)
249
    {
250
    return mCompleted.get(ordinal).mIconPresent;
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  public void setCompletedIcon(int ordinal, Bitmap icon)
286
    {
287
    UpdateInfo info = mCompleted.get(ordinal);
288
    info.mIcon = icon;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public void setStartedIcon(int ordinal, Bitmap icon)
294
    {
295
    UpdateInfo info = mStarted.get(ordinal);
296
    info.mIcon = icon;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public void showDebug()
302
    {
303
    android.util.Log.e("D", "url: "+mUrl);
304
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
305
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
306
    }
307
}
(4-4/4)