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
|
try
|
251
|
{
|
252
|
return mCompleted.get(ordinal).mIconPresent;
|
253
|
}
|
254
|
catch(IndexOutOfBoundsException ie)
|
255
|
{
|
256
|
// ignore; the UpdateInfo must have been removed already
|
257
|
// past a successful update; see updateDone()
|
258
|
}
|
259
|
return 0;
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
public int getStartedIconPresent(int ordinal)
|
265
|
{
|
266
|
return mStarted.get(ordinal).mIconPresent;
|
267
|
}
|
268
|
|
269
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
270
|
|
271
|
public String getCompletedURL(int ordinal)
|
272
|
{
|
273
|
UpdateInfo info = mCompleted.get(ordinal);
|
274
|
return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
public String getStartedURL(int ordinal)
|
280
|
{
|
281
|
UpdateInfo info = mStarted.get(ordinal);
|
282
|
return info!=null ? mUrl + info.mObjectShortName + ".png" : null;
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
public String getURL()
|
288
|
{
|
289
|
return mUrl;
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
public void setCompletedIcon(int ordinal, Bitmap icon)
|
295
|
{
|
296
|
try
|
297
|
{
|
298
|
UpdateInfo info = mCompleted.get(ordinal);
|
299
|
info.mIcon = icon;
|
300
|
}
|
301
|
catch(IndexOutOfBoundsException ie)
|
302
|
{
|
303
|
// ignore; the UpdateInfo must have been removed already
|
304
|
// past a successful update; see updateDone()
|
305
|
}
|
306
|
}
|
307
|
|
308
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
309
|
|
310
|
public void setStartedIcon(int ordinal, Bitmap icon)
|
311
|
{
|
312
|
UpdateInfo info = mStarted.get(ordinal);
|
313
|
info.mIcon = icon;
|
314
|
}
|
315
|
|
316
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
317
|
|
318
|
public void showDebug()
|
319
|
{
|
320
|
android.util.Log.e("D", "url: "+mUrl);
|
321
|
android.util.Log.e("D", "ready objects: "+debug(mCompleted));
|
322
|
android.util.Log.e("D", "next objects: "+debug(mStarted));
|
323
|
}
|
324
|
}
|