mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
add: simple code generation
This commit is contained in:
parent
0b884f5747
commit
7c1decdc04
@ -102,6 +102,7 @@ dependencies {
|
||||
compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'
|
||||
compile 'com.wang.avi:library:2.1.3'
|
||||
compile 'org.apache.commons:commons-lang3:3.6'
|
||||
compile 'com.thoughtbot:expandablerecyclerview:1.3'
|
||||
compile 'com.github.hyb1996:FloatingCircularActionMenu:0.0.2'
|
||||
compile(name: 'apkbuilder-release', ext: 'aar')
|
||||
|
||||
|
||||
@ -0,0 +1,306 @@
|
||||
package com.stardust.scriptdroid.ui.codegeneration;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.bignerdranch.expandablerecyclerview.ChildViewHolder;
|
||||
import com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter;
|
||||
import com.bignerdranch.expandablerecyclerview.ParentViewHolder;
|
||||
import com.bignerdranch.expandablerecyclerview.model.Parent;
|
||||
import com.stardust.autojs.codegeneration.UiSelectorGenerator;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.ui.widget.CheckBoxCompat;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
import com.stardust.theme.util.ListBuilder;
|
||||
import com.stardust.util.ClipboardUtil;
|
||||
import com.stardust.view.accessibility.NodeInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnCheckedChanged;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/11/6.
|
||||
*/
|
||||
|
||||
public class CodeGenerateDialog extends ThemeColorMaterialDialogBuilder {
|
||||
|
||||
private final List<OptionGroup> mOptionGroups = new ListBuilder<OptionGroup>()
|
||||
.add(new OptionGroup(R.string.text_options, false)
|
||||
.addOption(R.string.text_using_id_selector, true)
|
||||
.addOption(R.string.text_using_text_selector, true)
|
||||
.addOption(R.string.text_using_desc_selector, true))
|
||||
.add(new OptionGroup(R.string.text_select)
|
||||
.addOption(R.string.text_find_one, true)
|
||||
.addOption(R.string.text_until_find)
|
||||
.addOption(R.string.text_wait_for)
|
||||
.addOption(R.string.text_selector_exists))
|
||||
.add(new OptionGroup(R.string.text_action)
|
||||
.addOption(R.string.text_click)
|
||||
.addOption(R.string.text_long_click)
|
||||
.addOption(R.string.text_set_text)
|
||||
.addOption(R.string.text_scroll_forward)
|
||||
.addOption(R.string.text_scroll_backward))
|
||||
.list();
|
||||
|
||||
@BindView(R.id.options)
|
||||
RecyclerView mOptionsRecyclerView;
|
||||
|
||||
private NodeInfo mRootNode;
|
||||
private NodeInfo mTargetNode;
|
||||
private Adapter mAdapter;
|
||||
|
||||
public CodeGenerateDialog(@NonNull Context context, NodeInfo rootNode, NodeInfo targetNode) {
|
||||
super(context);
|
||||
mRootNode = rootNode;
|
||||
mTargetNode = targetNode;
|
||||
positiveText(R.string.text_generate_and_copy);
|
||||
neutralText(R.string.text_generate_and_open_editor);
|
||||
onPositive(((dialog, which) -> generateCodeAndCopy()));
|
||||
onNeutral(((dialog, which) -> generateCodeAndOpenEditor()));
|
||||
setupViews();
|
||||
}
|
||||
|
||||
private void generateCodeAndCopy() {
|
||||
String code = generateCode();
|
||||
if (code == null) {
|
||||
Toast.makeText(getContext(), R.string.text_generate_fail, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
ClipboardUtil.setClip(getContext(), code);
|
||||
Toast.makeText(getContext(), R.string.text_already_copy_to_clip, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void generateCodeAndOpenEditor() {
|
||||
|
||||
}
|
||||
|
||||
private String generateCode() {
|
||||
UiSelectorGenerator generator = new UiSelectorGenerator(mRootNode, mTargetNode);
|
||||
OptionGroup settings = getOptionGroup(R.string.text_options);
|
||||
generator.setUsingId(settings.getOption(R.string.text_using_id_selector).checked);
|
||||
generator.setUsingText(settings.getOption(R.string.text_using_text_selector).checked);
|
||||
generator.setUsingDesc(settings.getOption(R.string.text_using_desc_selector).checked);
|
||||
generator.setSearchMode(getSearchMode());
|
||||
setAction(generator);
|
||||
return generator.generate();
|
||||
}
|
||||
|
||||
private void setAction(UiSelectorGenerator generator) {
|
||||
OptionGroup action = getOptionGroup(R.string.text_action);
|
||||
if (action.getOption(R.string.text_click).checked) {
|
||||
generator.setAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
|
||||
}
|
||||
if (action.getOption(R.string.text_long_click).checked) {
|
||||
generator.setAction(AccessibilityNodeInfoCompat.ACTION_LONG_CLICK);
|
||||
}
|
||||
if (action.getOption(R.string.text_scroll_forward).checked) {
|
||||
generator.setAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
|
||||
}
|
||||
if (action.getOption(R.string.text_scroll_backward).checked) {
|
||||
generator.setAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
|
||||
}
|
||||
}
|
||||
|
||||
private int getSearchMode() {
|
||||
OptionGroup selectMode = getOptionGroup(R.string.text_select);
|
||||
if (selectMode.getOption(R.string.text_find_one).checked) {
|
||||
return UiSelectorGenerator.FIND_ONE;
|
||||
}
|
||||
if (selectMode.getOption(R.string.text_until_find).checked) {
|
||||
return UiSelectorGenerator.UNTIL_FIND;
|
||||
}
|
||||
if (selectMode.getOption(R.string.text_wait_for).checked) {
|
||||
return UiSelectorGenerator.WAIT_FOR;
|
||||
}
|
||||
if (selectMode.getOption(R.string.text_wait_for).checked) {
|
||||
return UiSelectorGenerator.EXISTS;
|
||||
}
|
||||
return UiSelectorGenerator.FIND_ONE;
|
||||
}
|
||||
|
||||
private void setupViews() {
|
||||
View view = View.inflate(context, R.layout.dialog_code_generate, null);
|
||||
ButterKnife.bind(this, view);
|
||||
customView(view, false);
|
||||
mOptionsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
mAdapter = new Adapter(mOptionGroups);
|
||||
mOptionsRecyclerView.setAdapter(mAdapter);
|
||||
}
|
||||
|
||||
|
||||
private OptionGroup getOptionGroup(int title) {
|
||||
for (OptionGroup group : mOptionGroups) {
|
||||
if (group.titleRes == title) {
|
||||
return group;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
|
||||
private void uncheckOthers(int parentAdapterPosition, Option child) {
|
||||
boolean notify = false;
|
||||
for (Option other : child.group.options) {
|
||||
if (other != child) {
|
||||
if (other.checked) {
|
||||
other.checked = false;
|
||||
notify = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (notify)
|
||||
mAdapter.notifyParentChanged(parentAdapterPosition);
|
||||
}
|
||||
|
||||
private static class Option {
|
||||
int titleRes;
|
||||
boolean checked;
|
||||
OptionGroup group;
|
||||
|
||||
Option(int titleRes, boolean checked) {
|
||||
this.titleRes = titleRes;
|
||||
this.checked = checked;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class OptionViewHolder extends ChildViewHolder<Option> {
|
||||
|
||||
@BindView(R.id.title)
|
||||
TextView title;
|
||||
@BindView(R.id.checkbox)
|
||||
CheckBoxCompat checkBox;
|
||||
|
||||
OptionViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
ButterKnife.bind(this, itemView);
|
||||
itemView.setOnClickListener(view -> checkBox.toggle());
|
||||
}
|
||||
|
||||
@OnCheckedChanged(R.id.checkbox)
|
||||
void onCheckedChanged() {
|
||||
getChild().checked = checkBox.isChecked();
|
||||
if (checkBox.isChecked() && getChild().group.titleRes != R.string.text_action)
|
||||
uncheckOthers(getParentAdapterPosition(), getChild());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class OptionGroup implements Parent<Option> {
|
||||
int titleRes;
|
||||
List<Option> options = new ArrayList<>();
|
||||
private final boolean mInitialExpanded;
|
||||
|
||||
|
||||
OptionGroup(int titleRes, boolean initialExpanded) {
|
||||
this.titleRes = titleRes;
|
||||
mInitialExpanded = initialExpanded;
|
||||
}
|
||||
|
||||
OptionGroup(int titleRes) {
|
||||
this(titleRes, true);
|
||||
}
|
||||
|
||||
Option getOption(int titleRes) {
|
||||
for (Option option : options) {
|
||||
if (option.titleRes == titleRes) {
|
||||
return option;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Option> getChildList() {
|
||||
return options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitiallyExpanded() {
|
||||
return mInitialExpanded;
|
||||
}
|
||||
|
||||
OptionGroup addOption(int titleRes) {
|
||||
return addOption(titleRes, false);
|
||||
}
|
||||
|
||||
OptionGroup addOption(int res, boolean checked) {
|
||||
Option option = new Option(res, checked);
|
||||
option.group = this;
|
||||
options.add(option);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class OptionGroupViewHolder extends ParentViewHolder<OptionGroup, Option> {
|
||||
|
||||
TextView title;
|
||||
ImageView icon;
|
||||
|
||||
OptionGroupViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
title = (TextView) itemView.findViewById(R.id.title);
|
||||
icon = (ImageView) itemView.findViewById(R.id.icon);
|
||||
itemView.setOnClickListener(view -> {
|
||||
if (isExpanded()) {
|
||||
collapseView();
|
||||
} else {
|
||||
expandView();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExpansionToggled(boolean expanded) {
|
||||
icon.setRotation(expanded ? -90 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
private class Adapter extends ExpandableRecyclerAdapter<OptionGroup, Option, OptionGroupViewHolder, OptionViewHolder> {
|
||||
|
||||
public Adapter(@NonNull List<OptionGroup> parentList) {
|
||||
super(parentList);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public OptionGroupViewHolder onCreateParentViewHolder(@NonNull ViewGroup parentViewGroup, int viewType) {
|
||||
return new OptionGroupViewHolder(LayoutInflater.from(parentViewGroup.getContext())
|
||||
.inflate(R.layout.dialog_code_generate_option_group, parentViewGroup, false));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public OptionViewHolder onCreateChildViewHolder(@NonNull ViewGroup childViewGroup, int viewType) {
|
||||
return new OptionViewHolder(LayoutInflater.from(childViewGroup.getContext())
|
||||
.inflate(R.layout.dialog_code_generate_option, childViewGroup, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindParentViewHolder(@NonNull OptionGroupViewHolder viewHolder, int parentPosition, @NonNull OptionGroup optionGroup) {
|
||||
viewHolder.title.setText(optionGroup.titleRes);
|
||||
viewHolder.icon.setRotation(viewHolder.isExpanded() ? 0 : -90);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindChildViewHolder(@NonNull OptionViewHolder viewHolder, int parentPosition, int childPosition, @NonNull Option option) {
|
||||
viewHolder.title.setText(option.titleRes);
|
||||
viewHolder.checkBox.setChecked(option.checked, false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -9,8 +9,10 @@ import android.view.WindowManager;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.afollestad.materialdialogs.Theme;
|
||||
import com.stardust.app.DialogUtils;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.ui.codegeneration.CodeGenerateDialog;
|
||||
import com.stardust.scriptdroid.ui.floating.FullScreenFloatyWindow;
|
||||
import com.stardust.view.accessibility.NodeInfo;
|
||||
import com.stardust.scriptdroid.ui.widget.BubblePopupMenu;
|
||||
@ -81,22 +83,27 @@ public class LayoutBoundsFloatyWindow extends FullScreenFloatyWindow {
|
||||
return;
|
||||
mBubblePopMenu = new BubblePopupMenu(mContext, Arrays.asList(
|
||||
mContext.getString(R.string.text_show_widget_infomation),
|
||||
mContext.getString(R.string.text_show_layout_hierarchy)));
|
||||
mBubblePopMenu.setOnItemClickListener(new BubblePopupMenu.OnItemClickListener() {
|
||||
@Override
|
||||
public void onClick(View view, int position) {
|
||||
mBubblePopMenu.dismiss();
|
||||
if (position == 0) {
|
||||
showNodeInfo();
|
||||
} else {
|
||||
showLayoutHierarchy();
|
||||
}
|
||||
mContext.getString(R.string.text_show_layout_hierarchy),
|
||||
mContext.getString(R.string.text_generate_code)));
|
||||
mBubblePopMenu.setOnItemClickListener((view, position) -> {
|
||||
mBubblePopMenu.dismiss();
|
||||
if (position == 0) {
|
||||
showNodeInfo();
|
||||
} else if (position == 1) {
|
||||
showLayoutHierarchy();
|
||||
} else {
|
||||
generateCode();
|
||||
}
|
||||
});
|
||||
mBubblePopMenu.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
mBubblePopMenu.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
}
|
||||
|
||||
private void generateCode() {
|
||||
DialogUtils.showDialog(new CodeGenerateDialog(mContext, mRootNode, mSelectedNode)
|
||||
.build());
|
||||
}
|
||||
|
||||
private void showLayoutHierarchy() {
|
||||
close();
|
||||
LayoutHierarchyFloatyWindow window = new LayoutHierarchyFloatyWindow(mRootNode);
|
||||
|
||||
@ -26,12 +26,12 @@ import com.stardust.scriptdroid.BuildConfig;
|
||||
import com.stardust.scriptdroid.Pref;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.ui.codegeneration.CodeGenerateDialog;
|
||||
import com.stardust.scriptdroid.ui.common.NotAskAgainDialog;
|
||||
import com.stardust.scriptdroid.ui.doc.OnlineDocsFragment_;
|
||||
import com.stardust.scriptdroid.ui.floating.FloatyWindowManger;
|
||||
import com.stardust.scriptdroid.io.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.main.community.CommunityFragment_;
|
||||
import com.stardust.scriptdroid.ui.main.sample.SampleListFragment;
|
||||
import com.stardust.scriptdroid.ui.log.LogActivity_;
|
||||
import com.stardust.scriptdroid.ui.main.sample.SampleListFragment_;
|
||||
import com.stardust.scriptdroid.ui.main.scripts.MyScriptListFragment_;
|
||||
@ -53,7 +53,6 @@ import org.androidannotations.annotations.EActivity;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
@EActivity(R.layout.activity_main)
|
||||
@ -262,7 +261,7 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if(item.getItemId() == R.id.action_log){
|
||||
if (item.getItemId() == R.id.action_log) {
|
||||
LogActivity_.intent(this).start();
|
||||
return true;
|
||||
}
|
||||
|
||||
7
app/src/main/res/layout/activity_code_generate.xml
Normal file
7
app/src/main/res/layout/activity_code_generate.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
</LinearLayout>
|
||||
11
app/src/main/res/layout/dialog_code_generate.xml
Normal file
11
app/src/main/res/layout/dialog_code_generate.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/options"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
</LinearLayout>
|
||||
22
app/src/main/res/layout/dialog_code_generate_option.xml
Normal file
22
app/src/main/res/layout/dialog_code_generate_option.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?selectableItemBackground"
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp">
|
||||
|
||||
<com.stardust.scriptdroid.ui.widget.CheckBoxCompat
|
||||
android:id="@+id/checkbox"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_marginLeft="12dp"
|
||||
android:textColor="#99000000"
|
||||
android:textSize="16sp"/>
|
||||
</LinearLayout>
|
||||
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?selectableItemBackground"
|
||||
android:orientation="horizontal"
|
||||
android:padding="8dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_marginRight="4dp"
|
||||
android:src="@drawable/ic_arrow_drop_down_black_36dp"
|
||||
android:tint="#424349"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center_vertical"
|
||||
android:textColor="#424349"
|
||||
android:textSize="13sp"/>
|
||||
</LinearLayout>
|
||||
@ -313,4 +313,22 @@
|
||||
<string name="text_select_all">全选</string>
|
||||
<string name="text_cut">剪切</string>
|
||||
<string name="text_copy">复制</string>
|
||||
<string name="text_generate_code">生成代码</string>
|
||||
<string name="text_action">动作</string>
|
||||
<string name="text_click">点击(click)</string>
|
||||
<string name="text_long_click">长按(longClick)</string>
|
||||
<string name="text_set_text">设置文本(setText)</string>
|
||||
<string name="text_scroll_forward">向前/右/下滑动(scrollForward)</string>
|
||||
<string name="text_scroll_backward">向后/上/左滑动(scrollBackward)</string>
|
||||
<string name="text_find_one">直到找到一个(findOne)</string>
|
||||
<string name="text_until_find">直到找到所有(untilFind)</string>
|
||||
<string name="text_wait_for">等待控件出现(waitFor)</string>
|
||||
<string name="text_selector_exists">判断控件存在(exists)</string>
|
||||
<string name="text_options">选项</string>
|
||||
<string name="text_using_id_selector">使用id选择</string>
|
||||
<string name="text_using_text_selector">使用文本选择(text)</string>
|
||||
<string name="text_using_desc_selector">使用描述选择(desc)</string>
|
||||
<string name="text_generate_and_copy">生成并复制</string>
|
||||
<string name="text_generate_and_open_editor">生成并打开编辑器</string>
|
||||
<string name="text_generate_fail">生成失败了o(╥﹏╥)o</string>
|
||||
</resources>
|
||||
|
||||
@ -21,6 +21,10 @@ android {
|
||||
}
|
||||
|
||||
|
||||
compileOptions {
|
||||
targetCompatibility 1.8
|
||||
sourceCompatibility 1.8
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
package com.stardust.autojs.codegeneration;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/4.
|
||||
*/
|
||||
|
||||
public class AutoScriptGenerator {
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,226 @@
|
||||
package com.stardust.autojs.codegeneration;
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
|
||||
|
||||
import com.stardust.automator.UiObject;
|
||||
import com.stardust.automator.UiObjectCollection;
|
||||
import com.stardust.view.accessibility.NodeInfo;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/11/5.
|
||||
*/
|
||||
|
||||
public class ReadOnlyUiObject extends UiObject {
|
||||
|
||||
private NodeInfo mNodeInfo;
|
||||
private boolean mUsingId;
|
||||
private boolean mUsingText;
|
||||
private boolean mUsingDesc;
|
||||
|
||||
public ReadOnlyUiObject(NodeInfo info) {
|
||||
super(info, info.depth);
|
||||
mNodeInfo = info;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public UiObject child(int i) {
|
||||
return new ReadOnlyUiObject(mNodeInfo.getChildren().get(i));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int childCount() {
|
||||
return mNodeInfo.getChildren().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return childCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String className() {
|
||||
return mNodeInfo.className.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getClassName() {
|
||||
return className();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String packageName() {
|
||||
return mNodeInfo.packageName.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPackageName() {
|
||||
return packageName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String id() {
|
||||
return mNodeInfo.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String desc() {
|
||||
return mNodeInfo.desc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getViewIdResourceName() {
|
||||
return id();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getContentDescription() {
|
||||
return desc();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Rect bounds() {
|
||||
return mNodeInfo.getBoundsInScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rect boundsInParent() {
|
||||
return mNodeInfo.getBoundsInParent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int drawingOrder() {
|
||||
return mNodeInfo.drawingOrder;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String text() {
|
||||
return mNodeInfo.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getText() {
|
||||
return text();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessibilityNodeInfoCompat getChild(int index) {
|
||||
return child(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDrawingOrder() {
|
||||
return drawingOrder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBoundsInParent(Rect outBounds) {
|
||||
outBounds.set(mNodeInfo.getBoundsInParent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBoundsInScreen(Rect outBounds) {
|
||||
outBounds.set(mNodeInfo.getBoundsInScreen());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int depth() {
|
||||
return mNodeInfo.depth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkable() {
|
||||
return mNodeInfo.checkable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checked() {
|
||||
return mNodeInfo.checked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean focusable() {
|
||||
return mNodeInfo.focusable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean focused() {
|
||||
return mNodeInfo.focused;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visibleToUser() {
|
||||
return mNodeInfo.visibleToUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accessibilityFocused() {
|
||||
return mNodeInfo.accessibilityFocused;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean selected() {
|
||||
return mNodeInfo.selected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clickable() {
|
||||
return mNodeInfo.clickable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean longClickable() {
|
||||
return mNodeInfo.longClickable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enabled() {
|
||||
return mNodeInfo.enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrollable() {
|
||||
return mNodeInfo.scrollable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int row() {
|
||||
return mNodeInfo.row;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int column() {
|
||||
return mNodeInfo.column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int rowSpan() {
|
||||
return mNodeInfo.rowSpan;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int columnSpan() {
|
||||
return mNodeInfo.columnSpan;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int rowCount() {
|
||||
return mNodeInfo.rowCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int columnCount() {
|
||||
return mNodeInfo.columnCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recycle() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,149 @@
|
||||
package com.stardust.autojs.codegeneration;
|
||||
|
||||
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
|
||||
|
||||
import com.stardust.automator.UiGlobalSelector;
|
||||
import com.stardust.util.Consumer;
|
||||
import com.stardust.view.accessibility.NodeInfo;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/4.
|
||||
*/
|
||||
|
||||
public class UiSelectorGenerator {
|
||||
|
||||
|
||||
public static final int UNTIL_FIND = 0;
|
||||
public static final int FIND_ONE = 1;
|
||||
public static final int WAIT_FOR = 2;
|
||||
public static final int EXISTS = 3;
|
||||
|
||||
private ReadOnlyUiObject mRoot;
|
||||
private ReadOnlyUiObject mTarget;
|
||||
private boolean mUsingId = true;
|
||||
private boolean mUsingDesc = true;
|
||||
private boolean mUsingText = true;
|
||||
private UiGlobalSelector mUiGlobalSelector;
|
||||
private int mSearchMode = FIND_ONE;
|
||||
private int mAction = -1;
|
||||
|
||||
public UiSelectorGenerator(NodeInfo root, NodeInfo target) {
|
||||
this(new ReadOnlyUiObject(root), new ReadOnlyUiObject(target));
|
||||
}
|
||||
|
||||
public UiSelectorGenerator(ReadOnlyUiObject root, ReadOnlyUiObject target) {
|
||||
mRoot = root;
|
||||
mTarget = target;
|
||||
}
|
||||
|
||||
public void setSearchMode(int searchMode) {
|
||||
mSearchMode = searchMode;
|
||||
}
|
||||
|
||||
public void setAction(int action) {
|
||||
mAction = action;
|
||||
}
|
||||
|
||||
public void setUsingId(boolean usingId) {
|
||||
mUsingId = usingId;
|
||||
}
|
||||
|
||||
public void setUsingDesc(boolean usingDesc) {
|
||||
mUsingDesc = usingDesc;
|
||||
}
|
||||
|
||||
public void setUsingText(boolean usingText) {
|
||||
mUsingText = usingText;
|
||||
}
|
||||
|
||||
|
||||
private boolean tryWithStringCondition(String name, String value, Consumer<String> condition, StringBuilder code) {
|
||||
if (value == null || value.isEmpty())
|
||||
return false;
|
||||
code.append('.').append(name).append("(\"").append(value).append("\")");
|
||||
condition.accept(value);
|
||||
return isConditionEnough();
|
||||
}
|
||||
|
||||
private boolean tryWithIntCondition(String name, int value, Consumer<Integer> condition, StringBuilder code) {
|
||||
code.append('.').append(name).append('(').append(value).append(')');
|
||||
condition.accept(value);
|
||||
return isConditionEnough();
|
||||
}
|
||||
|
||||
private boolean isConditionEnough() {
|
||||
if (mSearchMode == UNTIL_FIND) {
|
||||
return !mUiGlobalSelector.findAndReturnList(mRoot).isEmpty();
|
||||
} else {
|
||||
return mUiGlobalSelector.findAndReturnList(mRoot).size() == 1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public String generate() {
|
||||
String selector = generateSelector();
|
||||
if (selector == null) {
|
||||
return null;
|
||||
}
|
||||
//remove '.'
|
||||
selector = selector.substring(1);
|
||||
if (mSearchMode == WAIT_FOR) {
|
||||
return selector + ".waitFor()";
|
||||
}
|
||||
if (mSearchMode == EXISTS) {
|
||||
return "if(" + selector + ".exists()){\n \n}";
|
||||
}
|
||||
String action = getAction();
|
||||
if (action == null) {
|
||||
return selector;
|
||||
} else {
|
||||
return selector + action;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getAction() {
|
||||
switch (mAction) {
|
||||
case AccessibilityNodeInfoCompat.ACTION_CLICK:
|
||||
return ".click()";
|
||||
case AccessibilityNodeInfoCompat.ACTION_LONG_CLICK:
|
||||
return ".longClick()";
|
||||
|
||||
case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD:
|
||||
return ".scrollBackward()";
|
||||
|
||||
case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD:
|
||||
return ".scrollForward()";
|
||||
|
||||
case AccessibilityNodeInfoCompat.ACTION_SET_TEXT:
|
||||
return ".setText(\"\")";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String generateSelector() {
|
||||
mUiGlobalSelector = new UiGlobalSelector();
|
||||
StringBuilder code = new StringBuilder();
|
||||
if (mUsingId &&
|
||||
tryWithStringCondition("id", mTarget.id(), mUiGlobalSelector::id, code)) {
|
||||
return code.toString();
|
||||
}
|
||||
if (tryWithIntCondition("depth", mTarget.depth(), mUiGlobalSelector::depth, code)) {
|
||||
return code.toString();
|
||||
}
|
||||
if (tryWithStringCondition("className", mTarget.className(), mUiGlobalSelector::className, code)) {
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
if (mUsingText &&
|
||||
tryWithStringCondition("text", mTarget.text(), mUiGlobalSelector::text, code)) {
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
if (mUsingDesc &&
|
||||
tryWithStringCondition("desc", mTarget.desc(), mUiGlobalSelector::desc, code)) {
|
||||
return code.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -403,12 +403,16 @@ public class UiGlobalSelector {
|
||||
}
|
||||
|
||||
public UiObjectCollection findOf(UiObject node) {
|
||||
return UiObjectCollection.of(findAndReturnList(node));
|
||||
}
|
||||
|
||||
public List<UiObject> findAndReturnList(UiObject node) {
|
||||
List<UiObject> list = new ArrayList<>();
|
||||
list.add(node);
|
||||
for (ListFilter filter : mFilters) {
|
||||
list = filter.filter(list);
|
||||
}
|
||||
return UiObjectCollection.of(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@ -139,7 +139,8 @@ public class UiObject extends AccessibilityNodeInfoCompat {
|
||||
}
|
||||
|
||||
public String desc() {
|
||||
return getContentDescription().toString();
|
||||
CharSequence d = getContentDescription();
|
||||
return d == null ? null : d.toString();
|
||||
}
|
||||
|
||||
public String className() {
|
||||
@ -411,7 +412,7 @@ public class UiObject extends AccessibilityNodeInfoCompat {
|
||||
}
|
||||
|
||||
public List<UiObject> findByText(String text) {
|
||||
return compatListToUiObjectList(findAccessibilityNodeInfosByText(text), mAllocator);
|
||||
return new UiGlobalSelector().textContains(text).findAndReturnList(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -422,7 +423,7 @@ public class UiObject extends AccessibilityNodeInfoCompat {
|
||||
}
|
||||
|
||||
public List<UiObject> findByViewId(String viewId) {
|
||||
return compatListToUiObjectList(findAccessibilityNodeInfosByViewId(viewId), mAllocator);
|
||||
return new UiGlobalSelector().id(viewId).findAndReturnList(this);
|
||||
}
|
||||
|
||||
public static List<UiObject> compatListToUiObjectList(List<AccessibilityNodeInfoCompat> compats, AccessibilityNodeInfoAllocator allocator) {
|
||||
|
||||
@ -18,8 +18,8 @@ public class IdFilter extends ListFilter.Default {
|
||||
}
|
||||
};
|
||||
|
||||
public static IdFilter equals(String id) {
|
||||
return new IdFilter(id);
|
||||
public static StringEqualsFilter equals(String id) {
|
||||
return new StringEqualsFilter(id, ID_GETTER);
|
||||
}
|
||||
|
||||
public static StringStartsWithFilter startsWith(String prefix) {
|
||||
|
||||
@ -23,7 +23,7 @@ public class TextFilter extends ListFilter.Default {
|
||||
}
|
||||
|
||||
public static ListFilter contains(String str) {
|
||||
return new TextFilter(str);
|
||||
return new StringContainsFilter(str, TEXT_GETTER);
|
||||
}
|
||||
|
||||
public static ListFilter startsWith(String prefix) {
|
||||
|
||||
@ -18,13 +18,14 @@ import java.util.List;
|
||||
public class NodeInfo {
|
||||
|
||||
private List<NodeInfo> children = new ArrayList<>();
|
||||
private Rect mBoundsInScreen;
|
||||
private Rect mBoundsInScreen = new Rect();
|
||||
private Rect mBoundsInParent = new Rect();
|
||||
|
||||
public String id;
|
||||
public CharSequence desc;
|
||||
public CharSequence className;
|
||||
public CharSequence packageName;
|
||||
public CharSequence text;
|
||||
public String desc;
|
||||
public String className;
|
||||
public String packageName;
|
||||
public String text;
|
||||
public int depth;
|
||||
public int drawingOrder;
|
||||
public boolean accessibilityFocused;
|
||||
@ -45,14 +46,17 @@ public class NodeInfo {
|
||||
public boolean selected;
|
||||
public boolean scrollable;
|
||||
public String bounds;
|
||||
public boolean checkable;
|
||||
public boolean focused;
|
||||
public boolean visibleToUser;
|
||||
|
||||
|
||||
public NodeInfo(UiObject node) {
|
||||
id = simplifyId(node.getViewIdResourceName());
|
||||
desc = node.getContentDescription();
|
||||
className = node.getClassName();
|
||||
packageName = node.getPackageName();
|
||||
text = node.getText();
|
||||
desc = node.desc();
|
||||
className = node.className();
|
||||
packageName = node.packageName();
|
||||
text = node.text();
|
||||
|
||||
depth = node.depth();
|
||||
drawingOrder = node.getDrawingOrder();
|
||||
@ -66,18 +70,22 @@ public class NodeInfo {
|
||||
|
||||
accessibilityFocused = node.isAccessibilityFocused();
|
||||
checked = node.isChecked();
|
||||
checkable = node.isCheckable();
|
||||
clickable = node.isClickable();
|
||||
contextClickable = node.isContextClickable();
|
||||
dismissable = node.isDismissable();
|
||||
enabled = node.isEnabled();
|
||||
editable = node.isEditable();
|
||||
focusable = node.isFocusable();
|
||||
focused = node.focused();
|
||||
longClickable = node.isLongClickable();
|
||||
selected = node.isSelected();
|
||||
scrollable = node.isScrollable();
|
||||
mBoundsInScreen = new Rect();
|
||||
visibleToUser = node.visibleToUser();
|
||||
node.getBoundsInScreen(mBoundsInScreen);
|
||||
node.getBoundsInParent(mBoundsInParent);
|
||||
bounds = boundsToString(mBoundsInScreen);
|
||||
|
||||
}
|
||||
|
||||
private String simplifyId(String idResourceName) {
|
||||
@ -91,6 +99,11 @@ public class NodeInfo {
|
||||
return mBoundsInScreen;
|
||||
}
|
||||
|
||||
|
||||
public Rect getBoundsInParent() {
|
||||
return mBoundsInParent;
|
||||
}
|
||||
|
||||
public static String boundsToString(Rect rect) {
|
||||
return rect.toString().replace('-', ',').replace(" ", "").substring(4);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user