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.dialogs;
|
21
|
|
22
|
import java.lang.ref.WeakReference;
|
23
|
|
24
|
import android.app.Activity;
|
25
|
import android.graphics.Bitmap;
|
26
|
import android.graphics.Color;
|
27
|
import android.util.TypedValue;
|
28
|
import android.view.View;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.ImageView;
|
31
|
import android.widget.LinearLayout;
|
32
|
import android.widget.ProgressBar;
|
33
|
import android.widget.TextView;
|
34
|
|
35
|
import org.distorted.external.RubikFiles;
|
36
|
import org.distorted.main.R;
|
37
|
import org.distorted.external.RubikNetwork;
|
38
|
import org.distorted.external.RubikUpdates;
|
39
|
import org.distorted.objectlib.json.JsonReader;
|
40
|
import org.distorted.objects.RubikObjectList;
|
41
|
import org.distorted.screens.RubikScreenPlay;
|
42
|
import org.distorted.screens.ScreenList;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
|
47
|
{
|
48
|
private ImageView mIcon;
|
49
|
private RubikUpdates.UpdateInfo mInfo;
|
50
|
private ProgressBar mBar;
|
51
|
private Button mButton;
|
52
|
private TextView mDescription;
|
53
|
private WeakReference<Activity> mAct;
|
54
|
private boolean mIconSaved;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
|
59
|
LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
|
60
|
{
|
61
|
mIconSaved=false;
|
62
|
mAct = new WeakReference<>(act);
|
63
|
mInfo = info;
|
64
|
final RubikNetwork.Downloadee downloadee = this;
|
65
|
View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
|
66
|
TextView title = view.findViewById(R.id.updates_pane_title);
|
67
|
title.setText(info.mObjectLongName);
|
68
|
mDescription = view.findViewById(R.id.updates_pane_description);
|
69
|
mDescription.setText(info.mDescription);
|
70
|
|
71
|
mIcon = view.findViewById(R.id.updates_pane_image);
|
72
|
mIcon.setImageResource(R.drawable.unknown_icon);
|
73
|
|
74
|
view.setLayoutParams(pView);
|
75
|
|
76
|
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
77
|
mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
78
|
|
79
|
title.setLayoutParams(pText);
|
80
|
mDescription.setLayoutParams(pText);
|
81
|
|
82
|
view.setPadding(padding,padding,padding,padding);
|
83
|
|
84
|
mBar = view.findViewById(R.id.updates_pane_bar);
|
85
|
mButton = view.findViewById(R.id.updates_pane_button);
|
86
|
|
87
|
if( info.mPercent>=100 )
|
88
|
{
|
89
|
mBar.setVisibility(View.GONE);
|
90
|
mButton.setOnClickListener( new View.OnClickListener()
|
91
|
{
|
92
|
@Override
|
93
|
public void onClick(View v)
|
94
|
{
|
95
|
startDownload();
|
96
|
RubikNetwork network = RubikNetwork.getInstance();
|
97
|
network.downloadJSON(info,downloadee);
|
98
|
}
|
99
|
});
|
100
|
|
101
|
mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
|
102
|
mButton.setLayoutParams(pButt);
|
103
|
mBar.setLayoutParams(pButt);
|
104
|
}
|
105
|
else
|
106
|
{
|
107
|
mButton.setVisibility(View.GONE);
|
108
|
mBar.setLayoutParams(pButt);
|
109
|
mBar.setProgress(info.mPercent);
|
110
|
}
|
111
|
|
112
|
return view;
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
void setIcon(Bitmap icon, boolean downloaded)
|
118
|
{
|
119
|
mIcon.setImageBitmap(icon);
|
120
|
|
121
|
if( downloaded )
|
122
|
{
|
123
|
String name = mInfo.mObjectShortName + ".png";
|
124
|
Activity act = mAct.get();
|
125
|
RubikFiles files = RubikFiles.getInstance();
|
126
|
mIconSaved = files.saveIcon(act,mInfo.mIcon, name);
|
127
|
|
128
|
android.util.Log.e("D", "Saving icon "+name+" to a file "+mIconSaved);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
|
134
|
private void startDownload()
|
135
|
{
|
136
|
mDescription.setText(R.string.downloading);
|
137
|
mBar.setProgress(20);
|
138
|
mButton.setVisibility(View.GONE);
|
139
|
mBar.setVisibility(View.VISIBLE);
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public void jsonDownloaded()
|
145
|
{
|
146
|
if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
|
147
|
{
|
148
|
mDescription.setTextColor(Color.parseColor("#ff0000"));
|
149
|
mDescription.setText(R.string.networkError);
|
150
|
}
|
151
|
else
|
152
|
{
|
153
|
mBar.setProgress(50);
|
154
|
mDescription.setText(R.string.installing);
|
155
|
|
156
|
RubikFiles files = RubikFiles.getInstance();
|
157
|
boolean oSuccess=true, eSuccess=true;
|
158
|
|
159
|
if( mInfo.mObjectStream!=null )
|
160
|
{
|
161
|
String name = mInfo.mObjectShortName + "_object.json";
|
162
|
Activity act = mAct.get();
|
163
|
oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
|
164
|
|
165
|
android.util.Log.e("D", "Saving JSON "+name+" to a file "+oSuccess);
|
166
|
|
167
|
JsonReader reader = JsonReader.getInstance();
|
168
|
mInfo.mNumScrambles = reader.readNumScrambles(act,name);
|
169
|
|
170
|
android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
|
171
|
}
|
172
|
|
173
|
if( mInfo.mExtrasStream!=null )
|
174
|
{
|
175
|
String name = mInfo.mObjectShortName + "_extras.json";
|
176
|
Activity act = mAct.get();
|
177
|
eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
|
178
|
|
179
|
android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
|
180
|
}
|
181
|
|
182
|
if( mIconSaved || oSuccess || eSuccess )
|
183
|
{
|
184
|
mBar.setProgress(75);
|
185
|
mDescription.setText(R.string.configuring);
|
186
|
RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,mInfo.mNumScrambles, mInfo.mObjectMinorVersion,
|
187
|
mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
|
188
|
|
189
|
RubikNetwork network = RubikNetwork.getInstance();
|
190
|
network.updateDone(mInfo.mObjectShortName);
|
191
|
|
192
|
RubikScreenPlay play = (RubikScreenPlay)ScreenList.PLAY.getScreenClass();
|
193
|
play.recreatePopup();
|
194
|
|
195
|
mBar.setProgress(100);
|
196
|
mDescription.setText(R.string.success);
|
197
|
}
|
198
|
else
|
199
|
{
|
200
|
mDescription.setTextColor(Color.parseColor("#ff0000"));
|
201
|
mDescription.setText(R.string.saveError);
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
}
|