MainActivity.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. package com.usai.redant.photo;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.Serializable;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import com.usai.util.Network;
  12. import com.usai.util.dbUtil;
  13. import android.graphics.Bitmap;
  14. import android.graphics.BitmapFactory;
  15. import android.media.ThumbnailUtils;
  16. import android.net.Uri;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.provider.MediaStore;
  20. import android.app.Activity;
  21. import android.app.AlertDialog;
  22. import android.content.BroadcastReceiver;
  23. import android.content.ContentValues;
  24. import android.content.Context;
  25. import android.content.DialogInterface;
  26. import android.content.Intent;
  27. import android.content.IntentFilter;
  28. import android.database.sqlite.SQLiteDatabase;
  29. import android.text.Editable;
  30. import android.text.TextWatcher;
  31. import android.util.Log;
  32. import android.view.Menu;
  33. import android.view.MenuItem;
  34. import android.view.View;
  35. import android.widget.EditText;
  36. import android.widget.ImageButton;
  37. import android.widget.ImageView;
  38. import android.widget.TextView;
  39. public class MainActivity extends Activity
  40. {
  41. // static final int ACTIVITY_CAMERA = 0;
  42. // static final int ACTIVITY_VIEW = 1;
  43. // static final String serverurl = "http://192.168.23.1/xampp/";
  44. static final String TAG = "MainActivity";
  45. static final int REQUEST_TAKE_PHOTO = 1;
  46. static final int REQUEST_SCAN_BARCODE = 2;
  47. static final int REQUEST_LOCKER = 3;
  48. // HashMap<String, String> hashMap = new HashMap<String, String>();
  49. // ArrayList<String> pic_file = new ArrayList<String>();
  50. // ArrayList<bitmap>
  51. // ArrayList<>
  52. // private String thumbpath = null;
  53. private int iwidth = 640;
  54. private int iheight = 480;
  55. private File photoFile = null;
  56. Bitmap thumbBitmap = null;
  57. // private int iPhotoCount = 0;
  58. public String LastFileName = "";
  59. private boolean m_blocked = false;
  60. TextView m_tvalert;
  61. // private String user;
  62. // private String password;
  63. // String m_sName;
  64. // String m_sPassword;
  65. ArrayList<String> photoList = new ArrayList<String>();
  66. // private SurfaceView picSV;
  67. // private Camera camera;
  68. //
  69. // private final BroadcastReceiver m_lockedReceiver = new BroadcastReceiver()
  70. // {
  71. // @Override
  72. // public void onReceive(
  73. // Context context,
  74. // Intent intent)
  75. // {
  76. // String action = intent
  77. // .getAction();
  78. // if (action
  79. // .equals(Intent.ACTION_SCREEN_OFF))
  80. // {
  81. // Log.e("hg",
  82. // "�յ��㲥!!!=======>m_blocked="
  83. // + m_blocked);
  84. // if (m_blocked == true)
  85. // return;
  86. // Intent activityintent = new Intent();
  87. // activityintent
  88. // .setClass(
  89. // MainActivity.this,
  90. // LockerActivity.class);
  91. // startActivityForResult(
  92. // activityintent,
  93. // REQUEST_LOCKER);
  94. // Log.e(TAG,
  95. // "===================>start locker");
  96. // m_blocked = true;
  97. // }
  98. // // if
  99. // // (action.equals(Intent.ACTION_USER_PRESENT))
  100. // // {
  101. // //
  102. // // Log.e("hg",
  103. // // "�յ��㲥!!!=======>m_blocked="
  104. // // +
  105. // // m_blocked);
  106. // // if
  107. // // (m_blocked
  108. // // == true)
  109. // // return;
  110. // // Intent
  111. // // activityintent
  112. // // = new
  113. // // Intent();
  114. // // activityintent
  115. // // .setClass(MainActivity.this,
  116. // // LockerActivity.class);
  117. // // startActivityForResult(activityintent,
  118. // // REQUEST_LOCKER);
  119. // // Log.e(TAG,
  120. // // "===================>start locker");
  121. // // m_blocked
  122. // // = true;
  123. // // }
  124. // }
  125. // };
  126. // @Override
  127. // protected void onStart() {
  128. // Log.i(TAG, "onStart========>m_blocked="+m_blocked);
  129. // // TODO Auto-generated method stub
  130. //
  131. // if (m_blocked) {
  132. //
  133. // }
  134. // super.onStart();
  135. // }
  136. //
  137. // @Override
  138. // protected void onStop() {
  139. // Log.i(TAG, "onStop========>");
  140. // // TODO Auto-generated method stub
  141. //
  142. // super.onStop();
  143. // }
  144. private void StartCamera()
  145. {
  146. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  147. // Ensure that there's a camera activity to handle the intent
  148. if (takePictureIntent.resolveActivity(getPackageManager()) != null)
  149. {
  150. // Create the File where the photo should go
  151. // File photoFile = null;
  152. if (photoFile != null)
  153. photoFile = null;
  154. try
  155. {
  156. photoFile = createImageFile();
  157. }
  158. catch (IOException ex)
  159. {
  160. // Error occurred while creating the File
  161. // ...
  162. }
  163. // Continue only if the File was successfully created
  164. Log.i(TAG, photoFile.toString());
  165. if (photoFile != null)
  166. {
  167. Log.i(TAG, "start system camera====================>");
  168. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
  169. Uri.fromFile(photoFile));
  170. startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
  171. }
  172. }
  173. }
  174. @Override
  175. protected void onCreate(Bundle savedInstanceState)
  176. {
  177. Log.e(TAG, "=======================>MainActivity Created!");
  178. super.onCreate(savedInstanceState);
  179. setContentView(R.layout.activity_main1);
  180. ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
  181. thumbsImageView.setOnClickListener(new View.OnClickListener()
  182. {
  183. @Override
  184. public void onClick(View v)
  185. {
  186. Intent intent = new Intent(MainActivity.this,
  187. PhotoPreviewActivity.class);
  188. intent.putExtra("pic_list", (Serializable) photoList);
  189. startActivity(intent);
  190. // File folder = new
  191. // File(Environment.getExternalStorageDirectory().getPath()+"/redant/pop/temp");
  192. // File[] allFiles = folder.listFiles();
  193. // new SingleMediaScanner(MainActivity.this, allFiles[0]);
  194. // Intent intent = new Intent();
  195. // // Intent.ACTION_PICK,
  196. // //
  197. // android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  198. // // startActivityForResult(intent, REQUEST_SCAN_BARCODE);
  199. //
  200. // intent.setClass(MainActivity.this, CaptureActivity.class);
  201. // startActivityForResult(intent, REQUEST_SCAN_BARCODE);
  202. }
  203. });
  204. // m_sName = intent.getStringExtra("user");
  205. // m_sPassword = intent.getStringExtra("password");
  206. Log.e(TAG, "user=" + RedAntApplication.user + ",password="
  207. + RedAntApplication.password);
  208. IntentFilter msgFilter = new IntentFilter();
  209. Intent intent = getIntent();
  210. msgFilter.addAction("android.intent.action.SCREEN_OFF");
  211. // msgFilter.addAction("REDANT.POP.USER_CHECK_FAILED");
  212. msgFilter.addAction("REDANT.POP.FINISH_UPLOAD_QUEUE");
  213. msgFilter.addAction("REDANT.POP.FINISH_UPLOAD_QUEUE_WITH_ERROR");
  214. msgFilter.setPriority(2147483647);
  215. registerReceiver(MainActivityReceiver, msgFilter);
  216. Log.e(TAG, "=======================>msgFilter registed!");
  217. if (savedInstanceState != null)
  218. {
  219. thumbBitmap = getBitmap(savedInstanceState
  220. .getByteArray("thumbBitmap"));
  221. photoList.clear();
  222. // ArrayList list = (ArrayList)
  223. // Arrays.asList(savedInstanceState.getStringArray("arrayLabel"));
  224. photoList = savedInstanceState.getStringArrayList("photoList");
  225. // photoList = A
  226. // savedInstanceState.getStringArrayList("photoList");
  227. photoFile = (File) savedInstanceState.getSerializable("photoFile");
  228. // iPhotoCount = savedInstanceState.getInt("iPhotoCount");
  229. // thumbpath = savedInstanceState.getString("thumbpath");
  230. // if (savedInstanceState.getString("m_sName") != null)
  231. // m_sName = savedInstanceState.getString("m_sName");
  232. // if (savedInstanceState.getString("m_sPassword") != null)
  233. // m_sPassword = savedInstanceState.getString("m_sPassword");
  234. // ImageView thumbsImageView = (ImageView)
  235. // findViewById(R.id.ivThumbs);
  236. if (thumbBitmap != null)
  237. {
  238. thumbsImageView.setImageBitmap(thumbBitmap);
  239. // Log.e(TAG, "load thumbpath==" + thumbpath);
  240. }
  241. // photoFile.delete();
  242. TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
  243. countTextView.setText(photoList.size() + " Photos");
  244. }
  245. // EditText edit = (EditText) findViewById(R.id.pidval);
  246. // edit.addTextChangedListener(new TextWatcher()
  247. // {
  248. // @Override
  249. // public void onTextChanged(CharSequence s, int start, int before,
  250. // int count)
  251. // {
  252. //
  253. // }
  254. //
  255. // @Override
  256. // public void beforeTextChanged(CharSequence s, int start, int count,
  257. // int after)
  258. // {
  259. // }
  260. //
  261. // @Override
  262. // public void afterTextChanged(Editable s)
  263. // {
  264. // // EditText edit = (EditText) findViewById(R.id.edit);
  265. // ImageButton ibtnUpload = (ImageButton) findViewById(R.id.ibtnOk);
  266. // if (photoList.size() > 0 && s.length() > 0)
  267. // {
  268. // ibtnUpload.setVisibility(View.VISIBLE);
  269. //
  270. // }
  271. // else
  272. // {
  273. // ibtnUpload.setVisibility(View.INVISIBLE);
  274. //
  275. // }
  276. // }
  277. // });
  278. // if (getRequestedOrientation() ==
  279. // ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
  280. // LinearLayout landLayout = (LinearLayout) findViewById(R.id.land);
  281. // landLayout.setVisibility(View.VISIBLE);
  282. // } else {
  283. // LinearLayout portLayout = (LinearLayout) findViewById(R.id.port);
  284. // portLayout.setVisibility(View.VISIBLE);
  285. // }
  286. ImageButton ibtnscan = (ImageButton) findViewById(R.id.ibtnScan);
  287. ibtnscan.setOnClickListener(new View.OnClickListener()
  288. {
  289. @Override
  290. public void onClick(View v)
  291. {
  292. Log.e(TAG,"==============>Click Scan button");
  293. Intent intent = new Intent();
  294. // Intent.ACTION_PICK,
  295. // android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  296. // startActivityForResult(intent, REQUEST_SCAN_BARCODE);
  297. intent.setClass(MainActivity.this, CaptureActivity.class);
  298. startActivityForResult(intent, REQUEST_SCAN_BARCODE);
  299. }
  300. });
  301. ImageButton captureButton = (ImageButton) findViewById(R.id.ibtnCapture);
  302. captureButton.setOnClickListener(new View.OnClickListener()
  303. {
  304. @Override
  305. public void onClick(View v)
  306. {
  307. StartCamera();
  308. }
  309. });
  310. ImageButton uploadButton = (ImageButton) findViewById(R.id.ibtnOk);
  311. uploadButton.setOnClickListener(new View.OnClickListener()
  312. {
  313. @Override
  314. public void onClick(View v)
  315. {
  316. SQLiteDatabase db = dbUtil
  317. .OpenDB(MainActivity.this, null, true);
  318. TextView pidval = (TextView) findViewById(R.id.pidval);
  319. String pid = pidval.getText().toString();
  320. for (int i = 0; i < photoList.size(); i++)
  321. {
  322. ContentValues cv = new ContentValues();
  323. cv.put("picker", RedAntApplication.user);
  324. cv.put("err_code", Network.STATUS_WAIT);
  325. cv.put("pid", pid);
  326. cv.put("local_path", photoList.get(i));
  327. db.insert("pics", null, cv);
  328. }
  329. dbUtil.CloseDB(db);
  330. // new UploadTask().execute((Void) null);
  331. clearfornew();
  332. Intent intent = new Intent("REDANT.POP.MODIFY_QUEUE");
  333. sendBroadcast(intent);
  334. }
  335. });
  336. m_tvalert = (TextView) findViewById(R.id.tvalert);
  337. m_tvalert.setSelected(true);
  338. // Intent intent = ;
  339. sendBroadcast(new Intent("REDANT.POP.MODIFY_QUEUE"));
  340. }
  341. @Override
  342. protected void onDestroy()
  343. {
  344. // TODO Auto-generated method stub
  345. Log.e(TAG, "=======================>MainActivity Destroyed!");
  346. unregisterReceiver(MainActivityReceiver);
  347. Log.e(TAG, "=======================>msgFilter unregisted!");
  348. super.onDestroy();
  349. }
  350. @Override
  351. public boolean onCreateOptionsMenu(Menu menu)
  352. {
  353. // Inflate the menu; this adds items to the action bar if it is present.
  354. getMenuInflater().inflate(R.menu.main, menu);
  355. return true;
  356. }
  357. private File createImageFile() throws IOException
  358. {
  359. // Create an image file name
  360. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
  361. .format(new Date());
  362. String imageFileName = "JPEG_" + timeStamp + "_";
  363. File storageDir = new File(Environment.getExternalStorageDirectory()
  364. .getPath() + "/redant/pop/temp/");
  365. File dir1 = new File(Environment.getExternalStorageDirectory()
  366. .getPath() + "/redant");
  367. File dir2 = new File(Environment.getExternalStorageDirectory()
  368. .getPath() + "/redant/pop");
  369. File dir3 = new File(Environment.getExternalStorageDirectory()
  370. .getPath() + "/redant/pop/temp");
  371. if (!dir1.exists())
  372. {
  373. boolean b = dir1.mkdir();
  374. }
  375. if (!dir2.exists())
  376. {
  377. boolean b = dir2.mkdir();
  378. }
  379. if (!dir3.exists())
  380. {
  381. boolean b = dir3.mkdir();
  382. }
  383. // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  384. // Log.d(TAG, "DIR:" + storageDir);
  385. LastFileName = storageDir + imageFileName + ".jpg";
  386. Log.d(TAG, "File:" + LastFileName);
  387. File image = File.createTempFile(imageFileName, /* prefix */
  388. ".jpg", /* suffix */
  389. storageDir /* directory */
  390. );
  391. // Save a file: path for use with ACTION_VIEW intents
  392. // mCurrentPhotoPath = "file:" + image.getAbsolutePath();
  393. return image;
  394. }
  395. // public static void savePic(Bitmap b, String strFileName)
  396. // {
  397. // FileOutputStream fos = null;
  398. // try
  399. // {
  400. // fos = new FileOutputStream(strFileName);
  401. // if (null != fos)
  402. // {
  403. //
  404. // b.compress(Bitmap.CompressFormat.PNG, 90, fos);
  405. // fos.flush();
  406. // fos.close();
  407. // }
  408. // }
  409. // catch (FileNotFoundException e)
  410. // {
  411. // e.printStackTrace();
  412. // }
  413. // catch (IOException e)
  414. // {
  415. // e.printStackTrace();
  416. // }
  417. // }
  418. boolean createDir(String path)
  419. {
  420. Log.i(TAG, "readFile:createDir=" + path);
  421. boolean ret = false;
  422. try
  423. {
  424. File file = new File(path);
  425. if (!file.exists())
  426. file.mkdirs();
  427. if (!file.isDirectory())
  428. {
  429. ret = file.mkdirs();
  430. }
  431. }
  432. catch (Exception e)
  433. {
  434. Log.e(TAG, e.toString());
  435. }
  436. return ret;
  437. }
  438. void updateUploadButton()
  439. {
  440. TextView pidval = (TextView) findViewById(R.id.pidval);
  441. ImageButton ibtnUpload = (ImageButton) findViewById(R.id.ibtnOk);
  442. if (photoList.size() > 0 && pidval.getText().length() > 0)
  443. {
  444. ibtnUpload.setVisibility(View.VISIBLE);
  445. }
  446. else
  447. {
  448. ibtnUpload.setVisibility(View.INVISIBLE);
  449. }
  450. }
  451. /*
  452. * (non-Javadoc)
  453. *
  454. * @see android.app.Activity#onActivityResult(int, int,
  455. * android.content.Intent)
  456. */
  457. @Override
  458. public void onActivityResult(int requestCode, int resultCode, Intent data)
  459. {
  460. if (requestCode == REQUEST_TAKE_PHOTO)
  461. // ������������յ�ͼƬ
  462. if (resultCode == Activity.RESULT_OK) // ��������
  463. {
  464. // Log.e(TAG, msg)
  465. // Bundle bundle = data.getExtras();
  466. // if(thumbBitmap!=null)
  467. // thumbBitmap=null;
  468. // thumbBitmap = (Bitmap) bundle.get("data");
  469. // // ��ȡ������ص����ݣ���ת��ΪBitmapͼƬ��ʽ
  470. // Bitmap thumbBitmap = BitmapFactory.decodeFile(photoFile
  471. // .toString());
  472. // CreateThumb(photoFile);
  473. Log.e(TAG, photoFile.toString());
  474. photoList.add(photoFile.toString());
  475. // Bitmap.createScaledBitmap(
  476. // thumbBitmap, thumbsImageView.getWidth(),
  477. // thumbsImageView.getHeight(), false));
  478. // Log.i(TAG, "thumb size" + thumbBitmap.getHeight() + "x"
  479. // + thumbBitmap.getWidth());
  480. // thumbpath = photoFile.toString();
  481. // Log.e(TAG, "get thumbpath==" + thumbpath);
  482. StartCamera();
  483. // iPhotoCount++;
  484. super.onActivityResult(requestCode, resultCode, data);
  485. }
  486. else
  487. {
  488. // �˳�����
  489. // File emptyFile = new File(LastFileName);
  490. // boolean b=emptyFile.delete();
  491. // Log.i(TAG, "lastpicPath: " + thumbpath);
  492. ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
  493. // LayerDrawable
  494. // thumbsImageView.setImageDrawable(drawable);
  495. // thumbBitmap=getImageThumbnail(thumbpath, iwidth,iheight);
  496. if (photoList.size() > 0)
  497. {
  498. thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory
  499. .decodeFile(photoList.get(photoList.size() - 1)),
  500. iwidth, iheight);
  501. if (thumbBitmap != null)
  502. thumbsImageView.setImageBitmap(thumbBitmap);
  503. }
  504. // thumbBitmap = ThumbnailUtils.extractThumbnail(
  505. // BitmapFactory.decodeFile(thumbpath), iwidth, iheight);
  506. // saveMyBitmap("thumbtest",thumbBitmap);
  507. // if (thumbBitmap != null)
  508. // thumbsImageView.setImageBitmap(thumbBitmap);
  509. photoFile.delete();
  510. TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
  511. countTextView.setText(photoList.size() + " Photos");
  512. // ImageButton ibtnUpload = (ImageButton)
  513. // findViewById(R.id.ibtnOk);
  514. updateUploadButton();
  515. }
  516. else if (requestCode == REQUEST_LOCKER)
  517. {
  518. if (resultCode == Activity.RESULT_OK)
  519. m_blocked = false;
  520. }
  521. else if (requestCode == REQUEST_SCAN_BARCODE)
  522. {
  523. if (resultCode == Activity.RESULT_OK)
  524. {
  525. Bundle bundle = data.getExtras();
  526. String pid = bundle.getString("pid");
  527. Log.e(TAG,
  528. "REQUEST_SCAN_BARCODE==========>pid="
  529. + bundle.getString("pid"));
  530. TextView pidval = (TextView) findViewById(R.id.pidval);
  531. pidval.setText(pid);
  532. // updateUploadButton();
  533. }
  534. }
  535. super.onActivityResult(requestCode, resultCode, data);
  536. }
  537. public void saveMyBitmap(String bitName, Bitmap mBitmap)
  538. {
  539. File f = new File("/sdcard/" + bitName + ".png");
  540. try
  541. {
  542. f.createNewFile();
  543. }
  544. catch (IOException e)
  545. {
  546. // TODO Auto-generated catch block
  547. // DebugMessage.put("�ڱ���ͼƬʱ����" + e.toString());
  548. }
  549. FileOutputStream fOut = null;
  550. try
  551. {
  552. fOut = new FileOutputStream(f);
  553. }
  554. catch (FileNotFoundException e)
  555. {
  556. e.printStackTrace();
  557. }
  558. mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  559. try
  560. {
  561. fOut.flush();
  562. }
  563. catch (IOException e)
  564. {
  565. e.printStackTrace();
  566. }
  567. try
  568. {
  569. fOut.close();
  570. }
  571. catch (IOException e)
  572. {
  573. e.printStackTrace();
  574. }
  575. }
  576. @Override
  577. public boolean onOptionsItemSelected(MenuItem item)
  578. {
  579. // TODO Auto-generated method stub
  580. switch (item.getItemId())
  581. {
  582. case R.id.action_clear:
  583. clearfornew();
  584. break;
  585. case R.id.action_type_pid:
  586. {
  587. final View edit = new EditText(this);
  588. new AlertDialog.Builder(this)
  589. .setIconAttribute(android.R.attr.alertDialogIcon)
  590. .setTitle(R.string.title_input_pid)
  591. .setView(edit)
  592. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  593. public void onClick(DialogInterface dialog, int whichButton) {
  594. String pid = ((EditText)edit).getText().toString();
  595. TextView pidval = (TextView) findViewById(R.id.pidval);
  596. pidval.setText(pid);
  597. /* User clicked OK so do some stuff */
  598. }
  599. })
  600. .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  601. public void onClick(DialogInterface dialog, int whichButton) {
  602. /* User clicked cancel so do some stuff */
  603. }
  604. })
  605. .create().show();
  606. }
  607. break;
  608. case R.id.action_view_queue:
  609. {
  610. Intent intent = new Intent();
  611. intent.setClass(MainActivity.this, UploadQueueActivity.class);
  612. startActivity(intent);
  613. break;
  614. }
  615. default:
  616. break;
  617. }
  618. return super.onOptionsItemSelected(item);
  619. }
  620. @Override
  621. protected void onSaveInstanceState(Bundle outState)
  622. {
  623. // TODO Auto-generated method stub
  624. super.onSaveInstanceState(outState);
  625. if (photoFile != null)
  626. outState.putSerializable("photoFile", photoFile);
  627. if (thumbBitmap != null)
  628. outState.putByteArray("thumbBitmap", getBytes(thumbBitmap));// ("thumbBitmap",
  629. // (Object)thumbBitmap);
  630. // outState.putString("thumbpath",
  631. // thumbpath);
  632. // outState.putString("m_sName", m_sName);
  633. // outState.putString("m_sPassword", m_sPassword);
  634. outState.putStringArrayList("photoList", photoList);
  635. // Log.e(TAG, "save thumbpath==" + thumbpath);
  636. outState.putInt("iPhotoCount", photoList.size());
  637. }
  638. public static byte[] getBytes(Bitmap bitmap)
  639. {
  640. // ʵ�����ֽ����������
  641. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  642. bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);// ѹ��λͼ
  643. return baos.toByteArray();// ���������ֽ�����
  644. }
  645. public static Bitmap getBitmap(byte[] data)
  646. {
  647. if (data == null)
  648. return null;
  649. return BitmapFactory.decodeByteArray(data, 0, data.length);// ���ֽ��������λͼ
  650. }
  651. private void clearfornew()
  652. {
  653. photoList.clear();
  654. TextView pidval = (TextView) findViewById(R.id.pidval);
  655. pidval.setText("");
  656. updateUploadButton();
  657. ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
  658. thumbsImageView.setImageResource(R.drawable.no_pic);
  659. thumbBitmap = null;
  660. TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
  661. countTextView.setText("no photo");
  662. }
  663. // public class UploadTask extends AsyncTask<Void, Void, Boolean>
  664. // {
  665. // String err_msg="";
  666. // @Override
  667. // protected Boolean doInBackground(Void... params)
  668. // {
  669. // boolean bret=true;
  670. //
  671. //
  672. // SQLiteDatabase db = dbUtil.OpenDB(MainActivity.this, null, true);
  673. // EditText edit = (EditText) findViewById(R.id.edit);
  674. // String pid = edit.getText().toString();
  675. //
  676. // if (Network.NetworkIsAvailable(MainActivity.this))
  677. // {
  678. // int verifyresult = Network.VerifyUser(m_sName, m_sPassword);
  679. // if (verifyresult == Network.RESULT_TRUE)
  680. // {
  681. // /* user is authorized */
  682. // for (int i = 0; i < photoList.size(); i++)
  683. // {
  684. // int uploadresult = Network.UploadFile(photoList.get(i),
  685. // m_sName, m_sPassword, pid);
  686. // if (uploadresult != Network.RESULT_TRUE)
  687. // {
  688. // /*
  689. // * upload failed write to local db upload when
  690. // * connect to network next time;
  691. // */
  692. //
  693. // /*
  694. // * serious error occur must notify user
  695. // * immediately
  696. // */
  697. // if (uploadresult == Network.RESULT_LOCALFILE_ERROR)
  698. // err_msg+="Warring! Local picture file not found! ";
  699. // if( uploadresult == Network.RESULT_USERAUTH_ERROR)
  700. // err_msg+="Warring! Authorize check failed when uploading photos! ";
  701. //
  702. // ContentValues cv = new ContentValues();
  703. // cv.put("picker", m_sName);
  704. // cv.put("err_code", uploadresult);
  705. // cv.put("pid", pid);
  706. // cv.put("local_path", photoList.get(i));
  707. // db.insert("pics", null, cv);
  708. // bret=false;
  709. // }
  710. // else
  711. // {
  712. // /* upload success delete file */
  713. // File file = new File(photoList.get(i));
  714. // file.delete();
  715. // }
  716. //
  717. // }
  718. //
  719. // }
  720. // else
  721. // if (verifyresult == Network.RESULT_FALSE)
  722. // {
  723. // /*
  724. // * user is not authorized serious error occur must
  725. // * notify user
  726. // */
  727. // Log.e(TAG, "notification====================>");
  728. // // String text=m_tvalert.getText().toString();
  729. // err_msg+="Warring! Authorize check failed when uploading photos!";
  730. // bret=false;
  731. // for (int i = 0; i < photoList.size(); i++)
  732. // {
  733. // ContentValues cv = new ContentValues();
  734. // cv.put("picker", m_sName);
  735. // cv.put("err_code", Network.RESULT_USERAUTH_ERROR);
  736. // cv.put("pid", pid);
  737. // cv.put("local_path", photoList.get(i));
  738. // db.insert("pics", null, cv);
  739. // }
  740. //
  741. // }
  742. // else
  743. // {
  744. // /* an error occur */
  745. // for (int i = 0; i < photoList.size(); i++)
  746. // {
  747. // ContentValues cv = new ContentValues();
  748. // cv.put("picker", m_sName);
  749. // cv.put("err_code", Network.RESULT_NET_ERROR);
  750. // cv.put("pid", pid);
  751. // cv.put("local_path", photoList.get(i));
  752. // db.insert("pics", null, cv);
  753. // }
  754. // bret = false;
  755. // }
  756. // }
  757. // else
  758. // {
  759. // /*
  760. // * net work is not available store into local DB it will auto
  761. // * upload when connect to network next time;
  762. // */
  763. //
  764. // for (int i = 0; i < photoList.size(); i++)
  765. // {
  766. // ContentValues cv = new ContentValues();
  767. // cv.put("picker", m_sName);
  768. // cv.put("err_code", Network.RESULT_NET_ERROR);
  769. // cv.put("pid", pid);
  770. // cv.put("local_path", photoList.get(i));
  771. // db.insert("pics", null, cv);
  772. // }
  773. // bret=false;
  774. // // m_tvalert.setText("Upload failed when uploading photos!");
  775. //
  776. // }
  777. //
  778. // dbUtil.CloseDB(db);
  779. // return bret;
  780. // }
  781. //
  782. // @Override
  783. // protected void onPostExecute(final Boolean success)
  784. // {
  785. // if(success)
  786. // {
  787. // m_tvalert.setTextColor(getResources().getColor(R.color.message_success));
  788. // m_tvalert.setText("Upload successed!");
  789. // }
  790. // else
  791. // {
  792. // m_tvalert.setTextColor(getResources().getColor(R.color.message_fail));
  793. // m_tvalert.setText(err_msg);
  794. // }
  795. //
  796. //
  797. // // mAuthTask = null;
  798. // // showProgress(false);
  799. // //
  800. // // if (success) {
  801. // //
  802. // // // SharedPreferences.Editor editor = RunOnce.edit();
  803. // // // editor.putBoolean("FirstRun"+globalUtil.getVerName(this),
  804. // // false);
  805. // // // // Don't forget to commit your edits!!!
  806. // // // editor.commit();
  807. // // Intent intent = new Intent();
  808. // // intent.setClass(LoginActivity.this, MainActivity.class);
  809. // // startActivity(intent);
  810. // // finish();
  811. // // } else {
  812. // // m_etPassword
  813. // // .setError(getString(R.string.error_incorrect_password));
  814. // // m_etPassword.requestFocus();
  815. // // }
  816. // }
  817. //
  818. // @Override
  819. // protected void onCancelled()
  820. // {
  821. // // mAuthTask = null;
  822. // // showProgress(false);
  823. // }
  824. // }
  825. // public class SingleMediaScanner implements MediaScannerConnectionClient {
  826. //
  827. // private MediaScannerConnection mMs;
  828. // private File mFile;
  829. //
  830. // public SingleMediaScanner(Context context, File f) {
  831. // mFile = f;
  832. // mMs = new MediaScannerConnection(context, this);
  833. // mMs.connect();
  834. // }
  835. //
  836. // public void onMediaScannerConnected() {
  837. // Log.d(null, ""+mFile.getAbsolutePath());
  838. // Log.d(null,
  839. // ""+Environment.getExternalStorageDirectory().getPath()+"/photo/");
  840. // Log.d(null, new
  841. // File(Environment.getExternalStorageDirectory().getPath()+"/photo/").getAbsolutePath());
  842. //
  843. // mMs.scanFile(mFile.getAbsolutePath(), null);
  844. // }
  845. //
  846. // public void onScanCompleted(String path, Uri uri) {
  847. // Intent intent = new Intent(Intent.ACTION_VIEW);
  848. // intent.setData(uri);
  849. // startActivity(intent);
  850. // mMs.disconnect();
  851. // }
  852. //
  853. // }
  854. private final BroadcastReceiver MainActivityReceiver = new BroadcastReceiver()
  855. {
  856. public void onReceive(
  857. Context context,
  858. Intent intent)
  859. {
  860. String action = intent
  861. .getAction();
  862. // 如果捕捉到的action是ACTION_BATTERY_CHANGED
  863. if ("REDANT.POP.USER_CHECK_FAILED"
  864. .equals(action))
  865. {
  866. m_tvalert
  867. .setTextColor(getResources()
  868. .getColor(
  869. R.color.message_fail));
  870. m_tvalert
  871. .setText("Upload failed:user verify failure.");
  872. }
  873. else if ("REDANT.POP.FINISH_UPLOAD_QUEUE"
  874. .equals(action))
  875. {
  876. m_tvalert.setTextColor(getResources().getColor(R.color.message_success));
  877. m_tvalert.setText("Upload successes!");
  878. }
  879. else if ("REDANT.POP.FINISH_UPLOAD_QUEUE_WITH_ERROR"
  880. .equals(action))
  881. {
  882. m_tvalert.setTextColor(getResources().getColor(R.color.message_fail));
  883. m_tvalert.setText("Upload finish with some error!");
  884. }
  885. else if (action
  886. .equals(Intent.ACTION_SCREEN_OFF))
  887. {
  888. Log.e("hg",
  889. "�յ��㲥!!!=======>m_blocked="
  890. + m_blocked);
  891. if (m_blocked == true)
  892. return;
  893. Intent activityintent = new Intent();
  894. activityintent
  895. .setClass(
  896. MainActivity.this,
  897. LockerActivity.class);
  898. startActivityForResult(
  899. activityintent,
  900. REQUEST_LOCKER);
  901. Log.e(TAG,
  902. "===================>start locker");
  903. m_blocked = true;
  904. }
  905. }
  906. };
  907. }