Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 7fe62d1f

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 b88cdd91 Leszek Koltunski
    public Bitmap mIcon;
45 46be3ddf Leszek Koltunski
    public InputStream mObjectStream;
46
    public InputStream mExtrasStream;
47 c99db493 Leszek Koltunski
48 2c9ab085 Leszek Koltunski
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
49 b92ad5cd Leszek Koltunski
                      int extrasMinor, int percent, int iconPresent, boolean updateO, boolean updateE)
50 fcf7320f Leszek Koltunski
      {
51 c99db493 Leszek Koltunski
      mObjectShortName    = shortName;
52
      mObjectLongName     = longName;
53
      mDescription        = description;
54
      mObjectMinorVersion = objectMinor;
55
      mExtrasMinorVersion = extrasMinor;
56 2c9ab085 Leszek Koltunski
      mPercent            = percent;
57 b92ad5cd Leszek Koltunski
      mIconPresent        = iconPresent;
58 c99db493 Leszek Koltunski
      mUpdateObject       = updateO;
59
      mUpdateExtras       = updateE;
60 b88cdd91 Leszek Koltunski
61
      mIcon = null;
62 fcf7320f Leszek Koltunski
      }
63
    }
64
65
  private String mUrl;
66 2c9ab085 Leszek Koltunski
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
67 fcf7320f Leszek Koltunski
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
  public RubikUpdates()
71
    {
72 2c9ab085 Leszek Koltunski
    mCompleted = new ArrayList<>();
73
    mStarted   = new ArrayList<>();
74 fcf7320f Leszek Koltunski
    }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78
  private String debug(ArrayList<UpdateInfo> list)
