Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ master

1 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 68191e7d Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.dialogs;
11
12 b88cdd91 Leszek Koltunski
import java.util.ArrayList;
13
14 5f3801d3 Leszek Koltunski
import android.app.Dialog;
15 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
16 5f3801d3 Leszek Koltunski
import android.util.TypedValue;
17 2a2ca758 Leszek Koltunski
import android.view.View;
18 67d7fb28 Leszek Koltunski
import android.view.Window;
19
import android.view.WindowManager;
20 9c39179e Leszek Koltunski
import android.widget.LinearLayout;
21 5f3801d3 Leszek Koltunski
import android.widget.TextView;
22
23
import androidx.fragment.app.FragmentActivity;
24
25
import org.distorted.main.R;
26 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
27
import org.distorted.external.RubikUpdates;
28 5f3801d3 Leszek Koltunski
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 c02fa107 Leszek Koltunski
public class RubikDialogUpdates extends RubikDialogAbstract implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
32 5f3801d3 Leszek Koltunski
  {
33 9c39179e Leszek Koltunski
  private TextView mText;
34
  private LinearLayout mLayout;
35 99b8a069 Leszek Koltunski
  private int mMargin, mSize, mFontSize, mPadding;
36 b88cdd91 Leszek Koltunski
  private ArrayList<RubikDialogUpdateView> mPanes;
37
38 67d7fb28 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40
  @Override
41
  public void onResume()
42
    {
43
    super.onResume();
44
45
    Window window = getDialog().getWindow();
46
47
    if( window!=null )
48
      {
49
      WindowManager.LayoutParams params = window.getAttributes();
50
      params.width  = (int)Math.min( mHeight*0.65f,mWidth*0.98f );
51
      window.setAttributes(params);
52
      }
53
    }
54
55 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 8bf6f34c leszek
  public int getResource()            { return R.layout.dialog_scrollable_panes; }
58
  public int getTitleResource()       { return R.string.updates; }
59
  public boolean hasArgument()        { return false; }
60
  public int getPositive()            { return R.string.ok; }
61
  public int getNegative()            { return -1; }
62
  public void positiveAction()        { }
63
  public void negativeAction()        { }
64
  public static String getDialogTag() { return "DialogUpdates"; }
65 c02fa107 Leszek Koltunski
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
69
    {
70
    if( mPanes==null ) mPanes = new ArrayList<>();
71
72
    int minH  = (int)(mHeight*0.25f);
73
    mMargin   = (int)(mHeight*0.01f);
74
    mSize     = (int)(mHeight*0.14f);
75
    mFontSize = (int)(mHeight*0.02f);
76
    mPadding  = (int)(mHeight*0.01f);
77
78 67d7fb28 Leszek Koltunski
    mLayout= view.findViewById(R.id.dialog_scrollable_main_layout);
79
    mText  = view.findViewById(R.id.dialog_scrollable_message);
80 c02fa107 Leszek Koltunski
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
81
    mText.setText( act.getString(R.string.downloading) );
82
83
    mLayout.setMinimumHeight(minH);
84
    mText.setMinimumHeight(minH);
85
    view.setMinimumHeight(minH);
86 5f3801d3 Leszek Koltunski
87 c651024f Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
88
    network.signUpForUpdates(this);
89 9c39179e Leszek Koltunski
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 179f7189 Leszek Koltunski
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
94 9c39179e Leszek Koltunski
    {
95 2c9ab085 Leszek Koltunski
    int numC = updates.getCompletedNumber();
96 2e48adc9 Leszek Koltunski
    int numS = updates.getStartedNumber();
97 5f3801d3 Leszek Koltunski
98 2e48adc9 Leszek Koltunski
    if( numC+numS<=0 )
99 9c39179e Leszek Koltunski
      {
100
      mText.setText(act.getString(R.string.no_updates));
101
      }
102
    else
103
      {
104 2c9ab085 Leszek Koltunski
      int textH = (int)(mSize*0.27f);
105
      int buttH = (int)(mSize*0.35f);
106 c651024f Leszek Koltunski
107
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
108
      pV.setMargins(mMargin, mMargin, mMargin, 0);
109 1c89e2a7 Leszek Koltunski
      LinearLayout.LayoutParams pL = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
110
      pL.setMargins(mMargin, mMargin, mMargin, mMargin);
111 c651024f Leszek Koltunski
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
112
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
113
114 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
115
116
      if( mLayout!=null )
117
        {
118 2c9ab085 Leszek Koltunski
        for(int i=0; i<numC; i++)
119 9c39179e Leszek Koltunski
          {
120 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
121 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
122 96e67000 leszek
          View pane = rubikView.createView(act,info,mFontSize,mPadding,( (numS==0 && i==numC-1)?pL:pV),pT,pB);
123 9c39179e Leszek Koltunski
          mLayout.addView(pane);
124 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
125 9c39179e Leszek Koltunski
          }
126 2c9ab085 Leszek Koltunski
127
        for(int i=0; i<numS; i++)
128 9c39179e Leszek Koltunski
          {
129 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
130 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
131 1c89e2a7 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,(i==numS-1?pL:pV),pT,pB);
132 9c39179e Leszek Koltunski
          mLayout.addView(pane);
133 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
134 9c39179e Leszek Koltunski
          }
135 ffe7e55d Leszek Koltunski
136
        RubikNetwork network = RubikNetwork.getInstance();
137 7fe62d1f Leszek Koltunski
        network.downloadIcons(act,this);
138 9c39179e Leszek Koltunski
        }
139
      else
140
        {
141
        android.util.Log.e("D", "mainLayout NULL");
142
        }
143
      }
144 5f3801d3 Leszek Koltunski
    }
145
146 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  public void receiveUpdate(RubikUpdates updates)
149
    {
150
    FragmentActivity act = getActivity();
151
152
    if( act!=null )
153
      {
154
      act.runOnUiThread(new Runnable()
155
        {
156
        @Override
157
        public void run()
158
          {
159
          receiveUpdate(updates,act);
160
          }
161
        });
162
      }
163
    }
164
165 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  public void errorUpdate()
168
    {
169 46be3ddf Leszek Koltunski
    FragmentActivity act = getActivity();
170
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
171 c99db493 Leszek Koltunski
    }
172
173 1c04d054 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
  public void objectDownloaded(String shortName)
176
    {
177
178
    }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
  public int getType()
183
    {
184
    return 1;
185
    }
186
187 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 7fe62d1f Leszek Koltunski
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
190 ffe7e55d Leszek Koltunski
    {
191 b88cdd91 Leszek Koltunski
    FragmentActivity act = getActivity();
192 ffe7e55d Leszek Koltunski
193 b88cdd91 Leszek Koltunski
    if( act!=null )
194
      {
195
      act.runOnUiThread(new Runnable()
196
        {
197
        @Override
198
        public void run()
199
          {
200
          RubikDialogUpdateView view = mPanes.get(ordinal);
201 7fe62d1f Leszek Koltunski
          if( view!=null ) view.setIcon(icon,downloaded);
202 b88cdd91 Leszek Koltunski
          }
203
        });
204
      }
205 ffe7e55d Leszek Koltunski
    }
206 5f3801d3 Leszek Koltunski
  }