Project

General

Profile

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

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

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 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
import android.view.View;
29
import android.view.Window;
30
import android.widget.Button;
31
import android.widget.LinearLayout;
32
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
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.Updatee
47
  {
48
  private TextView mText;
49
  private LinearLayout mLayout;
50
  private int mMargin, mSize, mFontSize, mPadding;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private View createView(FragmentActivity act, LayoutInflater inflater, float size, int minH)
55
    {
56
    final View view = inflater.inflate(R.layout.dialog_updates, null);
57

    
58
    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

    
63
    mLayout.setMinimumHeight(minH);
64
    mText.setMinimumHeight(minH);
65

    
66
    return view;
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  @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
    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
    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

    
90
    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

    
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
    int minH = (int)(0.35f*h);
106
    View view = createView(act,inflater,okSize,minH);
107
    view.setMinimumHeight(minH);
108
    builder.setView(view);
109

    
110
    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
    RubikNetwork network = RubikNetwork.getInstance();
130
    network.signUpForUpdates(this);
131

    
132
    return dialog;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
138
    {
139
    int numC = updates.getCompletedNumber();
140

    
141
    if( numC<=0 )
142
      {
143
      mText.setText(act.getString(R.string.no_updates));
144
      }
145
    else
146
      {
147
      //mText.setText("Downloading...");
148

    
149
      int imagH = (int)(mSize*1.00f);
150
      int textH = (int)(mSize*0.27f);
151
      int buttH = (int)(mSize*0.35f);
152

    
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
      mText.setVisibility(View.GONE);
160

    
161
      if( mLayout!=null )
162
        {
163
        for(int i=0; i<numC; i++)
164
          {
165
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
166
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
167
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
168
          mLayout.addView(pane);
169
          }
170

    
171
        int numS = updates.getStartedNumber();
172

    
173
        for(int i=0; i<numS; i++)
174
          {
175
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
176
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
177
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pI,pV,pT,pB);
178
          mLayout.addView(pane);
179
          }
180

    
181
        RubikNetwork network = RubikNetwork.getInstance();
182
        network.downloadIcons(this);
183
        }
184
      else
185
        {
186
        android.util.Log.e("D", "mainLayout NULL");
187
        }
188
      }
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void errorUpdate()
213
    {
214
    mText.setText("Network error");
215
    }
216

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

    
219
  public void iconDownloaded()
220
    {
221

    
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  public static String getDialogTag()
227
    {
228
    return "DialogUpdates";
229
    }
230
  }
(21-21/21)