Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 298f3977

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.dialogs;
11

    
12
import java.lang.ref.WeakReference;
13

    
14
import android.app.Activity;
15
import android.graphics.Bitmap;
16
import android.graphics.Color;
17
import android.util.TypedValue;
18
import android.view.View;
19
import android.widget.Button;
20
import android.widget.ImageView;
21
import android.widget.LinearLayout;
22
import android.widget.ProgressBar;
23
import android.widget.TextView;
24

    
25
import org.distorted.external.RubikFiles;
26
import org.distorted.main.R;
27
import org.distorted.external.RubikNetwork;
28
import org.distorted.external.RubikUpdates;
29
import org.distorted.objectlib.json.JsonReader;
30
import org.distorted.objects.RubikObjectList;
31
import org.distorted.screens.RubikScreenPlay;
32
import org.distorted.screens.ScreenList;
33

    
34
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
39
  {
40
  private ImageView mIcon;
41
  private RubikUpdates.UpdateInfo mInfo;
42
  private ProgressBar mBar;
43
  private Button mButton;
44
  private TextView mDescription;
45
  private WeakReference<Activity> mAct;
46
  private boolean mIconSaved;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
51
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
52
    {
53
    mIconSaved=false;
54
    mAct = new WeakReference<>(act);
55
    mInfo = info;
56
    final RubikNetwork.Downloadee downloadee = this;
57
    View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
58
    TextView title = view.findViewById(R.id.updates_pane_title);
59
    title.setText(info.mObjectLongName);
60
    mDescription = view.findViewById(R.id.updates_pane_description);
61
    mDescription.setText(info.mDescription);
62

    
63
    mIcon = view.findViewById(R.id.updates_pane_image);
64
    mIcon.setImageResource(R.drawable.unknown_icon);
65

    
66
    view.setLayoutParams(pView);
67

    
68
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
69
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
70

    
71
    title.setLayoutParams(pText);
72
    mDescription.setLayoutParams(pText);
73

    
74
    view.setPadding(padding,padding,padding,padding);
75

    
76
    mBar    = view.findViewById(R.id.updates_pane_bar);
77
    mButton = view.findViewById(R.id.updates_pane_button);
78

    
79
    if( info.mPercent>=100 )
80
      {
81
      mBar.setVisibility(View.GONE);
82
      mButton.setOnClickListener( new View.OnClickListener()
83
        {
84
        @Override
85
        public void onClick(View v)
86
          {
87
          startDownload();
88
          RubikNetwork network = RubikNetwork.getInstance();
89
          network.downloadJSON(info,downloadee);
90
          }
91
        });
92

    
93
      mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
94
      mButton.setLayoutParams(pButt);
95
      mBar.setLayoutParams(pButt);
96
      }
97
    else
98
      {
99
      mButton.setVisibility(View.GONE);
100
      mBar.setLayoutParams(pButt);
101
      mBar.setProgress(info.mPercent);
102
      }
103

    
104
    return view;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  void setIcon(Bitmap icon, boolean downloaded)
110
    {
111
    mIcon.setImageBitmap(icon);
112

    
113
    if( downloaded )
114
      {
115
      String name = mInfo.mObjectShortName + ".png";
116
      Activity act = mAct.get();
117
      RubikFiles files = RubikFiles.getInstance();
118
      mIconSaved = files.saveIcon(act,mInfo.mIcon, name);
119

    
120
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving icon "+name+" to a file "+mIconSaved);
121
      }
122
    else
123
      {
124
      mIconSaved = true;
125
      }
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void startDownload()
131
    {
132
    mDescription.setText(R.string.downloading);
133
    mBar.setProgress(20);
134
    mButton.setVisibility(View.GONE);
135
    mBar.setVisibility(View.VISIBLE);
136
    }
137

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

    
140
  private void displayError(int error)
141
    {
142
    Activity act = mAct.get();
143

    
144
    act.runOnUiThread(new Runnable()
145
      {
146
      @Override
147
      public void run()
148
        {
149
        mDescription.setTextColor(Color.parseColor("#ff0000"));
150
        mDescription.setText(error);
151
        }
152
      });
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void makeProgress(int progress, int message)
158
    {
159
    Activity act = mAct.get();
160

    
161
    act.runOnUiThread(new Runnable()
162
      {
163
      @Override
164
      public void run()
165
        {
166
        mBar.setProgress(progress);
167
        mDescription.setText(message);
168
        }
169
      });
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public void jsonDownloaded()
175
    {
176
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
177
      {
178
      displayError(R.string.networkError);
179
      }
180
    else
181
      {
182
      makeProgress(50,R.string.installing);
183
      RubikFiles files = RubikFiles.getInstance();
184
      boolean oSuccess, eSuccess=false;
185

    
186
      if( mInfo.mObjectStream!=null )
187
        {
188
        String objectName = mInfo.mObjectShortName + "_object.json";
189
        Activity act = mAct.get();
190
        oSuccess = files.saveFile(act,mInfo.mObjectStream, objectName);
191

    
192
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving JSON "+objectName+" to a file "+oSuccess);
193

    
194
        try
195
          {
196
          JsonReader reader = JsonReader.getInstance();
197
          reader.readNumScramblesAndIsFree(act,objectName);
198
          mInfo.mNumScrambles = reader.getNumScrambles();
199
          mInfo.mIsFree       = reader.isFree();
200
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles+" isFree="+mInfo.mIsFree);
201

    
202
          if( mInfo.mExtrasStream!=null )
203
            {
204
            String name = mInfo.mObjectShortName + "_extras.json";
205
            eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
206

    
207
            if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Saving Extras "+name+" to a file "+eSuccess);
208
            }
209

    
210
          if( mIconSaved || oSuccess || eSuccess )
211
            {
212
            makeProgress(75,R.string.configuring);
213

    
214
            if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "1");
215

    
216
            boolean success = RubikObjectList.addDownloadedObject(act, mInfo.mObjectShortName, mInfo.mNumScrambles, mInfo.mIsFree,
217
                                                                  mInfo.mObjectMinorVersion, mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
218
            if( success )
219
              {
220
              if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "2");
221

    
222
              RubikNetwork network = RubikNetwork.getInstance();
223
              network.updateDone(mInfo.mObjectShortName);
224

    
225
              if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "3");
226

    
227
              RubikScreenPlay play = (RubikScreenPlay)ScreenList.PLAY.getScreenClass();
228
              play.recreatePopup();
229

    
230
              if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "4");
231

    
232
              makeProgress(100,R.string.success);
233

    
234
              if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "5");
235
              }
236
            else
237
              {
238
              displayError(R.string.saveError);
239
              }
240
            }
241
          else
242
            {
243
            displayError(R.string.saveError);
244
            }
245
          }
246
        catch(Exception ex)
247
          {
248
          displayError(R.string.saveError);
249
          files.deleteJsonObject(act,objectName);
250
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Error reading numScrambles");
251
          }
252
        }
253
      }
254
    }
255
  }
(22-22/23)