Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 1ba56d95

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

    
12
import java.io.InputStream;
13

    
14
import android.os.Build;
15
import android.os.Bundle;
16
import android.util.DisplayMetrics;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.view.WindowManager;
20
import android.webkit.WebView;
21
import android.widget.LinearLayout;
22

    
23
import androidx.appcompat.app.AppCompatActivity;
24

    
25
import com.google.firebase.analytics.FirebaseAnalytics;
26

    
27
import org.distorted.library.main.DistortedLibrary;
28

    
29
import org.distorted.objectlib.main.ObjectControl;
30
import org.distorted.objectlib.main.TwistyObject;
31

    
32
import org.distorted.main.R;
33
import org.distorted.dialogs.RubikDialogError;
34
import org.distorted.objects.RubikObject;
35
import org.distorted.objects.RubikObjectList;
36

    
37
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class TutorialActivity extends AppCompatActivity
42
{
43
    private static final String URL = "https://www.youtube.com/embed/";
44

    
45
    private static final int ACTIVITY_NUMBER = 1;
46
    public static final float BAR_RATIO = 0.17f;
47
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
48
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
49

    
50
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
51
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
52
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
53
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
54
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
55

    
56
    private FirebaseAnalytics mFirebaseAnalytics;
57
    private static int mScreenWidth, mScreenHeight;
58
    private int mCurrentApiVersion;
59
    private TutorialScreen mScreen;
60
    private String mURL;
61
    private int mObjectOrdinal;
62
    private TutorialWebView mWebView;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    @Override
67
    protected void onCreate(Bundle savedState)
68
      {
69
      super.onCreate(savedState);
70
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
71
      setTheme(R.style.MaterialThemeNoActionBar);
72
      setContentView(R.layout.tutorial);
73

    
74
      Bundle b = getIntent().getExtras();
75

    
76
      if(b != null)
77
        {
78
        mURL           = b.getString("url");
79
        mObjectOrdinal = b.getInt("obj");
80
        }
81

    
82
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
83

    
84
      DisplayMetrics displaymetrics = new DisplayMetrics();
85
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
86
      mScreenWidth =displaymetrics.widthPixels;
87
      mScreenHeight=displaymetrics.heightPixels;
88

    
89
      hideNavigationBar();
90
      cutoutHack();
91
      }
92

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

    
95
    private void hideNavigationBar()
96
      {
97
      mCurrentApiVersion = Build.VERSION.SDK_INT;
98

    
99
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
100
        {
101
        final View decorView = getWindow().getDecorView();
102

    
103
        decorView.setSystemUiVisibility(FLAGS);
104

    
105
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
106
          {
107
          @Override
108
          public void onSystemUiVisibilityChange(int visibility)
109
            {
110
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
111
              {
112
              decorView.setSystemUiVisibility(FLAGS);
113
              }
114
            }
115
          });
116
        }
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    @Override
122
    public void onAttachedToWindow()
123
      {
124
      super.onAttachedToWindow();
125

    
126
      if( mWebView==null )
127
        {
128
        WebView videoView = findViewById(R.id.tutorialVideoView);
129
        mWebView = new TutorialWebView(videoView);
130
        mWebView.load(URL+mURL);
131
        }
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// do not avoid cutouts
136

    
137
    private void cutoutHack()
138
      {
139
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
140
        {
141
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
142
        }
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    @Override
148
    public void onWindowFocusChanged(boolean hasFocus)
149
      {
150
      super.onWindowFocusChanged(hasFocus);
151

    
152
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
153
        {
154
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
155
        }
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    @Override
161
    protected void onPause() 
162
      {
163
      super.onPause();
164
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
165
      view.onPause();
166

    
167
      if( mWebView!=null ) mWebView.onPause();
168

    
169
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
    
174
    @Override
175
    protected void onResume() 
176
      {
177
      super.onResume();
178
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
179
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
180
      view.onResume();
181

    
182
      float width = getScreenWidthInPixels();
183

    
184
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
185
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
186
      paramsR.width = (int)(width*BAR_RATIO);
187
      viewR.setLayoutParams(paramsR);
188

    
189
      if( mScreen==null ) mScreen = new TutorialScreen();
190

    
191
      mScreen.createRightPane(this);
192

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

    
195
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
196
        {
197
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
    
203
    @Override
204
    protected void onDestroy() 
205
      {
206
      super.onDestroy();
207
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    void OpenGLError()
213
      {
214
      RubikDialogError errDiag = new RubikDialogError();
215
      errDiag.show(getSupportFragmentManager(), null);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    private void changeIfDifferent(int ordinal,ObjectControl control)
221
      {
222
      RubikObject object = RubikObjectList.getObject(ordinal);
223
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
224
      int iconMode  = TwistyObject.MODE_NORM;
225
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
226
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
227
      String name = object==null ? "NULL" : object.getUpperName();
228

    
229
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    TutorialScreen getState()
235
      {
236
      return mScreen;
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
// PUBLIC API
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
    public FirebaseAnalytics getAnalytics()
244
      {
245
      return mFirebaseAnalytics;
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 ObjectControl getControl()
265
      {
266
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
267
      return view.getObjectControl();
268
      }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
    public static int getDrawableSize()
273
      {
274
      if( mScreenHeight<1000 )
275
        {
276
        return 0;
277
        }
278
      if( mScreenHeight<1600 )
279
        {
280
        return 1;
281
        }
282
      if( mScreenHeight<1900 )
283
        {
284
        return 2;
285
        }
286

    
287
      return 3;
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public static int getDrawable(int small, int medium, int big, int huge)
293
      {
294
      int size = getDrawableSize();
295

    
296
      switch(size)
297
        {
298
        case 0 : return small;
299
        case 1 : return medium;
300
        case 2 : return big;
301
        default: return huge;
302
        }
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public boolean isVertical()
308
      {
309
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
310
      return view.isVertical();
311
      }
312
}
(1-1/6)