Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 526a5906

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
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void startDownload()
137
    {
138
    mDescription.setText(R.string.downloading);
139
    mBar.setProgress(20);
140
    mButton.setVisibility(View.GONE);
141
    mBar.setVisibility(View.VISIBLE);
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  private void displayError(int error)
147
    {
148
    Activity act = mAct.get();
149

    
150
    act.runOnUiThread(new Runnable()
151
      {
152
      @Override
153
      public void run()
154
        {
155
        mDescription.setTextColor(Color.parseColor("#ff0000"));
156
        mDescription.setText(error);
157
        }
158
      });
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void makeProgress(int progress, int message)
164
    {
165
    Activity act = mAct.get();
166

    
167
    act.runOnUiThread(new Runnable()
168
      {
169
      @Override
170
      public void run()
171
        {
172
        mBar.setProgress(progress);
173
        mDescription.setText(message);
174
        }
175
      });
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public void jsonDownloaded()
181
    {
182
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
183
      {
184
      displayError(R.string.networkError);
185
      }
186
    else
187
      {
188
      makeProgress(50,R.string.installing);
189
      RubikFiles files = RubikFiles.getInstance();
190
      boolean oSuccess=true, eSuccess=true;
191

    
192
      if( mInfo.mObjectStream!=null )
193
        {
194
        String name = mInfo.mObjectShortName + "_object.json";
195
        Activity act = mAct.get();
196
        oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
197

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

    
200
        JsonReader reader = JsonReader.getInstance();
201
        mInfo.mNumScrambles = reader.readNumScrambles(act,name);
202

    
203
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
204
        }
205

    
206
      if( mInfo.mExtrasStream!=null )
207
        {
208
        String name = mInfo.mObjectShortName + "_extras.json";
209
        Activity act = mAct.get();
210
        eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
211

    
212
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
213
        }
214

    
215
      if( mIconSaved || oSuccess || eSuccess )
216
        {
217
        makeProgress(75,R.string.configuring);
218

    
219
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "1");
220

    
221
        RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,mInfo.mNumScrambles, mInfo.mObjectMinorVersion,
222
                                            mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
223

    
224
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "2");
225

    
226
        RubikNetwork network = RubikNetwork.getInstance();
227
        network.updateDone(mInfo.mObjectShortName);
228

    
229
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "3");
230

    
231
        RubikScreenPlay play = (RubikScreenPlay)ScreenList.PLAY.getScreenClass();
232
        play.recreatePopup();
233

    
234
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "4");
235

    
236
        makeProgress(100,R.string.success);
237

    
238
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "5");
239
        }
240
      else
241
        {
242
        displayError(R.string.saveError);
243
        }
244
      }
245
    }
246
  }
(20-20/21)