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