Project

General

Profile

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

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

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
    public Bitmap mIcon;
45
    public InputStream mObjectStream;
46
    public InputStream mExtrasStream;
47

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

    
61
      mIcon = null;
62
      }
63
    }
64

    
65
  private String mUrl;
66
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  public RubikUpdates()
71
    {
72
    mCompleted = new ArrayList<>();
73
    mStarted   = new ArrayList<>();
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  private String debug(ArrayList<UpdateInfo> list)
79
    {
80
    String ret = "";
81

    
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
    return ret;
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

    
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
    try { oPercent = Integer.parseInt(percent); }
109
    catch (NumberFormatException ex) { oPercent = -1; }
110
    try { oIcon = Integer.parseInt(iconPresent); }
111
    catch (NumberFormatException ex) { oIcon = 0; }
112

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

    
118
      if( objOrdinal>=0 )
119
        {
120
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
121
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
122
        updateO = localObjectMinor<oMinor;
123
        updateE = localExtrasMinor<eMinor;
124
        }
125
      if( updateO || updateE )
126
        {
127
        UpdateInfo info = new UpdateInfo(shortName,longName,description,oMinor,eMinor,oPercent,oIcon,updateO,updateE);
128
        if(oPercent>=100) mCompleted.add(info);
129
        else              mStarted.add(info);
130
        }
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  void parse(String updates)
137
    {
138
    android.util.Log.e("D", updates);
139

    
140
    mCompleted.clear();
141
    mStarted.clear();
142

    
143
    String[] lines = updates.split("\n");
144
    int numLines = lines.length;
145

    
146
    if( numLines>=1 )
147
      {
148
      mUrl = lines[0];
149
      if( !mUrl.endsWith("/") ) mUrl += "/";
150

    
151
      for(int line=1; line<numLines; line++)
152
        {
153
        String[] elements = lines[line].split(",");
154
        if( elements.length>=7 ) parseLine(elements);
155
        }
156
      }
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public void updateDone(String shortName)
162
    {
163
    for( UpdateInfo info : mCompleted )
164
      {
165
      if( info.mObjectShortName.equals(shortName) )
166
        {
167
        mCompleted.remove(info);
168
        return;
169
        }
170
      }
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public UpdateInfo getCompletedUpdate(int ordinal)
176
    {
177
    return mCompleted.get(ordinal);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public UpdateInfo getStartedUpdate(int ordinal)
183
    {
184
    return mStarted.get(ordinal);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public int getCompletedNumber()
190
    {
191
    return mCompleted.size();
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public int getStartedNumber()
197
    {
198
    return mStarted.size();
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public Bitmap getCompletedIcon(Context context, int ordinal)
204
    {
205
    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
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public Bitmap getStartedIcon(Context context, int ordinal)
218
    {
219
    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
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public String getURL()
262
    {
263
    return mUrl;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public void showDebug()
285
    {
286
    android.util.Log.e("D", "url: "+mUrl);
287
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
288
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
289
    }
290
}
(4-4/4)