Project

General

Profile

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

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

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 boolean mUpdateObject;
39
    public final boolean mUpdateExtras;
40
    public Bitmap mIcon;
41

    
42
    public UpdateInfo(String shortName, String longName, String description, int objectMinor,
43
                      int extrasMinor, int percent, boolean updateO, boolean updateE)
44
      {
45
      mObjectShortName    = shortName;
46
      mObjectLongName     = longName;
47
      mDescription        = description;
48
      mObjectMinorVersion = objectMinor;
49
      mExtrasMinorVersion = extrasMinor;
50
      mPercent            = percent;
51
      mUpdateObject       = updateO;
52
      mUpdateExtras       = updateE;
53

    
54
      mIcon = null;
55
      }
56
    }
57

    
58
  private String mUrl;
59
  private final ArrayList<UpdateInfo> mCompleted, mStarted;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public RubikUpdates()
64
    {
65
    mCompleted = new ArrayList<>();
66
    mStarted   = new ArrayList<>();
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  private String debug(ArrayList<UpdateInfo> list)
72
    {
73
    String ret = "";
74

    
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
    return ret;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private void parseLine(String[] elements)
87
    {
88
    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

    
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
    try { oPercent = Integer.parseInt(percent); }
101
    catch (NumberFormatException ex) { oPercent = -1; }
102

    
103
    if( oMinor>=0 && eMinor>=0 && oPercent>=0 )
104
      {
105
      int objOrdinal = RubikObjectList.getOrdinal(shortName.toUpperCase());
106
      boolean updateO=true, updateE=true;
107

    
108
      if( objOrdinal>=0 )
109
        {
110
        int localObjectMinor = RubikObjectList.getLocalObjectMinor(objOrdinal);
111
        int localExtrasMinor = RubikObjectList.getLocalExtrasMinor(objOrdinal);
112
        updateO = localObjectMinor<oMinor;
113
        updateE = localExtrasMinor<eMinor;
114
        }
115
      if( updateO || updateE )
116
        {
117
        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
        }
121
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  void parse(String updates)
127
    {
128
    android.util.Log.e("D", updates);
129

    
130
    mCompleted.clear();
131
    mStarted.clear();
132

    
133
    String[] lines = updates.split("\n");
134
    int numLines = lines.length;
135

    
136
    if( numLines>=1 )
137
      {
138
      mUrl = lines[0];
139
      if( !mUrl.endsWith("/") ) mUrl += "/";
140

    
141
      for(int line=1; line<numLines; line++)
142
        {
143
        String[] elements = lines[line].split(",");
144
        if( elements.length>=6 ) parseLine(elements);
145
        }
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public void updateDone(String shortName)
152
    {
153
    for( UpdateInfo info : mCompleted)
154
      {
155
      if( info.mObjectShortName.equals(shortName) )
156
        {
157
        mCompleted.remove(info);
158
        return;
159
        }
160
      }
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  public UpdateInfo getCompletedUpdate(int ordinal)
166
    {
167
    return mCompleted.get(ordinal);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public UpdateInfo getStartedUpdate(int ordinal)
173
    {
174
    return mStarted.get(ordinal);
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public int getCompletedNumber()
180
    {
181
    return mCompleted.size();
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public int getStartedNumber()
187
    {
188
    return mStarted.size();
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  public void showDebug()
240
    {
241
    android.util.Log.e("D", "url: "+mUrl);
242
    android.util.Log.e("D", "ready objects: "+debug(mCompleted));
243
    android.util.Log.e("D", "next  objects: "+debug(mStarted));
244
    }
245
}
(3-3/3)