Skip to content

Commit b1c5d0a

Browse files
jquick-axwaysgtcoolguy
authored andcommitted
feat(android): use material based ProgressBar/ActivityIndicator
Fixes TIMOB-28351
1 parent c8b6ab7 commit b1c5d0a

File tree

2 files changed

+79
-90
lines changed

2 files changed

+79
-90
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,63 @@
11
/**
22
* 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.
44
* Licensed under the terms of the Apache Public License
55
* Please see the LICENSE included with this distribution for details.
66
*/
77
packageti.modules.titanium.ui.widget;
88

9+
importandroid.content.res.TypedArray;
10+
importandroid.graphics.Color;
11+
importandroid.view.Gravity;
12+
importandroid.view.View;
13+
importandroid.widget.LinearLayout;
14+
importandroidx.appcompat.view.ContextThemeWrapper;
15+
importcom.google.android.material.progressindicator.CircularProgressIndicator;
16+
importcom.google.android.material.textview.MaterialTextView;
917
importjava.util.HashMap;
10-
1118
importorg.appcelerator.kroll.KrollDict;
1219
importorg.appcelerator.kroll.KrollProxy;
1320
importorg.appcelerator.kroll.common.Log;
14-
importorg.appcelerator.titanium.TiApplication;
21+
importorg.appcelerator.titanium.R;
1522
importorg.appcelerator.titanium.TiC;
1623
importorg.appcelerator.titanium.proxy.TiViewProxy;
1724
importorg.appcelerator.titanium.util.TiConvert;
1825
importorg.appcelerator.titanium.util.TiUIHelper;
1926
importorg.appcelerator.titanium.view.TiUIView;
2027

21-
importandroid.app.Activity;
22-
importandroid.view.Gravity;
23-
importandroid.view.View;
24-
importandroid.widget.LinearLayout;
25-
importandroid.widget.ProgressBar;
26-
importandroid.widget.TextView;
27-
2828
publicclassTiUIActivityIndicatorextendsTiUIView
2929
{
3030
privatestaticfinalStringTAG = "TiUIActivityIndicator";
3131

32-
protectedintcurrentStyle;
33-
protectedbooleanvisible;
34-
privateTextViewlabel;
35-
privateProgressBarprogress;
32+
publicstaticfinalintPLAIN = 0;
33+
publicstaticfinalintBIG = 1;
34+
publicstaticfinalintDARK = 2;
35+
publicstaticfinalintBIG_DARK = 3;
3636

37-
publicstaticfinalintPLAIN = android.R.attr.progressBarStyleSmall;
38-
publicstaticfinalintBIG = android.R.attr.progressBarStyleLarge;
39-
publicstaticfinalintDARK = android.R.attr.progressBarStyleSmallInverse;
40-
publicstaticfinalintBIG_DARK = android.R.attr.progressBarStyleLargeInverse;
37+
privatebooleanvisible;
38+
privateMaterialTextViewlabel;
39+
privateCircularProgressIndicatorprogress;
4140

4241
publicTiUIActivityIndicator(TiViewProxyproxy)
4342
{
4443
super(proxy);
4544
Log.d(TAG, "Creating an activity indicator", Log.DEBUG_MODE);
4645

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-
Activityactivity = 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-
LinearLayoutview = newLinearLayout(activity);
46+
LinearLayoutview = newLinearLayout(proxy.getActivity());
6047
view.setOrientation(LinearLayout.HORIZONTAL);
6148
view.setGravity(Gravity.CENTER);
49+
view.setVisibility(View.INVISIBLE);
50+
this.visible = false;
6251

63-
label = newTextView(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 = newProgressBar(activity, null, currentStyle);
52+
this.progress = newCircularProgressIndicator(proxy.getActivity());
53+
this.progress.setIndeterminate(true);
54+
view.addView(this.progress);
7055

71-
view.addView(progress);
72-
view.addView(label);
73-
view.setVisibility(View.INVISIBLE);
74-
visible = false;
56+
this.label = newMaterialTextView(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);
7561

7662
setNativeView(view);
7763
}
@@ -86,9 +72,6 @@ public void processProperties(KrollDict d)
8672
return;
8773
}
8874

89-
if (d.containsKey(TiC.PROPERTY_STYLE)) {
90-
setStyle(TiConvert.toInt(d, TiC.PROPERTY_STYLE));
91-
}
9275
if (d.containsKey(TiC.PROPERTY_FONT)) {
9376
TiUIHelper.styleText(label, d.getKrollDict(TiC.PROPERTY_FONT));
9477
}
@@ -98,10 +81,7 @@ public void processProperties(KrollDict d)
9881
if (d.containsKey(TiC.PROPERTY_COLOR)) {
9982
label.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
10083
}
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();
10585

10686
view.invalidate();
10787
}
@@ -112,7 +92,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
11292
Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE);
11393

