1 /*
2  * Copyright 2018 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.media;
18 
19 import static android.media.Rating2.*;
20 
21 import android.content.Context;
22 import android.media.Rating2;
23 import android.media.Rating2.Style;
24 import android.media.update.Rating2Provider;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 import java.util.Objects;
29 
30 public final class Rating2Impl implements Rating2Provider {
31     private static final String TAG = "Rating2";
32 
33     private static final String KEY_STYLE = "android.media.rating2.style";
34     private static final String KEY_VALUE = "android.media.rating2.value";
35 
36     private final static float RATING_NOT_RATED = -1.0f;
37 
38     private final Rating2 mInstance;
39     private final int mRatingStyle;
40     private final float mRatingValue;
41 
Rating2Impl(@tyle int ratingStyle, float rating)42     private Rating2Impl(@Style int ratingStyle, float rating) {
43         mRatingStyle = ratingStyle;
44         mRatingValue = rating;
45         mInstance = new Rating2(this);
46     }
47 
48     @Override
toString_impl()49     public String toString_impl() {
50         return "Rating2:style=" + mRatingStyle + " rating="
51                 + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue));
52     }
53 
54     @Override
equals_impl(Object obj)55     public boolean equals_impl(Object obj) {
56         if (!(obj instanceof Rating2)) {
57             return false;
58         }
59         Rating2Impl other = (Rating2Impl) ((Rating2) obj).getProvider();
60         return mRatingStyle == other.mRatingStyle
61                 && mRatingValue == other.mRatingValue;
62     }
63 
64     @Override
hashCode_impl()65     public int hashCode_impl() {
66         return Objects.hash(mRatingStyle, mRatingValue);
67     }
68 
getInstance()69     Rating2 getInstance() {
70         return mInstance;
71     }
72 
fromBundle_impl(Bundle bundle)73     public static Rating2 fromBundle_impl(Bundle bundle) {
74         if (bundle == null) {
75             return null;
76         }
77         return new Rating2Impl(bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE)).getInstance();
78     }
79 
toBundle_impl()80     public Bundle toBundle_impl() {
81         Bundle bundle = new Bundle();
82         bundle.putInt(KEY_STYLE, mRatingStyle);
83         bundle.putFloat(KEY_VALUE, mRatingValue);
84         return bundle;
85     }
86 
newUnratedRating_impl(@tyle int ratingStyle)87     public static Rating2 newUnratedRating_impl(@Style int ratingStyle) {
88         switch(ratingStyle) {
89             case RATING_HEART:
90             case RATING_THUMB_UP_DOWN:
91             case RATING_3_STARS:
92             case RATING_4_STARS:
93             case RATING_5_STARS:
94             case RATING_PERCENTAGE:
95                 return new Rating2Impl(ratingStyle, RATING_NOT_RATED).getInstance();
96             default:
97                 return null;
98         }
99     }
100 
newHeartRating_impl(boolean hasHeart)101     public static Rating2 newHeartRating_impl(boolean hasHeart) {
102         return new Rating2Impl(RATING_HEART, hasHeart ? 1.0f : 0.0f).getInstance();
103     }
104 
newThumbRating_impl(boolean thumbIsUp)105     public static Rating2 newThumbRating_impl(boolean thumbIsUp) {
106         return new Rating2Impl(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f).getInstance();
107     }
108 
newStarRating_impl(int starRatingStyle, float starRating)109     public static Rating2 newStarRating_impl(int starRatingStyle, float starRating) {
110         float maxRating = RATING_NOT_RATED;
111         switch(starRatingStyle) {
112             case RATING_3_STARS:
113                 maxRating = 3.0f;
114                 break;
115             case RATING_4_STARS:
116                 maxRating = 4.0f;
117                 break;
118             case RATING_5_STARS:
119                 maxRating = 5.0f;
120                 break;
121             default:
122                 Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating");
123                 return null;
124         }
125         if ((starRating < 0.0f) || (starRating > maxRating)) {
126             Log.e(TAG, "Trying to set out of range star-based rating");
127             return null;
128         }
129         return new Rating2Impl(starRatingStyle, starRating).getInstance();
130     }
131 
newPercentageRating_impl(float percent)132     public static Rating2 newPercentageRating_impl(float percent) {
133         if ((percent < 0.0f) || (percent > 100.0f)) {
134             Log.e(TAG, "Invalid percentage-based rating value");
135             return null;
136         } else {
137             return new Rating2Impl(RATING_PERCENTAGE, percent).getInstance();
138         }
139     }
140 
141     @Override
isRated_impl()142     public boolean isRated_impl() {
143         return mRatingValue >= 0.0f;
144     }
145 
146     @Override
getRatingStyle_impl()147     public int getRatingStyle_impl() {
148         return mRatingStyle;
149     }
150 
151     @Override
hasHeart_impl()152     public boolean hasHeart_impl() {
153         if (mRatingStyle != RATING_HEART) {
154             return false;
155         } else {
156             return (mRatingValue == 1.0f);
157         }
158     }
159 
160     @Override
isThumbUp_impl()161     public boolean isThumbUp_impl() {
162         if (mRatingStyle != RATING_THUMB_UP_DOWN) {
163             return false;
164         } else {
165             return (mRatingValue == 1.0f);
166         }
167     }
168 
169     @Override
getStarRating_impl()170     public float getStarRating_impl() {
171         switch (mRatingStyle) {
172             case RATING_3_STARS:
173             case RATING_4_STARS:
174             case RATING_5_STARS:
175                 if (mInstance.isRated()) {
176                     return mRatingValue;
177                 }
178             default:
179                 return -1.0f;
180         }
181     }
182 
183     @Override
getPercentRating_impl()184     public float getPercentRating_impl() {
185         if ((mRatingStyle != RATING_PERCENTAGE) || !mInstance.isRated()) {
186             return -1.0f;
187         } else {
188             return mRatingValue;
189         }
190     }
191 }
192