Project

General

Profile

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

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

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.RubikObjectList;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class TutorialActivity extends AppCompatActivity
50
{
51
    private static final String URL = "https://www.youtube.com/embed/";
52

    
53
    private static final int ACTIVITY_NUMBER = 1;
54
    public static final float BAR_RATIO = 0.17f;
55
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
56
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
57

    
58
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
59
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
60
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
61
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
62
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
63

    
64
    private FirebaseAnalytics mFirebaseAnalytics;
65
    private static int mScreenWidth, mScreenHeight;
66
    private int mCurrentApiVersion;
67
    private TutorialScreen mScreen;
68
    private String mURL;
69
    private int mObjectOrdinal;
70
    private TutorialWebView mWebView;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
82
      Bundle b = getIntent().getExtras();
83

    
84
      if(b != null)
85
        {
86
        mURL           = b.getString("url");
87
        mObjectOrdinal = b.getInt("obj");
88
        }
89

    
90
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
91

    
92
      DisplayMetrics displaymetrics = new DisplayMetrics();
93
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
94
      mScreenWidth =displaymetrics.widthPixels;
95
      mScreenHeight=displaymetrics.heightPixels;
96

    
97
      hideNavigationBar();
98
      cutoutHack();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
    private void hideNavigationBar()
104
      {
105
      mCurrentApiVersion = Build.VERSION.SDK_INT;
106

    
107
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
108
        {
109
        final View decorView = getWindow().getDecorView();
110

    
111
        decorView.setSystemUiVisibility(FLAGS);
112

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    @Override
130
    public void onAttachedToWindow()
131
      {
132
      super.onAttachedToWindow();
133

    
134
      float width = getScreenWidthInPixels();
135

    
136
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
137
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
138
      paramsR.width = (int)(width*BAR_RATIO);
139
      viewR.setLayoutParams(paramsR);
140

    
141
      if( mScreen==null ) mScreen = new TutorialScreen();
142

    
143
      mScreen.createRightPane(this);
144

    
145
      WebView videoView = findViewById(R.id.tutorialVideoView);
146
      mWebView = new TutorialWebView(videoView);
147
      mWebView.load(URL+mURL);
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// do not avoid cutouts
152

    
153
    private void cutoutHack()
154
      {
155
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
156
        {
157
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
158
        }
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    @Override
164
    public void onWindowFocusChanged(boolean hasFocus)
165
      {
166
      super.onWindowFocusChanged(hasFocus);
167

    
168
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
169
        {
170
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
171
        }
172
      }
173

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

    
183
      if( mWebView!=null ) mWebView.onPause();
184

    
185
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    @Override
191
    protected void onResume() 
192
      {
193
      super.onResume();
194
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
195
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
196
      view.onResume();
197

    
198
      if( mWebView!=null ) mWebView.onResume();
199

    
200
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
201
        {
202
        changeIfDifferent(mObjectOrdinal,view.getObjectControl());
203
        }
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
    
208
    @Override
209
    protected void onDestroy() 
210
      {
211
      super.onDestroy();
212
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    void OpenGLError()
218
      {
219
      RubikDialogError errDiag = new RubikDialogError();
220
      errDiag.show(getSupportFragmentManager(), null);
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    private void changeIfDifferent(int objectOrdinal,ObjectControl control)
226
      {
227
      InputStream jsonStream = ObjectJson.getStream(this,objectOrdinal);
228
      InputStream meshStream = ObjectMesh.getStream(this,objectOrdinal);
229
      control.changeIfDifferent(objectOrdinal,jsonStream,meshStream);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    TutorialScreen getState()
235
      {
236
      return mScreen;
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
// PUBLIC API
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
    public FirebaseAnalytics getAnalytics()
244
      {
245
      return mFirebaseAnalytics;
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
    public int getScreenWidthInPixels()
251
      {
252
      return mScreenWidth;
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    public int getScreenHeightInPixels()
258
      {
259
      return mScreenHeight;
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    public ObjectControl getControl()
265
      {
266
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
267
      return view.getObjectControl();
268
      }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
    public static int getDrawableSize()
273
      {
274
      if( mScreenHeight<1000 )
275
        {
276
        return 0;
277
        }
278
      if( mScreenHeight<1600 )
279
        {
280
        return 1;
281
        }
282
      if( mScreenHeight<1900 )
283
        {
284
        return 2;
285
        }
286

    
287
      return 3;
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public static int getDrawable(int small, int medium, int big, int huge)
293
      {
294
      int size = getDrawableSize();
295

    
296
      switch(size)
297
        {
298
        case 0 : return small;
299
        case 1 : return medium;
300
        case 2 : return big;
301
        default: return huge;
302
        }
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public boolean isVertical()
308
      {
309
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
310
      return view.isVertical();
311
      }
312
}
(1-1/7)