Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 2cf21fdc

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.library.main.DistortedLibrary;
26

    
27
import org.distorted.objectlib.main.ObjectControl;
28
import org.distorted.objectlib.main.TwistyObject;
29

    
30
import org.distorted.main.R;
31
import org.distorted.dialogs.RubikDialogError;
32
import org.distorted.objects.RubikObject;
33
import org.distorted.objects.RubikObjectList;
34

    
35
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class TutorialActivity extends AppCompatActivity
40
{
41
    private static final String URL = "https://www.youtube.com/embed/";
42

    
43
    private static final int ACTIVITY_NUMBER = 1;
44
    public static final float BAR_RATIO = 0.17f;
45
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
46
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
47

    
48
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
49
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
50
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
51
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
52
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
53

    
54
    private static int mScreenWidth, mScreenHeight;
55
    private int mCurrentApiVersion;
56
    private TutorialScreen mScreen;
57
    private String mURL;
58
    private int mObjectOrdinal;
59
    private TutorialWebView mWebView;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

    
73
      if(b != null)
74
        {
75
        mURL           = b.getString("url");
76
        mObjectOrdinal = b.getInt("obj");
77
        }
78

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

    
84
      hideNavigationBar();
85
      cutoutHack();
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
    private void hideNavigationBar()
91
      {
92
      mCurrentApiVersion = Build.VERSION.SDK_INT;
93

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

    
98
        decorView.setSystemUiVisibility(FLAGS);
99

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
    @Override
117
    public void onAttachedToWindow()
118
      {
119
      super.onAttachedToWindow();
120

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// do not avoid cutouts
131

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

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

    
162
      if( mWebView!=null ) mWebView.onPause();
163

    
164
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
165
      }
166

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

    
177
      float width = getScreenWidthInPixels();
178

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

    
184
      if( mScreen==null ) mScreen = new TutorialScreen();
185

    
186
      mScreen.createRightPane(this);
187

    
188
      if( mWebView!=null ) mWebView.onResume();
189

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    private void changeIfDifferent(int ordinal,ObjectControl control)
216
      {
217
      RubikObject object = RubikObjectList.getObject(ordinal);
218
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
219
      int iconMode  = TwistyObject.MODE_NORM;
220
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
221
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
222
      String name = object==null ? "NULL" : object.getUpperName();
223

    
224
      control.changeIfDifferent(ordinal,name,meshState,iconMode,jsonStream,meshStream);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    TutorialScreen getState()
230
      {
231
      return mScreen;
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public int getScreenWidthInPixels()
239
      {
240
      return mScreenWidth;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public int getScreenHeightInPixels()
246
      {
247
      return mScreenHeight;
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public ObjectControl getControl()
253
      {
254
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
255
      return view.getObjectControl();
256
      }
257

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

    
260
    public static int getDrawableSize()
261
      {
262
      if( mScreenHeight<1000 )
263
        {
264
        return 0;
265
        }
266
      if( mScreenHeight<1600 )
267
        {
268
        return 1;
269
        }
270
      if( mScreenHeight<1900 )
271
        {
272
        return 2;
273
        }
274

    
275
      return 3;
276
      }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    public static int getDrawable(int small, int medium, int big, int huge)
281
      {
282
      int size = getDrawableSize();
283

    
284
      switch(size)
285
        {
286
        case 0 : return small;
287
        case 1 : return medium;
288
        case 2 : return big;
289
        default: return huge;
290
        }
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    public boolean isVertical()
296
      {
297
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
298
      return view.isVertical();
299
      }
300
}
(1-1/6)