forked from APIJSON/APIJSON-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoActivity.java
More file actions
234 lines (179 loc) · 6.67 KB
/
AutoActivity.java
File metadata and controls
234 lines (179 loc) · 6.67 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
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 apijson.demo.ui;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import apijson.demo.CodeUtil;
import apijson.demo.R;
import apijson.demo.RequestUtil;
import apijson.demo.StringUtil;
import zuo.biao.apijson.JSON;
/**自动生成代码
* @author Lemon
*/
public class AutoActivity extends UIAutoBaseActivity {
private static final String TAG = "AutoActivity";
public static final String KEY_REQUEST = "KEY_REQUEST";
/**
* @param context
* @return
*/
public static Intent createIntent(Context context) {
return new Intent(context, AutoActivity.class);
}
private Activity context;
private long id;
private String url;
private String request;
private TextView tvAutoRequest;
private TextView tvAutoResponse;
private TextView tvAutoCodePortrait;
private TextView tvAutoCodeLandscape;
private TextView tvAutoOrient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.auto_activity);
context = this;
//读取保存的配置
SharedPreferences sp = getSharedPreferences(SelectActivity.CONFIG_PATH, Context.MODE_PRIVATE);
id = sp.getLong(SelectActivity.KEY_ID, RequestUtil.DEFAULT_MOMENT_ID);
url = sp.getString(SelectActivity.KEY_URL, null);
request = sp.getString(KEY_REQUEST, null);
if (StringUtil.isEmpty(request, true)) {
request = "{\"Moment\":{\"id\":" + id + "},\"[]\":{\"count\":3,\"page\":0,\"Comment\":{\"momentId@\":\"Moment/id\",\"@column\":\"id,userId,content\"}}}";
}
tvAutoRequest = findViewById(R.id.tvAutoRequest);
tvAutoResponse = findViewById(R.id.tvAutoResponse);
tvAutoCodePortrait = findViewById(R.id.tvAutoCodePortrait);
tvAutoCodeLandscape = findViewById(R.id.tvAutoCodeLandscape);
tvAutoOrient = findViewById(R.id.tvAutoOrient);
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
onConfigurationChanged(getResources().getConfiguration());
}
});
}
@Override
protected void onResume() {
super.onResume();
onConfigurationChanged(getResources().getConfiguration());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
onScreenChange(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
super.onConfigurationChanged(newConfig);
}
private void onScreenChange(boolean isHorizontal) {
tvAutoCodePortrait.setVisibility(! isHorizontal ? View.VISIBLE : View.GONE);
tvAutoCodeLandscape.setVisibility(isHorizontal ? View.VISIBLE : View.GONE);
tvAutoOrient.setText(getString(R.string.screen) + getString(isHorizontal ? R.string.horizontal : R.string.vertical));
}
public void copy(View v) {
StringUtil.copyText(context, StringUtil.getString((TextView) v));
}
public void auto(View v) {
auto(StringUtil.getString(tvAutoRequest));
}
public void get(View v) {
request((TextView) v);
}
public void head(View v) {
request((TextView) v);
}
public void gets(View v) {
request((TextView) v);
}
public void heads(View v) {
request((TextView) v);
}
public void post(View v) {
request((TextView) v);
}
public void put(View v) {
request((TextView) v);
}
public void delete(View v) {
request((TextView) v);
}
public void orient(View v) {
setRequestedOrientation(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
? ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
}
public void auto(String request) {
String response = CodeUtil.parse(JSON.parseObject(request)); //newObjectRequest(request);
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + request + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n response = \n" + response + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
tvAutoResponse.setText(StringUtil.getTrimedString(response));
}
@SuppressLint("DefaultLocale")
public void request(TextView tv) {
request(StringUtil.getNoBlankString(tv).toLowerCase());
}
public void request(String method) {
startActivityForResult(RequestActivity.createIntent(context, id, url, method
, JSON.parseObject(StringUtil.getString(tvAutoRequest))), REQUEST_TO_REQUEST);
}
private static final int REQUEST_TO_REQUEST = 1;
private Intent result;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case REQUEST_TO_REQUEST:
if (data == null) {
Toast.makeText(context, "onActivityResult data == null !!!", Toast.LENGTH_SHORT).show();
} else {
result = data;
id = data.getLongExtra(RequestActivity.RESULT_ID, RequestUtil.DEFAULT_MOMENT_ID);
url = data.getStringExtra(RequestActivity.RESULT_URL);
tvAutoResponse.setText(StringUtil.getString(JSON.format(
data.getStringExtra(RequestActivity.RESULT_RESPONSE))));
}
break;
default:
break;
}
}
@Override
public void finish() {
//保存配置
getSharedPreferences(SelectActivity.CONFIG_PATH, Context.MODE_PRIVATE)
.edit()
.remove(KEY_REQUEST)
.putString(KEY_REQUEST, StringUtil.getTrimedString(tvAutoRequest))
.commit();
//需要在SelectActivity实时更新
setResult(RESULT_OK, new Intent().
putExtra(RequestActivity.RESULT_ID, id).
putExtra(RequestActivity.RESULT_URL, url));
super.finish();
}
}