-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathContent.java
More file actions
458 lines (360 loc) · 14.7 KB
/
Copy pathContent.java
File metadata and controls
458 lines (360 loc) · 14.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package andbas.Ch11TabHost2;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.google.android.gms.maps.model.LatLng;
import com.xml.LoginXMLHandler;
import com.xml.LoginXMLStruct;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Content extends Activity
{
String TAG ="Content";
private static final int MSG_LIST_OK = 1;
static public Content rContent;
private Spinner spinner_event;
//private Spinner spinner_limit;
EditText stitle, scontent, sphone;
private Button pic,area,send;
String[] action_name = new String[] { "日式","中式","台式","義式","美式","其他" };
String[] add_limit = new String[] { "不限制","1", "2", "3", "4", "5", "6" ,"7","8","9","10","11","12","13","14","15"
,"16","17","18","19","20"};
String picfilename[]= new String[3];
String IPAddress="";
ProgressDialog myDialog;
LatLng npos;
LoginXMLStruct data;
ByteArrayOutputStream baos;
int counter = 0;
int from; //This must be declared as global !
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
rContent = this;
IPAddress = (String) this.getResources().getText(R.string.url);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
spinner_event = (Spinner) findViewById(R.id.spinner_event);
//spinner_limit = (Spinner) findViewById(R.id.spinner_limit);
pic = (Button) findViewById(R.id.addpicture);
area = (Button) findViewById(R.id.getarea);
send = (Button) findViewById(R.id.Contentsend);
spinner_event.setEnabled(true);
//spinner_limit.setEnabled(true);
pic.setEnabled(true);
area.setEnabled(true);
send.setEnabled(true);
pic.setOnClickListener(listener);
area.setOnClickListener(listener);
send.setOnClickListener(listener);
npos = new LatLng(121.3631593, 25.0564237);
//picfilename ="pic.jpg";
buildViews();
}
private Button.OnClickListener listener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.addpicture:
//Toast.makeText(Content.this, "asd",Toast.LENGTH_SHORT).show();
final CharSequence[] choice = {"Choose from Gallery","Capture a photo"};
AlertDialog.Builder alert = new AlertDialog.Builder(Content.this);
alert.setTitle("上傳選擇");
alert.setSingleChoiceItems(choice, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (choice[which] == "Choose from Gallery") {
from = 1;
} else if (choice[which] == "Capture a photo") {
from = 2;
}
}
});
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (from == 0) {
Toast.makeText(Content.this, "Select One Choice",
Toast.LENGTH_SHORT).show();
} else if (from == 1) {
// Your Code
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
} else if (from == 2) {
// Your Code
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 2);
}
}
});
alert.show();
break;
case R.id.getarea:
//Toast.makeText(Content.this, "QWE", Toast.LENGTH_SHORT).show();
Intent maps = new Intent(Content.this, MapView3.class);
startActivity(maps);
break;
case R.id.Contentsend:
senddata();
Toast.makeText(Content.this, "加入成功", Toast.LENGTH_SHORT).show();
}
}
};
private void buildViews()
{
// TODO 自動產生的方法 Stub
stitle = (EditText) findViewById(R.id.editTitle);
scontent = (EditText) findViewById(R.id.editContent);
sphone = (EditText) findViewById(R.id.editPhone);
spinner_event = (Spinner) findViewById(R.id.spinner_event);
//spinner_limit = (Spinner) findViewById(R.id.spinner_limit);
ArrayAdapter<String> adapterAction = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, action_name);
adapterAction
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_event.setAdapter(adapterAction);
ArrayAdapter<String> adapterlimt = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, add_limit);
adapterAction
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner_limit.setAdapter(adapterlimt);
}
void senddata()
{
try
{
String subject = URLEncoder.encode((stitle.getText().toString().trim()), "UTF-8");
String content = URLEncoder.encode((scontent.getText().toString()), "UTF-8");
String pnum="";
//if (spinner_limit.getSelectedItemPosition() == 0)
pnum = "0";
//else
//pnum = add_limit[spinner_limit.getSelectedItemPosition()];
String type = Integer.toString(spinner_event.getSelectedItemPosition());
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String user = telephonyManager.getDeviceId();
String area = getCityFromGPSData(npos.latitude, npos.longitude);
String sarea = URLEncoder.encode(area, "UTF-8");
String uriAPI = IPAddress + "sendgroup.php?title=" + subject + "&content=" + content + "&pic=" + picfilename[0] + "&address=" + npos.latitude + "," + npos.longitude + "&type=" + type + "&pnum=" + pnum + "&user=" + user + "&area=" + sphone.getText().toString();
Log.i("TAG", uriAPI);
URL url = null;
try{
url = new URL(uriAPI);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
//Using login handler for xml
LoginXMLHandler myHandler = new LoginXMLHandler();
xr.setContentHandler(myHandler);
//open connection
xr.parse(new InputSource(url.openStream()));
//verify OK
data = myHandler.getParsedData();
stitle.setText("");
scontent.setText("");
}
catch(Exception e){
e.printStackTrace();
return;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static String getRealPathFromUri(Activity activity, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public void upload(byte [] ba)
{
String ba1 = Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
picfilename[counter] = sdf.format(c.getTime()) + ".jpg";
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("filename",picfilename[counter]));
Log.i("TAG", picfilename[counter]);
try{
HttpClient httpclient = new DefaultHttpClient();
String url = IPAddress + "upload.php";
Log.i("TAG", url);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
/* http response 200 = ok */
Log.v(TAG, "http response status: "+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
/* get data from server url */
String strResult = EntityUtils.toString(response.getEntity());
Log.d(TAG, "get Result:"+strResult);
} else {
//tools.showInfo("upload file error: "+httpResponse.getStatusLine().getStatusCode());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Bitmap mBitmap = null;
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
Uri chosenImageUri = data.getData();
try {
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
baos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
mBitmap = (Bitmap) data.getExtras().get("data");
Log.i("TAG", mBitmap.getHeight()+"");
baos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
}
//Progress
myDialog = ProgressDialog.show
(
Content.this,
"傳送中",
"等待",
true
);
new Thread()
{
public void run()
{
upload(baos.toByteArray());
Message msg = new Message();
msg.what = MSG_LIST_OK;
myHandler.sendMessage(msg);
}
}.start();
}
}
public Handler myHandler = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what)
{
case MSG_LIST_OK:
myDialog.dismiss();
break;
}
}
};
public String getCityFromGPSData(double lat, double lng)
{
HttpRetriever agent = new HttpRetriever();
String request = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&language=zh-TW&sensor=true";
String Address1 = "";
String Address2 = "";
String City = "";
String State = "";
String Country = "";
String County = "";
String PIN = "";
try {
String response = agent.retrieve(request);
JSONObject jsonObj = new JSONObject(response);
String Status = jsonObj.getString("status");
if (Status.equalsIgnoreCase("OK")) {
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject zero = Results.getJSONObject(0);
JSONArray address_components = zero.getJSONArray("address_components");
for (int i = 0; i < address_components.length(); i++) {
JSONObject zero2 = address_components.getJSONObject(i);
String long_name = zero2.getString("long_name");
JSONArray mtypes = zero2.getJSONArray("types");
String Type = mtypes.getString(0);
if (TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") {
if (Type.equalsIgnoreCase("street_number")) {
Address1 = long_name + " ";
} else if (Type.equalsIgnoreCase("route")) {
Address1 = Address1 + long_name;
} else if (Type.equalsIgnoreCase("sublocality")) {
Address2 = long_name;
} else if (Type.equalsIgnoreCase("locality")) {
// Address2 = Address2 + long_name + ", ";
City = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_2")) {
County = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_1")) {
State = long_name;
} else if (Type.equalsIgnoreCase("country")) {
Country = long_name;
} else if (Type.equalsIgnoreCase("postal_code")) {
PIN = long_name;
}
}
// JSONArray mtypes = zero2.getJSONArray("types");
// String Type = mtypes.getString(0);
// Log.e(Type,long_name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// Log.d("GeoCoder", response);
return City;
}
}