Project

General

Profile

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

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

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.objects.RubikObjectList;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
44
  {
45
  private ImageView mIcon;
46
  private RubikUpdates.UpdateInfo mInfo;
47
  private ProgressBar mBar;
48
  private Button mButton;
49
  private TextView mDescription;
50
  private WeakReference<Activity> mAct;
51
  private boolean mIconSaved;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
68
    mIcon = view.findViewById(R.id.updates_pane_image);
69
    mIcon.setImageResource(R.drawable.unknown_icon);
70

    
71
    view.setLayoutParams(pView);
72

    
73
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
74
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
75

    
76
    title.setLayoutParams(pText);
77
    mDescription.setLayoutParams(pText);
78

    
79
    view.setPadding(padding,padding,padding,padding);
80

    
81
    mBar    = view.findViewById(R.id.updates_pane_bar);
82
    mButton = view.findViewById(R.id.updates_pane_button);
83

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

    
98
      mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
99
      mButton.setLayoutParams(pButt);
100
      mBar.setLayoutParams(pButt);
101
      }
102
    else
103
      {
104
      mButton.setVisibility(View.GONE);
105
      mBar.setLayoutParams(pButt);
106
      mBar.setProgress(info.mPercent);
107
      }
108

    
109
    return view;
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  void setIcon(Bitmap icon, boolean downloaded)
115
    {
116
    mIcon.setImageBitmap(icon);
117

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  private void startDownload()
130
    {
131
    mDescription.setText(R.string.downloading);
132
    mBar.setProgress(20);
133
    mButton.setVisibility(View.GONE);
134
    mBar.setVisibility(View.VISIBLE);
135
    }
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

    
151
      RubikFiles files = RubikFiles.getInstance();
152
      boolean oSuccess=true, eSuccess=true;
153

    
154
      if( mInfo.mObjectStream!=null )
155
        {
156
        String name = mInfo.mObjectShortName + "_object.json";
157
        Activity act = mAct.get();
158
        oSuccess = files.saveFile(act,mInfo.mObjectStream, name);
159
        }
160

    
161
      if( mInfo.mExtrasStream!=null )
162
        {
163
        String name = mInfo.mObjectShortName + "_extras.json";
164
        Activity act = mAct.get();
165
        eSuccess = files.saveFile(act,mInfo.mExtrasStream, name);
166
        }
167

    
168
      if( mIconSaved || oSuccess || eSuccess )
169
        {
170
        mBar.setProgress(75);
171
        mDescription.setText(R.string.configuring);
172
        RubikObjectList.addDownloadedObject(mInfo.mObjectShortName,mIconSaved,oSuccess,eSuccess);
173
        mBar.setProgress(100);
174
        mDescription.setText(R.string.success);
175

    
176
        RubikNetwork network = RubikNetwork.getInstance();
177
        network.updateDone(mInfo.mObjectShortName);
178
        }
179
      else
180
        {
181
        mDescription.setTextColor(Color.parseColor("#ff0000"));
182
        mDescription.setText(R.string.saveError);
183
        }
184
      }
185
    }
186
  }
(20-20/21)