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
17package com.android.messaging.ui.mediapicker.camerafocus;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Path;
22import android.graphics.drawable.Drawable;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * Pie menu item
29 */
30public class PieItem {
31
32    public static interface OnClickListener {
33        void onClick(PieItem item);
34    }
35
36    private Drawable mDrawable;
37    private int level;
38    private float mCenter;
39    private float start;
40    private float sweep;
41    private float animate;
42    private int inner;
43    private int outer;
44    private boolean mSelected;
45    private boolean mEnabled;
46    private List<PieItem> mItems;
47    private Path mPath;
48    private OnClickListener mOnClickListener;
49    private float mAlpha;
50
51    // Gray out the view when disabled
52    private static final float ENABLED_ALPHA = 1;
53    private static final float DISABLED_ALPHA = (float) 0.3;
54    private boolean mChangeAlphaWhenDisabled = true;
55
56    public PieItem(Drawable drawable, int level) {
57        mDrawable = drawable;
58        this.level = level;
59        setAlpha(1f);
60        mEnabled = true;
61        setAnimationAngle(getAnimationAngle());
62        start = -1;
63        mCenter = -1;
64    }
65
66    public boolean hasItems() {
67        return mItems != null;
68    }
69
70    public List<PieItem> getItems() {
71        return mItems;
72    }
73
74    public void addItem(PieItem item) {
75        if (mItems == null) {
76            mItems = new ArrayList<PieItem>();
77        }
78        mItems.add(item);
79    }
80
81    public void setPath(Path p) {
82        mPath = p;
83    }
84
85    public Path getPath() {
86        return mPath;
87    }
88
89    public void setChangeAlphaWhenDisabled (boolean enable) {
90        mChangeAlphaWhenDisabled = enable;
91    }
92
93    public void setAlpha(float alpha) {
94        mAlpha = alpha;
95        mDrawable.setAlpha((int) (255 * alpha));
96    }
97
98    public void setAnimationAngle(float a) {
99        animate = a;
100    }
101
102    public float getAnimationAngle() {
103        return animate;
104    }
105
106    public void setEnabled(boolean enabled) {
107        mEnabled = enabled;
108        if (mChangeAlphaWhenDisabled) {
109            if (mEnabled) {
110                setAlpha(ENABLED_ALPHA);
111            } else {
112                setAlpha(DISABLED_ALPHA);
113            }
114        }
115    }
116
117    public boolean isEnabled() {
118        return mEnabled;
119    }
120
121    public void setSelected(boolean s) {
122        mSelected = s;
123    }
124
125    public boolean isSelected() {
126        return mSelected;
127    }
128
129    public int getLevel() {
130        return level;
131    }
132
133    public void setGeometry(float st, float sw, int inside, int outside) {
134        start = st;
135        sweep = sw;
136        inner = inside;
137        outer = outside;
138    }
139
140    public void setFixedSlice(float center, float sweep) {
141        mCenter = center;
142        this.sweep = sweep;
143    }
144
145    public float getCenter() {
146        return mCenter;
147    }
148
149    public float getStart() {
150        return start;
151    }
152
153    public float getStartAngle() {
154        return start + animate;
155    }
156
157    public float getSweep() {
158        return sweep;
159    }
160
161    public int getInnerRadius() {
162        return inner;
163    }
164
165    public int getOuterRadius() {
166        return outer;
167    }
168
169    public void setOnClickListener(OnClickListener listener) {
170        mOnClickListener = listener;
171    }
172
173    public void performClick() {
174        if (mOnClickListener != null) {
175            mOnClickListener.onClick(this);
176        }
177    }
178
179    public int getIntrinsicWidth() {
180        return mDrawable.getIntrinsicWidth();
181    }
182
183    public int getIntrinsicHeight() {
184        return mDrawable.getIntrinsicHeight();
185    }
186
187    public void setBounds(int left, int top, int right, int bottom) {
188        mDrawable.setBounds(left, top, right, bottom);
189    }
190
191    public void draw(Canvas canvas) {
192        mDrawable.draw(canvas);
193    }
194
195    public void setImageResource(Context context, int resId) {
196        Drawable d = context.getResources().getDrawable(resId).mutate();
197        d.setBounds(mDrawable.getBounds());
198        mDrawable = d;
199        setAlpha(mAlpha);
200    }
201
202}