From a4e7a693f9ddee8e3eb4dd165505a4762cdad0ee Mon Sep 17 00:00:00 2001 From: Christopher Beckmann Date: Sun, 31 Jan 2016 12:46:22 +0100 Subject: [PATCH] Corrected Parceable Implementation. Wasn't aware that my code was not called. Had to force it to clear the app out of the memory. --- .../controller/GameController.java | 18 ++++++++++++------ .../privacyfriendlysudoku/game/GameBoard.java | 7 +++---- .../privacyfriendlysudoku/game/GameCell.java | 5 +++-- .../privacyfriendlysudoku/game/GameType.java | 3 +-- .../privacyfriendlysudoku/ui/GameActivity.java | 1 + 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java index f7120bd..8ffc158 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java @@ -54,7 +54,7 @@ public class GameController implements IModelChangedListener, Parcelable { private int sectionWidth; private int usedHints = 0; private GameBoard gameBoard; - private int[] solution; + private int[] solution = new int[0]; private GameType gameType; private GameDifficulty difficulty; private CellConflictList errorList = new CellConflictList(); @@ -699,6 +699,7 @@ public class GameController implements IModelChangedListener, Parcelable { out.writeInt(usedHints); out.writeInt(time); + out.writeInt(solution.length); out.writeIntArray(solution); out.writeInt(noteStatus ? 1 : 0); @@ -738,18 +739,18 @@ public class GameController implements IModelChangedListener, Parcelable { usedHints = in.readInt(); time = in.readInt(); + solution = new int[in.readInt()]; in.readIntArray(solution); noteStatus = in.readInt() == 1; notifiedOnSolvedListeners = in.readInt() == 1; - gameType = in.readParcelable(null); - difficulty = in.readParcelable(null); - gameBoard = in.readParcelable(null); - undoRedoManager = in.readParcelable(null); + gameType = in.readParcelable(GameType.class.getClassLoader()); + difficulty = in.readParcelable(GameDifficulty.class.getClassLoader()); + gameBoard = in.readParcelable(GameBoard.class.getClassLoader()); + undoRedoManager = in.readParcelable(UndoRedoManager.class.getClassLoader()); removeAllListeners(); - } public void removeAllListeners() { @@ -758,4 +759,9 @@ public class GameController implements IModelChangedListener, Parcelable { hintListener = new LinkedList<>(); timerListeners = new LinkedList<>(); } + + public void setContextAndSettings(Context applicationContext, SharedPreferences sharedPref) { + context = applicationContext; + setSettings(sharedPref); + } } diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameBoard.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameBoard.java index ae34c9f..febef69 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameBoard.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameBoard.java @@ -290,7 +290,7 @@ public class GameBoard implements Cloneable, Parcelable { dest.writeInt(size); for(int i = 0; i < field.length; i++) { - dest.writeParcelableArray(field[i], 0); + dest.writeTypedArray(field[i], 0); } } @@ -308,7 +308,7 @@ public class GameBoard implements Cloneable, Parcelable { /** recreate object from parcel */ private GameBoard(Parcel in) { //private int id; - gameType = in.readParcelable(null); + gameType = in.readParcelable(GameType.class.getClassLoader()); sectionHeight = in.readInt(); sectionWidth = in.readInt(); size = in.readInt(); @@ -316,8 +316,7 @@ public class GameBoard implements Cloneable, Parcelable { field = new GameCell[size][size]; for(int i = 0; i < field.length; i++) { - // TODO: does this work?! - field[i] = (GameCell[]) in.readParcelableArray(null); + field[i] = in.createTypedArray(GameCell.CREATOR); } modelChangedListeners = new LinkedList<>(); diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameCell.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameCell.java index 2b9e456..928d72d 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameCell.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameCell.java @@ -213,10 +213,10 @@ public class GameCell implements Cloneable, Parcelable { dest.writeInt(row); dest.writeInt(col); dest.writeInt(value); + dest.writeInt(size); dest.writeInt(fixed ? 1 : 0); dest.writeInt(noteCount); dest.writeBooleanArray(notes); - dest.writeInt(size); } public static final Parcelable.Creator CREATOR @@ -235,9 +235,10 @@ public class GameCell implements Cloneable, Parcelable { row = in.readInt(); col = in.readInt(); value = in.readInt(); + size = in.readInt(); fixed = in.readInt() == 1; noteCount = in.readInt(); + notes = new boolean[size]; in.readBooleanArray(notes); - size = in.readInt(); } } diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameType.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameType.java index 4643f1f..a93478a 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameType.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/game/GameType.java @@ -72,8 +72,7 @@ public enum GameType implements Parcelable{ dest.writeInt(resIDImage); } - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public GameType createFromParcel(Parcel in) { GameType g = GameType.values()[in.readInt()]; g.resIDString = in.readInt(); diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java index b8606c2..ba358ee 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java @@ -103,6 +103,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On // in case we get the same object back // because parceling the Object does not always parcel it. Only if needed. gameController.removeAllListeners(); + gameController.setContextAndSettings(getApplicationContext(), sharedPref); gameSolved = savedInstanceState.getInt("gameSolved") == 1; }