Project

General

Profile

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

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

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
import android.graphics.Bitmap;
25
import org.distorted.objects.RubikObjectList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public class RubikUpdates
30
{
31
  public static class UpdateInfo
32
    {
33
    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
    public final int mPercent;
39
    public final int mIconPresent;
40
    public final boolean mUpdateObject;
41
    public final boolean mUpdateExtras;
42
    public Bitmap mIcon;
43
    public InputStream mObjectStream;
44
    public InputStream mExtrasStream;
45

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

    
59
      mIcon = null;
60
      }
61
    }
62

    
63
  private String mUrl;
64
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

    
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
    return ret;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

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

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

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

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

    
138
    mCompleted.clear();
139
    mStarted.clear();
140

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

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

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public int getCompletedNumber()
188
    {
189
    return mCompleted.size();
190
    }
191

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

    
194
  public int getStartedNumber()
195
    {
196
    return mStarted.size();
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  public String getURL()
246
    {
247
    return mUrl;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  public void showDebug()
269
    {
270
    android.util.Log.e("D", "url: "+mUrl);
271
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
272
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
273
    }
274
}
(3-3/3)