Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ c89c3b1b

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
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
49
  {
50
  private ImageView mIcon;
51
  private RubikUpdates.UpdateInfo mInfo;
52
  private ProgressBar mBar;
53
  private Button mButton;
54
  private TextView mDescription;
55
  private WeakReference<Activity> mAct;
56
  private boolean mIconSaved;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
61
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
62
    {
63
    mIconSaved=false;
64
    mAct = new WeakReference<>(act);
65
    mInfo = info;
66
    final RubikNetwork.Downloadee downloadee = this;
67
    View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
68
    TextView title = view.findViewById(R.id.updates_pane_title);
69
    title.setText(info.mObjectLongName);
70
    mDescription = view.findViewById(R.id.updates_pane_description);
71
    mDescription.setText(info.mDescription);
72

    
73
    mIcon = view.findViewById(R.id.updates_pane_image);
74
    mIcon.setImageResource(R.drawable.unknown_icon);
75

    
76
    view.setLayoutParams(pView);
77

    
78
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
79
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
80

    
81
    title.setLayoutParams(pText);
82
    mDescription.setLayoutParams(pText);
83

    
84
    view.setPadding(padding,padding,padding,padding);
85

    
86
    mBar    = view.findViewById(R.id.updates_pane_bar);
87
    mButton = view.findViewById(R.id.updates_pane_button);
88

    
89
    if( info.mPercent>=100 )
90
      {
91
      mBar.setVisibility(View.GONE);
92
      mButton.setOnClickListener( new View.OnClickListener()
93
        {
94
        @Override
95
        public void onClick(View v)
96
          {
97
          startDownload();
98
          RubikNetwork network = RubikNetwork.getInstance();
99
          network.downloadJSON(info,downloadee);
100
          }
101
        });
102

    
103
      mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
104
      mButton.setLayoutParams(pButt);
105
      mBar.setLayoutParams(pButt);
106
      }
107
    else
108
      {
109
      mButton.setVisibility(View.GONE);
110
      mBar.setLayoutParams(pButt);
111
      mBar.setProgress(info.mPercent);
112
      }
113

    
114
    return view;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  void setIcon(Bitmap icon, boolean downloaded)
120
    {
121
    mIcon.setImageBitmap(icon);
122

    
123
    if( downloaded )
124
      {
125
      String name = mInfo.mObjectShortName + ".png";
126
      Activity act = mAct.get();
127
      RubikFiles files = RubikFiles.getInstance();
128
      mIconSaved = files.saveIcon(act,mInfo.mIcon, name);
129

    
130
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving icon "+name+" to a file "+mIconSaved);
131
      }
132
    else
133
      {
134
      mIconSaved = true;
135
      }
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  private void startDownload()
141
    {
142
    mDescription.setText(R.string.downloading);
143
    mBar.setProgress(20);
144
    mButton.setVisibility(View.GONE);
145
    mBar.setVisibility(View.VISIBLE);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private void displayError(int error)
151
    {
152
    Activity act = mAct.get();
153

    
154
    act.runOnUiThread(new Runnable()
155
      {
156
      @Override
157
      public void run()
158
        {
159
        mDescription.setTextColor(Color.parseColor("#ff0000"));
160
        mDescription.setText(error);
161
        }
162
      });
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private void makeProgress(int progress, int message)
168
    {
169
    Activity act = mAct.get();
170

    
171
    act.runOnUiThread(new Runnable()
172
      {
173
      @Override
174
      public void run()
175
        {
176
        mBar.setProgress(progress);
177
        mDescription.setText(message);
178
        }
179
      });
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public void jsonDownloaded()
185
    {
186
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
187
      {
188
      displayError(R.string.networkError);
189
      }
190
    else
191
      {
192
      makeProgress(50,R.string.installing);
193
      RubikFiles files = RubikFiles.getInstance();
194
      boolean oSuccess=false, eSuccess=false;
195

    
196
      if( mInfo.mObjectStream!=null )
197
        {
198
        String name = mInfo.mObjectShortName + "_object.json";
199
        Activity act = mAct.get();
200
        oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
201

    
202
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving JSON "+name+" to a file "+oSuccess);
203

    
204
        JsonReader reader = JsonReader.getInstance();
205
        mInfo.mNumScrambles = reader.readNumScrambles(act,name);
206

    
207
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
208
        }
209

    
210
      if( mInfo.mExtrasStream!=null )
211
        {
212
        String name = mInfo.mObjectShortName + "_extras.json";
213
        Activity act = mAct.get();
214
        eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
215

    
216
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
217
        }
218

    
219
      if( mIconSaved || oSuccess || eSuccess )
220
        {
221
        makeProgress(75,R.string.configuring);
222

    
223
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "1");
224

    
225
        RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,mInfo.mNumScrambles, mInfo.mObjectMinorVersion,
226
                                            mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
227

    
228
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "2");
229

    
230
        RubikNetwork network = RubikNetwork.getInstance();
231
        network.updateDone(mInfo.mObjectShortName);
232

    
233
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "3");
234

    
235
        RubikScreenPlay play = (RubikScreenPlay)ScreenList.PLAY.getScreenClass();
236
        play.recreatePopup();
237

    
238
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "4");
239

    
240
        makeProgress(100,R.string.success);
241

    
242
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "5");
243
        }
244
      else
245
        {
246
        displayError(R.string.saveError);
247
        }
248
      }
249
    }
250
  }
(20-20/21)