sample: 自定义控件; fix: ActivityNotFoundException
This commit is contained in:
@@ -3,17 +3,23 @@ module.exports = function(runtime, global){
|
||||
var floaty = {};
|
||||
|
||||
floaty.window = function(layout){
|
||||
if(typeof(layout) == 'xml'){
|
||||
layout = layout.toXMLString();
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
return wrap(runtime.floaty.window(layout));
|
||||
return wrap(runtime.floaty.window(function(context, parent){
|
||||
runtime.ui.layoutInflater.setContext(context);
|
||||
return ui.__inflate__(runtime.ui.layoutInflater.inflate(xml.toString(), parent, true));
|
||||
}));
|
||||
}
|
||||
|
||||
floaty.rawWindow = function(layout){
|
||||
if(typeof(layout) == 'xml'){
|
||||
layout = layout.toXMLString();
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
return wrap(runtime.floaty.rawWindow(layout));
|
||||
return wrap(runtime.floaty.rawWindow(function(context, parent){
|
||||
runtime.ui.layoutInflater.setContext(context);
|
||||
return ui.__inflate__(runtime.ui.layoutInflater.inflate(xml.toString(), parent, true));
|
||||
}));
|
||||
}
|
||||
|
||||
function wrap(window){
|
||||
|
||||
@@ -46,4 +46,30 @@ J.toJsArray = function(list, nullListToEmptyArray){
|
||||
return arr;
|
||||
}
|
||||
|
||||
J.objectToMap = function(obj){
|
||||
if(obj == null || obj === undefined){
|
||||
return null;
|
||||
}
|
||||
let map = new java.util.HashMap();
|
||||
for(let key in obj){
|
||||
if(obj.hasOwnProperty(key)){
|
||||
map.put(key, obj[key]);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
J.mapToObject = function(map){
|
||||
if(map == null || map === undefined){
|
||||
return null;
|
||||
}
|
||||
let iter = map.entrySet().iterator();
|
||||
let obj = {};
|
||||
while(iter.hasNext()){
|
||||
let entry = iter.next();
|
||||
obj[entry.key] = entry.value;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
module.exports = J;
|
||||
@@ -6,27 +6,46 @@ module.exports = function (runtime, global) {
|
||||
var J = util.java;
|
||||
var ui = {};
|
||||
|
||||
ui.__widgets__ = {};
|
||||
|
||||
ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null);
|
||||
|
||||
ui.layout = function (xml) {
|
||||
if(!activity){
|
||||
throw new Error("需要在ui模式下运行才能使用该函数");
|
||||
}
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
runtime.ui.layoutInflater.setContext(activity);
|
||||
var view = runtime.ui.layoutInflater.inflate(xml, activity.window.decorView, false);
|
||||
ui.setContentView(view);
|
||||
}
|
||||
|
||||
ui.inflate = function(xml, parent){
|
||||
ui.inflate = function(xml, parent, attachToParent){
|
||||
if(!activity){
|
||||
throw new Error("需要在ui模式下运行才能使用该函数");
|
||||
}
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
parent = parent || null;
|
||||
attachToParent = !!attachToParent;
|
||||
runtime.ui.layoutInflater.setContext(activity);
|
||||
return decorate(runtime.ui.layoutInflater.inflate(xml.toString(), parent));
|
||||
return runtime.ui.layoutInflater.inflate(xml.toString(), parent, attachToParent);
|
||||
}
|
||||
|
||||
ui.__inflate__ = function(ctx, xml, parent, attachToParent){
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
parent = parent || null;
|
||||
attachToParent = !!attachToParent;
|
||||
return runtime.ui.layoutInflater.inflate(ctx, xml.toString(), parent, attachToParent);
|
||||
}
|
||||
|
||||
ui.registerWidget = function(name, widget){
|
||||
if(typeof(widget) !== 'function'){
|
||||
throw new TypeError('widget should be a class-like function');
|
||||
}
|
||||
ui.__widgets__[name] = widget;
|
||||
}
|
||||
|
||||
ui.setContentView = function (view) {
|
||||
@@ -103,64 +122,96 @@ module.exports = function (runtime, global) {
|
||||
runtime.ui.bindingContext = global;
|
||||
var layoutInflater = runtime.ui.layoutInflater;
|
||||
layoutInflater.setLayoutInflaterDelegate({
|
||||
beforeConvertXml: function (xml) {
|
||||
beforeConvertXml: function (context, xml) {
|
||||
return null;
|
||||
},
|
||||
afterConvertXml: function (xml) {
|
||||
afterConvertXml: function (context, xml) {
|
||||
return xml;
|
||||
},
|
||||
afterInflation: function (result, xml, parent) {
|
||||
afterInflation: function (context, result, xml, parent) {
|
||||
return result;
|
||||
},
|
||||
beforeInflation: function (xml, parent) {
|
||||
beforeInflation: function (context, xml, parent) {
|
||||
return null;
|
||||
},
|
||||
beforeInflateView: function (node, parent, attachToParent) {
|
||||
beforeInflateView: function (context, node, parent, attachToParent) {
|
||||
return null;
|
||||
},
|
||||
afterInflateView: function (view, node, parent, attachToParent) {
|
||||
afterInflateView: function (context, view, node, parent, attachToParent) {
|
||||
return view;
|
||||
},
|
||||
beforeCreateView: function (node, viewName, attrs) {
|
||||
beforeCreateView: function (context, node, viewName, parent, attrs) {
|
||||
if(ui.__widgets__.hasOwnProperty(viewName)){
|
||||
let Widget = ui.__widgets__[viewName];
|
||||
let widget = new Widget();
|
||||
let ctx = layoutInflater.newInflateContext();
|
||||
ctx.put("widget", widget);
|
||||
let view = ui.__inflate__(ctx, widget.renderInternal(), parent, false);
|
||||
return view;
|
||||
};
|
||||
return null;
|
||||
},
|
||||
afterCreateView: function (view, node, viewName, attrs) {
|
||||
afterCreateView: function (context, view, node, viewName, parent, attrs) {
|
||||
if (view.getClass().getName() == "com.stardust.autojs.core.ui.widget.JsListView" ||
|
||||
view.getClass().getName() == "com.stardust.autojs.core.ui.widget.JsGridView") {
|
||||
initListView(view);
|
||||
}
|
||||
var widget = context.get("widget");
|
||||
if(widget != null){
|
||||
widget.view = view;
|
||||
let viewAttrs = com.stardust.autojs.core.ui.ViewExtras.getViewAttributes(view, layoutInflater.resourceParser);
|
||||
viewAttrs.setViewAttributeDelegate({
|
||||
has: function(name) {
|
||||
return widget.hasAttr(name);
|
||||
},
|
||||
get: function(view, name, getter){
|
||||
return widget.getAttr(view, name, getter);
|
||||
},
|
||||
set: function(view, name, value, setter) {
|
||||
widget.setAttr(view, name, value, setter);
|
||||
}
|
||||
});
|
||||
widget.notifyViewCreated(view);
|
||||
}
|
||||
return view;
|
||||
},
|
||||
beforeApplyAttributes: function (view, inflater, attrs, parent) {
|
||||
beforeApplyAttributes: function (context, view, inflater, attrs, parent) {
|
||||
return false;
|
||||
},
|
||||
afterApplyAttributes: function (view, inflater, attrs, parent) {
|
||||
|
||||
afterApplyAttributes: function (context, view, inflater, attrs, parent) {
|
||||
context.remove("widget");
|
||||
},
|
||||
beforeInflateChildren: function (inflater, node, parent) {
|
||||
beforeInflateChildren: function (context, inflater, node, parent) {
|
||||
return false;
|
||||
},
|
||||
afterInflateChildren: function (inflater, node, parent) {
|
||||
afterInflateChildren: function (context, inflater, node, parent) {
|
||||
|
||||
},
|
||||
afterApplyPendingAttributesOfChildren: function (inflater, view) {
|
||||
afterApplyPendingAttributesOfChildren: function (context, inflater, view) {
|
||||
|
||||
},
|
||||
beforeApplyPendingAttributesOfChildren: function (inflater, view) {
|
||||
beforeApplyPendingAttributesOfChildren: function (context, inflater, view) {
|
||||
return false;
|
||||
},
|
||||
beforeApplyAttribute: function (inflater, view, ns, attrName, value, parent, attrs) {
|
||||
beforeApplyAttribute: function (context, inflater, view, ns, attrName, value, parent, attrs) {
|
||||
var isDynamic = layoutInflater.isDynamicValue(value);
|
||||
if ((isDynamic && layoutInflater.getInflateFlags() == layoutInflater.FLAG_IGNORES_DYNAMIC_ATTRS)
|
||||
|| (!isDynamic && layoutInflater.getInflateFlags() == layoutInflater.FLAG_JUST_DYNAMIC_ATTRS)) {
|
||||
return true;
|
||||
}
|
||||
value = bind(value);
|
||||
inflater.setAttr(view, ns, attrName, value, parent, attrs);
|
||||
this.afterApplyAttribute(inflater, view, ns, attrName, value, parent, attrs);
|
||||
let widget = context.get("widget");
|
||||
if(widget != null && widget.hasAttr(attrName)){
|
||||
widget.setAttr(view, attrName, value, (view, attrName, value)=>{
|
||||
inflater.setAttr(view, ns, attrName, value, parent, attrs);
|
||||
});
|
||||
} else {
|
||||
inflater.setAttr(view, ns, attrName, value, parent, attrs);
|
||||
}
|
||||
this.afterApplyAttribute(context, inflater, view, ns, attrName, value, parent, attrs);
|
||||
return true;
|
||||
},
|
||||
afterApplyAttribute: function (inflater, view, ns, attrName, value, parent, attrs) {
|
||||
afterApplyAttribute: function (context, inflater, view, ns, attrName, value, parent, attrs) {
|
||||
|
||||
}
|
||||
});
|
||||
@@ -229,6 +280,39 @@ module.exports = function (runtime, global) {
|
||||
}
|
||||
}
|
||||
|
||||
ui.Widget = (function(){
|
||||
function Widget(){
|
||||
this.__attrs__ = {};
|
||||
}
|
||||
Widget.prototype.renderInternal = function(){
|
||||
if(typeof(this.render) === 'function'){
|
||||
return this.render();
|
||||
}
|
||||
return (< />)
|
||||
};
|
||||
Widget.prototype.defineAttr = function(attrName, getter, setter){
|
||||
this.__attrs__[attrName] = {
|
||||
getter: getter,
|
||||
setter: setter
|
||||
};
|
||||
};
|
||||
Widget.prototype.hasAttr = function(attrName){
|
||||
return this.__attrs__.hasOwnProperty(attrName);
|
||||
};
|
||||
Widget.prototype.setAttr = function(view, attrName, value, setter){
|
||||
this.__attrs__[attrName].setter(view, attrName, value, setter);
|
||||
};
|
||||
Widget.prototype.getAttr = function(view, attrName, getter){
|
||||
return this.__attrs__[attrName].getter(view, attrName, getter);
|
||||
};
|
||||
Widget.prototype.notifyViewCreated = function(view){
|
||||
if(typeof(this.onViewCreated) == 'function'){
|
||||
this.onViewCreated(view);
|
||||
}
|
||||
};
|
||||
return Widget;
|
||||
})();
|
||||
|
||||
var proxy = runtime.ui;
|
||||
proxy.__proxy__ = {
|
||||
set: function (name, value) {
|
||||
|
||||
@@ -20,6 +20,16 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
var formatRegExp = /%[sdj%]/g;
|
||||
exports.extends = (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
exports.java = require("__java_util__");
|
||||
exports.format = function(f) {
|
||||
if (!isString(f)) {
|
||||
|
||||
Reference in New Issue
Block a user