1 |
5f3801d3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
b88cdd91
|
Leszek Koltunski
|
import java.util.ArrayList;
|
23 |
|
|
|
24 |
5f3801d3
|
Leszek Koltunski
|
import android.app.Dialog;
|
25 |
|
|
import android.content.DialogInterface;
|
26 |
b88cdd91
|
Leszek Koltunski
|
import android.graphics.Bitmap;
|
27 |
5f3801d3
|
Leszek Koltunski
|
import android.os.Bundle;
|
28 |
|
|
import android.util.DisplayMetrics;
|
29 |
|
|
import android.util.TypedValue;
|
30 |
|
|
import android.view.LayoutInflater;
|
31 |
2a2ca758
|
Leszek Koltunski
|
import android.view.View;
|
32 |
5f3801d3
|
Leszek Koltunski
|
import android.view.Window;
|
33 |
|
|
import android.widget.Button;
|
34 |
9c39179e
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
35 |
5f3801d3
|
Leszek Koltunski
|
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 |
acabdd83
|
Leszek Koltunski
|
import org.distorted.external.RubikNetwork;
|
45 |
|
|
import org.distorted.external.RubikUpdates;
|
46 |
5f3801d3
|
Leszek Koltunski
|
|
47 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48 |
|
|
|
49 |
46be3ddf
|
Leszek Koltunski
|
public class RubikDialogUpdates extends AppCompatDialogFragment implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
|
50 |
5f3801d3
|
Leszek Koltunski
|
{
|
51 |
9c39179e
|
Leszek Koltunski
|
private TextView mText;
|
52 |
|
|
private LinearLayout mLayout;
|
53 |
99b8a069
|
Leszek Koltunski
|
private int mMargin, mSize, mFontSize, mPadding;
|
54 |
9c39179e
|
Leszek Koltunski
|
|
55 |
b88cdd91
|
Leszek Koltunski
|
private ArrayList<RubikDialogUpdateView> mPanes;
|
56 |
|
|
|
57 |
9c39179e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58 |
|
|
|
59 |
179f7189
|
Leszek Koltunski
|
private View createView(FragmentActivity act, LayoutInflater inflater, float size, int minH)
|
60 |
2a2ca758
|
Leszek Koltunski
|
{
|
61 |
|
|
final View view = inflater.inflate(R.layout.dialog_updates, null);
|
62 |
|
|
|
63 |
9c39179e
|
Leszek Koltunski
|
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 |
2a2ca758
|
Leszek Koltunski
|
|
68 |
179f7189
|
Leszek Koltunski
|
mLayout.setMinimumHeight(minH);
|
69 |
|
|
mText.setMinimumHeight(minH);
|
70 |
|
|
|
71 |
2a2ca758
|
Leszek Koltunski
|
return view;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75 |
|
|
|
76 |
5f3801d3
|
Leszek Koltunski
|
@NonNull
|
77 |
|
|
@Override
|
78 |
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
79 |
|
|
{
|
80 |
b88cdd91
|
Leszek Koltunski
|
if( mPanes==null ) mPanes = new ArrayList<>();
|
81 |
|
|
|
82 |
5f3801d3
|
Leszek Koltunski
|
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 |
c651024f
|
Leszek Koltunski
|
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 |
99b8a069
|
Leszek Koltunski
|
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 |
5f3801d3
|
Leszek Koltunski
|
|
97 |
9c39179e
|
Leszek Koltunski
|
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 |
5f3801d3
|
Leszek Koltunski
|
|
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 |
179f7189
|
Leszek Koltunski
|
int minH = (int)(0.35f*h);
|
113 |
|
|
View view = createView(act,inflater,okSize,minH);
|
114 |
|
|
view.setMinimumHeight(minH);
|
115 |
2a2ca758
|
Leszek Koltunski
|
builder.setView(view);
|
116 |
|
|
|
117 |
5f3801d3
|
Leszek Koltunski
|
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 |
c651024f
|
Leszek Koltunski
|
RubikNetwork network = RubikNetwork.getInstance();
|
137 |
|
|
network.signUpForUpdates(this);
|
138 |
|
|
|
139 |
9c39179e
|
Leszek Koltunski
|
return dialog;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143 |
|
|
|
144 |
179f7189
|
Leszek Koltunski
|
private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
|
145 |
9c39179e
|
Leszek Koltunski
|
{
|
146 |
2c9ab085
|
Leszek Koltunski
|
int numC = updates.getCompletedNumber();
|
147 |
2e48adc9
|
Leszek Koltunski
|
int numS = updates.getStartedNumber();
|
148 |
5f3801d3
|
Leszek Koltunski
|
|
149 |
2e48adc9
|
Leszek Koltunski
|
if( numC+numS<=0 )
|
150 |
9c39179e
|
Leszek Koltunski
|
{
|
151 |
|
|
mText.setText(act.getString(R.string.no_updates));
|
152 |
|
|
}
|
153 |
|
|
else
|
154 |
|
|
{
|
155 |
2c9ab085
|
Leszek Koltunski
|
int textH = (int)(mSize*0.27f);
|
156 |
|
|
int buttH = (int)(mSize*0.35f);
|
157 |
c651024f
|
Leszek Koltunski
|
|
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 |
9c39179e
|
Leszek Koltunski
|
mText.setVisibility(View.GONE);
|
164 |
|
|
|
165 |
|
|
if( mLayout!=null )
|
166 |
|
|
{
|
167 |
2c9ab085
|
Leszek Koltunski
|
for(int i=0; i<numC; i++)
|
168 |
9c39179e
|
Leszek Koltunski
|
{
|
169 |
2c9ab085
|
Leszek Koltunski
|
RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
|
170 |
9c39179e
|
Leszek Koltunski
|
RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
|
171 |
06ba394a
|
Leszek Koltunski
|
View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
|
172 |
9c39179e
|
Leszek Koltunski
|
mLayout.addView(pane);
|
173 |
b88cdd91
|
Leszek Koltunski
|
mPanes.add(rubikView);
|
174 |
9c39179e
|
Leszek Koltunski
|
}
|
175 |
2c9ab085
|
Leszek Koltunski
|
|
176 |
|
|
for(int i=0; i<numS; i++)
|
177 |
9c39179e
|
Leszek Koltunski
|
{
|
178 |
2c9ab085
|
Leszek Koltunski
|
RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
|
179 |
9c39179e
|
Leszek Koltunski
|
RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
|
180 |
06ba394a
|
Leszek Koltunski
|
View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
|
181 |
9c39179e
|
Leszek Koltunski
|
mLayout.addView(pane);
|
182 |
b88cdd91
|
Leszek Koltunski
|
mPanes.add(rubikView);
|
183 |
9c39179e
|
Leszek Koltunski
|
}
|
184 |
ffe7e55d
|
Leszek Koltunski
|
|
185 |
|
|
RubikNetwork network = RubikNetwork.getInstance();
|
186 |
7fe62d1f
|
Leszek Koltunski
|
network.downloadIcons(act,this);
|
187 |
9c39179e
|
Leszek Koltunski
|
}
|
188 |
|
|
else
|
189 |
|
|
{
|
190 |
|
|
android.util.Log.e("D", "mainLayout NULL");
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
5f3801d3
|
Leszek Koltunski
|
}
|
194 |
|
|
|
195 |
179f7189
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
c99db493
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215 |
|
|
|
216 |
|
|
public void errorUpdate()
|
217 |
|
|
{
|
218 |
46be3ddf
|
Leszek Koltunski
|
FragmentActivity act = getActivity();
|
219 |
|
|
if( act!=null ) mText.setText(act.getString(R.string.networkError));
|
220 |
c99db493
|
Leszek Koltunski
|
}
|
221 |
|
|
|
222 |
ffe7e55d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223 |
|
|
|
224 |
7fe62d1f
|
Leszek Koltunski
|
public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
|
225 |
ffe7e55d
|
Leszek Koltunski
|
{
|
226 |
b88cdd91
|
Leszek Koltunski
|
FragmentActivity act = getActivity();
|
227 |
ffe7e55d
|
Leszek Koltunski
|
|
228 |
b88cdd91
|
Leszek Koltunski
|
if( act!=null )
|
229 |
|
|
{
|
230 |
|
|
act.runOnUiThread(new Runnable()
|
231 |
|
|
{
|
232 |
|
|
@Override
|
233 |
|
|
public void run()
|
234 |
|
|
{
|
235 |
|
|
RubikDialogUpdateView view = mPanes.get(ordinal);
|
236 |
7fe62d1f
|
Leszek Koltunski
|
if( view!=null ) view.setIcon(icon,downloaded);
|
237 |
b88cdd91
|
Leszek Koltunski
|
}
|
238 |
|
|
});
|
239 |
|
|
}
|
240 |
ffe7e55d
|
Leszek Koltunski
|
}
|
241 |
|
|
|
242 |
5f3801d3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243 |
|
|
|
244 |
|
|
public static String getDialogTag()
|
245 |
|
|
{
|
246 |
|
|
return "DialogUpdates";
|
247 |
|
|
}
|
248 |
|
|
}
|