Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 804293f0

1 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 eaf87d1d Leszek Koltunski
package org.distorted.tutorials;
21 af88bf2e Leszek Koltunski
22 b20e89d2 Leszek Koltunski
import java.io.InputStream;
23
24 af88bf2e Leszek Koltunski
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 e019c70b Leszek Koltunski
import androidx.appcompat.app.AppCompatActivity;
34
35 af88bf2e Leszek Koltunski
import com.google.firebase.analytics.FirebaseAnalytics;
36
37 b20e89d2 Leszek Koltunski
import org.distorted.dmesh.ObjectMesh;
38
import org.distorted.jsons.ObjectJson;
39 3f7a4363 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
40 2afc6754 Leszek Koltunski
41
import org.distorted.objectlib.main.ObjectControl;
42 eaf46415 Leszek Koltunski
43 af88bf2e Leszek Koltunski
import org.distorted.main.R;
44 eaf46415 Leszek Koltunski
import org.distorted.dialogs.RubikDialogError;
45 09cf2a36 Leszek Koltunski
import org.distorted.objects.RubikObject;
46 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObjectList;
47 af88bf2e Leszek Koltunski
48 09cf2a36 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
49
50 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 e019c70b Leszek Koltunski
public class TutorialActivity extends AppCompatActivity
53 af88bf2e Leszek Koltunski
{
54 2971588c Leszek Koltunski
    private static final String URL = "https://www.youtube.com/embed/";
55
56 1237d25d Leszek Koltunski
    private static final int ACTIVITY_NUMBER = 1;
57 943471aa Leszek Koltunski
    public static final float BAR_RATIO = 0.17f;
58 af88bf2e Leszek Koltunski
    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 1237d25d Leszek Koltunski
    private TutorialScreen mScreen;
71 2971588c Leszek Koltunski
    private String mURL;
72 7ac0ee88 Leszek Koltunski
    private int mObjectOrdinal;
73 5b4eaf7e Leszek Koltunski
    private TutorialWebView mWebView;
74 af88bf2e Leszek Koltunski
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
    @Override
78
    protected void onCreate(Bundle savedState)
79
      {
80
      super.onCreate(savedState);
81 1237d25d Leszek Koltunski
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
82 dd874ae8 Leszek Koltunski
      setTheme(R.style.MaterialThemeNoActionBar);
83 af88bf2e Leszek Koltunski
      setContentView(R.layout.tutorial);
84
85 2971588c Leszek Koltunski
      Bundle b = getIntent().getExtras();
86
87 79bf5d8b Leszek Koltunski
      if(b != null)
88
        {
89
        mURL           = b.getString("url");
90
        mObjectOrdinal = b.getInt("obj");
91
        }
92 2971588c Leszek Koltunski
93 af88bf2e Leszek Koltunski
      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 280dc794 Leszek Koltunski
      if( mWebView==null )
138
        {
139
        WebView videoView = findViewById(R.id.tutorialVideoView);
140
        mWebView = new TutorialWebView(videoView);
141
        mWebView.load(URL+mURL);
142
        }
143 af88bf2e Leszek Koltunski
      }
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 0de39b8e Leszek Koltunski
178
      if( mWebView!=null ) mWebView.onPause();
179
180 1237d25d Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
181 af88bf2e Leszek Koltunski
      }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onResume() 
187
      {
188
      super.onResume();
189 1237d25d Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
190 af88bf2e Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
191
      view.onResume();
192
193 280dc794 Leszek Koltunski
      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 0de39b8e Leszek Koltunski
      if( mWebView!=null ) mWebView.onResume();
205
206 d433b50e Leszek Koltunski
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
207 af88bf2e Leszek Koltunski
        {
208 d433b50e Leszek Koltunski
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
209 af88bf2e Leszek Koltunski
        }
210
      }
211 b20e89d2 Leszek Koltunski
212 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214
    @Override
215
    protected void onDestroy() 
216
      {
217
      super.onDestroy();
218 1237d25d Leszek Koltunski
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
219 af88bf2e Leszek Koltunski
      }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223
    void OpenGLError()
224
      {
225
      RubikDialogError errDiag = new RubikDialogError();
226
      errDiag.show(getSupportFragmentManager(), null);
227
      }
228
229 b20e89d2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231 09cf2a36 Leszek Koltunski
    private void changeIfDifferent(int ordinal,ObjectControl control)
232 b20e89d2 Leszek Koltunski
      {
233 09cf2a36 Leszek Koltunski
      RubikObject object = RubikObjectList.getObject(ordinal);
234
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
235
236 804293f0 Leszek Koltunski
      InputStream jsonStream = ObjectJson.getObjectStream(this,ordinal);
237
      InputStream meshStream = ObjectMesh.getMeshStream(this,ordinal);
238 09cf2a36 Leszek Koltunski
      control.changeIfDifferent(ordinal,meshState,jsonStream,meshStream);
239 b20e89d2 Leszek Koltunski
      }
240
241 344f290c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243 dd1a65c1 Leszek Koltunski
    TutorialScreen getState()
244 344f290c Leszek Koltunski
      {
245 1237d25d Leszek Koltunski
      return mScreen;
246 344f290c Leszek Koltunski
      }
247
248 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
249
// PUBLIC API
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252
    public FirebaseAnalytics getAnalytics()
253
      {
254
      return mFirebaseAnalytics;
255
      }
256
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
    public int getScreenWidthInPixels()
260
      {
261
      return mScreenWidth;
262
      }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
    public int getScreenHeightInPixels()
267
      {
268
      return mScreenHeight;
269
      }
270
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273 2afc6754 Leszek Koltunski
    public ObjectControl getControl()
274 55e6be1d Leszek Koltunski
      {
275
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
276 2afc6754 Leszek Koltunski
      return view.getObjectControl();
277 55e6be1d Leszek Koltunski
      }
278
279 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
    public static int getDrawableSize()
282
      {
283
      if( mScreenHeight<1000 )
284
        {
285
        return 0;
286
        }
287
      if( mScreenHeight<1600 )
288
        {
289
        return 1;
290
        }
291
      if( mScreenHeight<1900 )
292
        {
293
        return 2;
294
        }
295
296
      return 3;
297
      }
298
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
301
    public static int getDrawable(int small, int medium, int big, int huge)
302
      {
303
      int size = getDrawableSize();
304
305
      switch(size)
306
        {
307
        case 0 : return small;
308
        case 1 : return medium;
309
        case 2 : return big;
310
        default: return huge;
311
        }
312
      }
313
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316
    public boolean isVertical()
317
      {
318
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
319
      return view.isVertical();
320
      }
321
}