Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Revision:

sokoban / distorted-sokoban / src / main / java / org / distorted / sokoban / SokobanActivity.java @ 1f6d1786

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.sokoban;
21

    
22
import android.app.ListActivity;
23
import android.os.Build;
24
import android.os.Bundle;
25
import android.util.DisplayMetrics;
26
import android.util.TypedValue;
27
import android.view.DisplayCutout;
28
import android.view.View;
29
import android.view.ViewGroup;
30
import android.view.WindowManager;
31
import android.widget.AdapterView;
32
import android.widget.LinearLayout;
33
import android.widget.SimpleAdapter;
34
import android.widget.TextView;
35

    
36
import java.util.ArrayList;
37
import java.util.HashMap;
38
import java.util.List;
39
import java.util.Map;
40

    
41
import helpers.TransparentImageButton;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class SokobanActivity extends ListActivity
46
  {
47
  private static final String L_NUMBER = "lNumber";
48
  private static final String L_RECORD = "lRecord";
49
  private static final String L_IMAGE  = "lImage";
50
  private static final String R_NUMBER = "rNumber";
51
  private static final String R_RECORD = "rRecord";
52
  private static final String R_IMAGE  = "rImage";
53

    
54
  private static final float RATIO_BAR    = 0.10f;
55
  private static final float RATIO_INSET  = 0.09f;
56
  private static final float MENU_SIZE    = 0.05f;
57

    
58
  public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
59
                                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
60
                                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
61
                                 | View.SYSTEM_UI_FLAG_FULLSCREEN
62
                                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
63

    
64
  private static int mScreenWidth, mScreenHeight;
65

    
66
  private int mHeightUpperBar;
67
  private int mCurrentApiVersion;
68
  private TransparentImageButton mRecordsButton, mExitButton;
69
  private TextView mAppName;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
   
73
  @Override
74
  public void onCreate(Bundle savedInstanceState) 
75
    {
76
    super.onCreate(savedInstanceState);
77

    
78
    setTheme(R.style.MaterialThemeNoActionBar);
79
    setContentView(R.layout.main);
80

    
81
    DisplayMetrics displaymetrics = new DisplayMetrics();
82
    getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
83
    mScreenWidth =displaymetrics.widthPixels;
84
    mScreenHeight=displaymetrics.heightPixels;
85

    
86
    hideNavigationBar();
87
    cutoutHack();
88

    
89
    LinearLayout upper = findViewById(R.id.upper_layout);
90
    createUpperLayout(upper);
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  @Override
96
  public void onAttachedToWindow()
97
    {
98
    super.onAttachedToWindow();
99

    
100
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
101
      {
102
      DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
103
      int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
104
      LinearLayout layoutHid = findViewById(R.id.hiddenBar);
105
      ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
106
      paramsHid.height = (int)(insetHeight*RATIO_INSET);
107
      layoutHid.setLayoutParams(paramsHid);
108
      mHeightUpperBar += paramsHid.height;
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  @Override
115
  public void onWindowFocusChanged(boolean hasFocus)
116
    {
117
    super.onWindowFocusChanged(hasFocus);
118

    
119
    if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
120
      {
121
      getWindow().getDecorView().setSystemUiVisibility(FLAGS);
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  @Override
128
  public void onResume()
129
    {
130
    super.onResume();
131
    createListOfLevels();
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  @Override
137
  public void onPause()
138
    {
139
    super.onPause();
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private void createUpperLayout(LinearLayout upper)
145
    {
146
    mHeightUpperBar = (int)(mScreenHeight*RATIO_BAR);
147
    ViewGroup.LayoutParams paramsTop = upper.getLayoutParams();
148
    paramsTop.height = mHeightUpperBar;
149
    upper.setLayoutParams(paramsTop);
150

    
151
    setupRecordsButton();
152
    upper.addView(mRecordsButton);
153
    setupAppName();
154
    upper.addView(mAppName);
155
    setupExitButton();
156
    upper.addView(mExitButton);
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  private void setupRecordsButton()
162
    {
163
    final int icon = getDrawable(R.drawable.ui_records_s,R.drawable.ui_records_m, R.drawable.ui_records_b, R.drawable.ui_records_h);
164
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
165
    mRecordsButton = new TransparentImageButton(this, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
166

    
167
    mRecordsButton.setOnClickListener( new View.OnClickListener()
168
      {
169
      @Override
170
      public void onClick(View view)
171
        {
172

    
173
        }
174
      });
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void setupExitButton()
180
    {
181
    final int icon = getDrawable(R.drawable.ui_exit_s,R.drawable.ui_exit_m, R.drawable.ui_exit_b, R.drawable.ui_exit_h);
182
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
183
    mExitButton = new TransparentImageButton(this, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
184

    
185
    mExitButton.setOnClickListener( new View.OnClickListener()
186
      {
187
      @Override
188
      public void onClick(View view)
189
        {
190
        finish();
191
        }
192
      });
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  private void setupAppName()
198
    {
199
    int titleSize = (int)(mScreenWidth*MENU_SIZE);
200
    mAppName = new TextView(this);
201
    mAppName.setText(R.string.app_name);
202
    mAppName.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void hideNavigationBar()
208
    {
209
    mCurrentApiVersion = Build.VERSION.SDK_INT;
210

    
211
    if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
212
      {
213
      final View decorView = getWindow().getDecorView();
214
      decorView.setSystemUiVisibility(FLAGS);
215

    
216
      decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
217
        {
218
        @Override
219
        public void onSystemUiVisibilityChange(int visibility)
220
          {
221
          if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
222
            {
223
            decorView.setSystemUiVisibility(FLAGS);
224
            }
225
          }
226
        });
227
      }
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
// do not avoid cutouts
232

    
233
  private void cutoutHack()
234
    {
235
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
236
      {
237
      getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
238
      }
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public int getHeightUpperBar()
244
    {
245
    return mHeightUpperBar;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public int getScreenWidthInPixels()
251
    {
252
    return mScreenWidth;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public int getScreenHeightInPixels()
258
    {
259
    return mScreenHeight;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  public static int getDrawableSize()
265
    {
266
    if( mScreenHeight<1000 )
267
      {
268
      return 0;
269
      }
270
    if( mScreenHeight<1600 )
271
      {
272
      return 1;
273
      }
274
    if( mScreenHeight<1900 )
275
      {
276
      return 2;
277
      }
278
    return 3;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public static int getDrawable(int small, int medium, int big, int huge)
284
    {
285
    int size = getDrawableSize();
286

    
287
    switch(size)
288
      {
289
      case 0 : return small;
290
      case 1 : return medium;
291
      case 2 : return big;
292
      default: return huge;
293
      }
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private void createListOfLevels()
299
    {
300
    /*
301
    final List<Map<String, Object>> data = new ArrayList<>();
302

    
303
    int index=0;
304

    
305
    for( Application app : Application.values() )
306
      {
307
      final Map<String, Object> item = new HashMap<>();
308
      item.put(ITEM_IMAGE, app.icon);
309
      item.put(ITEM_TITLE, (index+1)+". "+getText(app.title));
310
      item.put(ITEM_SUBTITLE, getText(app.subtitle));
311
      data.add(item);
312
      }
313

    
314
    final SimpleAdapter dataAdapter = new SimpleAdapter( this,
315
                                                         data,
316
                                                         R.layout.toc_item,
317
                                                         new String[] {L_NUMBER, L_RECORD, L_IMAGE, R_NUMBER, R_RECORD, R_IMAGE},
318
                                                         new int[] {R.id.leftNumber , R.id.leftRecord , R.id.leftImage ,
319
                                                                    R.id.rightNumber, R.id.rightRecord, R.id.rightImage,}  );
320
    setListAdapter(dataAdapter);
321

    
322
    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
323
      {
324
      @Override
325
      public void onItemClick(AdapterView<?> parent, View view, int position, long id)
326
        {
327
        android.util.Log.e("D","id="+id+" position="+position+" clicked!!");
328
        }
329
      });
330

    
331
     */
332
    }
333
  }
(1-1/12)