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