Project

General

Profile

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

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

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.main.RubikSurfaceView;
38
import org.distorted.network.RubikScores;
39
import org.distorted.objectlib.effects.BaseEffect;
40
import org.distorted.objectlib.main.ObjectPreRender;
41
import org.distorted.objectlib.main.ObjectType;
42
import org.distorted.objectlib.main.TwistyObject;
43
import org.distorted.objectlib.helpers.BlockController;
44
import org.distorted.objectlib.helpers.TwistyActivity;
45

    
46
import org.distorted.main.R;
47
import org.distorted.dialogs.RubikDialogError;
48
import org.distorted.screens.ScreenList;
49

    
50
import static org.distorted.main.RubikRenderer.BRIGHTNESS;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
public class TutorialActivity extends TwistyActivity
55
{
56
    private static final String URL = "https://www.youtube.com/embed/";
57

    
58
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
59
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
60

    
61
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
62
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
63
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
64
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
65
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
66

    
67
    private FirebaseAnalytics mFirebaseAnalytics;
68
    private static int mScreenWidth, mScreenHeight;
69
    private int mCurrentApiVersion;
70
    private TutorialState mState;
71
    private String mURL;
72
    private int mObjectOrdinal;
73
    private TutorialWebView mWebView;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

    
85
      Bundle b = getIntent().getExtras();
86

    
87
      if(b != null)
88
        {
89
        mURL           = b.getString("url");
90
        mObjectOrdinal = b.getInt("obj");
91
        }
92

    
93
      unlock();
94
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
95

    
96
      DisplayMetrics displaymetrics = new DisplayMetrics();
97
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
98
      mScreenWidth =displaymetrics.widthPixels;
99
      mScreenHeight=displaymetrics.heightPixels;
100

    
101
      hideNavigationBar();
102
      cutoutHack();
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
    private void hideNavigationBar()
108
      {
109
      mCurrentApiVersion = Build.VERSION.SDK_INT;
110

    
111
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
112
        {
113
        final View decorView = getWindow().getDecorView();
114

    
115
        decorView.setSystemUiVisibility(FLAGS);
116

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    @Override
134
    public void onAttachedToWindow()
135
      {
136
      super.onAttachedToWindow();
137

    
138
      final float RATIO = 0.15f;
139
      float width = getScreenWidthInPixels();
140

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

    
146
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
147
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
148
      paramsR.width = (int)(width*RATIO);
149
      viewR.setLayoutParams(paramsR);
150

    
151
      final int color = (int)(BRIGHTNESS*255);
152
      viewR.setBackgroundColor( (0xFF<<24)+(color<<16)+(color<<8)+color);
153

    
154
      if( mState==null ) mState = new TutorialState();
155

    
156
      mState.createRightPane(this,width);
157

    
158
      WebView videoView = findViewById(R.id.tutorialVideoView);
159
      mWebView = new TutorialWebView(videoView);
160
      mWebView.load(URL+mURL);
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// do not avoid cutouts
165

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    @Override
177
    public void onWindowFocusChanged(boolean hasFocus)
178
      {
179
      super.onWindowFocusChanged(hasFocus);
180

    
181
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
182
        {
183
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
184
        }
185
      }
186

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

    
196
      if( mWebView!=null ) mWebView.onPause();
197

    
198
      DistortedLibrary.onPause(1);
199
      BlockController.onPause();
200
      }
201

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

    
215
      if( mWebView!=null ) mWebView.onResume();
216

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    private void restorePreferences()
236
      {
237
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
238
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
239
      view.getPreRender().restorePreferences(preferences);
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    void OpenGLError()
245
      {
246
      RubikDialogError errDiag = new RubikDialogError();
247
      errDiag.show(getSupportFragmentManager(), null);
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    TutorialState getState()
253
      {
254
      return mState;
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
// PUBLIC API
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public FirebaseAnalytics getAnalytics()
262
      {
263
      return mFirebaseAnalytics;
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    public TwistyObject getObject()
269
      {
270
      TutorialSurfaceView view = findViewById(R.id.rubikSurfaceView);
271
      ObjectPreRender pre = view.getPreRender();
272
      return pre.getObject();
273
      }
274

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

    
277
    public int getScreenWidthInPixels()
278
      {
279
      return mScreenWidth;
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public int getScreenHeightInPixels()
285
      {
286
      return mScreenHeight;
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    public ObjectPreRender getPreRender()
292
      {
293
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
294
      return view.getPreRender();
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public boolean isLocked()
300
    {
301
    return retLocked();
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    public static int getDrawableSize()
307
      {
308
      if( mScreenHeight<1000 )
309
        {
310
        return 0;
311
        }
312
      if( mScreenHeight<1600 )
313
        {
314
        return 1;
315
        }
316
      if( mScreenHeight<1900 )
317
        {
318
        return 2;
319
        }
320

    
321
      return 3;
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    public static int getDrawable(int small, int medium, int big, int huge)
327
      {
328
      int size = getDrawableSize();
329

    
330
      switch(size)
331
        {
332
        case 0 : return small;
333
        case 1 : return medium;
334
        case 2 : return big;
335
        default: return huge;
336
        }
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    public boolean isVertical()
342
      {
343
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
344
      return view.isVertical();
345
      }
346
}
(1-1/7)