Project

General

Profile

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

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

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 android.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.Color;
25
import android.util.TypedValue;
26
import android.view.View;
27
import android.widget.Button;
28
import android.widget.ImageView;
29
import android.widget.LinearLayout;
30
import android.widget.ProgressBar;
31
import android.widget.TextView;
32

    
33
import org.distorted.external.RubikFiles;
34
import org.distorted.main.R;
35
import org.distorted.external.RubikNetwork;
36
import org.distorted.external.RubikUpdates;
37
import org.distorted.objects.RubikObjectList;
38

    
39
import java.io.IOException;
40
import java.lang.ref.WeakReference;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
45
  {
46
  private ImageView mIcon;
47
  private RubikUpdates.UpdateInfo mInfo;
48
  private ProgressBar mBar;
49
  private Button mButton;
50
  private TextView mDescription;
51

    
52
  private WeakReference<Activity> mAct;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
57
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
58
    {
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)
115
    {
116
    mIcon.setImageBitmap(icon);
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  private void startDownload()
122
    {
123
    mDescription.setText(R.string.downloading);
124
    mBar.setProgress(20);
125
    mButton.setVisibility(View.GONE);
126
    mBar.setVisibility(View.VISIBLE);
127
    }
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

    
143
      RubikFiles files = RubikFiles.getInstance();
144
      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

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

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

    
167
      if( iSuccess && oSuccess )
168
        {
169
        mBar.setProgress(75);
170
        mDescription.setText(R.string.configuring);
171
        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
        }
178
      else
179
        {
180
        mDescription.setTextColor(Color.parseColor("#ff0000"));
181
        mDescription.setText(R.string.saveError);
182
        }
183
      }
184
    }
185
  }
(20-20/21)