Project

General

Profile

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

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

1 5f3801d3 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 b88cdd91 Leszek Koltunski
import java.util.ArrayList;
23
24 5f3801d3 Leszek Koltunski
import android.app.Dialog;
25
import android.content.DialogInterface;
26 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
27 5f3801d3 Leszek Koltunski
import android.os.Bundle;
28
import android.util.DisplayMetrics;
29
import android.util.TypedValue;
30
import android.view.LayoutInflater;
31 2a2ca758 Leszek Koltunski
import android.view.View;
32 5f3801d3 Leszek Koltunski
import android.view.Window;
33
import android.widget.Button;
34 9c39179e Leszek Koltunski
import android.widget.LinearLayout;
35 5f3801d3 Leszek Koltunski
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.network.RubikNetwork;
45
import org.distorted.network.RubikUpdates;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 c99db493 Leszek Koltunski
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.Updatee
50 5f3801d3 Leszek Koltunski
  {
51 9c39179e Leszek Koltunski
  private TextView mText;
52
  private LinearLayout mLayout;
53 99b8a069 Leszek Koltunski
  private int mMargin, mSize, mFontSize, mPadding;
54 9c39179e Leszek Koltunski
55 b88cdd91 Leszek Koltunski
  private ArrayList<RubikDialogUpdateView> mPanes;
56
57 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 179f7189 Leszek Koltunski
  private View createView(FragmentActivity act, LayoutInflater inflater, float size, int minH)
60 2a2ca758 Leszek Koltunski
    {
61
    final View view = inflater.inflate(R.layout.dialog_updates, null);
62
63 9c39179e Leszek Koltunski
    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 2a2ca758 Leszek Koltunski
68 179f7189 Leszek Koltunski
    mLayout.setMinimumHeight(minH);
69
    mText.setMinimumHeight(minH);
70
71 2a2ca758 Leszek Koltunski
    return view;
72
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 5f3801d3 Leszek Koltunski
  @NonNull
77
  @Override
78
  public Dialog onCreateDialog(Bundle savedInstanceState)
79
    {
80 b88cdd91 Leszek Koltunski
    if( mPanes==null ) mPanes = new ArrayList<>();
81
82 5f3801d3 Leszek Koltunski
    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 c651024f Leszek Koltunski
    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 99b8a069 Leszek Koltunski
    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 5f3801d3 Leszek Koltunski
97 9c39179e Leszek Koltunski
    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 5f3801d3 Leszek Koltunski
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 179f7189 Leszek Koltunski
    int minH = (int)(0.35f*h);
113
    View view = createView(act,inflater,okSize,minH);
114
    view.setMinimumHeight(minH);
115 2a2ca758 Leszek Koltunski
    builder.setView(view);
116
117 5f3801d3 Leszek Koltunski
    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 c651024f Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
137
    network.signUpForUpdates(this);
138
139 9c39179e Leszek Koltunski
    return dialog;
140
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 179f7189 Leszek Koltunski
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
145 9c39179e Leszek Koltunski
    {
146 2c9ab085 Leszek Koltunski
    int numC = updates.getCompletedNumber();
147 5f3801d3 Leszek Koltunski
148 2c9ab085 Leszek Koltunski
    if( numC<=0 )
149 9c39179e Leszek Koltunski
      {
150
      mText.setText(act.getString(R.string.no_updates));
151
      }
152
    else
153
      {
154 179f7189 Leszek Koltunski
      //mText.setText("Downloading...");
155
156 4429d6c8 Leszek Koltunski
      int imagH = (int)(mSize*1.00f);
157 2c9ab085 Leszek Koltunski
      int textH = (int)(mSize*0.27f);
158
      int buttH = (int)(mSize*0.35f);
159 c651024f Leszek Koltunski
160
      LinearLayout.LayoutParams pI = new LinearLayout.LayoutParams( imagH,imagH );
161
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
162
      pV.setMargins(mMargin, mMargin, mMargin, 0);
163
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
164
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
165
166 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
167
168
      if( mLayout!=null )
169
        {
170 2c9ab085 Leszek Koltunski
        for(int i=0; i<numC; i++)
171 9c39179e Leszek Koltunski
          {
172 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
173 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
174 2c9ab085 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
175 9c39179e Leszek Koltunski
          mLayout.addView(pane);
176 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
177 9c39179e Leszek Koltunski
          }
178 2c9ab085 Leszek Koltunski
179
        int numS = updates.getStartedNumber();
180
181
        for(int i=0; i<numS; i++)
182 9c39179e Leszek Koltunski
          {
183 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
184 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
185 2c9ab085 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
186 9c39179e Leszek Koltunski
          mLayout.addView(pane);
187 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
188 9c39179e Leszek Koltunski
          }
189 ffe7e55d Leszek Koltunski
190
        RubikNetwork network = RubikNetwork.getInstance();
191
        network.downloadIcons(this);
192 9c39179e Leszek Koltunski
        }
193
      else
194
        {
195
        android.util.Log.e("D", "mainLayout NULL");
196
        }
197
      }
198 5f3801d3 Leszek Koltunski
    }
199
200 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  public void receiveUpdate(RubikUpdates updates)
203
    {
204
    FragmentActivity act = getActivity();
205
206
    if( act!=null )
207
      {
208
      act.runOnUiThread(new Runnable()
209
        {
210
        @Override
211
        public void run()
212
          {
213
          receiveUpdate(updates,act);
214
          }
215
        });
216
      }
217
    }
218
219 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
  public void errorUpdate()
222
    {
223 9c39179e Leszek Koltunski
    mText.setText("Network error");
224 c99db493 Leszek Koltunski
    }
225
226 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 b88cdd91 Leszek Koltunski
  public void iconDownloaded(int ordinal, Bitmap icon)
229 ffe7e55d Leszek Koltunski
    {
230 b88cdd91 Leszek Koltunski
    FragmentActivity act = getActivity();
231 ffe7e55d Leszek Koltunski
232 b88cdd91 Leszek Koltunski
    if( act!=null )
233
      {
234
      act.runOnUiThread(new Runnable()
235
        {
236
        @Override
237
        public void run()
238
          {
239
          RubikDialogUpdateView view = mPanes.get(ordinal);
240
          if( view!=null ) view.setIcon(icon);
241
          }
242
        });
243
      }
244 ffe7e55d Leszek Koltunski
    }
245
246 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
  public static String getDialogTag()
249
    {
250
    return "DialogUpdates";
251
    }
252
  }