Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ dd1a65c1

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.tutorials;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Build;
24
import android.os.Bundle;
25
import android.preference.PreferenceManager;
26
import android.util.DisplayMetrics;
27
import android.view.View;
28
import android.view.ViewGroup;
29
import android.view.WindowManager;
30
import android.webkit.WebView;
31
import android.widget.LinearLayout;
32

    
33
import com.google.firebase.analytics.FirebaseAnalytics;
34

    
35
import org.distorted.library.main.DistortedLibrary;
36

    
37
import org.distorted.objectlib.main.ObjectPreRender;
38
import org.distorted.objectlib.main.ObjectType;
39
import org.distorted.objectlib.main.TwistyObject;
40
import org.distorted.objectlib.helpers.BlockController;
41
import org.distorted.objectlib.helpers.TwistyActivity;
42

    
43
import org.distorted.main.R;
44
import org.distorted.dialogs.RubikDialogError;
45

    
46
import static org.distorted.main.RubikRenderer.BRIGHTNESS;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class TutorialActivity extends TwistyActivity
51
{
52
    private static final String URL = "https://www.youtube.com/embed/";
53

    
54
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
55
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
56

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

    
63
    private FirebaseAnalytics mFirebaseAnalytics;
64
    private static int mScreenWidth, mScreenHeight;
65
    private int mCurrentApiVersion;
66
    private TutorialScreen mState;
67
    private String mURL;
68
    private int mObjectOrdinal;
69
    private TutorialWebView mWebView;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
    @Override
74
    protected void onCreate(Bundle savedState)
75
      {
76
      super.onCreate(savedState);
77
      DistortedLibrary.onCreate(1);
78
      setTheme(R.style.CustomActivityThemeNoActionBar);
79
      setContentView(R.layout.tutorial);
80

    
81
      Bundle b = getIntent().getExtras();
82

    
83
      if(b != null)
84
        {
85
        mURL           = b.getString("url");
86
        mObjectOrdinal = b.getInt("obj");
87
        }
88

    
89
      unlock();
90
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
91

    
92
      DisplayMetrics displaymetrics = new DisplayMetrics();
93
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
94
      mScreenWidth =displaymetrics.widthPixels;
95
      mScreenHeight=displaymetrics.heightPixels;
96

    
97
      hideNavigationBar();
98
      cutoutHack();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
    private void hideNavigationBar()
104
      {
105
      mCurrentApiVersion = Build.VERSION.SDK_INT;
106

    
107
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
108
        {
109
        final View decorView = getWindow().getDecorView();
110

    
111
        decorView.setSystemUiVisibility(FLAGS);
112

    
113
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
114
          {
115
          @Override
116
          public void onSystemUiVisibilityChange(int visibility)
117
            {
118
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
119
              {
120
              decorView.setSystemUiVisibility(FLAGS);
121
              }
122
            }
123
          });
124
        }
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    @Override
130
    public void onAttachedToWindow()
131
      {
132
      super.onAttachedToWindow();
133

    
134
      final float RATIO = 0.15f;
135
      float width = getScreenWidthInPixels();
136

    
137
      TutorialSurfaceView viewL = findViewById(R.id.tutorialSurfaceView);
138
      ViewGroup.LayoutParams paramsL = viewL.getLayoutParams();
139
      paramsL.width = (int)(width*(1.0f-RATIO));
140
      viewL.setLayoutParams(paramsL);
141

    
142
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
143
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
144
      paramsR.width = (int)(width*RATIO);
145
      viewR.setLayoutParams(paramsR);
146

    
147
      final int color = (int)(BRIGHTNESS*255);
148
      viewR.setBackgroundColor( (0xFF<<24)+(color<<16)+(color<<8)+color);
149

    
150
      if( mState==null ) mState = new TutorialScreen();
151

    
152
      mState.createRightPane(this,width);
153

    
154
      WebView videoView = findViewById(R.id.tutorialVideoView);
155
      mWebView = new TutorialWebView(videoView);
156
      mWebView.load(URL+mURL);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// do not avoid cutouts
161

    
162
    private void cutoutHack()
163
      {
164
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
165
        {
166
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
167
        }
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    @Override
173
    public void onWindowFocusChanged(boolean hasFocus)
174
      {
175
      super.onWindowFocusChanged(hasFocus);
176

    
177
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
178
        {
179
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
180
        }
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onPause() 
187
      {
188
      super.onPause();
189
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
190
      view.onPause();
191

    
192
      if( mWebView!=null ) mWebView.onPause();
193

    
194
      DistortedLibrary.onPause(1);
195
      BlockController.onPause();
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    @Override
201
    protected void onResume() 
202
      {
203
      super.onResume();
204
      DistortedLibrary.onResume(1);
205
      BlockController.onResume();
206
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
207
      view.onResume();
208
      view.initialize();
209
      restorePreferences();
210

    
211
      if( mWebView!=null ) mWebView.onResume();
212

    
213
      if( mObjectOrdinal>=0 && mObjectOrdinal< ObjectType.NUM_OBJECTS )
214
        {
215
        ObjectType obj = ObjectType.getObject(mObjectOrdinal);
216
        view.getPreRender().changeObject(obj);
217
        }
218
      }
219
    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
    
222
    @Override
223
    protected void onDestroy() 
224
      {
225
      super.onDestroy();
226
      DistortedLibrary.onDestroy(1);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    private void restorePreferences()
232
      {
233
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
234
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
235
      view.getPreRender().restorePreferences(preferences);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    void OpenGLError()
241
      {
242
      RubikDialogError errDiag = new RubikDialogError();
243
      errDiag.show(getSupportFragmentManager(), null);
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    TutorialScreen getState()
249
      {
250
      return mState;
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
// PUBLIC API
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    public FirebaseAnalytics getAnalytics()
258
      {
259
      return mFirebaseAnalytics;
260
      }
261

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

    
264
    public TwistyObject getObject()
265
      {
266
      TutorialSurfaceView view = findViewById(R.id.rubikSurfaceView);
267
      ObjectPreRender pre = view.getPreRender();
268
      return pre.getObject();
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public int getScreenWidthInPixels()
274
      {
275
      return mScreenWidth;
276
      }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    public int getScreenHeightInPixels()
281
      {
282
      return mScreenHeight;
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    public ObjectPreRender getPreRender()
288
      {
289
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
290
      return view.getPreRender();
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    public boolean isLocked()
296
    {
297
    return retLocked();
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    public static int getDrawableSize()
303
      {
304
      if( mScreenHeight<1000 )
305
        {
306
        return 0;
307
        }
308
      if( mScreenHeight<1600 )
309
        {
310
        return 1;
311
        }
312
      if( mScreenHeight<1900 )
313
        {
314
        return 2;
315
        }
316

    
317
      return 3;
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public static int getDrawable(int small, int medium, int big, int huge)
323
      {
324
      int size = getDrawableSize();
325

    
326
      switch(size)
327
        {
328
        case 0 : return small;
329
        case 1 : return medium;
330
        case 2 : return big;
331
        default: return huge;
332
        }
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    public boolean isVertical()
338
      {
339
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
340
      return view.isVertical();
341
      }
342
}
(1-1/7)