Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikUpdates.java @ 84d746d7

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
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikUpdates
32
{
33
  public static class UpdateInfo
34
    {
35
    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
    public final int mPercent;
41
    public final int mIconPresent;
42
    public final boolean mUpdateObject;
43
    public final boolean mUpdateExtras;
44

    
45
    public int mNumScrambles;
46
    public Bitmap mIcon;
47
    public InputStream mObjectStream;
48
    public InputStream mExtrasStream;
49

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

    
63
      mIcon = null;
64
      mNumScrambles = 0;
65
      }
66
    }
67

    
68
  private String mUrl;
69
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
85
    for( UpdateInfo info : list)
86
      {
87
      ret += (info.mObjectShortName+" "+info.mObjectLongName+" "+info.mDescription+" ");
88
      ret += (info.mObjectMinorVersion+" "+info.mExtrasMinorVersion+" "+info.mUpdateObject+" "+info.mUpdateExtras+" , ");
89
      }
90

    
91
    return ret;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

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

    
107
    try { oMinor = Integer.parseInt(objMinor); }
108
    catch (NumberFormatException ex) { oMinor = -1; }
109
    try { eMinor = Integer.parseInt(extMinor); }
110
    catch (NumberFormatException ex) { eMinor = -1; }
111
    try { oPercent = Integer.parseInt(percent); }
112
    catch (NumberFormatException ex) { oPercent = -1; }
113
    try { oIcon = Integer.parseInt(iconPresent); }
114
    catch (NumberFormatException ex) { oIcon = 0; }
115

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

    
121
android.util.Log.e("D", "downloaded object "+shortName+" oMinor="+oMinor+" eMinor="+eMinor);
122

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

    
130

    
131
android.util.Log.e("D", "object exists locally, localObjectMinor="+localObjectMinor+" localExtrasMinor="+localExtrasMinor);
132

    
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
android.util.Log.e("D", "object added to completed");
140

    
141
          mCompleted.add(info);
142
          }
143
        else
144
          {
145
android.util.Log.e("D", "object added to started");
146

    
147
          mStarted.add(info);
148
          }
149
        }
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  void parse(String updates)
156
    {
157
    android.util.Log.e("D", updates);
158

    
159
    mCompleted.clear();
160
    mStarted.clear();
161

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

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

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public int getCompletedNumber()
209
    {
210
    return mCompleted.size();
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public int getStartedNumber()
216
    {
217
    return mStarted.size();
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public Bitmap getCompletedIcon(Context context, int ordinal)
223
    {
224
    UpdateInfo info = mCompleted.get(ordinal);
225
    Bitmap bmp = info.mIcon;
226
    if( bmp!=null ) return bmp;
227

    
228
    RubikFiles files = RubikFiles.getInstance();
229
    bmp = files.getIcon(context,info.mObjectShortName+".png");
230
    info.mIcon = bmp;
231
    return bmp;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public Bitmap getStartedIcon(Context context, int ordinal)
237
    {
238
    UpdateInfo info = mStarted.get(ordinal);
239
    Bitmap bmp = info.mIcon;
240
    if( bmp!=null ) return bmp;
241

    
242
    RubikFiles files = RubikFiles.getInstance();
243
    bmp = files.getIcon(context,info.mObjectShortName+".png");
244
    info.mIcon = bmp;
245
    return bmp;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public String getURL()
281
    {
282
    return mUrl;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

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

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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