Project

General

Profile

« Previous | Next » 

Revision fe381d8e

Added by Leszek Koltunski over 5 years ago

Remember settings across app restarts.

View differences:

src/main/java/org/distorted/magic/RubikActivity.java
19 19

  
20 20
package org.distorted.magic;
21 21

  
22
import android.content.SharedPreferences;
22 23
import android.graphics.PorterDuff;
23 24
import android.graphics.drawable.Drawable;
24 25
import android.opengl.GLSurfaceView;
25 26
import android.os.Bundle;
27
import android.preference.PreferenceManager;
26 28
import android.support.v4.content.ContextCompat;
27 29
import android.support.v7.app.AppCompatActivity;
28 30
import android.view.View;
......
39 41
    private static final int SMALLEST_SIZE = 2;
40 42
    private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
41 43

  
44
    private static final int DEFAULT_APPEAR_POS     = 10;
45
    private static final int DEFAULT_DISAPPEAR_POS  = 10;
46
    private static final int DEFAULT_APPEAR_TYPE    = 1;
47
    private static final int DEFAULT_DISAPPEAR_TYPE = 1;
48

  
42 49
    private static int mSize = DEFAULT_SIZE;
43 50

  
44 51
    private int mAppearPos, mDisappearPos;
......
54 61
      setContentView(R.layout.layout);
55 62
      markButton(mSize);
56 63

  
57
      if( savedState==null )
58
        {
59
        onComplete(10,10,1,1);
60
        }
64
      restorePreferences();
65
      applyPreferences();
61 66
      }
62 67

  
63 68
///////////////////////////////////////////////////////////////////////////////////////////////////
......
68 73
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
69 74
      view.onPause();
70 75
      DistortedLibrary.onPause();
76
      savePreferences();
71 77
      super.onPause();
72 78
      }
73 79

  
......
90 96
      super.onDestroy();
91 97
      }
92 98

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

  
95
    @Override
96
    public void onSaveInstanceState(Bundle savedInstanceState)
97
      {
98
      super.onSaveInstanceState(savedInstanceState);
99

  
100
      savedInstanceState.putInt("appearPos"    , mAppearPos    );
101
      savedInstanceState.putInt("disappearPos" , mDisappearPos );
102
      savedInstanceState.putInt("appearType"   , mAppearType   );
103
      savedInstanceState.putInt("disappearType", mDisappearType);
104
      }
105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

  
108
    @Override
109
    public void onRestoreInstanceState(Bundle savedInstanceState)
110
      {
111
      super.onRestoreInstanceState(savedInstanceState);
112

  
113
      mAppearPos     = savedInstanceState.getInt("appearPos"    );
114
      mDisappearPos  = savedInstanceState.getInt("disappearPos" );
115
      mAppearType    = savedInstanceState.getInt("appearType"   );
116
      mDisappearType = savedInstanceState.getInt("disappearType");
117
      }
118

  
119 99
///////////////////////////////////////////////////////////////////////////////////////////////////
120 100

  
121 101
    static int getSize()
......
156 136
      mAppearType   = aT;
157 137
      mDisappearType= dT;
158 138

  
159
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
160
      RubikRenderer renderer = view.getRenderer();
161

  
162
      renderer.setAppearDuration(mAppearPos*100);
163
      renderer.setDisappearDuration(mDisappearPos*100);
164
      renderer.setAppearType(AppearEffect.getType(mAppearType));
165
      renderer.setDisappearType(DisappearEffect.getType(mDisappearType));
139
      applyPreferences();
166 140
      }
167 141

  
168 142
///////////////////////////////////////////////////////////////////////////////////////////////////
......
207 181
         }
208 182
       }
209 183
     }
184

  
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

  
187
   private void savePreferences()
188
     {
189
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
190
     SharedPreferences.Editor editor = preferences.edit();
191

  
192
     editor.putInt("appearPos"    , mAppearPos    );
193
     editor.putInt("disappearPos" , mDisappearPos );
194
     editor.putInt("appearType"   , mAppearType   );
195
     editor.putInt("disappearType", mDisappearType);
196

  
197
     editor.apply();
198
     }
199

  
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

  
202
   private void restorePreferences()
203
     {
204
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
205

  
206
     mAppearPos     = preferences.getInt("appearPos"    , DEFAULT_APPEAR_POS    );
207
     mDisappearPos  = preferences.getInt("disappearPos" , DEFAULT_DISAPPEAR_POS );
208
     mAppearType    = preferences.getInt("appearType"   , DEFAULT_APPEAR_TYPE   );
209
     mDisappearType = preferences.getInt("disappearType", DEFAULT_DISAPPEAR_TYPE);
210
     }
211

  
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

  
214
   private void applyPreferences()
215
     {
216
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
217
     RubikRenderer renderer = view.getRenderer();
218

  
219
     renderer.setAppearDuration(mAppearPos*100 +1);
220
     renderer.setDisappearDuration(mDisappearPos*100 +1);
221
     renderer.setAppearType(AppearEffect.getType(mAppearType));
222
     renderer.setDisappearType(DisappearEffect.getType(mDisappearType));
223
     }
210 224
}
src/main/java/org/distorted/magic/RubikRenderer.java
86 86
      mCanRotate = true;
87 87
      mCanDrag   = true;
88 88

  
89
      mAppearType    = AppearEffect.Type.SCALE;
90
      mDisappearType = DisappearEffect.Type.SCALE;
91

  
89
      mAppearType        = AppearEffect.Type.SCALE;
90
      mDisappearType     = DisappearEffect.Type.SCALE;
92 91
      mAppearDuration    = 1000;
93 92
      mDisappearDuration = 1000;
94 93

  
src/main/java/org/distorted/magic/RubikSettings.java
78 78

  
79 79
    Bundle args = getArguments();
80 80

  
81
    mAppearPos     = args.getInt("appearPos");
82
    mDisappearPos  = args.getInt("disappearPos");
83
    mAppearType    = args.getInt("appearType");
84
    mDisappearType = args.getInt("disappearType");
81
    try
82
      {
83
      mAppearPos     = args.getInt("appearPos");
84
      mDisappearPos  = args.getInt("disappearPos");
85
      mAppearType    = args.getInt("appearType");
86
      mDisappearType = args.getInt("disappearType");
87
      }
88
    catch(NullPointerException ex)
89
      {
90
      mAppearPos     = 10;
91
      mDisappearPos  = 10;
92
      mAppearType    =  1;
93
      mDisappearType =  1;
94
      }
85 95
    }
86 96

  
87 97
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff