Project

General

Profile

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

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

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
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import android.util.DisplayMetrics;
26
import android.util.TypedValue;
27
import android.view.LayoutInflater;
28 2a2ca758 Leszek Koltunski
import android.view.View;
29 5f3801d3 Leszek Koltunski
import android.view.Window;
30
import android.widget.Button;
31 9c39179e Leszek Koltunski
import android.widget.LinearLayout;
32 5f3801d3 Leszek Koltunski
import android.widget.TextView;
33
34
import androidx.annotation.NonNull;
35
import androidx.appcompat.app.AlertDialog;
36
import androidx.appcompat.app.AppCompatDialogFragment;
37
import androidx.fragment.app.FragmentActivity;
38
39
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41
import org.distorted.network.RubikNetwork;
42
import org.distorted.network.RubikUpdates;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 c99db493 Leszek Koltunski
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.Updatee
47 5f3801d3 Leszek Koltunski
  {
48 9c39179e Leszek Koltunski
  private TextView mText;
49
  private LinearLayout mLayout;
50 99b8a069 Leszek Koltunski
  private int mMargin, mSize, mFontSize, mPadding;
51 9c39179e Leszek Koltunski
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 179f7189 Leszek Koltunski
  private View createView(FragmentActivity act, LayoutInflater inflater, float size, int minH)
55 2a2ca758 Leszek Koltunski
    {
56
    final View view = inflater.inflate(R.layout.dialog_updates, null);
57
58 9c39179e Leszek Koltunski
    mLayout= view.findViewById(R.id.updates_main_layout);
59
    mText  = view.findViewById(R.id.updates_message);
60
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
61
    mText.setText( act.getString(R.string.downloading) );
62 2a2ca758 Leszek Koltunski
63 179f7189 Leszek Koltunski
    mLayout.setMinimumHeight(minH);
64
    mText.setMinimumHeight(minH);
65
66 2a2ca758 Leszek Koltunski
    return view;
67
    }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 5f3801d3 Leszek Koltunski
  @NonNull
72
  @Override
73
  public Dialog onCreateDialog(Bundle savedInstanceState)
74
    {
75
    FragmentActivity act = getActivity();
76
    LayoutInflater inflater = act.getLayoutInflater();
77
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
78
79
    DisplayMetrics displaymetrics = new DisplayMetrics();
80
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
81 c651024f Leszek Koltunski
    int w= displaymetrics.widthPixels;
82
    int h= displaymetrics.heightPixels;
83
    final float titleSize= w*RubikActivity.MENU_BIG_TEXT_SIZE;
84
    final float okSize   = w*RubikActivity.DIALOG_BUTTON_SIZE;
85 99b8a069 Leszek Koltunski
    mMargin   = (int)(w*RubikActivity.MEDIUM_MARGIN);
86
    mSize     = (int)(h*0.14f);
87
    mFontSize = (int)(h*0.02f);
88
    mPadding  = (int)(h*0.01f);
89 5f3801d3 Leszek Koltunski
90 9c39179e Leszek Koltunski
    TextView title = (TextView) inflater.inflate(R.layout.dialog_title, null);
91
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
92
    title.setText(R.string.updates);
93
    builder.setCustomTitle(title);
94 5f3801d3 Leszek Koltunski
95
    builder.setCancelable(true);
96
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
97
      {
98
      @Override
99
      public void onClick(DialogInterface dialog, int which)
100
        {
101
102
        }
103
      });
104
105 179f7189 Leszek Koltunski
    int minH = (int)(0.35f*h);
106
    View view = createView(act,inflater,okSize,minH);
107
    view.setMinimumHeight(minH);
108 2a2ca758 Leszek Koltunski
    builder.setView(view);
109
110 5f3801d3 Leszek Koltunski
    Dialog dialog = builder.create();
111
    dialog.setCanceledOnTouchOutside(false);
112
    Window window = dialog.getWindow();
113
114
    if( window!=null )
115
      {
116
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
117
      }
118
119
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
120
      {
121
      @Override
122
      public void onShow(DialogInterface dialog)
123
        {
124
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
125
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
126
        }
127
      });
128
129 c651024f Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
130
    network.signUpForUpdates(this);
131
132 9c39179e Leszek Koltunski
    return dialog;
133
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137 179f7189 Leszek Koltunski
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
138 9c39179e Leszek Koltunski
    {
139 2c9ab085 Leszek Koltunski
    int numC = updates.getCompletedNumber();
140 5f3801d3 Leszek Koltunski
141 2c9ab085 Leszek Koltunski
    if( numC<=0 )
142 9c39179e Leszek Koltunski
      {
143
      mText.setText(act.getString(R.string.no_updates));
144
      }
145
    else
146
      {
147 179f7189 Leszek Koltunski
      //mText.setText("Downloading...");
148
149 4429d6c8 Leszek Koltunski
      int imagH = (int)(mSize*1.00f);
150 2c9ab085 Leszek Koltunski
      int textH = (int)(mSize*0.27f);
151
      int buttH = (int)(mSize*0.35f);
152 c651024f Leszek Koltunski
153
      LinearLayout.LayoutParams pI = new LinearLayout.LayoutParams( imagH,imagH );
154
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
155
      pV.setMargins(mMargin, mMargin, mMargin, 0);
156
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
157
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
158
159 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
160
161
      if( mLayout!=null )
162
        {
163 2c9ab085 Leszek Koltunski
        for(int i=0; i<numC; i++)
164 9c39179e Leszek Koltunski
          {
165 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
166 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
167 2c9ab085 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
168 9c39179e Leszek Koltunski
          mLayout.addView(pane);
169
          }
170 2c9ab085 Leszek Koltunski
171
        int numS = updates.getStartedNumber();
172
173
        for(int i=0; i<numS; i++)
174 9c39179e Leszek Koltunski
          {
175 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
176 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
177 2c9ab085 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
178 9c39179e Leszek Koltunski
          mLayout.addView(pane);
179
          }
180 ffe7e55d Leszek Koltunski
181
        RubikNetwork network = RubikNetwork.getInstance();
182
        network.downloadIcons(this);
183 9c39179e Leszek Koltunski
        }
184
      else
185
        {
186
        android.util.Log.e("D", "mainLayout NULL");
187
        }
188
      }
189 5f3801d3 Leszek Koltunski
    }
190
191 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193
  public void receiveUpdate(RubikUpdates updates)
194
    {
195
    FragmentActivity act = getActivity();
196
197
    if( act!=null )
198
      {
199
      act.runOnUiThread(new Runnable()
200
        {
201
        @Override
202
        public void run()
203
          {
204
          receiveUpdate(updates,act);
205
          }
206
        });
207
      }
208
    }
209
210 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  public void errorUpdate()
213
    {
214 9c39179e Leszek Koltunski
    mText.setText("Network error");
215 c99db493 Leszek Koltunski
    }
216
217 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
  public void iconDownloaded()
220
    {
221
222
    }
223
224 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
  public static String getDialogTag()
227
    {
228
    return "DialogUpdates";
229
    }
230
  }