Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-18363] Android: leftTrackImage on slider is not being applied #6669

Merged
merged 4 commits into from Feb 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand Down Expand Up @@ -37,6 +37,7 @@ public class TiUISlider extends TiUIView
private int minRange;
private int maxRange;
private int scaleFactor;
private ClipDrawable rightClipDrawable;

private SoftReference<Drawable> thumbDrawable;

Expand Down Expand Up @@ -95,21 +96,32 @@ public void processProperties(KrollDict d)
updateThumb(seekBar, d);
}

if (d.containsKey("leftTrackImage") && d.containsKey("rightTrackImage")) {
if (d.containsKey("leftTrackImage") || d.containsKey("rightTrackImage")) {
updateTrackingImages(seekBar, d);
}
updateRange();
updateControl();
updateRightDrawable();
}

private void updateRightDrawable()
{
if(rightClipDrawable != null) {
SeekBar seekBar = (SeekBar) getNativeView();
double percent = (double) seekBar.getProgress()/ (double)seekBar.getMax();
int level = 10000 - (int)Math.floor(percent*10000);
rightClipDrawable.setLevel(level);
}
}

private void updateRange() {
minRange = Math.max(minRange, min);
minRange = Math.min(minRange, max);
proxy.setProperty("minRange", minRange, false);
proxy.setProperty("minRange", minRange);

maxRange = Math.min(maxRange, max);
maxRange = Math.max(maxRange, minRange);
proxy.setProperty("maxRange", maxRange, false);
proxy.setProperty("maxRange", maxRange);
}

private void updateControl() {
Expand Down Expand Up @@ -151,42 +163,53 @@ private void updateThumb(SeekBar seekBar, KrollDict d)

private void updateTrackingImages(SeekBar seekBar, KrollDict d)
{
TiFileHelper tfh = null;
String leftImage = TiConvert.toString(d, "leftTrackImage");
String rightImage = TiConvert.toString(d, "rightTrackImage");
if (leftImage != null && rightImage != null) {
if (tfh == null) {
tfh = new TiFileHelper(seekBar.getContext());
}

Drawable leftDrawable = null;
Drawable rightDrawable = null;
TiFileHelper tfh = new TiFileHelper(seekBar.getContext());

if(leftImage != null) {
String leftUrl = proxy.resolveUrl(null, leftImage);
String rightUrl = proxy.resolveUrl(null, rightImage);

Drawable rightDrawable = tfh.loadDrawable(rightUrl, false, true);
Drawable leftDrawable = tfh.loadDrawable(leftUrl, false, true);
if (rightDrawable != null && leftDrawable != null) {
Drawable[] lda = {
rightDrawable,
new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)
};
LayerDrawable ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.background);
ld.setId(1, android.R.id.progress);
seekBar.setProgressDrawable(ld);
} else {
if(leftUrl != null) {
leftDrawable = tfh.loadDrawable(leftUrl, false, true);
if (leftDrawable == null) {
Log.e(TAG, "Unable to locate left image for progress bar: " + leftUrl);
}
}
}

if(rightImage != null) {
String rightUrl = proxy.resolveUrl(null, rightImage);
if(rightUrl != null) {
rightDrawable = tfh.loadDrawable(rightUrl, false, true);
if (rightDrawable == null) {
Log.e(TAG, "Unable to locate right image for progress bar: " + rightUrl);
}
// release
leftDrawable = null;
rightDrawable = null;
}
} else if (leftImage == null && rightImage == null) {
seekBar.setProgressDrawable(null);
}

if(leftDrawable != null || rightDrawable != null) {
LayerDrawable ld = null;
if(rightDrawable == null) {
Drawable[] lda = {new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)};
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.progress);
} else if(leftDrawable == null) {
rightClipDrawable = new ClipDrawable(rightDrawable, Gravity.RIGHT, ClipDrawable.HORIZONTAL);
Drawable[] lda = {rightClipDrawable};
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.secondaryProgress);
} else {
Drawable[] lda = {rightDrawable, new ClipDrawable(leftDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)};
ld = new LayerDrawable(lda);
ld.setId(0, android.R.id.background);
ld.setId(1, android.R.id.progress);
}
seekBar.setProgressDrawable(ld);
} else {
Log.w(TAG, "Custom tracking images must both be set before they will be drawn.");
Log.w(TAG, "Custom tracking images could not be loaded.");
}
}

Expand Down Expand Up @@ -268,6 +291,8 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pos = maxRange;
}

updateRightDrawable();

Drawable thumb = (thumbDrawable != null) ? thumbDrawable.get() : null;
KrollDict offset = new KrollDict();
offset.put(TiC.EVENT_PROPERTY_X, 0);
Expand Down
8 changes: 8 additions & 0 deletions apidoc/Titanium/UI/Slider.yml
Expand Up @@ -24,6 +24,10 @@ description: |

On Android, both `min` and `max` must be specified for the slider to work properly.

Earlier versions of the Titanium SDK implicitly enforced that both the [leftTrackImage](Titanium.UI.Slider.leftTrackImage) and
[rightTrackImage](Titanium.UI.Slider.rightTrackImage) properties be specified before the properties would be honored. Beginning with
Titanium SDK 4.0.0 this limitation has been removed. However it is recommended that either both or neither be specified.

extends: Titanium.UI.View
excludes:
events: [ 'pinch' ]
Expand Down Expand Up @@ -234,6 +238,8 @@ properties:

- name: leftTrackImage
summary: Image URL of the slider left track.
description: |
See introduction of the <Titanium.UI.Slider> component for implementation specific information on Android Platform.
type: String
platforms: [android, iphone, ipad]

Expand Down Expand Up @@ -339,6 +345,8 @@ properties:

- name: rightTrackImage
summary: Image URL of the slider right track.
description: |
See introduction of the <Titanium.UI.Slider> component for implementation specific information on Android Platform.
type: String
platforms: [android, iphone, ipad]

Expand Down