1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.dialogs;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.InputStream;
|
15
|
import java.lang.ref.WeakReference;
|
16
|
|
17
|
import android.app.Activity;
|
18
|
import android.graphics.Bitmap;
|
19
|
import android.graphics.Color;
|
20
|
import android.util.TypedValue;
|
21
|
import android.view.View;
|
22
|
import android.widget.Button;
|
23
|
import android.widget.ImageView;
|
24
|
import android.widget.LinearLayout;
|
25
|
import android.widget.ProgressBar;
|
26
|
import android.widget.TextView;
|
27
|
|
28
|
import org.distorted.external.RubikFiles;
|
29
|
import org.distorted.main.R;
|
30
|
import org.distorted.external.RubikNetwork;
|
31
|
import org.distorted.external.RubikUpdates;
|
32
|
import org.distorted.objectlib.json.JsonReader;
|
33
|
import org.distorted.objects.RubikObjectList;
|
34
|
|
35
|
import static org.distorted.objects.RubikObjectList.SHOW_DOWNLOADED_DEBUG;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
|
40
|
{
|
41
|
private ImageView mIcon;
|
42
|
private RubikUpdates.UpdateInfo mInfo;
|
43
|
private ProgressBar mBar;
|
44
|
private Button mButton;
|
45
|
private TextView mDescription;
|
46
|
private WeakReference<Activity> mAct;
|
47
|
private boolean mIconSaved;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
|
52
|
LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
|
53
|
{
|
54
|
mIconSaved=false;
|
55
|
mAct = new WeakReference<>(act);
|
56
|
mInfo = info;
|
57
|
final RubikNetwork.Downloadee downloadee = this;
|
58
|
View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
|
59
|
TextView title = view.findViewById(R.id.updates_pane_title);
|
60
|
title.setText(info.mObjectLongName);
|
61
|
mDescription = view.findViewById(R.id.updates_pane_description);
|
62
|
mDescription.setText(info.mDescription);
|
63
|
|
64
|
mIcon = view.findViewById(R.id.updates_pane_image);
|
65
|
mIcon.setImageResource(R.drawable.unknown_icon);
|
66
|
|
67
|
view.setLayoutParams(pView);
|
68
|
|
69
|
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
70
|
mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
71
|
|
72
|
title.setLayoutParams(pText);
|
73
|
mDescription.setLayoutParams(pText);
|
74
|
|
75
|
view.setPadding(padding,padding,padding,padding);
|
76
|
|
77
|
mBar = view.findViewById(R.id.updates_pane_bar);
|
78
|
mButton = view.findViewById(R.id.updates_pane_button);
|
79
|
|
80
|
if( info.mPercent>=100 )
|
81
|
{
|
82
|
mBar.setVisibility(View.GONE);
|
83
|
mButton.setOnClickListener( new View.OnClickListener()
|
84
|
{
|
85
|
@Override
|
86
|
public void onClick(View v)
|
87
|
{
|
88
|
startDownload();
|
89
|
RubikNetwork network = RubikNetwork.getInstance();
|
90
|
network.downloadJSON(info,downloadee);
|
91
|
}
|
92
|
});
|
93
|
|
94
|
mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
95
|
mButton.setLayoutParams(pButt);
|
96
|
mBar.setLayoutParams(pButt);
|
97
|
}
|
98
|
else
|
99
|
{
|
100
|
mButton.setVisibility(View.GONE);
|
101
|
mBar.setLayoutParams(pButt);
|
102
|
mBar.setProgress(info.mPercent);
|
103
|
}
|
104
|
|
105
|
return view;
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
void setIcon(Bitmap icon, boolean downloaded)
|
111
|
{
|
112
|
mIcon.setImageBitmap(icon);
|
113
|
|
114
|
if( downloaded )
|
115
|
{
|
116
|
String name = mInfo.mObjectShortName + ".png";
|
117
|
Activity act = mAct.get();
|
118
|
RubikFiles files = RubikFiles.getInstance();
|
119
|
mIconSaved = files.saveIcon(act,mInfo.mIcon, name);
|
120
|
|
121
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving icon "+name+" to a file "+mIconSaved);
|
122
|
}
|
123
|
else
|
124
|
{
|
125
|
mIconSaved = true;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
private void startDownload()
|
132
|
{
|
133
|
mDescription.setText(R.string.downloading);
|
134
|
mBar.setProgress(20);
|
135
|
mButton.setVisibility(View.GONE);
|
136
|
mBar.setVisibility(View.VISIBLE);
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
|
141
|
private void displayError(int error)
|
142
|
{
|
143
|
Activity act = mAct.get();
|
144
|
|
145
|
act.runOnUiThread(new Runnable()
|
146
|
{
|
147
|
@Override
|
148
|
public void run()
|
149
|
{
|
150
|
mDescription.setTextColor(Color.parseColor("#ff0000"));
|
151
|
mDescription.setText(error);
|
152
|
}
|
153
|
});
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
private void makeProgress(int progress, int message)
|
159
|
{
|
160
|
Activity act = mAct.get();
|
161
|
|
162
|
act.runOnUiThread(new Runnable()
|
163
|
{
|
164
|
@Override
|
165
|
public void run()
|
166
|
{
|
167
|
mBar.setProgress(progress);
|
168
|
mDescription.setText(message);
|
169
|
}
|
170
|
});
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public void jsonDownloaded()
|
176
|
{
|
177
|
if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
|
178
|
{
|
179
|
displayError(R.string.networkError);
|
180
|
}
|
181
|
else
|
182
|
{
|
183
|
makeProgress(50,R.string.installing);
|
184
|
RubikFiles files = RubikFiles.getInstance();
|
185
|
boolean oSuccess, eSuccess=false;
|
186
|
|
187
|
if( mInfo.mObjectStream!=null )
|
188
|
{
|
189
|
String objectName = mInfo.mObjectShortName + "_object.json";
|
190
|
Activity act = mAct.get();
|
191
|
oSuccess = files.saveFile(act,mInfo.mObjectStream, objectName);
|
192
|
|
193
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving JSON "+objectName+" to a file "+oSuccess);
|
194
|
|
195
|
try
|
196
|
{
|
197
|
File file = new File(act.getFilesDir(), objectName);
|
198
|
InputStream stream = new FileInputStream(file);
|
199
|
JsonReader reader = new JsonReader();
|
200
|
reader.parseJsonFileMetadata(stream);
|
201
|
stream.close();
|
202
|
|
203
|
mInfo.mNumScrambles = reader.getNumScrambles();
|
204
|
mInfo.mPrice = reader.getPrice();
|
205
|
float diff = reader.getDifficulty();
|
206
|
int category = reader.getCategory();
|
207
|
int year = reader.getYearOfInvention();
|
208
|
String author = reader.getAuthor();
|
209
|
boolean adjColors = reader.getAdjustableColors();
|
210
|
|
211
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles+" price="+mInfo.mPrice);
|
212
|
|
213
|
if( mInfo.mExtrasStream!=null )
|
214
|
{
|
215
|
String name = mInfo.mObjectShortName + "_extras.json";
|
216
|
eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
|
217
|
|
218
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
|
219
|
}
|
220
|
|
221
|
if( mIconSaved || oSuccess || eSuccess )
|
222
|
{
|
223
|
makeProgress(75,R.string.configuring);
|
224
|
|
225
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "1");
|
226
|
|
227
|
boolean success = RubikObjectList.addDownloadedObject(act, mInfo.mObjectShortName, mInfo.mNumScrambles, mInfo.mPrice, mInfo.mObjectMinorVersion,
|
228
|
mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess, diff, category, year, author,adjColors);
|
229
|
if( success )
|
230
|
{
|
231
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "2");
|
232
|
|
233
|
RubikNetwork network = RubikNetwork.getInstance();
|
234
|
network.updateDone(mInfo.mObjectShortName);
|
235
|
|
236
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "3");
|
237
|
|
238
|
makeProgress(100,R.string.success);
|
239
|
|
240
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "4");
|
241
|
}
|
242
|
else
|
243
|
{
|
244
|
displayError(R.string.saveError);
|
245
|
}
|
246
|
}
|
247
|
else
|
248
|
{
|
249
|
displayError(R.string.saveError);
|
250
|
}
|
251
|
}
|
252
|
catch(Exception ex)
|
253
|
{
|
254
|
displayError(R.string.saveError);
|
255
|
files.deleteJsonObject(act,mInfo.mObjectShortName);
|
256
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Error reading numScrambles");
|
257
|
}
|
258
|
}
|
259
|
}
|
260
|
}
|
261
|
}
|