Project

General

Profile

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

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

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.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
import androidx.appcompat.app.AppCompatActivity;
24

    
25
import org.distorted.dialogs.RubikDialogTutorial;
26
import org.distorted.library.main.DistortedLibrary;
27

    
28
import org.distorted.main.MainActivity;
29
import org.distorted.objectlib.main.InitAssets;
30
import org.distorted.objectlib.main.ObjectControl;
31
import org.distorted.objectlib.main.TwistyObject;
32

    
33
import org.distorted.main.R;
34
import org.distorted.dialogs.RubikDialogError;
35
import org.distorted.objects.RubikObject;
36
import org.distorted.objects.RubikObjectList;
37
import org.distorted.os.OSInterface;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class TutorialActivity extends AppCompatActivity
42
{
43
    private static final String URL = "https://www.youtube.com/embed/";
44
    private static final int ACTIVITY_NUMBER = 0;
45
    public static final float BAR_RATIO = 0.17f;
46
    public static final int FLAGS = MainActivity.FLAGS;
47

    
48
    private static int mScreenWidth, mScreenHeight;
49
    private int mCurrentApiVersion;
50
    private TutorialScreen mScreen;
51
    private int mObjectOrdinal;
52
    private TutorialWebView mWebView;
53
    private boolean mIsFree;
54
    private boolean mErrorLoadingWebView;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
    @Override
59
    protected void onCreate(Bundle savedState)
60
      {
61
      setTheme(R.style.MaterialThemeNoActionBar);
62
      super.onCreate(savedState);
63
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
64

    
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

    
76
      mIsFree = true;
77

    
78
      Bundle b = getIntent().getExtras();
79

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

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

    
92
      hideNavigationBar();
93
      cutoutHack();
94
      showDialog();
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
    public void showDialog()
100
      {
101
      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
      }
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
    public void loadTutorial(String url)
140
      {
141
      if( !mErrorLoadingWebView )
142
        {
143
        if(mWebView==null)
144
          {
145
          WebView videoView = findViewById(R.id.tutorialVideoView);
146
          mWebView = new TutorialWebView(videoView);
147
          }
148

    
149
        mWebView.load(URL+url);
150
        }
151
      }
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

    
186
      if( mWebView!=null ) mWebView.onPause();
187

    
188
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onResume() 
195
      {
196
      super.onResume();
197
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
199
      view.onResume();
200

    
201
      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
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
209

    
210
      mScreen.createRightPane(this);
211

    
212
      if( mWebView!=null ) mWebView.onResume();
213

    
214
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
215
        {
216
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
217
        }
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
    
222
    @Override
223
    protected void onDestroy() 
224
      {
225
      super.onDestroy();
226
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    void OpenGLError()
232
      {
233
      RubikDialogError errDiag = new RubikDialogError();
234
      errDiag.show(getSupportFragmentManager(), null);
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
    private void changeIfDifferent(int ordinal,ObjectControl control)
240
      {
241
      RubikObject object = RubikObjectList.getObject(ordinal);
242
      int iconMode  = TwistyObject.MODE_NORM;
243
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
244
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
245
      String name = object==null ? "NULL" : object.getUpperName();
246
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
247
      OSInterface os = view.getInterface();
248
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
249
      control.changeIfDifferent(ordinal,name,iconMode,asset);
250
      }
251

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

    
254
    TutorialScreen getState()
255
      {
256
      return mScreen;
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public boolean getIsFree()
278
      {
279
      return mIsFree;
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public ObjectControl getControl()
285
      {
286
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
287
      return view.getObjectControl();
288
      }
289
}
(1-1/6)