Skip to content
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
49 changes: 46 additions & 3 deletions autojs/src/main/java/com/stardust/autojs/core/ui/ViewExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ public class ViewExtras {

private static final String LOG_TAG = "ViewExtras";
private NativeView mNativeView;

private ViewAttributes mViewAttributes;
/** 创建该 view 的 ViewAttributes 时所用的解析器,按 view 固定下来。 */
private ResourceParser mResourceParser;
// 只保留弱引用:脚本退出后不应再由 ViewExtras 静态持有 ScriptRuntime 的资源。
private static volatile java.lang.ref.WeakReference<ResourceParser> sResourceParser;

public static void setResourceParser(ResourceParser parser) {
sResourceParser = new java.lang.ref.WeakReference<>(parser);
}

private static ResourceParser globalParser() {
java.lang.ref.WeakReference<ResourceParser> ref = sResourceParser;
return ref == null ? null : ref.get();
}

public static ViewExtras get(View view) {
ViewExtras extras;
Expand All @@ -32,18 +44,25 @@ public static ViewExtras get(View view) {
return extras;
}

public static ViewAttributes getViewAttributes(View view) {
ViewExtras extras = get(view);
// 后启动的脚本会覆盖静态 parser;优先用本 view 创建时绑定的那个,
// 否则旧脚本控件的属性解析会落到另一个脚本的运行时上。
ResourceParser parser = extras.mResourceParser;
return getViewAttributes(view, parser != null ? parser : globalParser());
}

public static ViewAttributes getViewAttributes(View view, ResourceParser parser) {
ViewExtras extras = get(view);
ViewAttributes attributes = extras.getViewAttributes();
if (attributes == null) {
attributes = ViewAttributesFactory.create(parser, view);
extras.setViewAttributes(attributes);
extras.mResourceParser = parser;
}
return attributes;
}


public static NativeView getNativeView(Scriptable scope, View view, Class<?> staticType, ScriptRuntime runtime) {
ViewExtras extras = get(view);
NativeView nativeView = extras.getNativeView();
Expand All @@ -59,6 +78,30 @@ public static NativeView getNativeView(View view) {
return extras.getNativeView();
}

public static void setAttr(View view, String name, Object value) {
ViewAttributes attrs = getViewAttributes(view);
attrs.setAttr(name, String.valueOf(value));
}

public static Object getAttr(View view, String name) {
ViewExtras extras = get(view);
ViewAttributes attrs = extras.getViewAttributes();
if (attrs != null) {
return attrs.getAttrValue(name);
}
return null;
}

public static NativeView getOrCreateNativeView(Scriptable scope, View view, ScriptRuntime runtime) {
ViewExtras extras = get(view);
NativeView nativeView = extras.getNativeView();
if (nativeView == null) {
nativeView = new NativeView(scope, view, view.getClass(), runtime);
extras.setNativeView(nativeView);
}
return nativeView;
}

public final NativeView getNativeView() {
return mNativeView;
}
Expand All @@ -74,4 +117,4 @@ public final void setNativeView(NativeView nativeView) {
public final void setViewAttributes(ViewAttributes viewAttributes) {
mViewAttributes = viewAttributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public void setAttrValue(String name, String value) {
}
}

public void setAttr(String name, Object value) {
setAttrValue(name, String.valueOf(value));
}

@SuppressLint("ClickableViewAccessibility")
private void init() {
onRegisterAttrs();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.stardust.autojs.core.ui.widget;

import android.view.View;
import android.view.ViewGroup;

import com.stardust.autojs.core.ui.ViewExtras;
import com.stardust.autojs.core.ui.attribute.ViewAttributes;

import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Undefined;

public interface AttrSupport {

default void attr(String name, Object value) {
ViewAttributes attrs = ViewExtras.getViewAttributes((View) this);
if (attrs != null) {
attrs.setAttr(name, ScriptRuntime.toString(value));
}
}

default Object attr(String name) {
ViewAttributes attrs = ViewExtras.getViewAttributes((View) this);
if (attrs != null) {
return attrs.getAttrValue(name);
}
return Undefined.SCRIPTABLE_UNDEFINED;
}

default void setWidth(int width) {
if (this instanceof View) {
View view = (View) this;
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params != null) {
params.width = width;
view.setLayoutParams(params);
view.requestLayout();
}
}
}

default void setHeight(int height) {
if (this instanceof View) {
View view = (View) this;
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params != null) {
params.height = height;
view.setLayoutParams(params);
view.requestLayout();
}
}
}

default int getWidth() {
return this instanceof View ? ((View) this).getWidth() : 0;
}

default int getHeight() {
return this instanceof View ? ((View) this).getHeight() : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

@SuppressLint("AppCompatCustomView")
public class JsButton extends Button {
public class JsButton extends Button implements AttrSupport {
public JsButton(Context context) {
super(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

@SuppressLint("AppCompatCustomView")
public class JsEditText extends EditText {
public class JsEditText extends EditText implements AttrSupport {
public JsEditText(Context context) {
super(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Created by Stardust on 2017/5/14.
*/

public class JsFrameLayout extends FrameLayout {
public class JsFrameLayout extends FrameLayout implements AttrSupport {
public JsFrameLayout(@NonNull Context context) {
super(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Created by Stardust on 2018/3/30.
*/

public class JsGridView extends JsListView {
public class JsGridView extends JsListView implements AttrSupport {

public JsGridView(Context context, ScriptRuntime scriptRuntime) {
super(context, scriptRuntime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Created by Stardust on 2017/11/30.
*/

public class JsImageView extends RoundedImageView {
public class JsImageView extends RoundedImageView implements AttrSupport {

private boolean mCircle;
private Drawables mDrawables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Created by Stardust on 2017/5/14.
*/

public class JsLinearLayout extends LinearLayout {
public class JsLinearLayout extends LinearLayout implements AttrSupport {


public JsLinearLayout(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Created by Stardust on 2018/3/28.
*/

public class JsListView extends RecyclerView {
public class JsListView extends RecyclerView implements AttrSupport {

public interface DataSourceAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Created by Stardust on 2017/5/14.
*/

public class JsRelativeLayout extends RelativeLayout {
public class JsRelativeLayout extends RelativeLayout implements AttrSupport {
public JsRelativeLayout(Context context) {
super(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class JsSpinner extends androidx.appcompat.widget.AppCompatSpinner {
import com.stardust.autojs.core.ui.ViewExtras;

public class JsSpinner extends androidx.appcompat.widget.AppCompatSpinner implements AttrSupport {

private float mTextSize = -1;
private int mTextStyle = -1;
Expand All @@ -21,6 +24,13 @@
private int mEntryTextStyle = -1;
private int mEntryTextColor = 0;

private int mEntryHeight = -1;

private int mDropdownTextColorNormal;
private int mDropdownTextColorSelected;
private int mDropdownBgColorNormal;
private int mDropdownBgColorSelected;

public JsSpinner(Context context) {
super(context);
}
Expand All @@ -45,7 +55,6 @@
super(context, attrs, defStyleAttr, mode, popupTheme);
}


public float getTextSize() {
return mTextSize;
}
Expand Down Expand Up @@ -84,7 +93,6 @@
}
}


public int getTextStyle() {
return mTextStyle;
}
Expand Down Expand Up @@ -113,6 +121,59 @@
}
}

public void setDropDownMaxHeight(int maxHeightPx) {
try {
java.lang.reflect.Method method = androidx.appcompat.widget.AppCompatSpinner.class
.getDeclaredMethod("getInternalPopup");
method.setAccessible(true);

Check warning on line 128 in autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsSpinner.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsSpinner.java#L128

You should not modify visibility of constructors, methods or fields using setAccessible()
Object popup = method.invoke(this);
if (popup != null) {
java.lang.reflect.Method setHeight = popup.getClass()
.getMethod("setHeight", int.class);
setHeight.invoke(popup, maxHeightPx);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void setDropDownItemHeight(int heightPx) {
mEntryHeight = heightPx;
}

public void setDropdownTextColors(int normalColor, int selectedColor) {
mDropdownTextColorNormal = normalColor;
mDropdownTextColorSelected = selectedColor;
}

public void setDropdownBackgroundColors(int normalColor, int selectedColor) {
mDropdownBgColorNormal = normalColor;
mDropdownBgColorSelected = selectedColor;
}

public void setDropdownItems(String[] items) {
int defaultHeight = (int) (48 * getContext().getResources().getDisplayMetrics().density);
setDropdownItems(items, defaultHeight, null, null);
}

public void setDropdownItems(String[] items, Integer itemHeight) {
setDropdownItems(items, itemHeight, null, null);
}

public void setDropdownItems(String[] items, Integer itemHeight, Integer itemLayout, Integer dropDownLayout) {
if (items == null || items.length == 0) return;

if (itemHeight != null) {
mEntryHeight = itemHeight;
}

int layoutRes = itemLayout != null ? itemLayout : android.R.layout.simple_spinner_item;
int dropDownRes = dropDownLayout != null ? dropDownLayout : android.R.layout.simple_spinner_dropdown_item;

Adapter adapter = new Adapter(getContext(), layoutRes, items);
adapter.setDropDownViewResource(dropDownRes);
setAdapter(adapter);
}

public class Adapter extends ArrayAdapter<String> {

Expand Down Expand Up @@ -147,6 +208,7 @@
return view;
}
TextView textView = (TextView) view;

if (mEntryTextColor != 0) {
textView.setTextColor(mEntryTextColor);
}
Expand All @@ -156,7 +218,32 @@
if (mEntryTextStyle != -1) {
textView.setTypeface(textView.getTypeface(), mEntryTextStyle);
}

if (position == getSelectedItemPosition()) {
if (mDropdownTextColorSelected != 0) {
textView.setTextColor(mDropdownTextColorSelected);
}
if (mDropdownBgColorSelected != 0) {
textView.setBackgroundColor(mDropdownBgColorSelected);
}
} else {
if (mDropdownTextColorNormal != 0) {
textView.setTextColor(mDropdownTextColorNormal);
}
if (mDropdownBgColorNormal != 0) {
textView.setBackgroundColor(mDropdownBgColorNormal);
}
}

if (mEntryHeight > 0) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
mEntryHeight
);
textView.setLayoutParams(lp);
}

return textView;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.google.android.material.tabs.TabLayout;

public class JsTabLayout extends TabLayout {
public class JsTabLayout extends TabLayout implements AttrSupport {

public JsTabLayout(Context context) {
super(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

@SuppressLint("AppCompatCustomView")
public class JsTextView extends TextView {
public class JsTextView extends TextView implements AttrSupport {

public JsTextView(Context context) {
super(context);
Expand Down
Loading
Loading