Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 1c04d054

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.main_old.RubikActivity;
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
import org.distorted.purchase.PurchaseActivity;
39

    
40
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class TutorialActivity extends AppCompatActivity
45
{
46
    private static final String URL = "https://www.youtube.com/embed/";
47
    private static final int ACTIVITY_NUMBER = 1;
48
    public static final float BAR_RATIO = 0.17f;
49
    public static final int FLAGS = RubikActivity.FLAGS;
50

    
51
    private static int mScreenWidth, mScreenHeight;
52
    private int mCurrentApiVersion;
53
    private TutorialScreen mScreen;
54
    private String mURL;
55
    private int mObjectOrdinal;
56
    private TutorialWebView mWebView;
57
    private boolean mIsFree;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    @Override
62
    protected void onCreate(Bundle savedState)
63
      {
64
      super.onCreate(savedState);
65
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
66
      setTheme(R.style.MaterialThemeNoActionBar);
67
      setContentView(R.layout.tutorial);
68

    
69
      mIsFree = true;
70

    
71
      Bundle b = getIntent().getExtras();
72

    
73
      if(b != null)
74
        {
75
        mURL           = b.getString("url");
76
        mObjectOrdinal = b.getInt("obj");
77
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
78
        mIsFree = (obj!=null && obj.isFree());
79
        }
80

    
81
      DisplayMetrics displaymetrics = new DisplayMetrics();
82
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
83
      mScreenWidth =displaymetrics.widthPixels;
84
      mScreenHeight=displaymetrics.heightPixels;
85

    
86
      hideNavigationBar();
87
      cutoutHack();
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
    private void hideNavigationBar()
93
      {
94
      mCurrentApiVersion = Build.VERSION.SDK_INT;
95

    
96
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
97
        {
98
        final View decorView = getWindow().getDecorView();
99

    
100
        decorView.setSystemUiVisibility(FLAGS);
101

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    @Override
119
    public void onAttachedToWindow()
120
      {
121
      super.onAttachedToWindow();
122

    
123
      if( mWebView==null )
124
        {
125
        WebView videoView = findViewById(R.id.tutorialVideoView);
126
        mWebView = new TutorialWebView(videoView);
127
        mWebView.load(URL+mURL);
128
        }
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// do not avoid cutouts
133

    
134
    private void cutoutHack()
135
      {
136
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
137
        {
138
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
139
        }
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    @Override
145
    public void onWindowFocusChanged(boolean hasFocus)
146
      {
147
      super.onWindowFocusChanged(hasFocus);
148

    
149
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
150
        {
151
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
152
        }
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
    
157
    @Override
158
    protected void onPause() 
159
      {
160
      super.onPause();
161
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
162
      view.onPause();
163

    
164
      if( mWebView!=null ) mWebView.onPause();
165

    
166
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
    @Override
172
    protected void onResume() 
173
      {
174
      super.onResume();
175
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
176
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
177
      view.onResume();
178

    
179
      float width = getScreenWidthInPixels();
180

    
181
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
182
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
183
      paramsR.width = (int)(width*BAR_RATIO);
184
      viewR.setLayoutParams(paramsR);
185

    
186
      if( mScreen==null ) mScreen = new TutorialScreen(mIsFree);
187

    
188
      mScreen.createRightPane(this);
189

    
190
      if( mWebView!=null ) mWebView.onResume();
191

    
192
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
193
        {
194
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
195
        }
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    @Override
201
    protected void onDestroy() 
202
      {
203
      super.onDestroy();
204
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    void OpenGLError()
210
      {
211
      RubikDialogError errDiag = new RubikDialogError();
212
      errDiag.show(getSupportFragmentManager(), null);
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    private void changeIfDifferent(int ordinal,ObjectControl control)
218
      {
219
      RubikObject object = RubikObjectList.getObject(ordinal);
220
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
221
      int iconMode  = TwistyObject.MODE_NORM;
222
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
223
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
224
      String name = object==null ? "NULL" : object.getUpperName();
225
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
226
      OSInterface os = view.getInterface();
227
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
228
      control.changeIfDifferent(ordinal,name,meshState,iconMode,asset);
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    TutorialScreen getState()
234
      {
235
      return mScreen;
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
// PUBLIC API
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    public int getScreenWidthInPixels()
243
      {
244
      return mScreenWidth;
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
    public int getScreenHeightInPixels()
250
      {
251
      return mScreenHeight;
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
    public boolean getIsFree()
257
      {
258
      return mIsFree;
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    public void switchToPurchase()
272
      {
273
      Intent intent = new Intent(this, PurchaseActivity.class);
274
      intent.putExtra("obj", mObjectOrdinal);
275
      startActivity(intent);
276
      }
277
}
(1-1/6)