Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 422ebaef

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 af88bf2e Leszek Koltunski
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 e019c70b Leszek Koltunski
import androidx.appcompat.app.AppCompatActivity;
24
25 96e67000 leszek
import org.distorted.dialogs.RubikDialogTutorial;
26 3f7a4363 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
27 2afc6754 Leszek Koltunski
28 b710a574 leszek
import org.distorted.main.MainActivity;
29 50e6c5d6 Leszek Koltunski
import org.distorted.objectlib.main.InitAssets;
30 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
31 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
32 eaf46415 Leszek Koltunski
33 af88bf2e Leszek Koltunski
import org.distorted.main.R;
34 eaf46415 Leszek Koltunski
import org.distorted.dialogs.RubikDialogError;
35 09cf2a36 Leszek Koltunski
import org.distorted.objects.RubikObject;
36 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObjectList;
37 da57afae Leszek Koltunski
import org.distorted.os.OSInterface;
38 af88bf2e Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c9f72ca3 leszek
    private static final int ACTIVITY_NUMBER = 0;
45 943471aa Leszek Koltunski
    public static final float BAR_RATIO = 0.17f;
46 b710a574 leszek
    public static final int FLAGS = MainActivity.FLAGS;
47 af88bf2e Leszek Koltunski
48
    private static int mScreenWidth, mScreenHeight;
49
    private int mCurrentApiVersion;
50 1237d25d Leszek Koltunski
    private TutorialScreen mScreen;
51 7ac0ee88 Leszek Koltunski
    private int mObjectOrdinal;
52 5b4eaf7e Leszek Koltunski
    private TutorialWebView mWebView;
53 ea036986 Leszek Koltunski
    private boolean mIsFree;
54 ef2709dc leszek
    private boolean mErrorLoadingWebView;
55 af88bf2e Leszek Koltunski
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
    @Override
59
    protected void onCreate(Bundle savedState)
