Update local state when creating LayerDrawable from constant state

Also clears DrawableContainer's futures list when it's no longer needed,
correctly sets deep copy of state set in StateListDrawable, makes some
private methods into package-protected to avoid thunk, and propagates
state to StateListDrawable's super class so that getState() has correct
information.

Bug: 21840003
Change-Id: I0d4232807f280d663c03b4a80e4aab8626806440
This commit is contained in:
Alan Viverette
2015-06-24 14:42:44 -07:00
parent e4ffec210e
commit 06ff2af68a
5 changed files with 28 additions and 20 deletions

View File

@@ -575,7 +575,7 @@ public class AnimatedStateListDrawable extends StateListDrawable {
}
}
private void mutate() {
void mutate() {
mTransitions = mTransitions.clone();
mStateIds = mStateIds.clone();
}

View File

@@ -730,7 +730,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
if (origDf != null) {
mDrawableFutures = origDf.clone();
} else {
mDrawableFutures = new SparseArray<ConstantStateFuture>(mNumChildren);
mDrawableFutures = new SparseArray<>(mNumChildren);
}
// Create futures for drawables with constant states. If a
@@ -823,6 +823,9 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
final Drawable prepared = mDrawableFutures.valueAt(keyIndex).get(this);
mDrawables[index] = prepared;
mDrawableFutures.removeAt(keyIndex);
if (mDrawableFutures.size() == 0) {
mDrawableFutures = null;
}
return prepared;
}
}

View File

@@ -133,6 +133,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mLayerState.mChildren = r;
ensurePadding();
refreshPadding();
}
LayerDrawable() {
@@ -143,6 +144,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mLayerState = createConstantState(state, res);
if (mLayerState.mNum > 0) {
ensurePadding();
refreshPadding();
}
}
@@ -162,6 +164,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
inflateLayers(r, parser, attrs, theme);
ensurePadding();
refreshPadding();
}
/**
@@ -431,6 +434,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
final ChildDrawable layer = createLayer(dr);
final int index = addLayer(layer);
ensurePadding();
refreshChildPadding(index, layer);
return index;
}
@@ -568,6 +572,8 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
childDrawable.mDrawable = drawable;
mLayerState.invalidateCache();
refreshChildPadding(index, childDrawable);
}
/**
@@ -1623,6 +1629,14 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mPaddingB = new int[N];
}
void refreshPadding() {
final int N = mLayerState.mNum;
final ChildDrawable[] array = mLayerState.mChildren;
for (int i = 0; i < N; i++) {
refreshChildPadding(i, array[i]);
}
}
@Override
public ConstantState getConstantState() {
if (mLayerState.canConstantState()) {

View File

@@ -198,6 +198,7 @@ public class RippleDrawable extends LayerDrawable {
setColor(color);
ensurePadding();
refreshPadding();
updateLocalState();
}
@@ -1013,6 +1014,7 @@ public class RippleDrawable extends LayerDrawable {
if (mState.mNum > 0) {
ensurePadding();
refreshPadding();
}
if (res != null) {

View File

@@ -59,22 +59,10 @@ import android.util.StateSet;
* @attr ref android.R.styleable#DrawableStates_state_pressed
*/
public class StateListDrawable extends DrawableContainer {
private static final String TAG = StateListDrawable.class.getSimpleName();
private static final String TAG = "StateListDrawable";
private static final boolean DEBUG = false;
/**
* To be proper, we should have a getter for dither (and alpha, etc.)
* so that proxy classes like this can save/restore their delegates'
* values, but we don't have getters. Since we do have setters
* (e.g. setDither), which this proxy forwards on, we have to have some
* default/initial setting.
*
* The initial setting for dither is now true, since it almost always seems
* to improve the quality at negligible cost.
*/
private static final boolean DEFAULT_DITHER = true;
private StateListState mStateListState;
private boolean mMutated;
@@ -104,16 +92,16 @@ public class StateListDrawable extends DrawableContainer {
@Override
protected boolean onStateChange(int[] stateSet) {
final boolean changed = super.onStateChange(stateSet);
int idx = mStateListState.indexOfStateSet(stateSet);
if (DEBUG) android.util.Log.i(TAG, "onStateChange " + this + " states "
+ Arrays.toString(stateSet) + " found " + idx);
if (idx < 0) {
idx = mStateListState.indexOfStateSet(StateSet.WILD_CARD);
}
if (selectDrawable(idx)) {
return true;
}
return super.onStateChange(stateSet);
return selectDrawable(idx) || changed;
}
@Override
@@ -326,13 +314,14 @@ public class StateListDrawable extends DrawableContainer {
}
}
private void mutate() {
void mutate() {
mThemeAttrs = mThemeAttrs != null ? mThemeAttrs.clone() : null;
final int[][] stateSets = new int[mStateSets.length][];
for (int i = mStateSets.length - 1; i >= 0; i--) {
stateSets[i] = mStateSets[i] != null ? mStateSets[i].clone() : null;
}
mStateSets = stateSets;
}
int addStateSet(int[] stateSet, Drawable drawable) {