Project

General

Profile

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

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

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
      float width = getScreenWidthInPixels();
138 344f290c Leszek Koltunski
139 2971588c Leszek Koltunski
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
140 344f290c Leszek Koltunski
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
141 eb49fa90 Leszek Koltunski
      paramsR.width = (int)(width*BAR_RATIO);
142 344f290c Leszek Koltunski
      viewR.setLayoutParams(paramsR);
143
144 1237d25d Leszek Koltunski
      if( mScreen==null ) mScreen = new TutorialScreen();
145 344f290c Leszek Koltunski
146 1237d25d Leszek Koltunski
      mScreen.createRightPane(this);
147 2971588c Leszek Koltunski
148
      WebView videoView = findViewById(R.id.tutorialVideoView);
149 55e6be1d Leszek Koltunski
      mWebView = new TutorialWebView(videoView);
150 5b4eaf7e Leszek Koltunski
      mWebView.load(URL+mURL);
151 af88bf2e Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// do not avoid cutouts
155
156
    private void cutoutHack()
157
      {
158
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
159
        {
160
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
161
        }
162
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
    @Override
167
    public void onWindowFocusChanged(boolean hasFocus)
168
      {
169
      super.onWindowFocusChanged(hasFocus);
170
171
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
172
        {
173
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
174
        }
175
      }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    @Override
180
    protected void onPause() 
181
      {
182
      super.onPause();
183
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
184
      view.onPause();
185 0de39b8e Leszek Koltunski
186
      if( mWebView!=null ) mWebView.onPause();
187
188 1237d25d Leszek Koltunski
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
189 af88bf2e Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onResume() 
195
      {
196
      super.onResume();
197 1237d25d Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198 af88bf2e Leszek Koltunski
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
199
      view.onResume();
200
201 0de39b8e Leszek Koltunski
      if( mWebView!=null ) mWebView.onResume();
202
203 d433b50e Leszek Koltunski
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
204 af88bf2e Leszek Koltunski
        {
205 d433b50e Leszek Koltunski
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
206 af88bf2e Leszek Koltunski
        }
207
      }
208 b20e89d2 Leszek Koltunski
209 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
    @Override
212
    protected void onDestroy() 
213
      {
214
      super.onDestroy();
215 1237d25d Leszek Koltunski
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
216 af88bf2e Leszek Koltunski
      }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
    void OpenGLError()
221
      {
222
      RubikDialogError errDiag = new RubikDialogError();
223
      errDiag.show(getSupportFragmentManager(), null);
224
      }
225
226 b20e89d2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 09cf2a36 Leszek Koltunski
    private void changeIfDifferent(int ordinal,ObjectControl control)
229 b20e89d2 Leszek Koltunski
      {
230 09cf2a36 Leszek Koltunski
      RubikObject object = RubikObjectList.getObject(ordinal);
231
      int meshState = object!=null ? object.getMeshState() : MESH_NICE;
232
233
      InputStream jsonStream = ObjectJson.getStream(this,ordinal);
234
      InputStream meshStream = ObjectMesh.getStream(this,ordinal);
235
      control.changeIfDifferent(ordinal,meshState,jsonStream,meshStream);
236 b20e89d2 Leszek Koltunski
      }
237
238 344f290c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240 dd1a65c1 Leszek Koltunski
    TutorialScreen getState()
241 344f290c Leszek Koltunski
      {
242 1237d25d Leszek Koltunski
      return mScreen;
243 344f290c Leszek Koltunski
      }
244
245 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
// PUBLIC API
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
    public FirebaseAnalytics getAnalytics()
250
      {
251
      return mFirebaseAnalytics;
252
      }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
    public int getScreenWidthInPixels()
257
      {
258
      return mScreenWidth;
259
      }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
    public int getScreenHeightInPixels()
264
      {
265
      return mScreenHeight;
266
      }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270 2afc6754 Leszek Koltunski
    public ObjectControl getControl()
271 55e6be1d Leszek Koltunski
      {
272
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
273 2afc6754 Leszek Koltunski
      return view.getObjectControl();
274 55e6be1d Leszek Koltunski
      }
275
276 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278
    public static int getDrawableSize()
279
      {
280
      if( mScreenHeight<1000 )
281
        {
282
        return 0;
283
        }
284
      if( mScreenHeight<1600 )
285
        {
286
        return 1;
287
        }
288
      if( mScreenHeight<1900 )
289
        {
290
        return 2;
291
        }
292
293
      return 3;
294
      }
295
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
    public static int getDrawable(int small, int medium, int big, int huge)
299
      {
300
      int size = getDrawableSize();
301
302
      switch(size)
303
        {
304
        case 0 : return small;
305
        case 1 : return medium;
306
        case 2 : return big;
307
        default: return huge;
308
        }
309
      }
310
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
    public boolean isVertical()
314
      {
315
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
316
      return view.isVertical();
317
      }
318
}