Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.tutorials;
21

    
22
import java.io.InputStream;
23

    
24
import android.os.Build;
25
import android.os.Bundle;
26
import android.util.DisplayMetrics;
27
import android.view.View;
28
import android.view.ViewGroup;
29
import android.view.WindowManager;
30
import android.webkit.WebView;
31
import android.widget.LinearLayout;
32

    
33
import androidx.appcompat.app.AppCompatActivity;
34

    
35
import com.google.firebase.analytics.FirebaseAnalytics;
36

    
37
import org.distorted.dmesh.ObjectMesh;
38
import org.distorted.jsons.ObjectJson;
39
import org.distorted.library.main.DistortedLibrary;
40

    
41
import org.distorted.objectlib.main.ObjectControl;
42

    
43
import org.distorted.main.R;
44
import org.distorted.dialogs.RubikDialogError;
45
import org.distorted.objects.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47

    
48
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class TutorialActivity extends AppCompatActivity
53
{
54
    private static final String URL = "https://www.youtube.com/embed/";
55

    
56
    private static final int ACTIVITY_NUMBER = 1;
57
    public static final float BAR_RATIO = 0.17f;
58
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
59
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
60

    
61
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
62
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
63
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
64
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
65
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
66

    
67
    private FirebaseAnalytics mFirebaseAnalytics;
68
    private static int mScreenWidth, mScreenHeight;
69
    private int mCurrentApiVersion;
70
    private TutorialScreen mScreen;
71
    private String mURL;
72
    private int mObjectOrdinal;
73
    private TutorialWebView mWebView;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    @Override
78
    protected void onCreate(Bundle savedState)
79
      {
80
      super.onCreate(savedState);
81
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
82
      setTheme(R.style.MaterialThemeNoActionBar);
83
      setContentView(R.layout.tutorial);
84

    
85
      Bundle b = getIntent().getExtras();
86

    
87
      if(b != null)
88
        {
89
        mURL           = b.getString("url");
90
        mObjectOrdinal = b.getInt("obj");
91
        }
92

    
93
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
94

    
95
      DisplayMetrics displaymetrics = new DisplayMetrics();
96
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
97
      mScreenWidth =displaymetrics.widthPixels;
98
      mScreenHeight=displaymetrics.heightPixels;
99

    
100
      hideNavigationBar();
101
      cutoutHack();
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    private void hideNavigationBar()
107
      {
108
      mCurrentApiVersion = Build.VERSION.SDK_INT;
109

    
110
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
111
        {
112
        final View decorView = getWindow().getDecorView();
113

    
114
        decorView.setSystemUiVisibility(FLAGS);
115

    
116
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
117
          {
118
          @Override
119
          public void onSystemUiVisibilityChange(int visibility)
120
            {
121
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
122
              {
123
              decorView.setSystemUiVisibility(FLAGS);
124
              }
125
            }
126
          });
127
        }
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
    @Override
133
    public void onAttachedToWindow()
134
      {
135
      super.onAttachedToWindow();
136

    
137
      if( mWebView==null )
138
        {
139
        WebView videoView = findViewById(R.id.tutorialVideoView);
140
        mWebView = new TutorialWebView(videoView);
141
        mWebView.load(URL+mURL);
142
        }
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// do not avoid cutouts
147

    
148
    private void cutoutHack()
149
      {
150
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
151
        {
152
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    @Override
159
    public void onWindowFocusChanged(boolean hasFocus)
160
      {
161
      super.onWindowFocusChanged(hasFocus);
162

    
163
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
164
        {
165
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
166
        }
167
      }
168

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

    
178
      if( mWebView!=null ) mWebView.onPause();
179

    
180
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onResume() 
187
      {
188
      super.onResume();
189
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
190
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
191
      view.onResume();
192

    
193
      float width = getScreenWidthInPixels();
194

    
195
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
196
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
197
      paramsR.width = (int)(width*BAR_RATIO);
198
      viewR.setLayoutParams(paramsR);
199

    
200
      if( mScreen==null ) mScreen = new TutorialScreen();
201

    
202
      mScreen.createRightPane(this);
203

    
204
      if( mWebView!=null ) mWebView.onResume();
205

    
206
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
207
        {
208
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
209
        }
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214
    @Override
215
    protected void onDestroy() 
216
      {
217
      super.onDestroy();
218
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    void OpenGLError()
224
      {
225
      RubikDialogError errDiag = new RubikDialogError();
226
      errDiag.show(getSupportFragmentManager(), null);
227
      }
228

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

    
231
    private void changeIfDifferent(int ordinal,ObjectControl control)
232
      {
233
      RubikObject object = RubikObjectList.getObject(ordinal);
234
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
235
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
236
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
237
      String name = object==null ? "NULL" : object.getUpperName();
238

    
239
      control.changeIfDifferent(ordinal,name,meshState,jsonStream,meshStream);
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    TutorialScreen getState()
245
      {
246
      return mScreen;
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    public FirebaseAnalytics getAnalytics()
254
      {
255
      return mFirebaseAnalytics;
256
      }
257

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

    
260
    public int getScreenWidthInPixels()
261
      {
262
      return mScreenWidth;
263
      }
264

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

    
267
    public int getScreenHeightInPixels()
268
      {
269
      return mScreenHeight;
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public ObjectControl getControl()
275
      {
276
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
277
      return view.getObjectControl();
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public static int getDrawableSize()
283
      {
284
      if( mScreenHeight<1000 )
285
        {
286
        return 0;
287
        }
288
      if( mScreenHeight<1600 )
289
        {
290
        return 1;
291
        }
292
      if( mScreenHeight<1900 )
293
        {
294
        return 2;
295
        }
296

    
297
      return 3;
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    public static int getDrawable(int small, int medium, int big, int huge)
303
      {
304
      int size = getDrawableSize();
305

    
306
      switch(size)
307
        {
308
        case 0 : return small;
309
        case 1 : return medium;
310
        case 2 : return big;
311
        default: return huge;
312
        }
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
    public boolean isVertical()
318
      {
319
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
320
      return view.isVertical();
321
      }
322
}
(1-1/6)