Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.dialogs;
11

    
12
import java.util.ArrayList;
13

    
14
import android.app.Dialog;
15
import android.graphics.Bitmap;
16
import android.util.TypedValue;
17
import android.view.View;
18
import android.view.Window;
19
import android.view.WindowManager;
20
import android.widget.LinearLayout;
21
import android.widget.TextView;
22

    
23
import androidx.fragment.app.FragmentActivity;
24

    
25
import org.distorted.main.R;
26
import org.distorted.external.RubikNetwork;
27
import org.distorted.external.RubikUpdates;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikDialogUpdates extends RubikDialogAbstract implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
32
  {
33
  private TextView mText;
34
  private LinearLayout mLayout;
35
  private int mMargin, mSize, mFontSize, mPadding;
36
  private ArrayList<RubikDialogUpdateView> mPanes;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  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

    
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
    mLayout= view.findViewById(R.id.dialog_scrollable_main_layout);
79
    mText  = view.findViewById(R.id.dialog_scrollable_message);
80
    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

    
87
    RubikNetwork network = RubikNetwork.getInstance();
88
    network.signUpForUpdates(this);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
94
    {
95
    int numC = updates.getCompletedNumber();
96
    int numS = updates.getStartedNumber();
97

    
98
    if( numC+numS<=0 )
99
      {
100
      mText.setText(act.getString(R.string.no_updates));
101
      }
102
    else
103
      {
104
      int textH = (int)(mSize*0.27f);
105
      int buttH = (int)(mSize*0.35f);
106

    
107
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
108
      pV.setMargins(mMargin, mMargin, mMargin, 0);
109
      LinearLayout.LayoutParams pL = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
110
      pL.setMargins(mMargin, mMargin, mMargin, mMargin);
111
      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
      mText.setVisibility(View.GONE);
115

    
116
      if( mLayout!=null )
117
        {
118
        for(int i=0; i<numC; i++)
119
          {
120
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
121
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
122
          View pane = rubikView.createView(act,info,mFontSize,mPadding,( (numS==0 && i==numC-1)?pL:pV),pT,pB);
123
          mLayout.addView(pane);
124
          mPanes.add(rubikView);
125
          }
126

    
127
        for(int i=0; i<numS; i++)
128
          {
129
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
130
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
131
          View pane = rubikView.createView(act,info,mFontSize,mPadding,(i==numS-1?pL:pV),pT,pB);
132
          mLayout.addView(pane);
133
          mPanes.add(rubikView);
134
          }
135

    
136
        RubikNetwork network = RubikNetwork.getInstance();
137
        network.downloadIcons(act,this);
138
        }
139
      else
140
        {
141
        android.util.Log.e("D", "mainLayout NULL");
142
        }
143
      }
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public void errorUpdate()
168
    {
169
    FragmentActivity act = getActivity();
170
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public void objectDownloaded(String shortName)
176
    {
177

    
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public int getType()
183
    {
184
    return 1;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
190
    {
191
    FragmentActivity act = getActivity();
192

    
193
    if( act!=null )
194
      {
195
      act.runOnUiThread(new Runnable()
196
        {
197
        @Override
198
        public void run()
199
          {
200
          RubikDialogUpdateView view = mPanes.get(ordinal);
201
          if( view!=null ) view.setIcon(icon,downloaded);
202
          }
203
        });
204
      }
205
    }
206
  }
(25-25/25)