Project

General

Profile

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

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

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 9fde123a Leszek Koltunski
38 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 46be3ddf Leszek Koltunski
public class RubikDialogUpdateView implements RubikNetwork.Downloadee
41 9c39179e Leszek Koltunski
  {
42 b88cdd91 Leszek Koltunski
  private ImageView mIcon;
43 46be3ddf Leszek Koltunski
  private RubikUpdates.UpdateInfo mInfo;
44
  private ProgressBar mBar;
45
  private Button mButton;
46
  private TextView mDescription;
47 9c39179e Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 2c9ab085 Leszek Koltunski
  public View createView(Activity act, RubikUpdates.UpdateInfo info, int fontSize, int padding,
51 06ba394a Leszek Koltunski
                         LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText, LinearLayout.LayoutParams pButt )
52 9c39179e Leszek Koltunski
    {
53 46be3ddf Leszek Koltunski
    mInfo = info;
54
    final RubikNetwork.Downloadee downloadee = this;
55 9fde123a Leszek Koltunski
    View view = act.getLayoutInflater().inflate(R.layout.dialog_updates_pane, null);
56 2c9ab085 Leszek Koltunski
    TextView title = view.findViewById(R.id.updates_pane_title);
57 9c39179e Leszek Koltunski
    title.setText(info.mObjectLongName);
58 46be3ddf Leszek Koltunski
    mDescription = view.findViewById(R.id.updates_pane_description);
59
    mDescription.setText(info.mDescription);
60 9c39179e Leszek Koltunski
61 b88cdd91 Leszek Koltunski
    mIcon = view.findViewById(R.id.updates_pane_image);
62
    mIcon.setImageResource(R.drawable.unknown_icon);
63 7bee6064 Leszek Koltunski
64 2c9ab085 Leszek Koltunski
    view.setLayoutParams(pView);
65 c651024f Leszek Koltunski
66
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
67 46be3ddf Leszek Koltunski
    mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
68 c651024f Leszek Koltunski
69
    title.setLayoutParams(pText);
70 46be3ddf Leszek Koltunski
    mDescription.setLayoutParams(pText);
71 c651024f Leszek Koltunski
72 2c9ab085 Leszek Koltunski
    view.setPadding(padding,padding,padding,padding);
73
74 46be3ddf Leszek Koltunski
    mBar    = view.findViewById(R.id.updates_pane_bar);
75
    mButton = view.findViewById(R.id.updates_pane_button);
76 2c9ab085 Leszek Koltunski
77 9fde123a Leszek Koltunski
    if( info.mPercent>=100 )
78
      {
79 acabdd83 Leszek Koltunski
      mBar.setVisibility(View.GONE);
80 46be3ddf Leszek Koltunski
      mButton.setOnClickListener( new View.OnClickListener()
81 2c9ab085 Leszek Koltunski
        {
82
        @Override
83
        public void onClick(View v)
84
          {
85 46be3ddf Leszek Koltunski
          startDownload();
86
          RubikNetwork network = RubikNetwork.getInstance();
87
          network.downloadJSON(info,downloadee);
88 2c9ab085 Leszek Koltunski
          }
89
        });
90
91 46be3ddf Leszek Koltunski
      mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
92
      mButton.setLayoutParams(pButt);
93
      mBar.setLayoutParams(pButt);
94 2c9ab085 Leszek Koltunski
      }
95
    else
96
      {
97 acabdd83 Leszek Koltunski
      mButton.setVisibility(View.GONE);
98 46be3ddf Leszek Koltunski
      mBar.setLayoutParams(pButt);
99
      mBar.setProgress(info.mPercent);
100 2c9ab085 Leszek Koltunski
      }
101 99b8a069 Leszek Koltunski
102 2c9ab085 Leszek Koltunski
    return view;
103 9c39179e Leszek Koltunski
    }
104 b88cdd91 Leszek Koltunski
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
  void setIcon(Bitmap icon)
108
    {
109
    mIcon.setImageBitmap(icon);
110
    }
111 46be3ddf Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
  private void startDownload()
115
    {
116
    mDescription.setText(R.string.downloading);
117
    mBar.setProgress(20);
118 acabdd83 Leszek Koltunski
    mButton.setVisibility(View.GONE);
119
    mBar.setVisibility(View.VISIBLE);
120 46be3ddf Leszek Koltunski
    }
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 35161021 Leszek Koltunski
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 46be3ddf Leszek Koltunski
      }
162
    }
163 9c39179e Leszek Koltunski
  }