Project

General

Profile

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

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

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 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46 5f3801d3 Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 46be3ddf Leszek Koltunski
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.IconReceiver, 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 2c9ab085 Leszek Koltunski
      int textH = (int)(mSize*0.27f);
157
      int buttH = (int)(mSize*0.35f);
158 c651024f Leszek Koltunski
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 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
165
166
      if( mLayout!=null )
167
        {
168 2c9ab085 Leszek Koltunski
        for(int i=0; i<numC; i++)
169 9c39179e Leszek Koltunski
          {
170 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
171 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
172 06ba394a Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
173 9c39179e Leszek Koltunski
          mLayout.addView(pane);
174 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
175 9c39179e Leszek Koltunski
          }
176 2c9ab085 Leszek Koltunski
177
        int numS = updates.getStartedNumber();
178
179
        for(int i=0; i<numS; i++)
180 9c39179e Leszek Koltunski
          {
181 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
182 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
183 06ba394a Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
184 9c39179e Leszek Koltunski
          mLayout.addView(pane);
185 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
186 9c39179e Leszek Koltunski
          }
187 ffe7e55d Leszek Koltunski
188
        RubikNetwork network = RubikNetwork.getInstance();
189 7fe62d1f Leszek Koltunski
        network.downloadIcons(act,this);
190 9c39179e Leszek Koltunski
        }
191
      else
192
        {
193
        android.util.Log.e("D", "mainLayout NULL");
194
        }
195
      }
196 5f3801d3 Leszek Koltunski
    }
197
198 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
  public void errorUpdate()
220
    {
221 46be3ddf Leszek Koltunski
    FragmentActivity act = getActivity();
222
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
223 c99db493 Leszek Koltunski
    }
224
225 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227 7fe62d1f Leszek Koltunski
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
228 ffe7e55d Leszek Koltunski
    {
229 b88cdd91 Leszek Koltunski
    FragmentActivity act = getActivity();
230 ffe7e55d Leszek Koltunski
231 b88cdd91 Leszek Koltunski
    if( act!=null )
232
      {
233
      act.runOnUiThread(new Runnable()
234
        {
235
        @Override
236
        public void run()
237
          {
238
          RubikDialogUpdateView view = mPanes.get(ordinal);
239 7fe62d1f Leszek Koltunski
          if( view!=null ) view.setIcon(icon,downloaded);
240 b88cdd91 Leszek Koltunski
          }
241
        });
242
      }
243 ffe7e55d Leszek Koltunski
    }
244
245 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
  public static String getDialogTag()
248
    {
249
    return "DialogUpdates";
250
    }
251
  }