Use ParcelableCompat for CREATOR creation

Should fix BadParcelableException issues due to
using the wrong available class loader.

BUG: 24253575
Change-Id: Ia259559f15500203eff99c6eb6928339bd940278
diff --git a/design/api/current.txt b/design/api/current.txt
index 5ec5c54..f68ab1b 100644
--- a/design/api/current.txt
+++ b/design/api/current.txt
@@ -32,7 +32,7 @@
   }
 
   protected static class AppBarLayout.Behavior.SavedState extends android.view.View.BaseSavedState {
-    ctor public AppBarLayout.Behavior.SavedState(android.os.Parcel);
+    ctor public AppBarLayout.Behavior.SavedState(android.os.Parcel, java.lang.ClassLoader);
     ctor public AppBarLayout.Behavior.SavedState(android.os.Parcelable);
     field public static final android.os.Parcelable.Creator<android.support.design.widget.AppBarLayout.Behavior.SavedState> CREATOR;
   }
@@ -184,7 +184,7 @@
   }
 
   protected static class CoordinatorLayout.SavedState extends android.view.View.BaseSavedState {
-    ctor public CoordinatorLayout.SavedState(android.os.Parcel);
+    ctor public CoordinatorLayout.SavedState(android.os.Parcel, java.lang.ClassLoader);
     ctor public CoordinatorLayout.SavedState(android.os.Parcelable);
     field public static final android.os.Parcelable.Creator<android.support.design.widget.CoordinatorLayout.SavedState> CREATOR;
   }
