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

[Breaking change] Android: Ti.UI.Color parity #13273

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void setLargeIcon(Object icon)
}

@Kroll.setProperty
public void setColor(String color)
public void setColor(Object color)
{
notificationBuilder.setColor(TiColorHelper.parseColor(color, getActivity()));
setProperty(TiC.PROPERTY_COLOR, color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

@Kroll.proxy(creatableInModule = UIModule.class,
propertyAccessors = {
TiC.PROPERTY_DECODE_RETRIES,
TiC.PROPERTY_AUTOROTATE,
TiC.PROPERTY_DECODE_RETRIES,
TiC.PROPERTY_DEFAULT_IMAGE,
TiC.PROPERTY_DURATION,
TiC.PROPERTY_ENABLE_ZOOM_CONTROLS,
Expand All @@ -28,7 +28,8 @@
TiC.PROPERTY_IMAGE_TOUCH_FEEDBACK_COLOR,
TiC.PROPERTY_IMAGES,
TiC.PROPERTY_REPEAT_COUNT,
TiC.PROPERTY_SCALING_MODE
TiC.PROPERTY_SCALING_MODE,
TiC.PROPERTY_TINT_COLOR
})
public class ImageViewProxy extends ViewProxy
{
Expand Down Expand Up @@ -110,18 +111,6 @@ public TiBlob toBlob()
return getImageView().toBlob();
}

@Kroll.getProperty
public int getTintColor()
{
return getImageView().getTintColor();
}

@Kroll.setProperty(runOnUiThread = true)
public void setTintColor(String color)
{
getImageView().setTintColor(color);
}

@Override
public String getApiName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public void handleCreationDict(KrollDict options)
}

@Kroll.getProperty
public String getColor()
public Object getColor()
{
return (String) getProperty(TiC.PROPERTY_COLOR);
return getProperty(TiC.PROPERTY_COLOR);
}

@Kroll.setProperty
public void setColor(String color)
public void setColor(Object color)
{
setPropertyAndFire(TiC.PROPERTY_COLOR, color);
onRowChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiColorHelper;

Expand Down Expand Up @@ -175,11 +174,8 @@ private void onTintColorChanged(Object colorName)
// Fetch and store the new tint color value.
if (colorName == null) {
this.tintColor = RefreshControlProxy.DEFAULT_TINT_COLOR;
} else if (colorName instanceof String) {
this.tintColor = TiColorHelper.parseColor((String) colorName, getActivity());
} else {
Log.e(TAG, "Property '" + TiC.PROPERTY_TINT_COLOR + "' must be of type string.");
return;
this.tintColor = TiColorHelper.parseColor(colorName, getActivity());
}

// Do not continue if a view has not been assigned to the refresh control yet.
Expand Down
43 changes: 41 additions & 2 deletions android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.TiRootActivity;
import org.appcelerator.titanium.proxy.ColorProxy;
import org.appcelerator.titanium.util.TiAnimationCurve;
import org.appcelerator.titanium.util.TiColorHelper;
import org.appcelerator.titanium.util.TiDeviceOrientation;
Expand All @@ -42,6 +43,7 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.WeakHashMap;
import java.io.File;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -463,6 +465,31 @@ public class UIModule extends KrollModule implements TiApplication.Configuration

protected static final int MSG_LAST_ID = KrollProxy.MSG_LAST_ID + 101;

protected static WeakHashMap<UIStyleChangedListener, Object> uiStyleChangedListeners = new WeakHashMap<>();

public interface UIStyleChangedListener {
void onUserInterfaceStyleChanged(int styleId);
}

public static void addUIStyleChangedListener(UIStyleChangedListener a)
{
Log.d(TAG, "addUIStyleChangedListener");
uiStyleChangedListeners.put(a, null);
}

public static void removeUIStyleChangedListener(UIStyleChangedListener a)
{
Log.d(TAG, "removeUIStyleChangedListener");
uiStyleChangedListeners.remove(a);
}

public static void notifyUIStyleChangedListeners(int styleId)
{
for (UIStyleChangedListener listener: uiStyleChangedListeners.keySet()) {
listener.onUserInterfaceStyleChanged(styleId);
}
}

public UIModule()
{
super();
Expand All @@ -483,12 +510,12 @@ public void onDisposing(KrollRuntime runtime)
}

@Kroll.setProperty(runOnUiThread = true)
public void setBackgroundColor(String color)
public void setBackgroundColor(Object color)
{
doSetBackgroundColor(color);
}

protected void doSetBackgroundColor(String color)
protected void doSetBackgroundColor(Object color)
{
TiRootActivity root = TiApplication.getInstance().getRootActivity();
if (root != null) {
Expand Down Expand Up @@ -624,6 +651,17 @@ public int getUserInterfaceStyle()
return getUserInterfaceStyle(TiApplication.getInstance().getResources().getConfiguration());
}

@Kroll.method
public ColorProxy fetchSemanticColor(Object value)
{
if (value instanceof ColorProxy) {
return (ColorProxy) value;
} else if (value instanceof String) {
return TiColorHelper.getColorProxy(value);
}
return null;
}

@Kroll.getProperty
public int statusBarHeight()
{
Expand Down Expand Up @@ -709,6 +747,7 @@ public void onConfigurationChanged(@NonNull Configuration config)
KrollDict event = new KrollDict();
event.put(TiC.PROPERTY_VALUE, lastEmittedStyle);
fireEvent(TiC.EVENT_USER_INTERFACE_STYLE, event);
notifyUIStyleChangedListeners(lastEmittedStyle);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)

// Handle barColor property.
if (hasProperty(TiC.PROPERTY_BAR_COLOR)) {
int colorInt = TiColorHelper.parseColor(TiConvert.toString(getProperty(TiC.PROPERTY_BAR_COLOR)), activity);
int colorInt = TiColorHelper.parseColor(getProperty(TiC.PROPERTY_BAR_COLOR), activity);
ActionBar actionBar = activity.getSupportActionBar();
// Guard for using a theme with actionBar disabled.
if (actionBar != null) {
Expand Down Expand Up @@ -446,7 +446,7 @@ public void onPropertyChanged(String name, Object value)
if (actionBar != null) {
// Change to background to the new color.
actionBar.setBackgroundDrawable(
new ColorDrawable(TiColorHelper.parseColor(TiConvert.toString(value), activity)));
new ColorDrawable(TiColorHelper.parseColor(value, activity)));
} else {
// Log a warning if there is no ActionBar available.
Log.w(TAG, "There is no ActionBar available for this Window.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
import ti.modules.titanium.ui.widget.webview.TiUIWebView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.WindowManager;
import android.webkit.WebSettings;

import androidx.annotation.ColorInt;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;

import com.google.android.material.color.MaterialColors;
Expand Down Expand Up @@ -307,27 +304,7 @@ public void run()
@Kroll.method
public ColorProxy getColorResource(Object idOrName)
{
try {
// Color by resource id
if (idOrName instanceof Number) {
int colorResId = ((Number) idOrName).intValue();
Context context = TiApplication.getAppRootOrCurrentActivity();
if (context == null) {
context = TiApplication.getInstance();
}
@ColorInt int packedColorInt = ContextCompat.getColor(context, colorResId);
return new ColorProxy(packedColorInt);
}
// Color by name
String colorName = idOrName.toString();
if (TiColorHelper.hasColorResource(colorName)) {
@ColorInt int packedColorInt = TiColorHelper.getColorResource(colorName);
return new ColorProxy(packedColorInt);
}
} catch (Exception e) {
// ignore
}
return null;
return TiColorHelper.getColorProxy(idOrName);
}

@Kroll.method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ public void setOrientation(TiExifOrientation exifOrientation)
updateScaleType();
}

public void setTintColor(String color)
public void setTintColor(Object color)
{
if (color == null || color.isEmpty()) {
if (color == null) {
imageView.clearColorFilter();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public void setItems(TiViewProxy[] proxies)

/**
* Sets the background color of the toolbar
* @param color String in Titanium color format
* @param value Color in any format supported by Titanium.
*/
public void setToolbarColor(String color)
public void setToolbarColor(Object value)
{
toolbar.setBackgroundColor((TiColorHelper.parseColor(color, proxy.getActivity())));
toolbar.setBackgroundColor((TiColorHelper.parseColor(value, proxy.getActivity())));
if (proxy.hasProperty(TiC.PROPERTY_TRANSLUCENT)) {
if ((Boolean) proxy.getProperty(TiC.PROPERTY_TRANSLUCENT)) {
toolbar.getBackground().setAlpha(BACKGROUND_TRANSLUCENT_VALUE);
Expand Down Expand Up @@ -322,7 +322,7 @@ private void setTitle(String value)
* Sets title's text color
* @param value Color in any format supported by Titanium.
*/
private void setTitleTextColor(String value)
private void setTitleTextColor(Object value)
{
toolbar.setTitleTextColor(TiColorHelper.parseColor(value, proxy.getActivity()));
}
Expand All @@ -340,7 +340,7 @@ private void setSubtitle(String value)
* Sets subtitle's text color
* @param value Color in any format supported by Titanium.
*/
private void setSubtitleTextColor(String value)
private void setSubtitleTextColor(Object value)
{
toolbar.setSubtitleTextColor(TiColorHelper.parseColor(value, proxy.getActivity()));
}
Expand Down Expand Up @@ -394,7 +394,7 @@ public void processProperties(KrollDict d)
{
//region process common properties
if (d.containsKey(TiC.PROPERTY_BAR_COLOR)) {
setToolbarColor(d.getString(TiC.PROPERTY_BAR_COLOR));
setToolbarColor(d.get(TiC.PROPERTY_BAR_COLOR));
}
if (d.containsKey(TiC.PROPERTY_EXTEND_BACKGROUND)) {
if (d.getBoolean(TiC.PROPERTY_EXTEND_BACKGROUND)) {
Expand Down Expand Up @@ -422,7 +422,7 @@ public void processProperties(KrollDict d)
setTitle(d.getString(TiC.PROPERTY_TITLE));
}
if (d.containsKey(TiC.PROPERTY_TITLE_TEXT_COLOR)) {
setTitleTextColor(d.getString(TiC.PROPERTY_TITLE_TEXT_COLOR));
setTitleTextColor(d.get(TiC.PROPERTY_TITLE_TEXT_COLOR));
}
if (d.containsKey(TiC.PROPERTY_SUBTITLE)) {
setSubtitle(d.getString(TiC.PROPERTY_SUBTITLE));
Expand Down Expand Up @@ -456,7 +456,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
{
super.propertyChanged(key, oldValue, newValue, proxy);
if (key.equals(TiC.PROPERTY_BAR_COLOR)) {
setToolbarColor(((String) newValue));
setToolbarColor(newValue);
}
if (key.equals(TiC.PROPERTY_TRANSLUCENT)) {
setTranslucent(((Boolean) newValue));
Expand All @@ -477,13 +477,13 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
setTitle((String) newValue);
}
if (key.equals(TiC.PROPERTY_TITLE_TEXT_COLOR)) {
setTitleTextColor((String) newValue);
setTitleTextColor(newValue);
}
if (key.equals(TiC.PROPERTY_SUBTITLE)) {
setSubtitle((String) newValue);
}
if (key.equals(TiC.PROPERTY_SUBTITLE_TEXT_COLOR)) {
setSubtitleTextColor((String) newValue);
setSubtitleTextColor(newValue);
}
if (key.equals(TiC.PROPERTY_CONTENT_INSET_END_WITH_ACTIONS)) {
setContentInsetEndWithActions((Integer) newValue);
Expand Down
Loading
Loading