Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 1b3cbd5b

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.main;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
import android.preference.PreferenceManager;
25
import androidx.appcompat.app.AppCompatActivity;
26
import android.view.View;
27

    
28
import com.google.firebase.analytics.FirebaseAnalytics;
29

    
30
import org.distorted.dialogs.RubikDialogAbout;
31
import org.distorted.dialogs.RubikDialogScores;
32
import org.distorted.dialogs.RubikDialogEffects;
33
import org.distorted.effects.BaseEffect;
34
import org.distorted.library.main.DistortedLibrary;
35

    
36
import org.distorted.objects.RubikObject;
37
import org.distorted.scores.RubikScores;
38
import org.distorted.scores.RubikScoresDownloader;
39
import org.distorted.objects.RubikObjectList;
40
import org.distorted.states.RubikState;
41
import org.distorted.states.RubikStatePlay;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikActivity extends AppCompatActivity
46
{
47
    private boolean mJustStarted;
48
    private FirebaseAnalytics mFirebaseAnalytics;
49

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

    
52
    @Override
53
    protected void onCreate(Bundle savedState)
54
      {
55
      super.onCreate(savedState);
56
      setTheme(R.style.CustomActivityThemeNoActionBar);
57
      setContentView(R.layout.main);
58

    
59
      mJustStarted = true;
60
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
    
65
    @Override
66
    protected void onPause() 
67
      {
68
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
69
      view.onPause();
70
      DistortedLibrary.onPause();
71
      RubikScoresDownloader.onPause();
72
      savePreferences();
73
      super.onPause();
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
78
    @Override
79
    protected void onResume() 
80
      {
81
      super.onResume();
82
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
83
      view.onResume();
84
      restorePreferences();
85
      RubikState.setState(this);
86

    
87
      if( mJustStarted )
88
        {
89
        mJustStarted = false;
90
        RubikScores scores = RubikScores.getInstance();
91
        scores.incrementNumRuns();
92
        scores.setCountry(this);
93
        }
94

    
95
      boolean success = false;
96
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
97
      int object = play.getObject();
98
      int size   = play.getSize();
99

    
100
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
101
        {
102
        RubikObjectList obj = RubikObjectList.getObject(object);
103
        int[] sizes = obj.getSizes();
104
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
105

    
106
        if( sizeIndex>=0 && sizeIndex<sizes.length )
107
          {
108
          success = true;
109
          view.getPostRender().changeObject(obj,size);
110
          }
111

    
112
        }
113

    
114
      if( !success )
115
        {
116
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
117
        int s = RubikStatePlay.DEF_SIZE;
118

    
119
        play.setObjectAndSize(this,obj,s);
120
        view.getPostRender().changeObject(obj,s);
121
        }
122
      }
123
    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
    @Override
127
    protected void onDestroy() 
128
      {
129
      DistortedLibrary.onDestroy();
130
      super.onDestroy();
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    private void savePreferences()
136
      {
137
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
138
      SharedPreferences.Editor editor = preferences.edit();
139

    
140
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
141
        {
142
        BaseEffect.Type.getType(i).savePreferences(editor);
143
        }
144

    
145
      for (int i=0; i<RubikState.LENGTH; i++)
146
        {
147
        RubikState.getState(i).getStateClass().savePreferences(editor);
148
        }
149

    
150
      RubikState.savePreferences(editor);
151
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
152
      view.getPostRender().savePreferences(editor);
153

    
154
      editor.apply();
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    private void restorePreferences()
160
      {
161
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
162

    
163
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
164
        {
165
        BaseEffect.Type.getType(i).restorePreferences(preferences);
166
        }
167

    
168
      for (int i=0; i< RubikState.LENGTH; i++)
169
        {
170
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
171
        }
172

    
173
      RubikState.restorePreferences(preferences);
174

    
175
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
176
      view.getPostRender().restorePreferences(preferences);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
// PUBLIC API
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    public FirebaseAnalytics getAnalytics()
184
      {
185
      return mFirebaseAnalytics;
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    public RubikObject getObject()
191
      {
192
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
193
      RubikPostRender post = view.getPostRender();
194
      return post.getObject();
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    public RubikPostRender getPostRender()
200
      {
201
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
202
      return view.getPostRender();
203
      }
204

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

    
207
    public void changeObject(RubikObjectList object, int size)
208
      {
209
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
210
      RubikPostRender post = view.getPostRender();
211
      post.changeObject(object,size);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public void setupObject(RubikObjectList object, int size, int[][] moves)
217
      {
218
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
219
      RubikPostRender post = view.getPostRender();
220
      post.setupObject(object,size,moves);
221
      }
222

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

    
225
    public boolean isVertical()
226
      {
227
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
228
      return view.isVertical();
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    public void Play(View v)
234
      {
235
      RubikState.switchState(this,RubikState.PLAY);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    public void Effects(View v)
241
      {
242
      RubikDialogEffects settings = new RubikDialogEffects();
243
      settings.show(getSupportFragmentManager(), null);
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    public void Scores(View v)
249
      {
250
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
251
      int object = play.getObject();
252
      int size   = play.getSize();
253
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
254

    
255
      Bundle bundle = new Bundle();
256
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
257
      bundle.putBoolean("submitting", false);
258

    
259
      RubikDialogScores scores = new RubikDialogScores();
260
      scores.setArguments(bundle);
261
      scores.show(getSupportFragmentManager(), null);
262
      }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
    public void Patterns(View v)
267
      {
268
      RubikState.switchState(this,RubikState.PATT);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public void Solver(View v)
274
      {
275
      RubikState.switchState(this,RubikState.SVER);
276
      }
277

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

    
280
    public void About(View v)
281
      {
282
      RubikDialogAbout diag = new RubikDialogAbout();
283
      diag.show(getSupportFragmentManager(), null);
284
      }
285
}
(1-1/4)