Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 05a244ad

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.library.main.DistortedScreen;
38
import org.distorted.objectlib.main.ObjectPreRender;
39
import org.distorted.objectlib.main.ObjectType;
40
import org.distorted.objectlib.main.TwistyObject;
41
import org.distorted.objectlib.helpers.BlockController;
42
import org.distorted.objectlib.helpers.TwistyActivity;
43

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
55
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
56
    public static final float MENU_BIG_TEXT_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 FirebaseAnalytics mFirebaseAnalytics;
65
    private static int mScreenWidth, mScreenHeight;
66
    private int mCurrentApiVersion;
67
    private TutorialScreen mState;
68
    private String mURL;
69
    private int mObjectOrdinal;
70
    private TutorialWebView mWebView;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

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

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

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

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

    
112
        decorView.setSystemUiVisibility(FLAGS);
113

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

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

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

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

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

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

    
153
      mState.createRightPane(this,width);
154

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

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// do not avoid cutouts
162

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

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

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

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

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

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

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    void OpenGLError()
231
      {
232
      RubikDialogError errDiag = new RubikDialogError();
233
      errDiag.show(getSupportFragmentManager(), null);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    TutorialScreen getState()
239
      {
240
      return mState;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public FirebaseAnalytics getAnalytics()
248
      {
249
      return mFirebaseAnalytics;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public TwistyObject getObject()
255
      {
256
      TutorialSurfaceView view = findViewById(R.id.rubikSurfaceView);
257
      ObjectPreRender pre = view.getPreRender();
258
      return pre.getObject();
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public int getScreenWidthInPixels()
264
      {
265
      return mScreenWidth;
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public int getScreenHeightInPixels()
271
      {
272
      return mScreenHeight;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public ObjectPreRender getPreRender()
278
      {
279
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
280
      return view.getPreRender();
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    public DistortedScreen getScreen()
286
      {
287
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
288
      TutorialRenderer renderer = view.getRenderer();
289
      return renderer.getScreen();
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

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

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
316
      return 3;
317
      }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

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

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

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