79
    {
80
    String ret = "";
81 c99db493 Leszek Koltunski
82
    for( UpdateInfo info : list)
83
      {
84
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
85
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
86
      }
87
88 fcf7320f Leszek Koltunski
    return ret;
89
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93
  private void parseLine(String[] elements)
94
    {
95 2c9ab085 Leszek Koltunski
    String shortName   = elements[0].trim();
96
    String objMinor    = elements[1].trim();
97
    String extMinor    = elements[2].trim();
98
    String percent     = elements[3].trim();
99 b92ad5cd Leszek Koltunski
    String iconPresent = elements[4].trim();
100
    String longName    = elements[5];
101
    String description = elements[6];
102
    int oMinor, eMinor, oPercent, oIcon;
103 fcf7320f Leszek Koltunski
104
    try { oMinor = Integer.parseInt(objMinor); }
105
    catch (NumberFormatException ex) { oMinor = -1; }
106
    try { eMinor = Integer.parseInt(extMinor); }
107
    catch (NumberFormatException ex) { eMinor = -1; }
108 2c9ab085 Leszek Koltunski
    try { oPercent = Integer.parseInt(percent); }
109
    catch (NumberFormatException ex) { oPercent = -1; }
110 b92ad5cd Leszek Koltunski
    try { oIcon = Integer.parseInt(iconPresent); }
111
    catch (NumberFormatException ex) { oIcon = 0; }
112 fcf7320f Leszek Koltunski
113 2c9ab085 Leszek Koltunski
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
114 fcf7320f Leszek Koltunski
      {
115 c99db493 Leszek Koltunski
      int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
116 2c9ab085 Leszek Koltunski
      boolean updateO=true, updateE=true;
117 fcf7320f Leszek Koltunski
118
      if( objOrdinal>=0 )
119
        {
120 c99db493 Leszek Koltunski
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
121 fcf7320f Leszek Koltunski
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
122 2c9ab085 Leszek Koltunski
        updateO = localObjectMinor<oMinor;
123
        updateE = localExtrasMinor<eMinor;
124 fcf7320f Leszek Koltunski
        }
125 2c9ab085 Leszek Koltunski
      if( updateO || updateE )
126 fcf7320f Leszek Koltunski
        {
127 b92ad5cd Leszek Koltunski
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
128 2c9ab085 Leszek Koltunski
        if(oPercent>=100) mCompleted.add(info);
129
        else              mStarted.add(info);
130 fcf7320f Leszek Koltunski
        }
131
      }
132
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  void parse(String updates)
137
    {
138
    android.util.Log.e("D", updates);
139
140 2c9ab085 Leszek Koltunski
    mCompleted.clear();
141
    mStarted.clear();
142 fcf7320f Leszek Koltunski
143
    String[] lines = updates.split("\n");
144
    int numLines = lines.length;
145
146
    if( numLines>=1 )
147
      {
148
      mUrl = lines[0];
149 b88cdd91 Leszek Koltunski
      if( !mUrl.endsWith("/") ) mUrl += "/";
150
151 fcf7320f Leszek Koltunski
      for(int line=1; line<numLines; line++)
152
        {
153 c99db493 Leszek Koltunski
        String[] elements = lines[line].split(",");
154 b92ad5cd Leszek Koltunski
        if( elements.length>=7 ) parseLine(elements);
155 fcf7320f Leszek Koltunski
        }
156
      }
157 63dd19c4 Leszek Koltunski
    }
158
159 903c7bbc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  public void updateDone(String shortName)
162
    {
163 7fe62d1f Leszek Koltunski
    for( UpdateInfo info : mCompleted )
164 903c7bbc Leszek Koltunski
      {
165
      if( info.mObjectShortName.equals(shortName) )
166
        {
167 2c9ab085 Leszek Koltunski
        mCompleted.remove(info);
168 903c7bbc Leszek Koltunski
        return;
169
        }
170
      }
171
    }
172
173 10373dc7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175 2c9ab085 Leszek Koltunski
  public UpdateInfo getCompletedUpdate(int ordinal)
176 10373dc7 Leszek Koltunski
    {
177 2c9ab085 Leszek Koltunski
    return mCompleted.get(ordinal);
178 9c39179e Leszek Koltunski
    }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182 2c9ab085 Leszek Koltunski
  public UpdateInfo getStartedUpdate(int ordinal)
183 9c39179e Leszek Koltunski
    {
184 2c9ab085 Leszek Koltunski
    return mStarted.get(ordinal);
185 9c39179e Leszek Koltunski
    }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 2c9ab085 Leszek Koltunski
  public int getCompletedNumber()
190 9c39179e Leszek Koltunski
    {
191 2c9ab085 Leszek Koltunski
    return mCompleted.size();
192 9c39179e Leszek Koltunski
    }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 2c9ab085 Leszek Koltunski
  public int getStartedNumber()
197 9c39179e Leszek Koltunski
    {
198 2c9ab085 Leszek Koltunski
    return mStarted.size();
199 10373dc7 Leszek Koltunski
    }
200
201 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203 7fe62d1f Leszek Koltunski
  public Bitmap getCompletedIcon(Context context, int ordinal)
204 b88cdd91 Leszek Koltunski
    {
205 7fe62d1f Leszek Koltunski
    UpdateInfo info = mCompleted.get(ordinal);
206
    Bitmap bmp = info.mIcon;
207
    if( bmp!=null ) return bmp;
208
209
    RubikFiles files = RubikFiles.getInstance();
210
    bmp = files.getIcon(context,info.mObjectShortName+".png");
211
    info.mIcon = bmp;
212
    return bmp;
213 b88cdd91 Leszek Koltunski
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217 7fe62d1f Leszek Koltunski
  public Bitmap getStartedIcon(Context context, int ordinal)
218 b88cdd91 Leszek Koltunski
    {
219 7fe62d1f Leszek Koltunski
    UpdateInfo info = mStarted.get(ordinal);
220
    Bitmap bmp = info.mIcon;
221
    if( bmp!=null ) return bmp;
222
223
    RubikFiles files = RubikFiles.getInstance();
224
    bmp = files.getIcon(context,info.mObjectShortName+".png");
225
    info.mIcon = bmp;
226
    return bmp;
227 b88cdd91 Leszek Koltunski
    }
228
229 b92ad5cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  public int getCompletedIconPresent(int ordinal)
232
    {
233
    return mCompleted.get(ordinal).mIconPresent;
234
    }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
  public int getStartedIconPresent(int ordinal)
239
    {
240
    return mStarted.get(ordinal).mIconPresent;
241
    }
242
243 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
  public String getCompletedURL(int ordinal)
246
    {
247
    UpdateInfo info = mCompleted.get(ordinal);
248
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
249
    }
250
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
  public String getStartedURL(int ordinal)
254
    {
255
    UpdateInfo info = mStarted.get(ordinal);
256
    return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
257
    }
258
259 46be3ddf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
  public String getURL()
262
    {
263
    return mUrl;
264
    }
265
266 b88cdd91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268
  public void setCompletedIcon(int ordinal, Bitmap icon)
269
    {
270
    UpdateInfo info = mCompleted.get(ordinal);
271
    info.mIcon = icon;
272
    }
273
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
  public void setStartedIcon(int ordinal, Bitmap icon)
277
    {
278
    UpdateInfo info = mStarted.get(ordinal);
279
    info.mIcon = icon;
280
    }
281
282 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
283 fcf7320f Leszek Koltunski
284 63dd19c4 Leszek Koltunski
  public void showDebug()
285
    {
286 fcf7320f Leszek Koltunski
    android.util.Log.e("D", "url: "+mUrl);
287 2c9ab085 Leszek Koltunski
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
288
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
289 fcf7320f Leszek Koltunski
    }
290
}