-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathMainActivity.java
151 lines (121 loc) · 5.18 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*Copyright ©2020 TommyLemon(https://github.com/TommyLemon/UnitAuto)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package unitauto.demo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import unitauto.JSON;
import unitauto.MethodUtil;
import unitauto.apk.UnitAutoActivity;
import unitauto.test.TestSDK;
public class MainActivity extends FragmentActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.flMain, fragment)
.show(fragment)
.commit();
}
// 用 UnitAuto 后台管理界面(http://apijson.cn/unit/)测试以下方法
public boolean test() {
return true;
}
public String hello(String name) {
return "Hello, " + (name == null ? "Activity" : name) + "!";
}
public void onClickHello(View v) {
Toast.makeText(this, ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
public void onClickUnit(View v) {
startActivity(UnitAutoActivity.createIntent(this));
}
public void onClickInit(View v) {
try {
MethodUtil.InterfaceProxy globalInterfaceProxy = MethodUtil.GLOBAL_CALLBACK_MAP.get(TestSDK.class);
if (globalInterfaceProxy == null) {
globalInterfaceProxy = new MethodUtil.InterfaceProxy();
}
globalInterfaceProxy.$_putCallback("response(Map<String, java.lang.Object>)", new MethodUtil.Listener<Object>() {
@Override
public void complete(Object data, Method method, MethodUtil.InterfaceProxy proxy, Object... extras) throws Exception {
Log.d(TAG, "main globalInterfaceProxy.Listener.complete method = " + method + "; data = " + JSON.toJSONString(data));
}
});
MethodUtil.GLOBAL_CALLBACK_MAP.put(TestSDK.class, globalInterfaceProxy);
/**
* 初始化
*/
Map<String, String> config = new HashMap<>();
config.put("ip", "192.168.1.1"); //若没有代理,则不需要此行
config.put("port", "8888");//若没有代理,则不需要此行
// config.put("user", mEtnUser.getText().toString());//若没有代理,则不需要此行
// config.put("passwd", mEtnPassword.getText().toString());//若没有代理,则不需要此行
// config.put("proxy_type", 1 ); //若没有代理,则不需要此行
// config.put("perform_mode", "LOW_PERFORM");//低性能表现,默认关闭美颜等
final boolean[] called = new boolean[]{false};
TestSDK.getInstance().init(config, new TestSDK.Callback() {
@Override
public void response(final Map<String, Object> info) {
MethodUtil.InterfaceProxy globalCallback = MethodUtil.GLOBAL_CALLBACK_MAP.get(TestSDK.class);
try {
@SuppressWarnings("unchecked")
MethodUtil.Listener<Object> listener = (MethodUtil.Listener<Object>) globalCallback.$_getCallback("response(Map<String, java.lang.Object>)");
listener.complete(info);
} catch (Exception e) {
e.printStackTrace();
}
if (info == null) {
System.out.println("TestSDK.main 调用返回为空, 请查看日志");
new RuntimeException("调用返回为空").printStackTrace();
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
String code = (String) info.get("return_code");
String msg = (String) info.get("return_msg");
Toast.makeText(MainActivity.this, "main TestSDK.getInstance().Callback.response "
+ (called[0] ? "支付回调" : "初始化完成") + ":code = " + code + ";msg = " + msg, Toast.LENGTH_LONG).show();
called[0] = true;
}
});
}
});
} catch (Throwable e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "初始化失败,报错:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onClickPay(View v) {
// 发起支付
try {
Map<String, String> req = new HashMap<>();
req.put("order_id", "123456");
req.put("price", "15.9");
TestSDK.getInstance().pay(req);
} catch (Throwable e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "支付失败,报错:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}