Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 32fbd026

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