Project

General

Profile

Download (8.68 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / MainSettingsPopup.java @ 9881dc03

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.content.res.Resources;
14
import android.os.Build;
15
import android.util.TypedValue;
16
import android.view.Gravity;
17
import android.view.LayoutInflater;
18
import android.view.View;
19
import android.widget.AdapterView;
20
import android.widget.ArrayAdapter;
21
import android.widget.ListPopupWindow;
22
import android.widget.PopupWindow;
23
import android.widget.Spinner;
24
import android.widget.TextView;
25

    
26
import java.lang.ref.WeakReference;
27
import java.lang.reflect.Field;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class MainSettingsPopup implements AdapterView.OnItemSelectedListener
32
  {
33
  public static final int SORT_CLASSIC    = 0;
34
  public static final int SORT_SHAPE      = 1;
35
  public static final int SORT_DIFFICULTY = 2;
36
  public static final int SORT_AUTHOR     = 3;
37
  public static final int SORT_YEAR       = 4;
38

    
39
  public static final int SORT_DEFAULT    = SORT_SHAPE;
40

    
41
  private static final float MENU_TITLE_SIZE= 0.070f;
42
  private static final float MENU_TEXT_SIZE = 0.060f;
43
  private static final int[] mLocation = new int[2];
44

    
45
  private PopupWindow mPopup;
46
  private WeakReference<MainActivity> mAct;
47
  private int mCurrMethod;
48
  private String[] mSortNames;
49
  private int mCurrTheme;
50
  private String[] mThemeNames;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
// this is supposed to prevent showing the navigational bar when we show the drop down list,
54
// but it doesn't quite work... At least not in the emulator.
55
// see https://gist.github.com/kakajika/a236ba721a5c0ad3c1446e16a7423a63
56
//
57
// EDIT: the bar does not appear on API 34 without the need to call this method. Must have been
58
// fixed by Android developers.
59

    
60
  public static void avoidSpinnerDropdownFocus(Spinner spinner)
61
    {
62
    try
63
      {
64
      Field listPopupField = Spinner.class.getDeclaredField("mPopup");
65
      listPopupField.setAccessible(true);
66
      Object listPopup = listPopupField.get(spinner);
67

    
68
      if( listPopup instanceof ListPopupWindow )
69
        {
70
        Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
71
        popupField.setAccessible(true);
72
        Object popup = popupField.get((ListPopupWindow) listPopup);
73

    
74
        if( popup instanceof PopupWindow )
75
          {
76
          ((PopupWindow) popup).setFocusable(false);
77
          }
78
        }
79
      }
80
    catch (NoSuchFieldException | IllegalAccessException ignored)
81
      {
82

    
83
      }
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  MainSettingsPopup(MainActivity act, int sortMethod, int themeValue, int width, int height)
89
    {
90
    mAct = new WeakReference<>(act);
91

    
92
    // due to bugs in Android API <=25, a Spinner inside a PopupWindow will crash once you click on it.
93
    // solution: on those APIs, use a special Spinner in dialog mode (this does not crash)
94
    int id = android.os.Build.VERSION.SDK_INT <= 25 ? R.layout.settings_popup_android25 : R.layout.settings_popup;
95

    
96
    LayoutInflater layoutInflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
97
    final View layout = layoutInflater.inflate(id, null);
98

    
99
    mPopup = new PopupWindow(act);
100
    mPopup.setContentView(layout);
101
    mPopup.setFocusable(true);
102

    
103
    int titleSize = (int)(MENU_TITLE_SIZE*width);
104
    int textSize  = (int)(MENU_TEXT_SIZE*width);
105

    
106
    TextView title = layout.findViewById(R.id.settingsTitle);
107
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
108
    TextView sortText = layout.findViewById(R.id.sortText);
109
    sortText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
110
    TextView themeTitle = layout.findViewById(R.id.themeText);
111
    themeTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
112

    
113
    Spinner sortSpinner  = layout.findViewById(R.id.sortMethod);
114
    sortSpinner.setOnItemSelectedListener(this);
115

    
116
    mCurrMethod = sortMethod;
117
    buildSortOptions(act);
118

    
119
    ArrayAdapter<String> sortAdapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, mSortNames);
120
    sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
121
    sortSpinner.setAdapter(sortAdapter);
122

    
123
    if( sortMethod>=0 && sortMethod<mSortNames.length ) sortSpinner.setSelection(sortMethod);
124

    
125
    Spinner themeSpinner  = layout.findViewById(R.id.themeValue);
126
    themeSpinner.setOnItemSelectedListener(this);
127

    
128
    mCurrTheme = themeValue;
129
    buildThemeOptions(act);
130

    
131
    ArrayAdapter<String> themeAdapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, mThemeNames);
132
    themeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
133
    themeSpinner.setAdapter(themeAdapter);
134

    
135
    if( themeValue>=0 && themeValue<mThemeNames.length ) themeSpinner.setSelection(themeValue);
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  private void buildSortOptions(MainActivity act)
141
    {
142
    Resources res = act.getResources();
143
    mSortNames = new String[5];
144

    
145
    mSortNames[0] = res.getString(R.string.sort_classic);
146
    mSortNames[1] = res.getString(R.string.sort_shape);
147
    mSortNames[2] = res.getString(R.string.sort_difficulty);
148
    mSortNames[3] = res.getString(R.string.sort_author);
149
    mSortNames[4] = res.getString(R.string.sort_year);
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private void buildThemeOptions(MainActivity act)
155
    {
156
    Resources res = act.getResources();
157
    mThemeNames = new String[3];
158

    
159
    mThemeNames[0] = res.getString(R.string.theme_grey);
160
    mThemeNames[1] = res.getString(R.string.theme_white);
161
    mThemeNames[2] = res.getString(R.string.theme_green);
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
166

    
167
  public void displayPopup(MainActivity act, View view, int w, int h, int xoff, int yoff)
168
    {
169
    View v = mPopup.getContentView();
170
    v.setSystemUiVisibility(MainActivity.FLAGS);
171

    
172
    View topLayout = act.findViewById(R.id.relativeLayout);
173

    
174
    boolean isFullScreen;
175

    
176
    if( topLayout!=null )
177
      {
178
      topLayout.getLocationOnScreen(mLocation);
179
      isFullScreen = (mLocation[1]==0);
180
      }
181
    else
182
      {
183
      isFullScreen = true;
184
      }
185

    
186
    try
187
      {
188
      // if on Android 11 or we are fullscreen
189
      if (Build.VERSION_CODES.R<=Build.VERSION.SDK_INT || isFullScreen )
190
        {
191
        mPopup.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
192
        mPopup.update(view, w, h);
193
        }
194
      else  // Android 10 or below in pop-up mode or split-screen mode
195
        {
196
        view.getLocationOnScreen(mLocation);
197
        int width  = view.getWidth();
198
        int height = view.getHeight();
199
        int x = mLocation[0]+(width-w)/2;
200
        int y = mLocation[1]+height+yoff;
201
        mPopup.showAsDropDown(view);
202
        mPopup.update(x,y,w,h);
203
        }
204
      }
205
    catch( IllegalArgumentException iae )
206
      {
207
      // ignore, this means window is 'not attached to window manager' -
208
      // which most probably is because we are already exiting the app.
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
215
    {
216
    if( parent.getId()==R.id.sortMethod && mCurrMethod!=pos )
217
      {
218
      mPopup.dismiss();
219
      mCurrMethod = pos;
220
      MainActivity act = mAct.get();
221
      act.sortObjectsBy(pos);
222
      }
223
    if( parent.getId()==R.id.themeValue && mCurrTheme!=pos )
224
      {
225
      mPopup.dismiss();
226
      mPopup = null;
227
      mCurrTheme = pos;
228
      MainActivity act = mAct.get();
229
      mAct = null;
230
      act.changeThemeTo(pos);
231
      }
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void onNothingSelected(AdapterView<?> parent)
237
    {
238

    
239
    }
240
  }
241

    
(4-4/4)