sample: 自定义控件; fix: ActivityNotFoundException

This commit is contained in:
hyb1996
2018-10-14 10:33:23 +08:00
parent db3571851c
commit 14b7a7e85e
39 changed files with 615 additions and 220 deletions

View File

@@ -0,0 +1,50 @@
"ui";
var ColoredButton = (function() {
//继承至ui.Widget
util.extends(ColoredButton, ui.Widget);
function ColoredButton() {
//调用父类构造函数
ui.Widget.call(this);
//自定义属性color定义按钮颜色
this.defineAttr("color", (view, name, defaultGetter) => {
return this._color;
}, (view, name, value, defaultSetter) => {
this._color = value;
view.attr("backgroundTint", value);
});
//自定义属性onClick定义被点击时执行的代码
this.defineAttr("onClick", (view, name, defaultGetter) => {
return this._onClick;
}, (view, name, value, defaultSetter) => {
this._onClick = value;
});
}
ColoredButton.prototype.render = function() {
return (
<button textSize="16sp" style="Widget.AppCompat.Button.Colored" w="auto"/>
);
}
ColoredButton.prototype.onViewCreated = function(view) {
view.on("click", () => {
if (this._onClick) {
eval(this._onClick);
}
});
}
ui.registerWidget("colored-button", ColoredButton);
return ColoredButton;
})();
ui.layout(
<vertical>
<colored-button text="第一个按钮" color="#ff5722"/>
<colored-button text="第二个按钮" onClick="hello()"/>
</vertical>
);
function hello() {
alert("Hello ~");
}

View File

@@ -0,0 +1,54 @@
"ui";
//这个自定义控件是一个勾选框checkbox能够保存自己的勾选状态在脚本重新启动时能恢复状态
var PrefCheckBox = (function() {
//继承至ui.Widget
util.extends(PrefCheckBox, ui.Widget);
function PrefCheckBox() {
//调用父类构造函数
ui.Widget.call(this);
//自定义属性key定义在配置中保存时的key
this.defineAttr("key");
}
PrefCheckBox.prototype.render = function() {
return (
<checkbox />
);
}
PrefCheckBox.prototype.onViewCreated = function(view) {
view.setChecked(PrefCheckBox.getPref().get(this.getKey(), false));
view.on("check", (checked) => {
PrefCheckBox.getPref().put(this.getKey(), checked);
});
}
PrefCheckBox.prototype.getKey = function() {
return this.key || view.attr("id").replace("@+id/", "");
}
PrefCheckBox.setPref = function(pref) {
PrefCheckBox._pref = pref;
}
PrefCheckBox.getPref = function(){
if(!PrefCheckBox._pref){
PrefCheckBox._pref = storages.create("pref_pref");
}
return PrefCheckBox._pref;
}
ui.registerWidget("pref-checkbox", PrefCheckBox);
return PrefCheckBox;
})();
ui.layout(
<vertical>
<pref-checkbox id="perf1" text="配置1"/>
<pref-checkbox id="perf2" text="配置2"/>
<button id="btn" text="获取配置"/>
</vertical>
);
ui.btn.on("click", function(){
toast("配置1为" + PrefCheckBox.getPref().get("perf1"));
toast("配置2为" + PrefCheckBox.getPref().get("perf2"));
});

View File

