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