Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 7fe62d1f

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 806329e3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
40
41 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 46be3ddf Leszek Koltunski
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
44 9c39179e Leszek Koltunski
  {
45 b88cdd91 Leszek Koltunski
  private ImageView mIcon;
46 46be3ddf Leszek Koltunski
  private RubikUpdates.UpdateInfo mInfo;
47
  private ProgressBar mBar;
48
  private Button mButton;
49
  private TextView mDescription;
50 806329e3 Leszek Koltunski
  private WeakReference<Activity> mAct;
51 7fe62d1f Leszek Koltunski
  private boolean mIconSaved;
52 806329e3 Leszek Koltunski
53 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 2c9ab085 Leszek Koltunski
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
56 06ba394a Leszek Koltunski
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
57 9c39179e Leszek Koltunski
    {
58 7fe62d1f Leszek Koltunski
    mIconSaved=false;
59 806329e3 Leszek Koltunski
    mAct = new WeakReference<>(act);
60 46be3ddf Leszek Koltunski
    mInfo = info;
61
    final RubikNetwork.Downloadee downloadee = this;
62 9fde123a Leszek Koltunski
    View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
63 2c9ab085 Leszek Koltunski
    TextView title = view.findViewById(R.id.updates_pane_title);
64 9c39179e Leszek Koltunski
    title.setText(info.mObjectLongName);
65 46be3ddf Leszek Koltunski
    mDescription = view.findViewById(R.id.updates_pane_description);
66
    mDescription.setText(info.mDescription);
67 9c39179e Leszek Koltunski
68 b88cdd91 Leszek Koltunski
    mIcon = view.findViewById(R.id.updates_pane_image);
69
    mIcon.setImageResource(R.drawable.unknown_icon);
70 7bee6064 Leszek Koltunski
71 2c9ab085 Leszek Koltunski
    view.setLayoutParams(pView);
72 c651024f Leszek Koltunski
73
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
74 46be3ddf Leszek Koltunski
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
75 c651024f Leszek Koltunski
76
    title.setLayoutParams(pText);
77 46be3ddf Leszek Koltunski
    mDescription.setLayoutParams(pText);
78 c651024f Leszek Koltunski
79 2c9ab085 Leszek Koltunski
    view.setPadding(padding,padding,padding,padding);
80
81 46be3ddf Leszek Koltunski
    mBar    = view.findViewById(R.id.updates_pane_bar);
82
    mButton = view.findViewById(R.id.updates_pane_button);
83 2c9ab085 Leszek Koltunski
84 9fde123a Leszek Koltunski
    if( info.mPercent>=100 )
85
      {
86 acabdd83 Leszek Koltunski
      mBar.setVisibility(View.GONE);
87 46be3ddf Leszek Koltunski
      mButton.setOnClickListener( new View.OnClickListener()
88 2c9ab085 Leszek Koltunski
        {
89
        @Override
90
        public void onClick(View v)
91
          {
92 46be3ddf Leszek Koltunski
          startDownload();
93
          RubikNetwork network = RubikNetwork.getInstance();
94
          network.downloadJSON(info,downloadee);
95 2c9ab085 Leszek Koltunski
          }
96
        });
97
98 46be3ddf Leszek Koltunski
      mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
99
      mButton.setLayoutParams(pButt);
100
      mBar.setLayoutParams(pButt);
101 2c9ab085 Leszek Koltunski
      }
102
    else
103
      {
104 acabdd83 Leszek Koltunski
      mButton.setVisibility(View.GONE);
105 46be3ddf Leszek Koltunski
      mBar.setLayoutParams(pButt);
106
      mBar.setProgress(info.mPercent);
107 2c9ab085 Leszek Koltunski
      }
108 99b8a069 Leszek Koltunski
109 2c9ab085 Leszek Koltunski
    return view;
110 9c39179e Leszek Koltunski
    }
111 b88cdd91 Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 7fe62d1f Leszek Koltunski
  void setIcon(Bitmap icon, boolean downloaded)
115 b88cdd91 Leszek Koltunski
    {
116
    mIcon.setImageBitmap(icon);
117 7fe62d1f Leszek Koltunski
118
    if( downloaded )
119
      {
120
      String name = mInfo.mObjectShortName + ".png";
121
      Activity act = mAct.get();
122
      RubikFiles files = RubikFiles.getInstance();
123
      mIconSaved = files.saveIcon(act,mInfo.mIcon, name);
124
      }
125 b88cdd91 Leszek Koltunski
    }
126 46be3ddf Leszek Koltunski
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
  private void startDownload()
130
    {
131
    mDescription.setText(R.string.downloading);
132
    mBar.setProgress(20);
133 acabdd83 Leszek Koltunski
    mButton.setVisibility(View.GONE);
134
    mBar.setVisibility(View.VISIBLE);
135 46be3ddf Leszek Koltunski
    }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
  public void jsonDownloaded()
140
    {
141
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
142
      {
143
      mDescription.setTextColor(Color.parseColor("#ff0000"));
144
      mDescription.setText(R.string.networkError);
145
      }
146
    else
147
      {
148
      mBar.setProgress(50);
149
      mDescription.setText(R.string.installing);
150 35161021 Leszek Koltunski
151
      RubikFiles files = RubikFiles.getInstance();
152 7fe62d1f Leszek Koltunski
      boolean oSuccess=true, eSuccess=true;
153 35161021 Leszek Koltunski
154
      if( mInfo.mObjectStream!=null )
155
        {
156
        String name = mInfo.mObjectShortName + "_object.json";
157 806329e3 Leszek Koltunski
        Activity act = mAct.get();
158
        oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
159 35161021 Leszek Koltunski
        }
160
161
      if( mInfo.mExtrasStream!=null )
162
        {
163
        String name = mInfo.mObjectShortName + "_extras.json";
164 806329e3 Leszek Koltunski
        Activity act = mAct.get();
165
        eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
166 35161021 Leszek Koltunski
        }
167
168 7fe62d1f Leszek Koltunski
      if( mIconSaved || oSuccess || eSuccess )
169 35161021 Leszek Koltunski
        {
170
        mBar.setProgress(75);
171
        mDescription.setText(R.string.configuring);
172 7fe62d1f Leszek Koltunski
        RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,mIconSaved,oSuccess,eSuccess);
173 806329e3 Leszek Koltunski
        mBar.setProgress(100);
174
        mDescription.setText(R.string.success);
175
176
        RubikNetwork network = RubikNetwork.getInstance();
177
        network.updateDone(mInfo.mObjectShortName);
178 35161021 Leszek Koltunski
        }
179
      else
180
        {
181
        mDescription.setTextColor(Color.parseColor("#ff0000"));
182
        mDescription.setText(R.string.saveError);
183
        }
184 46be3ddf Leszek Koltunski
      }
185
    }
186 9c39179e Leszek Koltunski
  }