Project

General

Profile

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

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

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
import org.distorted.objectlib.main.ObjectControl;
29 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
30 eaf46415 Leszek Koltunski
31 af88bf2e Leszek Koltunski
import org.distorted.main.R;
32 eaf46415 Leszek Koltunski
import org.distorted.dialogs.RubikDialogError;
33 09cf2a36 Leszek Koltunski
import org.distorted.objects.RubikObject;
34 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObjectList;
35 18d47965 Leszek Koltunski
import org.distorted.purchase.PurchaseActivity;
36 af88bf2e Leszek Koltunski
37 09cf2a36 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
38
39 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 e019c70b Leszek Koltunski
public class TutorialActivity extends AppCompatActivity
42 af88bf2e Leszek Koltunski
{
43 2971588c Leszek Koltunski
    private static final String URL = "https://www.youtube.com/embed/";
44
45 1237d25d Leszek Koltunski
    private static final int ACTIVITY_NUMBER = 1;
46 943471aa Leszek Koltunski
    public static final float BAR_RATIO = 0.17f;
47 af88bf2e Leszek Koltunski
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
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 1237d25d Leszek Koltunski
    private TutorialScreen mScreen;
58 2971588c Leszek Koltunski
    private String mURL;
59 7ac0ee88 Leszek Koltunski
    private int mObjectOrdinal;
60 5b4eaf7e Leszek Koltunski
    private TutorialWebView mWebView;
61 ea036986 Leszek Koltunski
    private boolean mIsFree;
62 af88bf2e Leszek Koltunski
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
    @Override
66
    protected void onCreate(Bundle savedState)
67
      {
68
      super.onCreate(savedState);
69 1237d25d Leszek Koltunski
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
70 dd874ae8 Leszek Koltunski
      setTheme(R.style.MaterialThemeNoActionBar);
71 af88bf2e Leszek Koltunski
      setContentView(R.layout.tutorial);
72
73 ea036986 Leszek Koltunski
      mIsFree = true;
74
75 2971588c Leszek Koltunski
      Bundle b = getIntent().getExtras();
76
77 79bf5d8b Leszek Koltunski
      if(b != null)
78
        {
79
        mURL           = b.getString("url");
80
        mObjectOrdinal = b.getInt("obj");
81 ea036986 Leszek Koltunski
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
82
        mIsFree = (obj!=null && obj.isFree());
83 79bf5d8b Leszek Koltunski
        }
84 2971588c Leszek Koltunski
85 af88bf2e Leszek Koltunski
      DisplayMetrics displaymetrics = new DisplayMetrics();
86 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
87 af88bf2e Leszek Koltunski
      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 280dc794 Leszek Koltunski
      if( mWebView==null )
128
        {
129
        WebView videoView = findViewById(R.id.tutorialVideoView);
130
        mWebView = new TutorialWebView(videoView);
131
        mWebView.load(URL+mURL);
132
        }
133 af88bf2e Leszek Koltunski
      }
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 0de39b8e Leszek Koltunski
168
      if( mWebView!=null ) mWebView.onPause();
169
170 1237d25d Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
171 af88bf2e Leszek Koltunski
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    @Override
176
    protected void onResume() 
177
      {
178
      super.onResume();
179 1237d25d Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
180 af88bf2e Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
181
      view.onResume();
182
183 280dc794 Leszek Koltunski
      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 ea036986 Leszek Koltunski
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
191 280dc794 Leszek Koltunski
192
      mScreen.createRightPane(this);
193
194 0de39b8e Leszek Koltunski
      if( mWebView!=null ) mWebView.onResume();
195
196 d433b50e Leszek Koltunski
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
197 af88bf2e Leszek Koltunski
        {
198 d433b50e Leszek Koltunski
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
199 af88bf2e Leszek Koltunski
        }
200
      }
201 b20e89d2 Leszek Koltunski
202 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
203
    
204
    @Override
205
    protected void onDestroy() 
206
      {
207
      super.onDestroy();
208 1237d25d Leszek Koltunski
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
209 af88bf2e Leszek Koltunski
      }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
    void OpenGLError()
214
      {
215
      RubikDialogError errDiag = new RubikDialogError();
216
      errDiag.show(getSupportFragmentManager(), null);
217
      }
218
219 b20e89d2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221 09cf2a36 Leszek Koltunski
    private void changeIfDifferent(int ordinal,ObjectControl control)
222 b20e89d2 Leszek Koltunski
      {
223 09cf2a36 Leszek Koltunski
      RubikObject object = RubikObjectList.getObject(ordinal);
224
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
225 7cb8d4b0 Leszek Koltunski
      int iconMode  = TwistyObject.MODE_NORM;
226 314e9ff0 Leszek Koltunski
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
227
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
228 aec421fd Leszek Koltunski
      String name = object==null ? "NULL" : object.getUpperName();
229 09cf2a36 Leszek Koltunski
230 7cb8d4b0 Leszek Koltunski
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
231 b20e89d2 Leszek Koltunski
      }
232
233 344f290c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 dd1a65c1 Leszek Koltunski
    TutorialScreen getState()
236 344f290c Leszek Koltunski
      {
237 1237d25d Leszek Koltunski
      return mScreen;
238 344f290c Leszek Koltunski
      }
239
240 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ea036986 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258
    public boolean getIsFree()
259
      {
260
      return mIsFree;
261
      }
262
263 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265 2afc6754 Leszek Koltunski
    public ObjectControl getControl()
266 55e6be1d Leszek Koltunski
      {
267
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
268 2afc6754 Leszek Koltunski
      return view.getObjectControl();
269 55e6be1d Leszek Koltunski
      }
270
271 18d47965 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    public static int getDrawableSize()
283
      {
284
      if( mScreenHeight<1000 )
285
        {
286
        return 0;
287
        }
288
      if( mScreenHeight<1600 )
289
        {
290
        return 1;
291
        }
292
      if( mScreenHeight<1900 )
293
        {
294
        return 2;
295
        }
296
297
      return 3;
298
      }
299
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
302
    public static int getDrawable(int small, int medium, int big, int huge)
303
      {
304
      int size = getDrawableSize();
305
306
      switch(size)
307
        {
308
        case 0 : return small;
309
        case 1 : return medium;
310
        case 2 : return big;
311
        default: return huge;
312
        }
313
      }
314
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317
    public boolean isVertical()
318
      {
319
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
320
      return view.isVertical();
321
      }
322
}