Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ 7fe62d1f

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 java.util.ArrayList;
23

    
24
import android.app.Dialog;
25
import android.content.DialogInterface;
26
import android.graphics.Bitmap;
27
import android.os.Bundle;
28
import android.util.DisplayMetrics;
29
import android.util.TypedValue;
30
import android.view.LayoutInflater;
31
import android.view.View;
32
import android.view.Window;
33
import android.widget.Button;
34
import android.widget.LinearLayout;
35
import android.widget.TextView;
36

    
37
import androidx.annotation.NonNull;
38
import androidx.appcompat.app.AlertDialog;
39
import androidx.appcompat.app.AppCompatDialogFragment;
40
import androidx.fragment.app.FragmentActivity;
41

    
42
import org.distorted.main.R;
43
import org.distorted.main.RubikActivity;
44
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
50
  {
51
  private TextView mText;
52
  private LinearLayout mLayout;
53
  private int mMargin, mSize, mFontSize, mPadding;
54

    
55
  private ArrayList<RubikDialogUpdateView> mPanes;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private View createView(FragmentActivity act, LayoutInflater inflater, float size, int minH)
60
    {
61
    final View view = inflater.inflate(R.layout.dialog_updates, null);
62

    
63
    mLayout= view.findViewById(R.id.updates_main_layout);
64
    mText  = view.findViewById(R.id.updates_message);
65
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
66
    mText.setText( act.getString(R.string.downloading) );
67

    
68
    mLayout.setMinimumHeight(minH);
69
    mText.setMinimumHeight(minH);
70

    
71
    return view;
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  @NonNull
77
  @Override
78
  public Dialog onCreateDialog(Bundle savedInstanceState)
79
    {
80
    if( mPanes==null ) mPanes = new ArrayList<>();
81

    
82
    FragmentActivity act = getActivity();
83
    LayoutInflater inflater = act.getLayoutInflater();
84
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
85

    
86
    DisplayMetrics displaymetrics = new DisplayMetrics();
87
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
88
    int w= displaymetrics.widthPixels;
89
    int h= displaymetrics.heightPixels;
90
    final float titleSize= w*RubikActivity.MENU_BIG_TEXT_SIZE;
91
    final float okSize   = w*RubikActivity.DIALOG_BUTTON_SIZE;
92
    mMargin   = (int)(w*RubikActivity.MEDIUM_MARGIN);
93
    mSize     = (int)(h*0.14f);
94
    mFontSize = (int)(h*0.02f);
95
    mPadding  = (int)(h*0.01f);
96

    
97
    TextView title = (TextView) inflater.inflate(R.layout.dialog_title, null);
98
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
99
    title.setText(R.string.updates);
100
    builder.setCustomTitle(title);
101

    
102
    builder.setCancelable(true);
103
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
104
      {
105
      @Override
106
      public void onClick(DialogInterface dialog, int which)
107
        {
108

    
109
        }
110
      });
111

    
112
    int minH = (int)(0.35f*h);
113
    View view = createView(act,inflater,okSize,minH);
114
    view.setMinimumHeight(minH);
115
    builder.setView(view);
116

    
117
    Dialog dialog = builder.create();
118
    dialog.setCanceledOnTouchOutside(false);
119
    Window window = dialog.getWindow();
120

    
121
    if( window!=null )
122
      {
123
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
124
      }
125

    
126
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
127
      {
128
      @Override
129
      public void onShow(DialogInterface dialog)
130
        {
131
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
132
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
133
        }
134
      });
135

    
136
    RubikNetwork network = RubikNetwork.getInstance();
137
    network.signUpForUpdates(this);
138

    
139
    return dialog;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
145
    {
146
    int numC = updates.getCompletedNumber();
147

    
148
    if( numC<=0 )
149
      {
150
      mText.setText(act.getString(R.string.no_updates));
151
      }
152
    else
153
      {
154
      //mText.setText("Downloading...");
155

    
156
      int textH = (int)(mSize*0.27f);
157
      int buttH = (int)(mSize*0.35f);
158

    
159
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
160
      pV.setMargins(mMargin, mMargin, mMargin, 0);
161
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
162
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
163

    
164
      mText.setVisibility(View.GONE);
165

    
166
      if( mLayout!=null )
167
        {
168
        for(int i=0; i<numC; i++)
169
          {
170
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
171
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
172
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
173
          mLayout.addView(pane);
174
          mPanes.add(rubikView);
175
          }
176

    
177
        int numS = updates.getStartedNumber();
178

    
179
        for(int i=0; i<numS; i++)
180
          {
181
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
182
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
183
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
184
          mLayout.addView(pane);
185
          mPanes.add(rubikView);
186
          }
187

    
188
        RubikNetwork network = RubikNetwork.getInstance();
189
        network.downloadIcons(act,this);
190
        }
191
      else
192
        {
193
        android.util.Log.e("D", "mainLayout NULL");
194
        }
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public void receiveUpdate(RubikUpdates updates)
201
    {
202
    FragmentActivity act = getActivity();
203

    
204
    if( act!=null )
205
      {
206
      act.runOnUiThread(new Runnable()
207
        {
208
        @Override
209
        public void run()
210
          {
211
          receiveUpdate(updates,act);
212
          }
213
        });
214
      }
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public void errorUpdate()
220
    {
221
    FragmentActivity act = getActivity();
222
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
228
    {
229
    FragmentActivity act = getActivity();
230

    
231
    if( act!=null )
232
      {
233
      act.runOnUiThread(new Runnable()
234
        {
235
        @Override
236
        public void run()
237
          {
238
          RubikDialogUpdateView view = mPanes.get(ordinal);
239
          if( view!=null ) view.setIcon(icon,downloaded);
240
          }
241
        });
242
      }
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public static String getDialogTag()
248
    {
249
    return "DialogUpdates";
250
    }
251
  }
(21-21/21)