summaryrefslogtreecommitdiff
path: root/src/com/android/calendar/MultiStateButton.kt
blob: f86ee6bafd3f84e4946c66f76036c0b075194811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2021 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.calendar

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.widget.Button

/**
 * A button with more than two states. When the button is pressed
 * or clicked, the state transitions automatically.
 *
 * **XML attributes**
 * See [ MultiStateButton Attributes][R.styleable.MultiStateButton],
 * [Button][android.R.styleable.Button], [TextView Attributes][android.R.styleable.TextView],
 * [ ][android.R.styleable.View]
 *
 */
class MultiStateButton(context: Context?, attrs: AttributeSet?, defStyle: Int) :
                       Button(context, attrs, defStyle) {
    //The current state for this button, ranging from 0 to maxState-1
    var mState = 0
        private set

    //The maximum number of states allowed for this button.
    private var mMaxStates = 1

    //The currently displaying resource ID. This gets set to a default on creation and remains
    //on the last set if the resources get set to null.
    private var mButtonResource = 0

    //A list of all drawable resources used by this button in the order it uses them.
    private var mButtonResources: IntArray
    private var mButtonDrawable: Drawable? = null

    constructor(context: Context?) : this(context, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) {}

    override fun performClick(): Boolean {
        /* When clicked, toggle the state */
        transitionState()
        return super.performClick()
    }

    fun transitionState() {
        mState = (mState + 1) % mMaxStates
        setButtonDrawable(mButtonResources[mState])
    }

    /**
     * Allows for a new set of drawable resource ids to be set.
     *
     * This sets the maximum states allowed to the length of the resources array. It will also
     * set the current state to the maximum allowed if it's greater than the new max.
     */
    //@Throws(IllegalArgumentException::class)
    fun setButtonResources(resources: IntArray?) {
        if (resources == null) {
            throw IllegalArgumentException("Button resources cannot be null")
        }
        mMaxStates = resources.size
        if (mState >= mMaxStates) {
            mState = mMaxStates - 1
        }
        mButtonResources = resources
    }

    /**
     * Attempts to set the state. Returns true if successful, false otherwise.
     */
    fun setState(state: Int): Boolean {
        if (state >= mMaxStates || state < 0) {
            //When moved out of Calendar the tag should be changed.
            Log.w("Cal", "MultiStateButton state set to value greater than maxState or < 0")
            return false
        }
        mState = state
        setButtonDrawable(mButtonResources[mState])
        return true
    }

    /**
     * Set the background to a given Drawable, identified by its resource id.
     *
     * @param resid the resource id of the drawable to use as the background
     */
    fun setButtonDrawable(resid: Int) {
        if (resid != 0 && resid == mButtonResource) {
            return
        }
        mButtonResource = resid
        var d: Drawable? = null
        if (mButtonResource != 0) {
            d = getResources().getDrawable(mButtonResource)
        }
        setButtonDrawable(d)
    }

    /**
     * Set the background to a given Drawable
     *
     * @param d The Drawable to use as the background
     */
    fun setButtonDrawable(d: Drawable?) {
        if (d != null) {
            if (mButtonDrawable != null) {
                mButtonDrawable?.setCallback(null)
                unscheduleDrawable(mButtonDrawable)
            }
            d.setCallback(this)
            d.setState(getDrawableState())
            d.setVisible(getVisibility() === VISIBLE, false)
            mButtonDrawable = d
            mButtonDrawable?.setState(getDrawableState())
            setMinHeight(mButtonDrawable?.getIntrinsicHeight() ?: 0)
            setWidth(mButtonDrawable?.getIntrinsicWidth() ?: 0)
        }
        refreshDrawableState()
    }

    protected override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (mButtonDrawable != null) {
            val verticalGravity: Int = getGravity() and Gravity.VERTICAL_GRAVITY_MASK
            val horizontalGravity: Int = getGravity() and Gravity.HORIZONTAL_GRAVITY_MASK
            val height: Int = mButtonDrawable?.getIntrinsicHeight() ?: 0
            val width: Int = mButtonDrawable?.getIntrinsicWidth() ?: 0
            var y = 0
            var x = 0
            when (verticalGravity) {
                Gravity.BOTTOM -> y = getHeight() - height
                Gravity.CENTER_VERTICAL -> y = (getHeight() - height) / 2
            }
            when (horizontalGravity) {
                Gravity.RIGHT -> x = getWidth() - width
                Gravity.CENTER_HORIZONTAL -> x = (getWidth() - width) / 2
            }
            mButtonDrawable?.setBounds(x, y, x + width, y + height)
            mButtonDrawable?.draw(canvas)
        }
    }

    init {
        //Currently using the standard buttonStyle, will update when new resources are added.
        //TODO add a more generic default button
        mButtonResources = intArrayOf(R.drawable.widget_show)
        setButtonDrawable(mButtonResources[mState])
    }
}