Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdateView.java @ 35161021

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

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
41
  {
42
  private ImageView mIcon;
43
  private RubikUpdates.UpdateInfo mInfo;
44
  private ProgressBar mBar;
45
  private Button mButton;
46
  private TextView mDescription;
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
    mInfo = info;
54
    final RubikNetwork.Downloadee downloadee = this;
55
    View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
56
    TextView title = view.findViewById(R.id.updates_pane_title);
57
    title.setText(info.mObjectLongName);
58
    mDescription = view.findViewById(R.id.updates_pane_description);
59
    mDescription.setText(info.mDescription);
60

    
61
    mIcon = view.findViewById(R.id.updates_pane_image);
62
    mIcon.setImageResource(R.drawable.unknown_icon);
63

    
64
    view.setLayoutParams(pView);
65

    
66
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
67
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
68

    
69
    title.setLayoutParams(pText);
70
    mDescription.setLayoutParams(pText);
71

    
72
    view.setPadding(padding,padding,padding,padding);
73

    
74
    mBar    = view.findViewById(R.id.updates_pane_bar);
75
    mButton = view.findViewById(R.id.updates_pane_button);
76

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

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

    
102
    return view;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  void setIcon(Bitmap icon)
108
    {
109
    mIcon.setImageBitmap(icon);
110
    }
111

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

    
114
  private void startDownload()
115
    {
116
    mDescription.setText(R.string.downloading);
117
    mBar.setProgress(20);
118
    mButton.setVisibility(View.GONE);
119
    mBar.setVisibility(View.VISIBLE);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public void jsonDownloaded()
125
    {
126
    if( mInfo.mUpdateObject && mInfo.mObjectStream==null )
127
      {
128
      mDescription.setTextColor(Color.parseColor("#ff0000"));
129
      mDescription.setText(R.string.networkError);
130
      }
131
    else
132
      {
133
      mBar.setProgress(50);
134
      mDescription.setText(R.string.installing);
135

    
136
      RubikFiles files = RubikFiles.getInstance();
137
      boolean oSuccess=true, eSuccess=true;
138

    
139
      if( mInfo.mObjectStream!=null )
140
        {
141
        String name = mInfo.mObjectShortName + "_object.json";
142
        oSuccess = files.saveFile(mInfo.mObjectStream, name);
143
        }
144

    
145
      if( mInfo.mExtrasStream!=null )
146
        {
147
        String name = mInfo.mObjectShortName + "_extras.json";
148
        eSuccess = files.saveFile(mInfo.mExtrasStream, name);
149
        }
150

    
151
      if( oSuccess )
152
        {
153
        mBar.setProgress(75);
154
        mDescription.setText(R.string.configuring);
155
        }
156
      else
157
        {
158
        mDescription.setTextColor(Color.parseColor("#ff0000"));
159
        mDescription.setText(R.string.saveError);
160
        }
161
      }
162
    }
163
  }
(20-20/21)