|
| 1 | +/* |
| 2 | + * Copyright 2018-2024 Pranav Pandey |
| 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.pranavpandey.android.dynamic.support.widget; |
| 18 | + |
| 19 | +import android.annotation.TargetApi; |
| 20 | +import android.content.Context; |
| 21 | +import android.content.res.TypedArray; |
| 22 | +import android.os.Build; |
| 23 | +import android.util.AttributeSet; |
| 24 | + |
| 25 | +import androidx.annotation.ColorInt; |
| 26 | +import androidx.annotation.NonNull; |
| 27 | +import androidx.annotation.Nullable; |
| 28 | + |
| 29 | +import com.google.android.material.progressindicator.CircularProgressIndicator; |
| 30 | +import com.pranavpandey.android.dynamic.support.Defaults; |
| 31 | +import com.pranavpandey.android.dynamic.support.Dynamic; |
| 32 | +import com.pranavpandey.android.dynamic.support.R; |
| 33 | +import com.pranavpandey.android.dynamic.support.theme.DynamicTheme; |
| 34 | +import com.pranavpandey.android.dynamic.support.util.DynamicShapeUtils; |
| 35 | +import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget; |
| 36 | +import com.pranavpandey.android.dynamic.theme.Theme; |
| 37 | +import com.pranavpandey.android.dynamic.util.DynamicColorUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * A {@link CircularProgressIndicator} to apply {@link DynamicTheme} according to the |
| 41 | + * supplied parameters. |
| 42 | + */ |
| 43 | +public class DynamicCircularProgressIndicator extends CircularProgressIndicator |
| 44 | + implements DynamicWidget { |
| 45 | + |
| 46 | + /** |
| 47 | + * Color type applied to this view. |
| 48 | + * |
| 49 | + * @see Theme.ColorType |
| 50 | + */ |
| 51 | + protected @Theme.ColorType int mColorType; |
| 52 | + |
| 53 | + /** |
| 54 | + * Background color type for this view so that it will remain in contrast with this |
| 55 | + * color type. |
| 56 | + */ |
| 57 | + protected @Theme.ColorType int mContrastWithColorType; |
| 58 | + |
| 59 | + /** |
| 60 | + * Color applied to this view. |
| 61 | + */ |
| 62 | + protected @ColorInt int mColor; |
| 63 | + |
| 64 | + /** |
| 65 | + * Color applied to this view after considering the background aware properties. |
| 66 | + */ |
| 67 | + protected @ColorInt int mAppliedColor; |
| 68 | + |
| 69 | + /** |
| 70 | + * Background color for this view so that it will remain in contrast with this color. |
| 71 | + */ |
| 72 | + protected @ColorInt int mContrastWithColor; |
| 73 | + |
| 74 | + /** |
| 75 | + * The background aware functionality to change this view color according to the background. |
| 76 | + * It was introduced to provide better legibility for colored views and to avoid dark view |
| 77 | + * on dark background like situations. |
| 78 | + * |
| 79 | + * <p>If this is enabled then, it will check for the contrast color and do color |
| 80 | + * calculations according to that color so that this text view will always be visible on |
| 81 | + * that background. If no contrast color is found then, it will take the default |
| 82 | + * background color. |
| 83 | + * |
| 84 | + * @see Theme.BackgroundAware |
| 85 | + * @see #mContrastWithColor |
| 86 | + */ |
| 87 | + protected @Theme.BackgroundAware int mBackgroundAware; |
| 88 | + |
| 89 | + /** |
| 90 | + * Minimum contrast value to generate contrast color for the background aware functionality. |
| 91 | + */ |
| 92 | + protected int mContrast; |
| 93 | + |
| 94 | + public DynamicCircularProgressIndicator(@NonNull Context context) { |
| 95 | + this(context, null); |
| 96 | + } |
| 97 | + |
| 98 | + public DynamicCircularProgressIndicator(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 99 | + super(context, attrs); |
| 100 | + |
| 101 | + loadFromAttributes(attrs); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void loadFromAttributes(@Nullable AttributeSet attrs) { |
| 106 | + TypedArray a = getContext().obtainStyledAttributes(attrs, |
| 107 | + R.styleable.DynamicCircularProgressIndicator); |
| 108 | + |
| 109 | + try { |
| 110 | + mColorType = a.getInt( |
| 111 | + R.styleable.DynamicCircularProgressIndicator_adt_colorType, |
| 112 | + Theme.ColorType.ACCENT); |
| 113 | + mContrastWithColorType = a.getInt( |
| 114 | + R.styleable.DynamicCircularProgressIndicator_adt_contrastWithColorType, |
| 115 | + Theme.ColorType.BACKGROUND); |
| 116 | + mColor = a.getColor( |
| 117 | + R.styleable.DynamicCircularProgressIndicator_adt_color, |
| 118 | + Theme.Color.UNKNOWN); |
| 119 | + mContrastWithColor = a.getColor( |
| 120 | + R.styleable.DynamicCircularProgressIndicator_adt_contrastWithColor, |
| 121 | + Defaults.getContrastWithColor(getContext())); |
| 122 | + mBackgroundAware = a.getInteger( |
| 123 | + R.styleable.DynamicCircularProgressIndicator_adt_backgroundAware, |
| 124 | + Defaults.getBackgroundAware()); |
| 125 | + mContrast = a.getInteger( |
| 126 | + R.styleable.DynamicCircularProgressIndicator_adt_contrast, |
| 127 | + Theme.Contrast.AUTO); |
| 128 | + } finally { |
| 129 | + a.recycle(); |
| 130 | + } |
| 131 | + |
| 132 | + initialize(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void initialize() { |
| 137 | + if (mColorType != Theme.ColorType.NONE |
| 138 | + && mColorType != Theme.ColorType.CUSTOM) { |
| 139 | + mColor = DynamicTheme.getInstance().resolveColorType(mColorType); |
| 140 | + } |
| 141 | + |
| 142 | + if (mContrastWithColorType != Theme.ColorType.NONE |
| 143 | + && mContrastWithColorType != Theme.ColorType.CUSTOM) { |
| 144 | + mContrastWithColor = DynamicTheme.getInstance() |
| 145 | + .resolveColorType(mContrastWithColorType); |
| 146 | + } |
| 147 | + |
| 148 | + setColor(); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public @Theme.ColorType int getColorType() { |
| 153 | + return mColorType; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void setColorType(@Theme.ColorType int colorType) { |
| 158 | + this.mColorType = colorType; |
| 159 | + |
| 160 | + initialize(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public @Theme.ColorType int getContrastWithColorType() { |
| 165 | + return mContrastWithColorType; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void setContrastWithColorType(@Theme.ColorType int contrastWithColorType) { |
| 170 | + this.mContrastWithColorType = contrastWithColorType; |
| 171 | + |
| 172 | + initialize(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public @ColorInt int getColor(boolean resolve) { |
| 177 | + return resolve ? mAppliedColor : mColor; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public @ColorInt int getColor() { |
| 182 | + return getColor(true); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void setColor(@ColorInt int color) { |
| 187 | + this.mColorType = Theme.ColorType.CUSTOM; |
| 188 | + this.mColor = color; |
| 189 | + |
| 190 | + setColor(); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public @ColorInt int getContrastWithColor() { |
| 195 | + return mContrastWithColor; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void setContrastWithColor(@ColorInt int contrastWithColor) { |
| 200 | + this.mContrastWithColorType = Theme.ColorType.CUSTOM; |
| 201 | + this.mContrastWithColor = contrastWithColor; |
| 202 | + |
| 203 | + setColor(); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public @Theme.BackgroundAware int getBackgroundAware() { |
| 208 | + return mBackgroundAware; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public boolean isBackgroundAware() { |
| 213 | + return Dynamic.isBackgroundAware(this); |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void setBackgroundAware(@Theme.BackgroundAware int backgroundAware) { |
| 218 | + this.mBackgroundAware = backgroundAware; |
| 219 | + |
| 220 | + setColor(); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public int getContrast(boolean resolve) { |
| 225 | + if (resolve) { |
| 226 | + return Dynamic.getContrast(this); |
| 227 | + } |
| 228 | + |
| 229 | + return mContrast; |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public int getContrast() { |
| 234 | + return getContrast(true); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public float getContrastRatio() { |
| 239 | + return getContrast() / (float) Theme.Contrast.MAX; |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public void setContrast(int contrast) { |
| 244 | + this.mContrast = contrast; |
| 245 | + |
| 246 | + setBackgroundAware(getBackgroundAware()); |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public void setEnabled(boolean enabled) { |
| 251 | + super.setEnabled(enabled); |
| 252 | + |
| 253 | + setAlpha(enabled ? Defaults.ADS_ALPHA_ENABLED : Defaults.ADS_ALPHA_DISABLED); |
| 254 | + } |
| 255 | + |
| 256 | + @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 257 | + @Override |
| 258 | + public void setColor() { |
| 259 | + setTrackCornerRadius(DynamicShapeUtils.getSlideCornerSize( |
| 260 | + DynamicTheme.getInstance().get().getCornerSize())); |
| 261 | + |
| 262 | + if (mColor != Theme.Color.UNKNOWN) { |
| 263 | + mAppliedColor = mColor; |
| 264 | + if (isBackgroundAware() && mContrastWithColor != Theme.Color.UNKNOWN) { |
| 265 | + mAppliedColor = Dynamic.withContrastRatio(mColor, mContrastWithColor, this); |
| 266 | + } |
| 267 | + |
| 268 | + setIndicatorColor(mAppliedColor); |
| 269 | + setTrackColor(DynamicColorUtils.adjustAlpha( |
| 270 | + mAppliedColor, Defaults.ADS_ALPHA_PRESSED)); |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments