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 mDrawable; 34 private int level; 35 private float mCenter; 36 private float start; 37 private float sweep; 38 private float animate; 39 private int inner; 40 private int outer; 41 private boolean mSelected; 42 private boolean mEnabled; 43 private List<PieItem> mItems; 44 private Path mPath; 45 private OnClickListener mOnClickListener; 46 private float mAlpha; 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 mDrawable = drawable; 54 this.level = level; 55 setAlpha(1f); 56 mEnabled = true; 57 setAnimationAngle(getAnimationAngle()); 58 start = -1; 59 mCenter = -1; 60 } 61 hasItems()62 public boolean hasItems() { 63 return mItems != null; 64 } 65 getItems()66 public List<PieItem> getItems() { 67 return mItems; 68 } 69 setPath(Path p)70 public void setPath(Path p) { 71 mPath = p; 72 } 73 getPath()74 public Path getPath() { 75 return mPath; 76 } 77 setAlpha(float alpha)78 public void setAlpha(float alpha) { 79 mAlpha = alpha; 80 mDrawable.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 mEnabled = enabled; 93 if (mEnabled) { 94 setAlpha(ENABLED_ALPHA); 95 } else { 96 setAlpha(DISABLED_ALPHA); 97 } 98 } 99 isEnabled()100 public boolean isEnabled() { 101 return mEnabled; 102 } 103 setSelected(boolean s)104 public void setSelected(boolean s) { 105 mSelected = s; 106 } 107 isSelected()108 public boolean isSelected() { 109 return mSelected; 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 mCenter; 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 mOnClickListener = listener; 149 } 150 performClick()151 public void performClick() { 152 if (mOnClickListener != null) { 153 mOnClickListener.onClick(this); 154 } 155 } 156 getIntrinsicWidth()157 public int getIntrinsicWidth() { 158 return mDrawable.getIntrinsicWidth(); 159 } 160 getIntrinsicHeight()161 public int getIntrinsicHeight() { 162 return mDrawable.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 mDrawable.setBounds(left, top, right, bottom); 167 } 168 draw(Canvas canvas)169 public void draw(Canvas canvas) { 170 mDrawable.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(mDrawable.getBounds()); 176 mDrawable = d; 177 setAlpha(mAlpha); 178 } 179 } 180