Project

General

Profile

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

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

1 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 bb62ca3f Leszek Koltunski
// 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 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 eaf87d1d Leszek Koltunski
package org.distorted.tutorials;
11 af88bf2e Leszek Koltunski
12 b20e89d2 Leszek Koltunski
import java.io.InputStream;
13
14 18d47965 Leszek Koltunski
import android.content.Intent;
15 af88bf2e Leszek Koltunski
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 e019c70b Leszek Koltunski
import androidx.appcompat.app.AppCompatActivity;
25
26 3f7a4363 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
27 2afc6754 Leszek Koltunski
28 50e6c5d6 Leszek Koltunski
import org.distorted.objectlib.main.InitAssets;
29 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
30 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
31 eaf46415 Leszek Koltunski
32 af88bf2e Leszek Koltunski
import org.distorted.main.R;
33 eaf46415 Leszek Koltunski
import org.distorted.dialogs.RubikDialogError;
34 09cf2a36 Leszek Koltunski
import org.distorted.objects.RubikObject;
35 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObjectList;
36 da57afae Leszek Koltunski
import org.distorted.os.OSInterface;
37 18d47965 Leszek Koltunski
import org.distorted.purchase.PurchaseActivity;
38 af88bf2e Leszek Koltunski
39 09cf2a36 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
40
41 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 e019c70b Leszek Koltunski
public class TutorialActivity extends AppCompatActivity
44 af88bf2e Leszek Koltunski
{
45 2971588c Leszek Koltunski
    private static final String URL = "https://www.youtube.com/embed/";
46
47 1237d25d Leszek Koltunski
    private static final int ACTIVITY_NUMBER = 1;
48 943471aa Leszek Koltunski
    public static final float BAR_RATIO = 0.17f;
49 af88bf2e Leszek Koltunski
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 1237d25d Leszek Koltunski
    private TutorialScreen mScreen;
59 2971588c Leszek Koltunski
    private String mURL;
60 7ac0ee88 Leszek Koltunski
    private int mObjectOrdinal;
61 5b4eaf7e Leszek Koltunski
    private TutorialWebView mWebView;
62 ea036986 Leszek Koltunski
    private boolean mIsFree;
63 af88bf2e Leszek Koltunski
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
    @Override
67
    protected void onCreate(Bundle savedState)
68
      {
69
      super.onCreate(savedState);
70 1237d25d Leszek Koltunski
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
71 dd874ae8 Leszek Koltunski
      setTheme(R.style.MaterialThemeNoActionBar);
72 af88bf2e Leszek Koltunski
      setContentView(R.layout.tutorial);
73
74 ea036986 Leszek Koltunski
      mIsFree = true;
75
76 2971588c Leszek Koltunski
      Bundle b = getIntent().getExtras();
77
78 79bf5d8b Leszek Koltunski
      if(b != null)
79
        {
80
        mURL           = b.getString("url");
81
        mObjectOrdinal = b.getInt("obj");
82 ea036986 Leszek Koltunski
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
83
        mIsFree = (obj!=null && obj.isFree());
84 79bf5d8b Leszek Koltunski
        }
85 2971588c Leszek Koltunski
86 af88bf2e Leszek Koltunski
      DisplayMetrics displaymetrics = new DisplayMetrics();
87 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
88 af88bf2e Leszek Koltunski
      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 280dc794 Leszek Koltunski
      if( mWebView==null )
129
        {
130
        WebView videoView = findViewById(R.id.tutorialVideoView);
131
        mWebView = new TutorialWebView(videoView);
132
        mWebView.load(URL+mURL);
133
        }
134 af88bf2e Leszek Koltunski
      }
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 0de39b8e Leszek Koltunski
169
      if( mWebView!=null ) mWebView.onPause();
170
171 1237d25d Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
172 af88bf2e Leszek Koltunski
      }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
    
176
    @Override
177
    protected void onResume() 
178
      {
179
      super.onResume();
180 1237d25d Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
181 af88bf2e Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
182
      view.onResume();
183
184 280dc794 Leszek Koltunski
      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 ea036986 Leszek Koltunski
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
192 280dc794 Leszek Koltunski
193
      mScreen.createRightPane(this);
194
195 0de39b8e Leszek Koltunski
      if( mWebView!=null ) mWebView.onResume();
196
197 d433b50e Leszek Koltunski
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
198 af88bf2e Leszek Koltunski
        {
199 d433b50e Leszek Koltunski
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
200 af88bf2e Leszek Koltunski
        }
201
      }
202 b20e89d2 Leszek Koltunski
203 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    @Override
206
    protected void onDestroy() 
207
      {
208
      super.onDestroy();
209 1237d25d Leszek Koltunski
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
210 af88bf2e Leszek Koltunski
      }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
    void OpenGLError()
215
      {
216
      RubikDialogError errDiag = new RubikDialogError();
217
      errDiag.show(getSupportFragmentManager(), null);
218
      }
219
220 b20e89d2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222 09cf2a36 Leszek Koltunski
    private void changeIfDifferent(int ordinal,ObjectControl control)
223 b20e89d2 Leszek Koltunski
      {
224 09cf2a36 Leszek Koltunski
      RubikObject object = RubikObjectList.getObject(ordinal);
225
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
226 7cb8d4b0 Leszek Koltunski
      int iconMode  = TwistyObject.MODE_NORM;
227 314e9ff0 Leszek Koltunski
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
228
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
229 aec421fd Leszek Koltunski
      String name = object==null ? "NULL" : object.getUpperName();
230 da57afae Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
231
      OSInterface os = view.getInterface();
232
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
233 50e6c5d6 Leszek Koltunski
      control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
234 b20e89d2 Leszek Koltunski
      }
235
236 344f290c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 dd1a65c1 Leszek Koltunski
    TutorialScreen getState()
239 344f290c Leszek Koltunski
      {
240 1237d25d Leszek Koltunski
      return mScreen;
241 344f290c Leszek Koltunski
      }
242
243 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ea036986 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
    public boolean getIsFree()
262
      {
263
      return mIsFree;
264
      }
265
266 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 2afc6754 Leszek Koltunski
    public ObjectControl getControl()
269 55e6be1d Leszek Koltunski
      {
270
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
271 2afc6754 Leszek Koltunski
      return view.getObjectControl();
272 55e6be1d Leszek Koltunski
      }
273
274 18d47965 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285
    public static int getDrawableSize()
286
      {
287 aa622165 Leszek Koltunski
      if( mScreenHeight<1000 ) return 0;
288
      if( mScreenHeight<1600 ) return 1;
289
      if( mScreenHeight<1900 ) return 2;
290 af88bf2e Leszek Koltunski
      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
}