Project

General

Profile

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

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

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
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

    
196
        android.util.Log.e("D", "Saving JSON "+name+" to a file "+oSuccess);
197

    
198
        JsonReader reader = JsonReader.getInstance();
199
        mInfo.mNumScrambles = reader.readNumScrambles(act,name);
200

    
201
        android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
202
        }
203

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

    
210
        android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
211
        }
212

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

    
217
        android.util.Log.e("D", "1");
218

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

    
222
        android.util.Log.e("D", "2");
223

    
224
        RubikNetwork network = RubikNetwork.getInstance();
225
        network.updateDone(mInfo.mObjectShortName);
226

    
227
        android.util.Log.e("D", "3");
228

    
229
        RubikScreenPlay play = (RubikScreenPlay)ScreenList.PLAY.getScreenClass();
230
        play.recreatePopup();
231

    
232
        android.util.Log.e("D", "4");
233

    
234
        makeProgress(100,R.string.success);
235

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