Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikUpdates.java @ b88cdd91

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