Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f83cd4ec authored by James Lemieux's avatar James Lemieux Committed by Android (Google) Code Review
Browse files

Merge "NumberPickers in settings now survive device rotations" into ub-deskclock-charm

parents ea9f0d60 b3d1bc33
Loading
Loading
Loading
Loading
+69 −1
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock;

import android.annotation.TargetApi;
@@ -6,13 +22,16 @@ import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.NumberPicker;

import java.lang.reflect.Field;

/**
 * Subclass of NumberPicker that allows customizing divider color.
 * Subclass of NumberPicker that allows customizing divider color and saves/restores its value
 * across device rotations.
 */
public class NumberPickerCompat extends NumberPicker {

@@ -61,4 +80,53 @@ public class NumberPickerCompat extends NumberPicker {
            }
        }
    }

    /**
     * @return the state of this NumberPicker including the currently selected value
     */
    @Override
    protected Parcelable onSaveInstanceState() {
        return new State(super.onSaveInstanceState(), getValue());
    }

    /**
     * @param state the state of this NumberPicker including the value to select
     */
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        final State instanceState = (State) state;
        super.onRestoreInstanceState(instanceState.getSuperState());
        setValue(instanceState.mValue);
    }

    /**
     * The state of this NumberPicker including the selected value. Used to preserve values across
     * device rotation.
     */
    private static final class State extends BaseSavedState {

        private final int mValue;

        public State(Parcel source) {
            super(source);
            mValue = source.readInt();
        }

        public State(Parcelable superState, int value) {
            super(superState);
            mValue = value;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(mValue);
        }

        public static final Parcelable.Creator<State> CREATOR =
                new Parcelable.Creator<State>() {
                    public State createFromParcel(Parcel in) { return new State(in); }
                    public State[] newArray(int size) { return new State[size]; }
                };
    }
}
 No newline at end of file
+15 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package com.android.deskclock.settings;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
@@ -85,10 +86,22 @@ public final class CrescendoLengthDialog extends DialogPreference {
        return a.getString(index);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        // Restore the value to the NumberPicker.
        super.onRestoreInstanceState(state);

        // Update the unit display in response to the new value.
        updateUnits();
    }

    private void updateUnits() {
        final int visibility = mNumberPickerView.getValue() == 0 ? View.INVISIBLE : View.VISIBLE;
        if (mNumberPickerView != null) {
            final int value = mNumberPickerView.getValue();
            final int visibility = value == 0 ? View.INVISIBLE : View.VISIBLE;
            mNumberPickerSecondsView.setVisibility(visibility);
        }
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
+17 −2
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.deskclock.settings;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
@@ -94,9 +96,22 @@ public final class SnoozeLengthDialog extends DialogPreference {
        return a.getString(index);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        // Restore the value to the NumberPicker.
        super.onRestoreInstanceState(state);

        // Update the unit display in response to the new value.
        updateUnits();
    }

    private void updateUnits() {
        mNumberPickerMinutesView.setText(mContext.getResources()
                .getQuantityText(R.plurals.snooze_picker_label, mNumberPickerView.getValue()));
        if (mNumberPickerView != null) {
            final Resources res = mContext.getResources();
            final int value = mNumberPickerView.getValue();
            final CharSequence units = res.getQuantityText(R.plurals.snooze_picker_label, value);
            mNumberPickerMinutesView.setText(units);
        }
    }

    @Override