@@ -250,7 +250,7 @@
   }
 
   public static class NavigationView.SavedState extends android.view.View.BaseSavedState {
-    ctor public NavigationView.SavedState(android.os.Parcel);
+    ctor public NavigationView.SavedState(android.os.Parcel, java.lang.ClassLoader);
     ctor public NavigationView.SavedState(android.os.Parcelable);
     field public static final android.os.Parcelable.Creator<android.support.design.widget.NavigationView.SavedState> CREATOR;
     field public android.os.Bundle menuState;
diff --git a/design/src/android/support/design/internal/ParcelableSparseArray.java b/design/src/android/support/design/internal/ParcelableSparseArray.java
index 74abcc2..588950b 100644
--- a/design/src/android/support/design/internal/ParcelableSparseArray.java
+++ b/design/src/android/support/design/internal/ParcelableSparseArray.java
@@ -18,6 +18,8 @@
 
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.support.v4.os.ParcelableCompat;
+import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.util.SparseArray;
 
 /**
@@ -29,13 +31,12 @@
         super();
     }
 
-    public ParcelableSparseArray(Parcel source) {
+    public ParcelableSparseArray(Parcel source, ClassLoader loader) {
         super();
         int size = source.readInt();
         int[] keys = new int[size];
         source.readIntArray(keys);
-        Parcelable[] values = source.readParcelableArray(
-                ParcelableSparseArray.class.getClassLoader());
+        Parcelable[] values = source.readParcelableArray(loader);
         for (int i = 0; i < size; ++i) {
             put(keys[i], values[i]);
         }
@@ -60,17 +61,18 @@
         parcel.writeParcelableArray(values, flags);
     }
 
-    public static final Parcelable.Creator<ParcelableSparseArray> CREATOR
-            = new Creator<ParcelableSparseArray>() {
-        @Override
-        public ParcelableSparseArray createFromParcel(Parcel source) {
-            return new ParcelableSparseArray(source);
-        }
+    public static final Parcelable.Creator<ParcelableSparseArray> CREATOR =
+            ParcelableCompat
+                    .newCreator(new ParcelableCompatCreatorCallbacks<ParcelableSparseArray>() {
+                        @Override
+                        public ParcelableSparseArray createFromParcel(Parcel source,
+                                ClassLoader loader) {
+                            return new ParcelableSparseArray(source, loader);
+                        }
 
-        @Override
-        public ParcelableSparseArray[] newArray(int size) {
-            return new ParcelableSparseArray[size];
-        }
-    };
-
+                        @Override
+                        public ParcelableSparseArray[] newArray(int size) {
+                            return new ParcelableSparseArray[size];
+                        }
+                    });
 }
diff --git a/design/src/android/support/design/widget/AppBarLayout.java b/design/src/android/support/design/widget/AppBarLayout.java
index d0b5309..f0cfe34 100644
--- a/design/src/android/support/design/widget/AppBarLayout.java
+++ b/design/src/android/support/design/widget/AppBarLayout.java
@@ -24,6 +24,8 @@
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.design.R;
+import android.support.v4.os.ParcelableCompat;
+import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.support.v4.view.ViewCompat;
 import android.support.v4.view.WindowInsetsCompat;
 import android.util.AttributeSet;
@@ -1061,12 +1063,12 @@
             }
         }
 
-        protected static class SavedState extends View.BaseSavedState {
+        protected static class SavedState extends BaseSavedState {
             int firstVisibleChildIndex;
             float firstVisibileChildPercentageShown;
             boolean firstVisibileChildAtMinimumHeight;
 
-            public SavedState(Parcel source) {
+            public SavedState(Parcel source, ClassLoader loader) {
                 super(source);
                 firstVisibleChildIndex = source.readInt();
                 firstVisibileChildPercentageShown = source.readFloat();
@@ -1086,17 +1088,17 @@
             }
 
             public static final Parcelable.Creator<SavedState> CREATOR =
-                    new Parcelable.Creator<SavedState>() {
+                    ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {
                         @Override
-                        public SavedState createFromParcel(Parcel source) {
-                            return new SavedState(source);
+                        public SavedState createFromParcel(Parcel source, ClassLoader loader) {
+                            return new SavedState(source, loader);
                         }
 
                         @Override
                         public SavedState[] newArray(int size) {
                             return new SavedState[size];
                         }
-                    };
+                    });
         }
     }
 
diff --git a/design/src/android/support/design/widget/CoordinatorLayout.java b/design/src/android/support/design/widget/CoordinatorLayout.java
index 2ca18c3..5808ce8 100644
--- a/design/src/android/support/design/widget/CoordinatorLayout.java
+++ b/design/src/android/support/design/widget/CoordinatorLayout.java
@@ -31,6 +31,8 @@
 import android.os.SystemClock;
 import android.support.design.R;
 import android.support.v4.content.ContextCompat;
+import android.support.v4.os.ParcelableCompat;
+import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.view.MotionEventCompat;
 import android.support.v4.view.NestedScrollingParent;
@@ -2586,7 +2588,7 @@
     protected static class SavedState extends BaseSavedState {
         SparseArray<Parcelable> behaviorStates;
 
-        public SavedState(Parcel source) {
+        public SavedState(Parcel source, ClassLoader loader) {
             super(source);
 
             final int size = source.readInt();
@@ -2594,8 +2596,7 @@
             final int[] ids = new int[size];
             source.readIntArray(ids);
 
-            final Parcelable[] states = source.readParcelableArray(
-                    CoordinatorLayout.class.getClassLoader());
+            final Parcelable[] states = source.readParcelableArray(loader);
 
             behaviorStates = new SparseArray<>(size);
             for (int i = 0; i < size; i++) {
@@ -2626,18 +2627,18 @@
 
         }
 
-        public static final Parcelable.Creator<SavedState> CREATOR =
-                new Parcelable.Creator<SavedState>() {
-                    @Override
-                    public SavedState createFromParcel(Parcel source) {
-                        return new SavedState(source);
-                    }
+        public static final Parcelable.Creator<SavedState> CREATOR
+                = ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {
+            @Override
+            public SavedState createFromParcel(Parcel in, ClassLoader loader) {
+                return new SavedState(in, loader);
+            }
 
-                    @Override
-                    public SavedState[] newArray(int size) {
-                        return new SavedState[size];
-                    }
-                };
+            @Override
+            public SavedState[] newArray(int size) {
+                return new SavedState[size];
+            }
+        });
     }
 
     private static void selectionSort(final List<View> list, final Comparator<View> comparator) {
diff --git a/design/src/android/support/design/widget/NavigationView.java b/design/src/android/support/design/widget/NavigationView.java
index 6f606d4..4ce8cbb 100644
--- a/design/src/android/support/design/widget/NavigationView.java
+++ b/design/src/android/support/design/widget/NavigationView.java
@@ -35,6 +35,8 @@
 import android.support.design.internal.NavigationMenuView;
 import android.support.design.internal.ScrimInsetsFrameLayout;
 import android.support.v4.content.ContextCompat;
+import android.support.v4.os.ParcelableCompat;
+import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.support.v4.view.ViewCompat;
 import android.support.v7.internal.view.SupportMenuInflater;
 import android.support.v7.internal.view.menu.MenuBuilder;
@@ -452,9 +454,9 @@
     public static class SavedState extends BaseSavedState {
         public Bundle menuState;
 
-        public SavedState(Parcel in) {
+        public SavedState(Parcel in, ClassLoader loader) {
             super(in);
-            menuState = in.readBundle();
+            menuState = in.readBundle(loader);
         }
 
         public SavedState(Parcelable superState) {
@@ -468,17 +470,17 @@
         }
 
         public static final Parcelable.Creator<SavedState> CREATOR
-                = new Parcelable.Creator<SavedState>() {
+                = ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {
             @Override
-            public SavedState createFromParcel(Parcel parcel) {
-                return new SavedState(parcel);
+            public SavedState createFromParcel(Parcel parcel, ClassLoader loader) {
+                return new SavedState(parcel, loader);
             }
 
             @Override
             public SavedState[] newArray(int size) {
                 return new SavedState[size];
             }
-        };
+        });
     }
 
 }
diff --git a/v7/appcompat/src/android/support/v7/app/AppCompatDelegateImplV7.java b/v7/appcompat/src/android/support/v7/app/AppCompatDelegateImplV7.java
index a438cd7..c61f979 100644
--- a/v7/appcompat/src/android/support/v7/app/AppCompatDelegateImplV7.java
+++ b/v7/appcompat/src/android/support/v7/app/AppCompatDelegateImplV7.java
@@ -31,6 +31,8 @@
 import android.os.Parcelable;
 import android.support.annotation.NonNull;
 import android.support.v4.app.NavUtils;
+import android.support.v4.os.ParcelableCompat;
+import android.support.v4.os.ParcelableCompatCreatorCallbacks;
 import android.support.v4.view.LayoutInflaterCompat;
 import android.support.v4.view.LayoutInflaterFactory;
 import android.support.v4.view.OnApplyWindowInsetsListener;
@@ -1956,28 +1958,31 @@
                 }
             }
 
-            private static SavedState readFromParcel(Parcel source) {
+            private static SavedState readFromParcel(Parcel source, ClassLoader loader) {
                 SavedState savedState = new SavedState();
                 savedState.featureId = source.readInt();
                 savedState.isOpen = source.readInt() == 1;
 
                 if (savedState.isOpen) {
-                    savedState.menuState = source.readBundle();
+                    savedState.menuState = source.readBundle(loader);
                 }
 
                 return savedState;
             }
 
             public static final Parcelable.Creator<SavedState> CREATOR
-                    = new Parcelable.Creator<SavedState>() {
-                public SavedState createFromParcel(Parcel in) {
-                    return readFromParcel(in);
-                }
+                    = ParcelableCompat.newCreator(
+                    new ParcelableCompatCreatorCallbacks<SavedState>() {
+                        @Override
+                        public SavedState createFromParcel(Parcel in, ClassLoader loader) {
+                            return readFromParcel(in, loader);
+                        }
 
-                public SavedState[] newArray(int size) {
-                    return new SavedState[size];
-                }
-            };
+                        @Override
+                        public SavedState[] newArray(int size) {
+                            return new SavedState[size];
+                        }
+                    });
         }
     }