@@ -48,7 +48,7 @@ public class TaskerScriptEditActivity extends BaseActivity {
.putExtra(EXTRA_RUN_ENABLED, false)
.putExtra(EXTRA_SAVE_ENABLED, false))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Observers.consumer(),
.subscribe(Observers.emptyConsumer(),
ex -> {
Toast.makeText(TaskerScriptEditActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
finish();

View File

@@ -101,7 +101,7 @@ public class AndroidClassIndices {
.doOnNext(this::load)
.subscribeOn(Schedulers.from(mSingleThreadExecutor))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Observers.consumer(), t -> {
.subscribe(Observers.emptyConsumer(), t -> {
mLoadThrowable = t;
t.printStackTrace();
});

View File

@@ -58,11 +58,11 @@ public class TimedTaskManager {
return;
if (task.isDisposable()) {
mTimedTaskDatabase.delete(task)
.subscribe(Observers.consumer(), Throwable::printStackTrace);
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);
} else {
task.setScheduled(false);
mTimedTaskDatabase.update(task)
.subscribe(Observers.consumer(), Throwable::printStackTrace);
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);
}
}
@@ -70,13 +70,13 @@ public class TimedTaskManager {
public void removeTask(TimedTask timedTask) {
TimedTaskScheduler.cancel(mContext, timedTask);
mTimedTaskDatabase.delete(timedTask)
.subscribe(Observers.consumer(), Throwable::printStackTrace);
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);
}
@SuppressLint("CheckResult")
public void addTask(TimedTask timedTask) {
mTimedTaskDatabase.insert(timedTask)
.subscribe(Observers.consumer(), Throwable::printStackTrace);;
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);;
TimedTaskScheduler.scheduleTaskIfNeeded(mContext, timedTask);
}
@@ -119,7 +119,7 @@ public class TimedTaskManager {
public void notifyTaskScheduled(TimedTask timedTask) {
timedTask.setScheduled(true);
mTimedTaskDatabase.update(timedTask)
.subscribe(Observers.consumer(), Throwable::printStackTrace);
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);
}
@@ -134,7 +134,7 @@ public class TimedTaskManager {
@SuppressLint("CheckResult")
public void updateTask(TimedTask task) {
mTimedTaskDatabase.update(task)
.subscribe(Observers.consumer(), Throwable::printStackTrace);
.subscribe(Observers.emptyConsumer(), Throwable::printStackTrace);
TimedTaskScheduler.cancel(mContext, task);
TimedTaskScheduler.scheduleTaskIfNeeded(mContext, task);
}

View File

@@ -2,6 +2,10 @@ package org.autojs.autojs.tool;
import com.stardust.app.GlobalAppContext;
import org.autojs.autojs.model.explorer.ExplorerFileItem;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
public class Observers {
@@ -17,11 +21,36 @@ public class Observers {
@SuppressWarnings("unchecked")
public static <T> Consumer<T> consumer() {
public static <T> Consumer<T> emptyConsumer() {
return CONSUMER;
}
public static Consumer<Throwable> toastMessage() {
return TOAST_MESSAGE;
}
public static <T> Observer<T> emptyObserver() {
return new Observer<T>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(T t) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
}
}

View File

@@ -51,6 +51,7 @@ import java.io.InputStream;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
@@ -267,17 +268,19 @@ public class ScriptOperations {
}
}
public Observable<Boolean> rename(final ExplorerFileItem item) {
public Observable<ExplorerFileItem> rename(final ExplorerFileItem item) {
final ScriptFile oldFile = new ScriptFile(item.getPath());
String originalName = item.getName();
return showNameInputDialog(originalName, new InputCallback(oldFile.isDirectory() ? null : PFiles.getExtension(item.getName()),
originalName))
.map(newName -> {
ExplorerFileItem newItem = item.rename(newName);
if (newItem != null) {
notifyFileChanged(mCurrentDirectory, item, newItem);
if (ObjectHelper.equals(newItem.toScriptFile(), item.toScriptFile())) {
showMessage(R.string.error_cannot_rename);
throw new IOException();
}
return newItem != null;
notifyFileChanged(mCurrentDirectory, item, newItem);
return newItem;
});
}

View File

@@ -81,7 +81,7 @@ EditActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHo
void setUpViews() {
mEditorView.handleIntent(getIntent())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Observers.consumer(),
.subscribe(Observers.emptyConsumer(),
ex -> onLoadFileError(ex.getMessage()));
mEditorMenu = new EditorMenu(mEditorView);
setUpToolbar();

View File

@@ -440,7 +440,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
public void saveFile() {
save()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Observers.consumer(), e -> {
.subscribe(Observers.emptyConsumer(), e -> {
e.printStackTrace();
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
});

View File

@@ -119,7 +119,6 @@ public class CodeEditText extends AppCompatEditText {
@Override
protected void onDraw(Canvas canvas) {
Log.v(LOG_TAG, "onDraw");
mLogger.reset();
if (mParentScrollView == null) {
mParentScrollView = (HVScrollView) getParent();

View File

@@ -31,8 +31,6 @@ import org.autojs.autojs.model.explorer.ExplorerProjectPage;
import org.autojs.autojs.model.explorer.ExplorerSampleItem;
import org.autojs.autojs.model.explorer.ExplorerSamplePage;
import org.autojs.autojs.model.explorer.Explorers;
import org.autojs.autojs.model.explorer.WorkspaceFileProvider;
import org.autojs.autojs.model.sample.SampleFile;
import org.autojs.autojs.model.script.ScriptFile;
import org.autojs.autojs.model.script.Scripts;
import org.autojs.autojs.tool.Observers;
@@ -268,7 +266,7 @@ public class ExplorerView extends ThemeColorSwipeRefreshLayout implements SwipeR
case R.id.rename:
new ScriptOperations(getContext(), this, getCurrentPage())
.rename((ExplorerFileItem) mSelectedItem)
.subscribe();
.subscribe(Observers.emptyObserver());
break;
case R.id.delete:
new ScriptOperations(getContext(), this, getCurrentPage())

View File

@@ -279,7 +279,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
.input("", host, (dialog, input) -> {
Pref.saveServerAddress(input.toString());
DevPluginService.getInstance().connectToServer(input.toString())
.subscribe(Observers.consumer(), this::onConnectException);
.subscribe(Observers.emptyConsumer(), this::onConnectException);
})
.neutralText(R.string.text_help)
.onNeutral((dialog, which) -> {

View File

@@ -317,7 +317,7 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
.positiveText(R.string.text_install)
.negativeText(R.string.cancel)
.onPositive((dialog, which) ->
IntentUtil.installApk(BuildActivity.this, outApk.getPath(), AppFileProvider.AUTHORITY)
IntentUtil.installApkOrToast(BuildActivity.this, outApk.getPath(), AppFileProvider.AUTHORITY)
)
.show();

View File

@@ -119,7 +119,7 @@ public class UpdateInfoDialogBuilder extends MaterialDialog.Builder {
final String path = new File(Pref.getScriptDirPath(), "AutoJs.apk").getPath();
DownloadManager.getInstance().downloadWithProgress(getContext(), downloadUrl, path)
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(file -> IntentUtil.installApk(getContext(), file.getPath(), AppFileProvider.AUTHORITY),
.subscribe(file -> IntentUtil.installApkOrToast(getContext(), file.getPath(), AppFileProvider.AUTHORITY),
error -> {
error.printStackTrace();
Toast.makeText(getContext(), R.string.text_download_failed, Toast.LENGTH_SHORT).show();

View File

@@ -6,13 +6,13 @@
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:hint="@string/text_class_or_package_name"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/keywords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/text_class_or_package_name"/>
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<FrameLayout

View File

@@ -423,4 +423,5 @@
<string name="text_run_on_config_change">某些设置(屏幕方向,地区等)更改时</string>
<string name="error_pattern_syntax">正则表达式错误</string>
<string name="text_invalid_package_name">非法包名</string>
<string name="error_cannot_rename">重命名失败</string>
</resources>