/
RouletteFragment.java
415 lines (379 loc) 路 17.3 KB
/
RouletteFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.minar.randomix.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.SwitchCompat;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.minar.randomix.R;
import com.minar.randomix.activities.MainActivity;
import com.minar.randomix.utilities.Constants;
import com.minar.randomix.utilities.ShakeEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class RouletteFragment extends androidx.fragment.app.Fragment implements OnClickListener, View.OnLongClickListener, TextView.OnEditorActionListener {
private MainActivity act;
// Shake variables
private boolean shakeEnabled = false;
private SensorManager sensorManager;
private ShakeEventListener sensorListener;
private final List<String> options = new ArrayList<>();
private final RouletteBottomSheet bottomSheet = new RouletteBottomSheet(this);
private boolean inRangeMode = false;
private EditText optionText;
private EditText rangeMin;
private EditText rangeMax;
private TextView result;
private SharedPreferences sp = null;
@Override
public void onResume() {
super.onResume();
if (shakeEnabled) {
sensorManager.registerListener(sensorListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
}
@Override
public void onPause() {
if (shakeEnabled)
sensorManager.unregisterListener(sensorListener);
super.onPause();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_roulette, container, false);
sp = PreferenceManager.getDefaultSharedPreferences(getContext());
// Hide description if needed
if (sp.getBoolean("hide_descriptions", false))
v.findViewById(R.id.descriptionRoulette).setVisibility(View.GONE);
// Set the listeners and the views
ImageView insert = v.findViewById(R.id.insertButton);
ImageView recent = v.findViewById(R.id.recentButton);
ImageView spin = v.findViewById(R.id.buttonSpinRoulette);
optionText = v.findViewById(R.id.entryRoulette);
rangeMin = v.findViewById(R.id.rangeMinRoulette);
rangeMax = v.findViewById(R.id.rangeMaxRoulette);
result = v.findViewById(R.id.resultRoulette);
SwitchCompat rangeSwitch = v.findViewById(R.id.rangeSwitchRoulette);
ChipGroup chipList = v.findViewById(R.id.rouletteChipList);
ImageView range = v.findViewById(R.id.animatedRangeRoulette);
LinearLayout rangeArea = v.findViewById(R.id.rangeOptionRoulette);
LinearLayout standardArea = v.findViewById(R.id.textOptionRoulette);
// Animate the avd, even if it's initially gone
Drawable animatedRange = range.getDrawable();
if (animatedRange instanceof Animatable2) {
((Animatable2) animatedRange).registerAnimationCallback(new Animatable2.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
((Animatable2) animatedRange).start();
}
});
((Animatable2) animatedRange).start();
}
insert.setOnClickListener(this);
recent.setOnClickListener(this);
recent.setOnLongClickListener(this);
spin.setOnClickListener(this);
spin.setOnLongClickListener(this);
optionText.setOnEditorActionListener(this);
rangeSwitch.setOnCheckedChangeListener((compoundButton, checked) -> {
if (checked) {
result.setTextSize(62);
rangeArea.setVisibility(View.VISIBLE);
standardArea.setVisibility(View.GONE);
chipList.setVisibility(View.INVISIBLE);
inRangeMode = true;
} else {
result.setTextSize(36);
rangeArea.setVisibility(View.GONE);
standardArea.setVisibility(View.VISIBLE);
chipList.setVisibility(View.VISIBLE);
inRangeMode = false;
}
});
// Manage the shake to launch option and bind the activity
act = (MainActivity) getActivity();
if (act != null) {
shakeEnabled = act.shakeAllowed();
if (shakeEnabled) {
sensorManager = (SensorManager) act.getSystemService(Context.SENSOR_SERVICE);
sensorListener = new ShakeEventListener();
sensorListener.setOnShakeListener(this::mainThrow);
}
}
return v;
}
@Override
public boolean onLongClick(View v) {
Activity act = getActivity();
// Vibrate using the common method in MainActivity
if (act instanceof MainActivity) ((MainActivity) act).vibrate();
int pressedId = v.getId();
if (pressedId == R.id.recentButton) {
// Restore the last used options
bottomSheet.restoreLatest(getContext());
return true;
}
if (pressedId == R.id.buttonSpinRoulette) {
if (!inRangeMode) {
// Insert three quick options, or clear them
if (options.isEmpty()) {
String option1 = getResources().getString(R.string.generic_option) + "1";
String option2 = getResources().getString(R.string.generic_option) + "2";
String option3 = getResources().getString(R.string.generic_option) + "3";
insertRouletteChip(option1, true);
insertRouletteChip(option2, true);
insertRouletteChip(option3, true);
} else {
// Ask for confirmation and eventually remove every chip
new MaterialAlertDialogBuilder(requireContext())
// Use strings from the Android sources
.setTitle(getString(android.R.string.dialog_alert_title))
.setMessage(getString(R.string.delete_all_roulette_confirmation))
.setPositiveButton(getString(android.R.string.ok), (dialogInterface, i) -> removeAllChips())
.setNegativeButton(getString(android.R.string.cancel), (dialogInterface, i) -> {
})
.show();
}
}
// Populate the range text fields
else {
if (rangeMin.getText().length() != 0 || rangeMax.getText().length() != 0) {
rangeMin.setText("");
rangeMax.setText("");
} else {
rangeMin.setText(String.valueOf(1));
rangeMax.setText(String.valueOf(10));
}
}
return true;
}
return true;
}
@Override
public void onClick(View v) {
Activity act = getActivity();
final View view = requireView();
final ImageView recentAnimation = view.findViewById(R.id.recentButton);
int pressedId = v.getId();
// Open recent choices
if (pressedId == R.id.recentButton) {
// Start the animated vector drawable
Drawable recent = recentAnimation.getDrawable();
if (recent instanceof Animatable) ((Animatable) recent).start();
// Vibrate using the common method in MainActivity
if (act instanceof MainActivity) ((MainActivity) act).vibrate();
// Open a dialog with the recent searches
if (bottomSheet.isAdded()) return;
bottomSheet.show(getChildFragmentManager(), "roulette_bottom_sheet");
return;
}
// Insert entry in roulette
if (pressedId == R.id.insertButton) {
// Start the animated vector drawable
ImageView insertAnimation = requireView().findViewById(R.id.insertButton);
Drawable insert = insertAnimation.getDrawable();
if (insert instanceof Animatable) ((Animatable) insert).start();
// Vibrate and play sound using the common method in MainActivity
if (act instanceof MainActivity) {
((MainActivity) act).vibrate();
((MainActivity) act).playSound(1);
}
// Insert in both the list and the layout
insertRouletteChip("", true);
return;
}
// Spin the roulette
if (pressedId == R.id.buttonSpinRoulette) {
mainThrow();
}
}
private void mainThrow() {
final ImageView spinAnimation = requireView().findViewById(R.id.buttonSpinRoulette);
final ChipGroup optionsList = requireView().findViewById(R.id.rouletteChipList);
final SwitchCompat rangeSwitch = requireView().findViewById(R.id.rangeSwitchRoulette);
final ImageView recentAnimation = requireView().findViewById(R.id.recentButton);
int minValue = -1, maxValue = -1;
if (!inRangeMode) {
// Send a toast if the list is empty to avoid crashes and null pointers
if (options.size() < 2) {
Toast.makeText(getContext(), getString(R.string.no_entry_roulette), Toast.LENGTH_SHORT).show();
return;
}
} else {
// Send a toast if the ranges are empty or have wrong values
try {
minValue = Integer.parseInt(rangeMin.getText().toString());
maxValue = Integer.parseInt(rangeMax.getText().toString());
} catch (Exception ignored) {
}
if (minValue == -1 || maxValue == -1 || minValue >= maxValue) {
Toast.makeText(getContext(), getString(R.string.wrong_range_roulette), Toast.LENGTH_SHORT).show();
return;
}
}
// Make the button un-clickable and the device un-shakeable
if (shakeEnabled) sensorManager.unregisterListener(sensorListener);
recentAnimation.setClickable(false);
recentAnimation.setLongClickable(false);
spinAnimation.setClickable(false);
spinAnimation.setLongClickable(false);
rangeSwitch.setClickable(false);
final int childCount = optionsList.getChildCount();
for (int i = 0; i < childCount; i++) {
Chip option = (Chip) optionsList.getChildAt(i);
option.setClickable(false);
}
Drawable spin = spinAnimation.getDrawable();
if (spin instanceof Animatable) ((Animatable) spin).start();
// Vibrate and play sound using the common method in MainActivity
if (act != null) {
act.vibrate();
act.playSound(1);
}
int n;
if (inRangeMode) {
// Best way to generate number in range
n = ThreadLocalRandom.current().nextInt(minValue, maxValue + 1);
} else {
Random ran = new Random();
n = ran.nextInt(options.size());
// Insert in the recent list
bottomSheet.updateRecent(options, getContext());
}
// Create the animations
final Animation animIn = new AlphaAnimation(1.0f, 0.0f);
animIn.setDuration(1500);
result.startAnimation(animIn);
final Animation animOut = new AlphaAnimation(0.0f, 1.0f);
animOut.setDuration(1000);
requireView().postDelayed(() -> {
if (shakeEnabled) sensorManager.registerListener(sensorListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
if (inRangeMode) result.setText(String.valueOf(n));
else result.setText(options.get(n));
result.startAnimation(animOut);
spinAnimation.setClickable(true);
spinAnimation.setLongClickable(true);
recentAnimation.setClickable(true);
recentAnimation.setLongClickable(true);
rangeSwitch.setClickable(true);
for (int i = 0; i < childCount; i++) {
Chip option = (Chip) optionsList.getChildAt(i);
option.setClickable(true);
}
// Remove the selected option from the list, if the option is enabled
boolean removeLast = sp.getBoolean("remove_last", false);
if (removeLast) removeChip((Chip) optionsList.getChildAt(n));
}, 1500);
}
// Handle the keyboard actions, like enter, done, send and so on.
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_SEND) {
// Start the animated vector drawable
ImageView insertAnimation = requireView().findViewById(R.id.insertButton);
Drawable insert = insertAnimation.getDrawable();
if (insert instanceof Animatable) ((Animatable) insert).start();
// Insert in both the list and the layout
insertRouletteChip("", true);
return true;
}
return false;
}
// Insert a chip in the roulette (15 chips limit)
private void insertRouletteChip(String option, boolean limitNumber) {
String currentOption;
boolean allowEquals = sp.getBoolean("allow_equals", false);
if (!option.equals("")) currentOption = option;
else {
currentOption = optionText.getText().toString().trim();
currentOption = currentOption.replaceAll("\\s+", " ");
// Return if the string entered is a duplicate, reset the text field
if ((!allowEquals && options.contains(currentOption)) || currentOption.equals(""))
return;
optionText.setText("");
}
final ChipGroup optionsList = requireView().findViewById(R.id.rouletteChipList);
// Check if the limit is reached
if (options.size() > 29 && limitNumber) {
Toast.makeText(getContext(), getString(R.string.too_much_entries_roulette), Toast.LENGTH_SHORT).show();
return;
}
// Add to the list
options.add(currentOption);
// Inflate the layout and its onclick action
LayoutInflater inflater = LayoutInflater.from(getActivity());
final Chip chip = (Chip) inflater.inflate(R.layout.custom_chip, optionsList, false);
chip.setText(currentOption);
chip.setId(options.size());
// Add the chip with an animation
optionsList.addView(chip);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.chip_enter_anim);
chip.startAnimation(animation);
// Remove the chip and the element from the list
chip.setOnClickListener(view -> removeChip(chip));
}
// Remove a single chip
private void removeChip(final Chip chip) {
final ChipGroup optionsList = requireView().findViewById(R.id.rouletteChipList);
final ImageView spinAnimation = requireView().findViewById(R.id.buttonSpinRoulette);
// Remove the chip with an animation
if (chip == null) return;
options.remove(chip.getText().toString());
spinAnimation.setClickable(false);
spinAnimation.setLongClickable(false);
final Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.chip_exit_anim);
chip.startAnimation(animation);
chip.postDelayed(() -> {
optionsList.removeView(chip);
spinAnimation.setClickable(true);
spinAnimation.setLongClickable(true);
}, 400);
}
// Remove every chip in the list
private void removeAllChips() {
final ChipGroup optionsList = requireView().findViewById(R.id.rouletteChipList);
final int childCount = optionsList.getChildCount();
for (int i = 0; i < childCount; i++) {
Chip chip = (Chip) optionsList.getChildAt(i);
removeChip(chip);
}
}
// Insert a certain combination in the roulette
void restoreOption(List<String> option) {
removeAllChips();
for (String item : option) {
// It's awful. I know.
if (item.equals(Constants.PIN_WORKAROUND_ENTRY)) continue;
insertRouletteChip(item, false);
}
}
}