Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ 4429d6c8

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.Context;
24
import android.content.DialogInterface;
25
import android.os.Bundle;
26
import android.util.DisplayMetrics;
27
import android.util.TypedValue;
28
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.view.Window;
31
import android.view.WindowManager;
32
import android.widget.Button;
33
import android.widget.LinearLayout;
34
import android.widget.ScrollView;
35
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
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.Updatee
50
  {
51
  private TextView mText;
52
  private ScrollView mScroll;
53
  private LinearLayout mLayout;
54
  private int mMargin, mSize, mFontSize;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  private View createView(FragmentActivity act, LayoutInflater inflater, float size)
59
    {
60
    final View view = inflater.inflate(R.layout.dialog_updates, null);
61

    
62
    mLayout= view.findViewById(R.id.updates_main_layout);
63
    mScroll= view.findViewById(R.id.updates_scroll);
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

    
68
    return view;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  @NonNull
74
  @Override
75
  public Dialog onCreateDialog(Bundle savedInstanceState)
76
    {
77
    FragmentActivity act = getActivity();
78
    LayoutInflater inflater = act.getLayoutInflater();
79
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
80

    
81
    DisplayMetrics displaymetrics = new DisplayMetrics();
82
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
83
    int w= displaymetrics.widthPixels;
84
    int h= displaymetrics.heightPixels;
85
    final float titleSize= w*RubikActivity.MENU_BIG_TEXT_SIZE;
86
    final float okSize   = w*RubikActivity.DIALOG_BUTTON_SIZE;
87
    mMargin              = (int)(w*RubikActivity.MEDIUM_MARGIN);
88
    mSize                = (int)(h*0.14f);
89
    mFontSize            = (int)(h*0.02f);
90

    
91
    TextView title = (TextView) inflater.inflate(R.layout.dialog_title, null);
92
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
93
    title.setText(R.string.updates);
94
    builder.setCustomTitle(title);
95

    
96
    builder.setCancelable(true);
97
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
98
      {
99
      @Override
100
      public void onClick(DialogInterface dialog, int which)
101
        {
102

    
103
        }
104
      });
105

    
106
    View view = createView(act,inflater,okSize);
107
    builder.setView(view);
108

    
109
    Dialog dialog = builder.create();
110
    dialog.setCanceledOnTouchOutside(false);
111
    Window window = dialog.getWindow();
112

    
113
    if( window!=null )
114
      {
115
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
116
      }
117

    
118
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
119
      {
120
      @Override
121
      public void onShow(DialogInterface dialog)
122
        {
123
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
124
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
125
        }
126
      });
127

    
128
    RubikNetwork network = RubikNetwork.getInstance();
129
    network.signUpForUpdates(this);
130

    
131
    return dialog;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  @Override
137
  public void onResume()
138
    {
139
    super.onResume();
140

    
141
    Window window = getDialog().getWindow();
142
    Context context = getContext();
143

    
144
    if( window!=null && context!=null )
145
      {
146
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
147
      final float height= metrics.heightPixels;
148

    
149
      WindowManager.LayoutParams params = window.getAttributes();
150
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
151
      params.height = (int)(0.75f*height);
152
      window.setAttributes(params);
153

    
154
      int textHeight = (int)(0.75f*params.height);
155
      mText.setHeight(textHeight);
156
      mLayout.setMinimumHeight(textHeight);
157
      }
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  public void receiveUpdate(RubikUpdates updates)
163
    {
164
    FragmentActivity act = getActivity();
165
    int numN = updates.getNewNumber();
166
    int numU = updates.getUpdNumber();
167
    int num = numN+numU;
168

    
169
    if( num<=0 )
170
      {
171
      mScroll.setVerticalScrollBarEnabled(false);
172
      mText.setText(act.getString(R.string.no_updates));
173
      }
174
    else
175
      {
176
      int imagH = (int)(mSize*1.00f);
177
      int textH = (int)(mSize*0.23f);
178
      int buttH = (int)(mSize*0.49f);
179

    
180
      LinearLayout.LayoutParams pI = new LinearLayout.LayoutParams( imagH,imagH );
181
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
182
      pV.setMargins(mMargin, mMargin, mMargin, 0);
183
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
184
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
185

    
186
      mText.setVisibility(View.GONE);
187

    
188
      if( mLayout!=null )
189
        {
190
        for(int i=0; i<numN; i++)
191
          {
192
          RubikUpdates.UpdateInfo info = updates.getNewUpdate(i);
193
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
194
          View pane = rubikView.createView(act,info,mFontSize,true,pI,pV,pT,pB);
195
          mLayout.addView(pane);
196
          }
197
        for(int i=0; i<numU; i++)
198
          {
199
          RubikUpdates.UpdateInfo info = updates.getUpdUpdate(i);
200
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
201
          View pane = rubikView.createView(act,info,mFontSize,false,pI,pV,pT,pB);
202
          mLayout.addView(pane);
203
          }
204
        }
205
      else
206
        {
207
        android.util.Log.e("D", "mainLayout NULL");
208
        }
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public void errorUpdate()
215
    {
216
    mScroll.setVerticalScrollBarEnabled(false);
217
    mText.setText("Network error");
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public static String getDialogTag()
223
    {
224
    return "DialogUpdates";
225
    }
226
  }
(21-21/21)