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