Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ 9c39179e

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
61
    mLayout= view.findViewById(R.id.updates_main_layout);
62
    mScroll= view.findViewById(R.id.updates_scroll);
63
    mText  = view.findViewById(R.id.updates_message);
64
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
65
    mText.setText( act.getString(R.string.downloading) );
66

    
67
    return view;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

    
80
    DisplayMetrics displaymetrics = new DisplayMetrics();
81
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
82
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
83
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
84

    
85
    TextView title = (TextView) inflater.inflate(R.layout.dialog_title, null);
86
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
87
    title.setText(R.string.updates);
88
    builder.setCustomTitle(title);
89

    
90
    builder.setCancelable(true);
91
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
92
      {
93
      @Override
94
      public void onClick(DialogInterface dialog, int which)
95
        {
96

    
97
        }
98
      });
99

    
100
    View view = createView(act,inflater,okSize);
101
    builder.setView(view);
102

    
103
    Dialog dialog = builder.create();
104
    dialog.setCanceledOnTouchOutside(false);
105
    Window window = dialog.getWindow();
106

    
107
    if( window!=null )
108
      {
109
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
110
      }
111

    
112
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
113
      {
114
      @Override
115
      public void onShow(DialogInterface dialog)
116
        {
117
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
118
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
119
        }
120
      });
121

    
122
    return dialog;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  @Override
128
  public void onResume()
129
    {
130
    super.onResume();
131

    
132
    RubikNetwork network = RubikNetwork.getInstance();
133
    network.signUpForUpdates(this);
134

    
135
    Window window = getDialog().getWindow();
136
    Context context = getContext();
137

    
138
    if( window!=null && context!=null )
139
      {
140
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
141
      final float height= metrics.heightPixels;
142

    
143
      WindowManager.LayoutParams params = window.getAttributes();
144
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
145
      params.height = (int)(0.75f*height);
146
      window.setAttributes(params);
147

    
148
      int textHeight = (int)(0.8f*params.height);
149
      mText.setHeight(textHeight);
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public void receiveUpdate(RubikUpdates updates)
156
    {
157
    FragmentActivity act = getActivity();
158
    int numN = updates.getNewNumber();
159
    int numU = updates.getUpdNumber();
160
    int num = numN+numU;
161

    
162
    if( num<=0 )
163
      {
164
      mScroll.setVerticalScrollBarEnabled(false);
165
      mText.setText(act.getString(R.string.no_updates));
166
      }
167
    else
168
      {
169
      mText.setVisibility(View.GONE);
170

    
171
      if( mLayout!=null )
172
        {
173
        for(int i=0; i<numN; i++)
174
          {
175
          RubikUpdates.UpdateInfo info = updates.getNewUpdate(i);
176
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
177
          View pane = rubikView.createView(act,info,true);
178
          mLayout.addView(pane);
179
          }
180
        for(int i=0; i<numU; i++)
181
          {
182
          RubikUpdates.UpdateInfo info = updates.getUpdUpdate(i);
183
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
184
          View pane = rubikView.createView(act,info,false);
185
          mLayout.addView(pane);
186
          }
187
        }
188
      else
189
        {
190
        android.util.Log.e("D", "mainLayout NULL");
191
        }
192
      }
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public void errorUpdate()
198
    {
199
    mScroll.setVerticalScrollBarEnabled(false);
200
    mText.setText("Network error");
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  public static String getDialogTag()
206
    {
207
    return "DialogUpdates";
208
    }
209
  }
(21-21/21)