1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 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.main;
|
11
|
|
12
|
import android.content.Context;
|
13
|
import android.os.Build;
|
14
|
import android.util.TypedValue;
|
15
|
import android.view.Gravity;
|
16
|
import android.view.LayoutInflater;
|
17
|
import android.view.View;
|
18
|
import android.widget.AdapterView;
|
19
|
import android.widget.ArrayAdapter;
|
20
|
import android.widget.ListPopupWindow;
|
21
|
import android.widget.PopupWindow;
|
22
|
import android.widget.Spinner;
|
23
|
import android.widget.TextView;
|
24
|
|
25
|
import java.lang.ref.WeakReference;
|
26
|
import java.lang.reflect.Field;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
|
30
|
public class MainSettingsPopup implements AdapterView.OnItemSelectedListener
|
31
|
{
|
32
|
public static final int SORT_CLASSIC = 0;
|
33
|
public static final int SORT_CATEGORY = 1;
|
34
|
public static final int SORT_DIFFICULTY = 2;
|
35
|
public static final int SORT_AUTHOR = 3;
|
36
|
public static final int SORT_YEAR = 4;
|
37
|
|
38
|
private static final String[] mSortNames = { "Classic" , "Category" , "Difficulty", "Author" , "Year" };
|
39
|
private static final float MENU_TITLE_SIZE= 0.070f;
|
40
|
private static final float MENU_TEXT_SIZE = 0.060f;
|
41
|
private static final int[] mLocation = new int[2];
|
42
|
|
43
|
private final PopupWindow mPopup;
|
44
|
private final WeakReference<MainActivity> mAct;
|
45
|
private int mCurrMethod;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
// this is supposed to prevent showing the navigational bar when we show the drop down list,
|
49
|
// but it doesn't quite work... At least not in the emulator.
|
50
|
// see https://gist.github.com/kakajika/a236ba721a5c0ad3c1446e16a7423a63
|
51
|
//
|
52
|
// EDIT: the bar does not appear on API 34 without the need to call this method. Must have been
|
53
|
// fixed by Android developers.
|
54
|
|
55
|
public static void avoidSpinnerDropdownFocus(Spinner spinner)
|
56
|
{
|
57
|
try
|
58
|
{
|
59
|
Field listPopupField = Spinner.class.getDeclaredField("mPopup");
|
60
|
listPopupField.setAccessible(true);
|
61
|
Object listPopup = listPopupField.get(spinner);
|
62
|
|
63
|
if( listPopup instanceof ListPopupWindow )
|
64
|
{
|
65
|
Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
|
66
|
popupField.setAccessible(true);
|
67
|
Object popup = popupField.get((ListPopupWindow) listPopup);
|
68
|
|
69
|
if( popup instanceof PopupWindow )
|
70
|
{
|
71
|
((PopupWindow) popup).setFocusable(false);
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
catch (NoSuchFieldException | IllegalAccessException ignored)
|
76
|
{
|
77
|
|
78
|
}
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
MainSettingsPopup(MainActivity act, int width, int height)
|
84
|
{
|
85
|
mAct = new WeakReference<>(act);
|
86
|
|
87
|
// due to bugs in Android API <=25, a Spinner inside a PopupWindow will crash once you click on it.
|
88
|
// solution: on those APIs, use a special Spinner in dialog mode (this does not crash)
|
89
|
int id = android.os.Build.VERSION.SDK_INT <= 25 ? R.layout.settings_popup_android25 : R.layout.settings_popup;
|
90
|
|
91
|
LayoutInflater layoutInflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
92
|
final View layout = layoutInflater.inflate(id, null);
|
93
|
|
94
|
mPopup = new PopupWindow(act);
|
95
|
mPopup.setContentView(layout);
|
96
|
mPopup.setFocusable(true);
|
97
|
|
98
|
int titleSize = (int)(MENU_TITLE_SIZE*width);
|
99
|
int textSize = (int)(MENU_TEXT_SIZE*width);
|
100
|
|
101
|
TextView title = layout.findViewById(R.id.sortTitle);
|
102
|
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
|
103
|
TextView text = layout.findViewById(R.id.sortText);
|
104
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
105
|
|
106
|
Spinner actSpinner = layout.findViewById(R.id.sortMethod);
|
107
|
actSpinner.setOnItemSelectedListener(this);
|
108
|
|
109
|
mCurrMethod = -1;
|
110
|
|
111
|
ArrayAdapter<String> actAdapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, mSortNames);
|
112
|
actAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
113
|
actSpinner.setAdapter(actAdapter);
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
|
118
|
|
119
|
public void displayPopup(MainActivity act, View view, int w, int h, int xoff, int yoff)
|
120
|
{
|
121
|
View v = mPopup.getContentView();
|
122
|
v.setSystemUiVisibility(MainActivity.FLAGS);
|
123
|
|
124
|
View topLayout = act.findViewById(R.id.relativeLayout);
|
125
|
|
126
|
boolean isFullScreen;
|
127
|
|
128
|
if( topLayout!=null )
|
129
|
{
|
130
|
topLayout.getLocationOnScreen(mLocation);
|
131
|
isFullScreen = (mLocation[1]==0);
|
132
|
}
|
133
|
else
|
134
|
{
|
135
|
isFullScreen = true;
|
136
|
}
|
137
|
|
138
|
try
|
139
|
{
|
140
|
// if on Android 11 or we are fullscreen
|
141
|
if (Build.VERSION_CODES.R<=Build.VERSION.SDK_INT || isFullScreen )
|
142
|
{
|
143
|
mPopup.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
|
144
|
mPopup.update(view, w, h);
|
145
|
}
|
146
|
else // Android 10 or below in pop-up mode or split-screen mode
|
147
|
{
|
148
|
view.getLocationOnScreen(mLocation);
|
149
|
int width = view.getWidth();
|
150
|
int height = view.getHeight();
|
151
|
int x = mLocation[0]+(width-w)/2;
|
152
|
int y = mLocation[1]+height+yoff;
|
153
|
mPopup.showAsDropDown(view);
|
154
|
mPopup.update(x,y,w,h);
|
155
|
}
|
156
|
}
|
157
|
catch( IllegalArgumentException iae )
|
158
|
{
|
159
|
// ignore, this means window is 'not attached to window manager' -
|
160
|
// which most probably is because we are already exiting the app.
|
161
|
}
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
|
166
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
167
|
{
|
168
|
if( parent.getId()==R.id.sortMethod && mCurrMethod!=pos )
|
169
|
{
|
170
|
mCurrMethod = pos;
|
171
|
MainActivity act = mAct.get();
|
172
|
act.sortObjectsBy(pos);
|
173
|
}
|
174
|
}
|
175
|
|
176
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177
|
|
178
|
public void onNothingSelected(AdapterView<?> parent)
|
179
|
{
|
180
|
|
181
|
}
|
182
|
}
|
183
|
|