Project

General

Profile

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

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

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.RubikDialogTutorialSingle;
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 = 1;
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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
65
      mIsFree = true;
66

    
67
      Bundle b = getIntent().getExtras();
68

    
69
      if(b != null)
70
        {
71
        mObjectOrdinal = b.getInt("obj");
72
        RubikObject obj= RubikObjectList.getObject(mObjectOrdinal);
73
        mIsFree = (obj!=null && obj.isFree());
74
        }
75

    
76
      DisplayMetrics displaymetrics = new DisplayMetrics();
77
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
78
      mScreenWidth =displaymetrics.widthPixels;
79
      mScreenHeight=displaymetrics.heightPixels;
80

    
81
      hideNavigationBar();
82
      cutoutHack();
83
      showDialog();
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
    private void showDialog()
89
      {
90
      RubikObjectList.setCurrObject(mObjectOrdinal);
91
      Bundle bundle = new Bundle();
92
      bundle.putString("argument",RubikObjectList.getCurrentName());
93
      RubikDialogTutorialSingle diag = new RubikDialogTutorialSingle();
94
      diag.setArguments(bundle);
95
      diag.show( getSupportFragmentManager(), RubikDialogTutorialSingle.getDialogTag() );
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    private void hideNavigationBar()
101
      {
102
      mCurrentApiVersion = Build.VERSION.SDK_INT;
103

    
104
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
105
        {
106
        final View decorView = getWindow().getDecorView();
107

    
108
        decorView.setSystemUiVisibility(FLAGS);
109

    
110
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
111
          {
112
          @Override
113
          public void onSystemUiVisibilityChange(int visibility)
114
            {
115
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
116
              {
117
              decorView.setSystemUiVisibility(FLAGS);
118
              }
119
            }
120
          });
121
        }
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    public void loadTutorial(String url)
127
      {
128
      if( mWebView==null )
129
        {
130
        WebView videoView = findViewById(R.id.tutorialVideoView);
131
        mWebView = new TutorialWebView(videoView);
132
        mWebView.load(URL+url);
133
        }
134
      }
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

    
169
      if( mWebView!=null ) mWebView.onPause();
170

    
171
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
    
176
    @Override
177
    protected void onResume() 
178
      {
179
      super.onResume();
180
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
181
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
182
      view.onResume();
183

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

    
193
      mScreen.createRightPane(this);
194

    
195
      if( mWebView!=null ) mWebView.onResume();
196

    
197
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
198
        {
199
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
200
        }
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    @Override
206
    protected void onDestroy() 
207
      {
208
      super.onDestroy();
209
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    void OpenGLError()
215
      {
216
      RubikDialogError errDiag = new RubikDialogError();
217
      errDiag.show(getSupportFragmentManager(), null);
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    TutorialScreen getState()
238
      {
239
      return mScreen;
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
// PUBLIC API
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public int getScreenWidthInPixels()
247
      {
248
      return mScreenWidth;
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    public int getScreenHeightInPixels()
254
      {
255
      return mScreenHeight;
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    public boolean getIsFree()
261
      {
262
      return mIsFree;
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    public ObjectControl getControl()
268
      {
269
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
270
      return view.getObjectControl();
271
      }
272
}
(1-1/6)