Project

General

Profile

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

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

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