• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.widget.RatingBar;
4 import android.widget.RatingBar.OnRatingBarChangeListener;
5 
6 import com.xtremelabs.robolectric.internal.Implementation;
7 import com.xtremelabs.robolectric.internal.Implements;
8 import com.xtremelabs.robolectric.internal.RealObject;
9 
10 @Implements(RatingBar.class)
11 public class ShadowRatingBar extends ShadowAbsSeekBar {
12 
13     @RealObject
14     private RatingBar realRatingBar;
15     private int mNumStars = 5;
16     private OnRatingBarChangeListener listener;
17 
applyAttributes()18     @Override public void applyAttributes() {
19         super.applyAttributes();
20 
21         setIsIndicator(attributeSet.getAttributeBooleanValue("android", "isIndicator", false));
22         final int numStars = attributeSet.getAttributeIntValue("android", "numStars", mNumStars);
23         final float rating = attributeSet.getAttributeFloatValue("android", "rating", -1);
24         final float stepSize = attributeSet.getAttributeFloatValue("android", "stepSize", -1);
25 
26         if (numStars > 0 && numStars != mNumStars) {
27             setNumStars(numStars);
28         }
29 
30         if (stepSize >= 0) {
31             setStepSize(stepSize);
32         } else {
33             setStepSize(0.5f);
34         }
35 
36         if (rating >= 0) {
37             setRating(rating);
38         }
39     }
40 
41     @Implementation
setNumStars(final int numStars)42     public void setNumStars(final int numStars) {
43         if (numStars <= 0) {
44             return;
45         }
46 
47         mNumStars = numStars;
48     }
49 
50     @Implementation
getNumStars()51     public int getNumStars() {
52         return mNumStars;
53     }
54 
55     @Implementation
setRating(float rating)56     public void setRating(float rating) {
57         setProgress(Math.round(rating * getProgressPerStar()));
58     }
59 
60     @Implementation
getRating()61     public float getRating() {
62         return getProgress() / getProgressPerStar();
63     }
64 
65     @Implementation
setIsIndicator(boolean isIndicator)66     public void setIsIndicator(boolean isIndicator) {
67         mIsUserSeekable = !isIndicator;
68         setFocusable(!isIndicator);
69     }
70 
71     @Implementation
isIndicator()72     public boolean isIndicator() {
73         return !mIsUserSeekable;
74     }
75 
76     @Implementation
setStepSize(float stepSize)77     public void setStepSize(float stepSize) {
78         if (stepSize <= 0) {
79             return;
80         }
81 
82         final float newMax = mNumStars / stepSize;
83         final int newProgress = (int) (newMax / getMax() * getProgress());
84         setMax((int) newMax);
85         setProgress(newProgress);
86     }
87 
88     @Implementation
getStepSize()89     public float getStepSize() {
90         return (float) getNumStars() / getMax();
91     }
92 
getProgressPerStar()93     private float getProgressPerStar() {
94         if (mNumStars > 0) {
95             return 1f * getMax() / mNumStars;
96         } else {
97             return 1;
98         }
99     }
100 
101     @Implementation
102     @Override
setProgress(int progress)103     public void setProgress(int progress) {
104         super.setProgress(progress);
105         if (listener != null)
106             listener.onRatingChanged(realRatingBar, getRating(), true);
107     }
108 
109     @Implementation
setOnRatingBarChangeListener(OnRatingBarChangeListener listener)110     public void setOnRatingBarChangeListener(OnRatingBarChangeListener listener) {
111         this.listener = listener;
112     }
113 
114     @Implementation
getOnRatingBarChangeListener()115     public OnRatingBarChangeListener getOnRatingBarChangeListener() {
116         return listener;
117     }
118 }
119