1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.support.car.ui;
17 
18 import android.content.Context;
19 import android.os.Parcelable;
20 import android.util.AttributeSet;
21 import android.widget.CheckBox;
22 
23 /**
24  * A wrapper class for CheckBox that is required because the state is created by two different
25  * class loaders, which causes a crash. When the state and class are created by different class
26  * loaders, the default state will be restored instead of the saved state.
27  * Reflection cannot be used to recreate the state because the class that stores the state
28  * (CompoundButton$SavedState) has been stripped out of the Android SDK.
29  */
30 public class CheckboxWrapperView extends CheckBox {
31 
CheckboxWrapperView(Context context)32     public CheckboxWrapperView(Context context) {
33         super(context);
34     }
35 
CheckboxWrapperView(Context context, AttributeSet attrs)36     public CheckboxWrapperView(Context context, AttributeSet attrs) {
37         super(context, attrs);
38     }
39 
CheckboxWrapperView(Context context, AttributeSet attrs, int defStyle)40     public CheckboxWrapperView(Context context, AttributeSet attrs, int defStyle) {
41         super(context, attrs, defStyle);
42     }
43 
44     @Override
onRestoreInstanceState(Parcelable state)45     public void onRestoreInstanceState(Parcelable state) {
46         // If the class loaders are different, just restore the default state. This is fine
47         // since we refetch the menus anyways and any state will be restored then.
48         if (state.getClass().getClassLoader() != getClass().getClassLoader()) {
49             super.onRestoreInstanceState(onSaveInstanceState());
50         } else {
51             super.onRestoreInstanceState(state);
52         }
53     }
54 }
55