11494
if (key.equals(TiC.PROPERTY_STYLE)) {
115-
setStyle(TiConvert.toInt(newValue));
95+
updateIndicator();
11696
} elseif (key.equals(TiC.PROPERTY_FONT) && newValueinstanceofHashMap) {
11797
TiUIHelper.styleText(label, (HashMap) newValue);
11898
label.requestLayout();
@@ -122,8 +102,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
122102
} elseif (key.equals(TiC.PROPERTY_COLOR)) {
123103
label.setTextColor(TiConvert.toColor((String) newValue));
124104
} elseif (key.equals(TiC.PROPERTY_INDICATOR_COLOR)) {
125-
progress.getIndeterminateDrawable().setColorFilter(TiConvert.toColor((String) newValue),
126-
android.graphics.PorterDuff.Mode.SRC_IN);
105+
updateIndicator();
127106
} else {
128107
super.propertyChanged(key, oldValue, newValue, proxy);
129108
}
@@ -149,35 +128,47 @@ public void hide()
149128
visible = false;
150129
}
151130

152-
protectedintgetStyle()
153-
{
154-
if (proxy.hasProperty(TiC.PROPERTY_STYLE)) {
155-
intstyle = 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-
returnPLAIN;
159-
}
160-
returnstyle;
161-
}
162-
returnPLAIN;
163-
}
164-
165-
protectedvoidsetStyle(intstyle)
131+
privatevoidupdateIndicator()
166132
{
167-
if (style == currentStyle) {
133+
// Do not continue if proxy has been released.
134+
if (this.proxy == null) {
168135
return;
169136
}
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+
intstyleId = 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;
173143
}
174-
LinearLayoutview = (LinearLayout) getNativeView();
175144

176-
view.removeAllViews();
177-
progress = newProgressBar(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 = newint[] {
147+
R.attr.trackThickness,
148+
R.attr.indicatorSize,
149+
R.attr.indicatorInset
150+
};
151+
intthemeId = R.style.Widget_MaterialComponents_CircularProgressIndicator_ExtraSmall;
152+
if ((styleId == BIG) || (styleId == BIG_DARK)) {
153+
themeId = R.style.Widget_MaterialComponents_CircularProgressIndicator_Medium;
154+
}
155+
ContextThemeWrappercontext = newContextThemeWrapper(this.progress.getContext(), themeId);
156+
TypedArraytypedArray = context.obtainStyledAttributes(null, idArray, 0, 0);
157+
intvalue = 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+
intcolor = TiConvert.toColor(TiConvert.toString(this.proxy.getProperty(TiC.PROPERTY_INDICATOR_COLOR)));
166+
this.progress.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
167+
} elseif ((styleId == DARK) || (styleId == BIG_DARK)) {
168+
intcolor = Color.DKGRAY;
169+
this.progress.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
170+
} else {
171+
this.progress.getIndeterminateDrawable().clearColorFilter();
172+
}
182173
}
183174
}

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIProgressBar.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/**
22
* 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.
44
* Licensed under the terms of the Apache Public License
55
* Please see the LICENSE included with this distribution for details.
66
*/
77
packageti.modules.titanium.ui.widget;
88

9+
importandroid.content.res.ColorStateList;
10+
importandroid.view.Gravity;
11+
importandroid.widget.LinearLayout;
12+
importcom.google.android.material.progressindicator.LinearProgressIndicator;
13+
importcom.google.android.material.textview.MaterialTextView;
914
importorg.appcelerator.kroll.KrollDict;
1015
importorg.appcelerator.kroll.KrollProxy;
1116
importorg.appcelerator.titanium.TiC;
@@ -14,17 +19,10 @@
1419
importorg.appcelerator.titanium.util.TiUIHelper;
1520
importorg.appcelerator.titanium.view.TiUIView;
1621

17-
importandroid.content.res.ColorStateList;
18-
importandroid.view.Gravity;
19-
importandroid.widget.LinearLayout;
20-
importandroid.widget.ProgressBar;
21-
importandroid.widget.TextView;
22-
2322
publicclassTiUIProgressBarextendsTiUIView
2423
{
25-
26-
privateTextViewlabel;
27-
privateProgressBarprogress;
24+
privateMaterialTextViewlabel;
25+
privateLinearProgressIndicatorprogress;
2826
privateLinearLayoutview;
2927

3028
publicTiUIProgressBar(finalTiViewProxyproxy)
@@ -40,12 +38,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
4038
}
4139
};
4240
view.setOrientation(LinearLayout.VERTICAL);
43-
label = newTextView(proxy.getActivity());
44-
label.setGravity(Gravity.TOP | Gravity.LEFT);
41+
label = newMaterialTextView(proxy.getActivity());
42+
label.setGravity(Gravity.TOP | Gravity.START);
4543
label.setPadding(0, 0, 0, 0);
4644
label.setSingleLine(false);
4745

48-
progress = newProgressBar(proxy.getActivity(), null, android.R.attr.progressBarStyleHorizontal);
46+
progress = newLinearProgressIndicator(proxy.getActivity());
4947
progress.setIndeterminate(false);
5048
progress.setMax(1000);
5149

0 commit comments

Comments
 (0)
close