60
      {
61
      super.onCreate(savedState);
62 1237d25d Leszek Koltunski
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
63 dd874ae8 Leszek Koltunski
      setTheme(R.style.MaterialThemeNoActionBar);
64 ef2709dc leszek
65
      try
66
        {
67
        setContentView(R.layout.tutorial);
68
        mErrorLoadingWebView = false;
69
        }
70
      catch(Exception e)
71
        {
72
        setContentView(R.layout.tutorial_no_webview);
73
        mErrorLoadingWebView = true;
74
        }
75 af88bf2e Leszek Koltunski
76 ea036986 Leszek Koltunski
      mIsFree = true;
77
78 2971588c Leszek Koltunski
      Bundle b = getIntent().getExtras();
79
80 79bf5d8b Leszek Koltunski
      if(b != null)
81
        {
82
        mObjectOrdinal = b.getInt("obj");
83 ea036986 Leszek Koltunski
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
84
        mIsFree = (obj!=null && obj.isFree());
85 79bf5d8b Leszek Koltunski
        }
86 2971588c Leszek Koltunski
87 af88bf2e Leszek Koltunski
      DisplayMetrics displaymetrics = new DisplayMetrics();
88 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
89 af88bf2e Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
90
      mScreenHeight=displaymetrics.heightPixels;
91
92
      hideNavigationBar();
93
      cutoutHack();
94 b710a574 leszek
      showDialog();
95
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 ab19c113 leszek
    public void showDialog()
100 b710a574 leszek
      {
101 ef2709dc leszek
      if( !mErrorLoadingWebView )
102
        {
103
        Bundle bundle = new Bundle();
104
        bundle.putString("argument", String.valueOf(mObjectOrdinal));
105
        RubikDialogTutorial diag = new RubikDialogTutorial();
106
        diag.setArguments(bundle);
107
        diag.show(getSupportFragmentManager(), RubikDialogTutorial.getDialogTag());
108
        }
109 af88bf2e Leszek Koltunski
      }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
    private void hideNavigationBar()
114
      {
115
      mCurrentApiVersion = Build.VERSION.SDK_INT;
116
117
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
118
        {
119
        final View decorView = getWindow().getDecorView();
120
121
        decorView.setSystemUiVisibility(FLAGS);
122
123
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
124
          {
125
          @Override
126
          public void onSystemUiVisibilityChange(int visibility)
127
            {
128
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
129
              {
130
              decorView.setSystemUiVisibility(FLAGS);
131
              }
132
            }
133
          });
134
        }
135
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139 b710a574 leszek
    public void loadTutorial(String url)
140 af88bf2e Leszek Koltunski
      {
141 ef2709dc leszek
      if( !mErrorLoadingWebView )
142 280dc794 Leszek Koltunski
        {
143 ef2709dc leszek
        if(mWebView==null)
144
          {
145
          WebView videoView = findViewById(R.id.tutorialVideoView);
146
          mWebView = new TutorialWebView(videoView);
147
          }
148 ab19c113 leszek
149 ef2709dc leszek
        mWebView.load(URL+url);
150
        }
151 af88bf2e Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// do not avoid cutouts
155
156
    private void cutoutHack()
157
      {
158
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
159
        {
160
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
161
        }
162
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
    @Override
167
    public void onWindowFocusChanged(boolean hasFocus)
168
      {
169
      super.onWindowFocusChanged(hasFocus);
170
171
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
172
        {
173
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
174
        }
175
      }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    @Override
180
    protected void onPause() 
181
      {
182
      super.onPause();
183
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
184
      view.onPause();
185 0de39b8e Leszek Koltunski
186
      if( mWebView!=null ) mWebView.onPause();
187
188 1237d25d Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
189 af88bf2e Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onResume() 
195
      {
196
      super.onResume();
197 1237d25d Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198 af88bf2e Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
199
      view.onResume();
200
201 280dc794 Leszek Koltunski
      float width = getScreenWidthInPixels();
202
203
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
204
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
205
      paramsR.width = (int)(width*BAR_RATIO);
206
      viewR.setLayoutParams(paramsR);
207
208 ea036986 Leszek Koltunski
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
209 280dc794 Leszek Koltunski
210
      mScreen.createRightPane(this);
211
212 0de39b8e Leszek Koltunski
      if( mWebView!=null ) mWebView.onResume();
213
214 d433b50e Leszek Koltunski
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
215 af88bf2e Leszek Koltunski
        {
216 d433b50e Leszek Koltunski
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
217 af88bf2e Leszek Koltunski
        }
218
      }
219 b20e89d2 Leszek Koltunski
220 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221
    
222
    @Override
223
    protected void onDestroy() 
224
      {
225
      super.onDestroy();
226 1237d25d Leszek Koltunski
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
227 af88bf2e Leszek Koltunski
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
    void OpenGLError()
232
      {
233
      RubikDialogError errDiag = new RubikDialogError();
234
      errDiag.show(getSupportFragmentManager(), null);
235
      }
236
237 b20e89d2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 09cf2a36 Leszek Koltunski
    private void changeIfDifferent(int ordinal,ObjectControl control)
240 b20e89d2 Leszek Koltunski
      {
241 09cf2a36 Leszek Koltunski
      RubikObject object = RubikObjectList.getObject(ordinal);
242 7cb8d4b0 Leszek Koltunski
      int iconMode  = TwistyObject.MODE_NORM;
243 314e9ff0 Leszek Koltunski
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
244
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
245 aec421fd Leszek Koltunski
      String name = object==null ? "NULL" : object.getUpperName();
246 da57afae Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
247
      OSInterface os = view.getInterface();
248
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
249 337f4660 leszek
      control.changeIfDifferent(ordinal,name,iconMode,asset);
250 b20e89d2 Leszek Koltunski
      }
251
252 344f290c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254 dd1a65c1 Leszek Koltunski
    TutorialScreen getState()
255 344f290c Leszek Koltunski
      {
256 1237d25d Leszek Koltunski
      return mScreen;
257 344f290c Leszek Koltunski
      }
258
259 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
// PUBLIC API
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
    public int getScreenWidthInPixels()
264
      {
265
      return mScreenWidth;
266
      }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
    public int getScreenHeightInPixels()
271
      {
272
      return mScreenHeight;
273
      }
274
275 ea036986 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
    public boolean getIsFree()
278
      {
279
      return mIsFree;
280
      }
281
282 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 2afc6754 Leszek Koltunski
    public ObjectControl getControl()
285 55e6be1d Leszek Koltunski
      {
286
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
287 2afc6754 Leszek Koltunski
      return view.getObjectControl();
288 55e6be1d Leszek Koltunski
      }
289 af88bf2e Leszek Koltunski
}