Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 2876aeb6

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.purchase.PurchaseActivity;
37

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
46
    private static final int ACTIVITY_NUMBER = 1;
47
    public static final float BAR_RATIO = 0.17f;
48

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

    
55
    private static int mScreenWidth, mScreenHeight;
56
    private int mCurrentApiVersion;
57
    private TutorialScreen mScreen;
58
    private String mURL;
59
    private int mObjectOrdinal;
60
    private TutorialWebView mWebView;
61
    private boolean mIsFree;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
73
      mIsFree = true;
74

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

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

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

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

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

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

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

    
104
        decorView.setSystemUiVisibility(FLAGS);
105

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// do not avoid cutouts
137

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

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

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

    
170
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
171
      }
172

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

    
183
      float width = getScreenWidthInPixels();
184

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

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

    
192
      mScreen.createRightPane(this);
193

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

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

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

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

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// PUBLIC API
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    public int getScreenWidthInPixels()
245
      {
246
      return mScreenWidth;
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    public int getScreenHeightInPixels()
252
      {
253
      return mScreenHeight;
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
    public boolean getIsFree()
259
      {
260
      return mIsFree;
261
      }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
    public ObjectControl getControl()
266
      {
267
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
268
      return view.getObjectControl();
269
      }
270

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

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public static int getDrawableSize()
283
      {
284
      if( mScreenHeight<1000 ) return 0;
285
      if( mScreenHeight<1600 ) return 1;
286
      if( mScreenHeight<1900 ) return 2;
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)