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.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
39    private boolean mSelected;
40    private boolean mEnabled;
41    private List<PieItem> mItems;
42    private Path mPath;
43    private OnClickListener mOnClickListener;
44    private float mAlpha;
45    private CharSequence mLabel;
46
47    // Gray out the view when disabled
48    private static final float ENABLED_ALPHA = 1;
49    private static final float DISABLED_ALPHA = (float) 0.3;
50    private boolean mChangeAlphaWhenDisabled = true;
51
52    public PieItem(Drawable drawable, int level) {
53        mDrawable = drawable;
54        this.level = level;
55        if (drawable != null) {
56            setAlpha(1f);
57        }
58        mEnabled = true;
59    }
60
61    public void setLabel(CharSequence txt) {
62        mLabel = txt;
63    }
64
65    public CharSequence getLabel() {
66        return mLabel;
67    }
68
69    public boolean hasItems() {
70        return mItems != null;
71    }
72
73    public List<PieItem> getItems() {
74        return mItems;
75    }
76
77    public void addItem(PieItem item) {
78        if (mItems == null) {
79            mItems = new ArrayList<PieItem>();
80        }
81        mItems.add(item);
82    }
83
84    public void clearItems() {
85        mItems = null;
86    }
87
88    public void setLevel(int level) {
89        this.level = level;
90    }
91
92    public void setPath(Path p) {
93        mPath = p;
94    }
95
96    public Path getPath() {
97        return mPath;
98    }
99
100    public void setChangeAlphaWhenDisabled (boolean enable) {
101        mChangeAlphaWhenDisabled = enable;
102    }
103
104    public void setAlpha(float alpha) {
105        mAlpha = alpha;
106        mDrawable.setAlpha((int) (255 * alpha));
107    }
108
109    public void setEnabled(boolean enabled) {
110        mEnabled = enabled;
111        if (mChangeAlphaWhenDisabled) {
112            if (mEnabled) {
113                setAlpha(ENABLED_ALPHA);
114            } else {
115                setAlpha(DISABLED_ALPHA);
116            }
117        }
118    }
119
120    public boolean isEnabled() {
121        return mEnabled;
122    }
123
124    public void setSelected(boolean s) {
125        mSelected = s;
126    }
127
128    public boolean isSelected() {
129        return mSelected;
130    }
131
132    public int getLevel() {
133        return level;
134    }
135
136
137    public void setOnClickListener(OnClickListener listener) {
138        mOnClickListener = listener;
139    }
140
141    public void performClick() {
142        if (mOnClickListener != null) {
143            mOnClickListener.onClick(this);
144        }
145    }
146
147    public int getIntrinsicWidth() {
148        return mDrawable.getIntrinsicWidth();
149    }
150
151    public int getIntrinsicHeight() {
152        return mDrawable.getIntrinsicHeight();
153    }
154
155    public void setBounds(int left, int top, int right, int bottom) {
156        mDrawable.setBounds(left, top, right, bottom);
157    }
158
159    public void draw(Canvas canvas) {
160        mDrawable.draw(canvas);
161    }
162
163    public void setImageResource(Context context, int resId) {
164        Drawable d = context.getResources().getDrawable(resId).mutate();
165        d.setBounds(mDrawable.getBounds());
166        mDrawable = d;
167        setAlpha(mAlpha);
168    }
169
170}
171