Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ 99b8a069

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
    int numN = updates.getNewNumber();
140
    int numU = updates.getUpdNumber();
141
    int num = numN+numU;
142 5f3801d3 Leszek Koltunski
143 9c39179e Leszek Koltunski
    if( num<=0 )
144
      {
145 179f7189 Leszek Koltunski
146 9c39179e Leszek Koltunski
      mText.setText(act.getString(R.string.no_updates));
147
      }
148
    else
149
      {
150 179f7189 Leszek Koltunski
      //mText.setText("Downloading...");
151
152 4429d6c8 Leszek Koltunski
      int imagH = (int)(mSize*1.00f);
153
      int textH = (int)(mSize*0.23f);
154
      int buttH = (int)(mSize*0.49f);
155 c651024f Leszek Koltunski
156
      LinearLayout.LayoutParams pI = new LinearLayout.LayoutParams( imagH,imagH );
157
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
158
      pV.setMargins(mMargin, mMargin, mMargin, 0);
159
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
160
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
161
162 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
163
164
      if( mLayout!=null )
165
        {
166
        for(int i=0; i<numN; i++)
167
          {
168
          RubikUpdates.UpdateInfo info = updates.getNewUpdate(i);
169
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
170 99b8a069 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,true,pI,pV,pT,pB);
171 9c39179e Leszek Koltunski
          mLayout.addView(pane);
172
          }
173
        for(int i=0; i<numU; i++)
174
          {
175
          RubikUpdates.UpdateInfo info = updates.getUpdUpdate(i);
176
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
177 99b8a069 Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,false,pI,pV,pT,pB);
178 9c39179e Leszek Koltunski
          mLayout.addView(pane);
179
          }
180
        }
181
      else
182
        {
183
        android.util.Log.e("D", "mainLayout NULL");
184
        }
185
      }
186 5f3801d3 Leszek Koltunski
    }
187
188 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190
  public void receiveUpdate(RubikUpdates updates)
191
    {
192
    FragmentActivity act = getActivity();
193
194
    if( act!=null )
195
      {
196
      act.runOnUiThread(new Runnable()
197
        {
198
        @Override
199
        public void run()
200
          {
201
          receiveUpdate(updates,act);
202
          }
203
        });
204
      }
205
    }
206
207 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
  public void errorUpdate()
210
    {
211 9c39179e Leszek Koltunski
    mText.setText("Network error");
212 c99db493 Leszek Koltunski
    }
213
214 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
  public static String getDialogTag()
217
    {
218
    return "DialogUpdates";
219
    }
220
  }