Project

General

Profile

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

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

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.content.Intent;
15
import android.os.Build;
16
import android.os.Bundle;
17
import android.util.DisplayMetrics;
18
import android.view.View;
19
import android.view.ViewGroup;
20
import android.view.WindowManager;
21
import android.webkit.WebView;
22
import android.widget.LinearLayout;
23

    
24
import androidx.appcompat.app.AppCompatActivity;
25

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

    
28
import org.distorted.objectlib.main.InitAssets;
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
import org.distorted.os.OSInterface;
37
import org.distorted.purchase.PurchaseActivity;
38

    
39
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
47
    private static final int ACTIVITY_NUMBER = 1;
48
    public static final float BAR_RATIO = 0.17f;
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 static int mScreenWidth, mScreenHeight;
57
    private int mCurrentApiVersion;
58
    private TutorialScreen mScreen;
59
    private String mURL;
60
    private int mObjectOrdinal;
61
    private TutorialWebView mWebView;
62
    private boolean mIsFree;
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
      mIsFree = true;
75

    
76
      Bundle b = getIntent().getExtras();
77

    
78
      if(b != null)
79
        {
80
        mURL           = b.getString("url");
81
        mObjectOrdinal = b.getInt("obj");
82
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
83
        mIsFree = (obj!=null && obj.isFree());
84
        }
85

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

    
91
      hideNavigationBar();
92
      cutoutHack();
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
    private void hideNavigationBar()
98
      {
99
      mCurrentApiVersion = Build.VERSION.SDK_INT;
100

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

    
105
        decorView.setSystemUiVisibility(FLAGS);
106

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    @Override
124
    public void onAttachedToWindow()
125
      {
126
      super.onAttachedToWindow();
127

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// do not avoid cutouts
138

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

    
169
      if( mWebView!=null ) mWebView.onPause();
170

    
171
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
172
      }
173

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

    
184
      float width = getScreenWidthInPixels();
185

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

    
191
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
192

    
193
      mScreen.createRightPane(this);
194

    
195
      if( mWebView!=null ) mWebView.onResume();
196

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

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
    private void changeIfDifferent(int ordinal,ObjectControl control)
223
      {
224
      RubikObject object = RubikObjectList.getObject(ordinal);
225
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
226
      int iconMode  = TwistyObject.MODE_NORM;
227
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
228
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
229
      String name = object==null ? "NULL" : object.getUpperName();
230
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
231
      OSInterface os = view.getInterface();
232
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
233
      control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
234
      }
235

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

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

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

    
247
    public int getScreenWidthInPixels()
248
      {
249
      return mScreenWidth;
250
      }
251

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

    
254
    public int getScreenHeightInPixels()
255
      {
256
      return mScreenHeight;
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public boolean getIsFree()
262
      {
263
      return mIsFree;
264
      }
265

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

    
268
    public ObjectControl getControl()
269
      {
270
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
271
      return view.getObjectControl();
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public void switchToPurchase()
277
      {
278
      Intent intent = new Intent(this, PurchaseActivity.class);
279
      intent.putExtra("obj", mObjectOrdinal);
280
      startActivity(intent);
281
      }
282

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

    
285
    public static int getDrawableSize()
286
      {
287
      if( mScreenHeight<1000 ) return 0;
288
      if( mScreenHeight<1600 ) return 1;
289
      if( mScreenHeight<1900 ) return 2;
290
      return 3;
291
      }
292

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

    
295
    public static int getDrawable(int small, int medium, int big, int huge)
296
      {
297
      int size = getDrawableSize();
298

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

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

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