Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 806329e3

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
import android.app.Activity;
23 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
24 46be3ddf Leszek Koltunski
import android.graphics.Color;
25 c651024f Leszek Koltunski
import android.util.TypedValue;
26 9c39179e Leszek Koltunski
import android.view.View;
27
import android.widget.Button;
28 7bee6064 Leszek Koltunski
import android.widget.ImageView;
29 c651024f Leszek Koltunski
import android.widget.LinearLayout;
30 2c9ab085 Leszek Koltunski
import android.widget.ProgressBar;
31 9c39179e Leszek Koltunski
import android.widget.TextView;
32
33 35161021 Leszek Koltunski
import org.distorted.external.RubikFiles;
34 9c39179e Leszek Koltunski
import org.distorted.main.R;
35 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
36
import org.distorted.external.RubikUpdates;
37 806329e3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
38
39
import java.io.IOException;
40
import java.lang.ref.WeakReference;
41 9fde123a Leszek Koltunski
42 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 46be3ddf Leszek Koltunski
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
45 9c39179e Leszek Koltunski
  {
46 b88cdd91 Leszek Koltunski
  private ImageView mIcon;
47 46be3ddf Leszek Koltunski
  private RubikUpdates.UpdateInfo mInfo;
48
  private ProgressBar mBar;
49
  private Button mButton;
50
  private TextView mDescription;
51 9c39179e Leszek Koltunski
52 806329e3 Leszek Koltunski
  private WeakReference<Activity> mAct;
53
54 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 2c9ab085 Leszek Koltunski
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
57 06ba394a Leszek Koltunski
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
58 9c39179e Leszek Koltunski
    {
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
  void setIcon(Bitmap icon)
115
    {
116
    mIcon.setImageBitmap(icon);
117
    }
118 46be3ddf Leszek Koltunski
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
  private void startDownload()
122
    {
123
    mDescription.setText(R.string.downloading);
124
    mBar.setProgress(20);
125 acabdd83 Leszek Koltunski
    mButton.setVisibility(View.GONE);
126
    mBar.setVisibility(View.VISIBLE);
127 46be3ddf Leszek Koltunski
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131
  public void jsonDownloaded()
132
    {
133
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
134
      {
135
      mDescription.setTextColor(Color.parseColor("#ff0000"));
136
      mDescription.setText(R.string.networkError);
137
      }
138
    else
139
      {
140
      mBar.setProgress(50);
141
      mDescription.setText(R.string.installing);
142 35161021 Leszek Koltunski
143
      RubikFiles files = RubikFiles.getInstance();
144 806329e3 Leszek Koltunski
      boolean iSuccess=true,oSuccess=true, eSuccess=true;
145
146
      if( mInfo.mIcon!=null )
147
        {
148
        String name = mInfo.mObjectShortName + ".png";
149
        Activity act = mAct.get();
150
        iSuccess = files.saveFile(act,mInfo.mIcon, name);
151
        }
152 35161021 Leszek Koltunski
153
      if( mInfo.mObjectStream!=null )
154
        {
155
        String name = mInfo.mObjectShortName + "_object.json";
156 806329e3 Leszek Koltunski
        Activity act = mAct.get();
157
        oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
158 35161021 Leszek Koltunski
        }
159
160
      if( mInfo.mExtrasStream!=null )
161
        {
162
        String name = mInfo.mObjectShortName + "_extras.json";
163 806329e3 Leszek Koltunski
        Activity act = mAct.get();
164
        eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
165 35161021 Leszek Koltunski
        }
166
167 806329e3 Leszek Koltunski
      if( iSuccess && oSuccess )
168 35161021 Leszek Koltunski
        {
169
        mBar.setProgress(75);
170
        mDescription.setText(R.string.configuring);
171 806329e3 Leszek Koltunski
        RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,iSuccess,oSuccess,eSuccess);
172
        mBar.setProgress(100);
173
        mDescription.setText(R.string.success);
174
175
        RubikNetwork network = RubikNetwork.getInstance();
176
        network.updateDone(mInfo.mObjectShortName);
177 35161021 Leszek Koltunski
        }
178
      else
179
        {
180
        mDescription.setTextColor(Color.parseColor("#ff0000"));
181
        mDescription.setText(R.string.saveError);
182
        }
183 46be3ddf Leszek Koltunski
      }
184
    }
185 9c39179e Leszek Koltunski
  }