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