1
1
/**
2
2
* Appcelerator Titanium Mobile
3
- * Copyright (c) 2009-2012 by Appcelerator , Inc. All Rights Reserved.
3
+ * Copyright (c) 2009-2021 by Axway , Inc. All Rights Reserved.
4
4
* Licensed under the terms of the Apache Public License
5
5
* Please see the LICENSE included with this distribution for details.
6
6
*/
7
7
package ti .modules .titanium .ui .widget ;
8
8
9
+ import android .content .res .TypedArray ;
10
+ import android .graphics .Color ;
11
+ import android .view .Gravity ;
12
+ import android .view .View ;
13
+ import android .widget .LinearLayout ;
14
+ import androidx .appcompat .view .ContextThemeWrapper ;
15
+ import com .google .android .material .progressindicator .CircularProgressIndicator ;
16
+ import com .google .android .material .textview .MaterialTextView ;
9
17
import java .util .HashMap ;
10
-
11
18
import org .appcelerator .kroll .KrollDict ;
12
19
import org .appcelerator .kroll .KrollProxy ;
13
20
import org .appcelerator .kroll .common .Log ;
14
- import org .appcelerator .titanium .TiApplication ;
21
+ import org .appcelerator .titanium .R ;
15
22
import org .appcelerator .titanium .TiC ;
16
23
import org .appcelerator .titanium .proxy .TiViewProxy ;
17
24
import org .appcelerator .titanium .util .TiConvert ;
18
25
import org .appcelerator .titanium .util .TiUIHelper ;
19
26
import org .appcelerator .titanium .view .TiUIView ;
20
27
21
- import android .app .Activity ;
22
- import android .view .Gravity ;
23
- import android .view .View ;
24
- import android .widget .LinearLayout ;
25
- import android .widget .ProgressBar ;
26
- import android .widget .TextView ;
27
-
28
28
public class TiUIActivityIndicator extends TiUIView
29
29
{
30
30
private static final String TAG = "TiUIActivityIndicator" ;
31
31
32
- protected int currentStyle ;
33
- protected boolean visible ;
34
- private TextView label ;
35
- private ProgressBar progress ;
32
+ public static final int PLAIN = 0 ;
33
+ public static final int BIG = 1 ;
34
+ public static final int DARK = 2 ;
35
+ public static final int BIG_DARK = 3 ;
36
36
37
- public static final int PLAIN = android .R .attr .progressBarStyleSmall ;
38
- public static final int BIG = android .R .attr .progressBarStyleLarge ;
39
- public static final int DARK = android .R .attr .progressBarStyleSmallInverse ;
40
- public static final int BIG_DARK = android .R .attr .progressBarStyleLargeInverse ;
37
+ private boolean visible ;
38
+ private MaterialTextView label ;
39
+ private CircularProgressIndicator progress ;
41
40
42
41
public TiUIActivityIndicator (TiViewProxy proxy )
43
42
{
44
43
super (proxy );
45
44
Log .d (TAG , "Creating an activity indicator" , Log .DEBUG_MODE );
46
45
47
- /*
48
- * use getAppCurrentActivity over getActivity since technically the activity indicator
49
- * should show up on top of the current activity when called - not just the
50
- * activity it was created in
51
- */
52
- Activity activity = TiApplication .getAppCurrentActivity ();
53
-
54
- if (activity == null ) {
55
- Log .w (TAG , "Unable to create an activity indicator. Activity is null" );
56
- return ;
57
- }
58
-
59
- LinearLayout view = new LinearLayout (activity );
46
+ LinearLayout view = new LinearLayout (proxy .getActivity ());
60
47
view .setOrientation (LinearLayout .HORIZONTAL );
61
48
view .setGravity (Gravity .CENTER );
49
+ view .setVisibility (View .INVISIBLE );
50
+ this .visible = false ;
62
51
63
- label = new TextView (activity );
64
- label .setGravity (Gravity .CENTER_VERTICAL | Gravity .LEFT );
65
- label .setPadding (0 , 0 , 0 , 0 );
66
- label .setSingleLine (false );
67
-
68
- currentStyle = getStyle ();
69
- progress = new ProgressBar (activity , null , currentStyle );
52
+ this .progress = new CircularProgressIndicator (proxy .getActivity ());
53
+ this .progress .setIndeterminate (true );
54
+ view .addView (this .progress );
70
55
71
- view .addView (progress );
72
- view .addView (label );
73
- view .setVisibility (View .INVISIBLE );
74
- visible = false ;
56
+ this .label = new MaterialTextView (proxy .getActivity ());
57
+ this .label .setGravity (Gravity .CENTER_VERTICAL | Gravity .START );
58
+ this .label .setPadding (0 , 0 , 0 , 0 );
59
+ this .label .setSingleLine (false );
60
+ view .addView (this .label );
75
61
76
62
setNativeView (view );
77
63
}
@@ -86,9 +72,6 @@ public void processProperties(KrollDict d)
86
72
return ;
87
73
}
88
74
89
- if (d .containsKey (TiC .PROPERTY_STYLE )) {
90
- setStyle (TiConvert .toInt (d , TiC .PROPERTY_STYLE ));
91
- }
92
75
if (d .containsKey (TiC .PROPERTY_FONT )) {
93
76
TiUIHelper .styleText (label , d .getKrollDict (TiC .PROPERTY_FONT ));
94
77
}
@@ -98,10 +81,7 @@ public void processProperties(KrollDict d)
98
81
if (d .containsKey (TiC .PROPERTY_COLOR )) {
99
82
label .setTextColor (TiConvert .toColor (d , TiC .PROPERTY_COLOR ));
100
83
}
101
- if (d .containsKey (TiC .PROPERTY_INDICATOR_COLOR )) {
102
- progress .getIndeterminateDrawable ().setColorFilter (TiConvert .toColor (d , TiC .PROPERTY_INDICATOR_COLOR ),
103
- android .graphics .PorterDuff .Mode .SRC_IN );
104
- }
84
+ updateIndicator ();
105
85
106
86
view .invalidate ();
107
87
}
@@ -112,7 +92,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
112
92
Log .d (TAG , "Property: " + key + " old: " + oldValue + " new: " + newValue , Log .DEBUG_MODE );
113
93
114
94
if (key .equals (TiC .PROPERTY_STYLE )) {
115
- setStyle ( TiConvert . toInt ( newValue ) );
95
+ updateIndicator ( );
116
96
} else if (key .equals (TiC .PROPERTY_FONT ) && newValue instanceof HashMap ) {
117
97
TiUIHelper .styleText (label , (HashMap ) newValue );
118
98
label .requestLayout ();
@@ -122,8 +102,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
122
102
} else if (key .equals (TiC .PROPERTY_COLOR )) {
123
103
label .setTextColor (TiConvert .toColor ((String ) newValue ));
124
104
} else if (key .equals (TiC .PROPERTY_INDICATOR_COLOR )) {
125
- progress .getIndeterminateDrawable ().setColorFilter (TiConvert .toColor ((String ) newValue ),
126
- android .graphics .PorterDuff .Mode .SRC_IN );
105
+ updateIndicator ();
127
106
} else {
128
107
super .propertyChanged (key , oldValue , newValue , proxy );
129
108
}
@@ -149,35 +128,47 @@ public void hide()
149
128
visible = false ;
150
129
}
151
130
152
- protected int getStyle ()
153
- {
154
- if (proxy .hasProperty (TiC .PROPERTY_STYLE )) {
155
- int style = TiConvert .toInt (proxy .getProperty (TiC .PROPERTY_STYLE ));
156
- if (style != PLAIN && style != BIG && style != DARK && style != BIG_DARK ) {
157
- Log .w (TAG , "Invalid value \" " + style + "\" for style." );
158
- return PLAIN ;
159
- }
160
- return style ;
161
- }
162
- return PLAIN ;
163
- }
164
-
165
- protected void setStyle (int style )
131
+ private void updateIndicator ()
166
132
{
167
- if (style == currentStyle ) {
133
+ // Do not continue if proxy has been released.
134
+ if (this .proxy == null ) {
168
135
return ;
169
136
}
170
- if (style != PLAIN && style != BIG && style != DARK && style != BIG_DARK ) {
171
- Log .w (TAG , "Invalid value \" " + style + "\" for style." );
172
- return ;
137
+
138
+ // Fetch assigned style ID.
139
+ int styleId = TiConvert .toInt (this .proxy .getProperty (TiC .PROPERTY_STYLE ), PLAIN );
140
+ if ((styleId != PLAIN ) && (styleId != BIG ) && (styleId != DARK ) && (styleId != BIG_DARK )) {
141
+ Log .w (TAG , "Invalid value \" " + styleId + "\" for style." );
142
+ styleId = PLAIN ;
173
143
}
174
- LinearLayout view = (LinearLayout ) getNativeView ();
175
144
176
- view .removeAllViews ();
177
- progress = new ProgressBar (TiApplication .getAppCurrentActivity (), null , style );
178
- currentStyle = style ;
179
- view .addView (progress );
180
- view .addView (label );
181
- view .requestLayout ();
145
+ // Update indicator to use a big or small style.
146
+ int [] idArray = new int [] {
147
+ R .attr .trackThickness ,
148
+ R .attr .indicatorSize ,
149
+ R .attr .indicatorInset
150
+ };
151
+ int themeId = R .style .Widget_MaterialComponents_CircularProgressIndicator_ExtraSmall ;
152
+ if ((styleId == BIG ) || (styleId == BIG_DARK )) {
153
+ themeId = R .style .Widget_MaterialComponents_CircularProgressIndicator_Medium ;
154
+ }
155
+ ContextThemeWrapper context = new ContextThemeWrapper (this .progress .getContext (), themeId );
156
+ TypedArray typedArray = context .obtainStyledAttributes (null , idArray , 0 , 0 );
157
+ int value = typedArray .getDimensionPixelSize (0 , this .progress .getTrackThickness ());
158
+ this .progress .setTrackThickness (typedArray .getDimensionPixelSize (0 , this .progress .getTrackThickness ()));
159
+ this .progress .setIndicatorSize (typedArray .getDimensionPixelSize (1 , this .progress .getIndicatorSize ()));
160
+ this .progress .setIndicatorInset (typedArray .getDimensionPixelSize (2 , this .progress .getIndicatorInset ()));
161
+ typedArray .recycle ();
162
+
163
+ // Update indicator's color.
164
+ if (this .proxy .hasPropertyAndNotNull (TiC .PROPERTY_INDICATOR_COLOR )) {
165
+ int color = TiConvert .toColor (TiConvert .toString (this .proxy .getProperty (TiC .PROPERTY_INDICATOR_COLOR )));
166
+ this .progress .getIndeterminateDrawable ().setColorFilter (color , android .graphics .PorterDuff .Mode .SRC_IN );
167
+ } else if ((styleId == DARK ) || (styleId == BIG_DARK )) {
168
+ int color = Color .DKGRAY ;
169
+ this .progress .getIndeterminateDrawable ().setColorFilter (color , android .graphics .PorterDuff .Mode .SRC_IN );
170
+ } else {
171
+ this .progress .getIndeterminateDrawable ().clearColorFilter ();
172
+ }
182
173
}
183
174
}
0 commit comments