package com.ejoyshop.android;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import org.json.JSONException;
import org.json.JSONObject;
import com.ejoyshop.AsTask.LoadForCheckCouponAsynTask;
import com.ejoyshop.AsTask.LoadForCheckUserAsynTask;
import com.ejoyshop.AsTask.LoadForCheckUserCardAsynTask;
import com.ejoyshop.dal.DALUserCouponLog;
import com.ejoyshop.model.UserCouponLog;
import com.ejoyshop.utils.EjoyLog;
import com.ejoyshop.utils.ErrorCode;
import com.ejoyshop.utils.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class CaptureActivity extends Activity implements Callback {
private CaptureActivityHandler handler;
private ViewfinderView viewfinderView;
private boolean hasSurface;
private Vector<BarcodeFormat> decodeFormats;
private String characterSet;
private final int DIALOG_Exit = 5;
private Dialog mExitDialog = null;
private InactivityTimer inactivityTimer;
private MediaPlayer mediaPlayer;
private boolean playBeep;
private static final float BEEP_VOLUME = 0.10f;
private boolean vibrate;
private DALUserCouponLog mDalUserCouponLog = null;
/***
* 提示消息tip
*/
private TextView txtTextView = null;
private ImageView mivManualCode = null;
private ImageView mSendCardImageView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化 CameraManager
CameraManager.init(CaptureActivity.this);
mDalUserCouponLog = new DALUserCouponLog(CaptureActivity.this);
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
txtTextView = (TextView) findViewById(R.id.txtResult);
txtTextView.setText(R.string.caturetip);
mivManualCode = (ImageView) findViewById(R.id.capture_maucalcode);
mSendCardImageView = (ImageView) findViewById(R.id.capture_sendcard);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
new UploadofflinedataTask().execute("");
viewfinderView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
@Override
protected void onResume() {
super.onResume();
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
initCamera(surfaceHolder);
} else {
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
decodeFormats = null;
characterSet = null;
playBeep = true;
AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
playBeep = false;
}
initBeepSound();
vibrate = true;
}
@Override
protected void onPause() {
super.onPause();
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
CameraManager.get().closeDriver();
}
@Override
protected void onDestroy() {
inactivityTimer.shutdown();
if (mExitDialog != null) {
if (mExitDialog.isShowing()) {
mExitDialog.dismiss();
}
}
if (mDalUserCouponLog != null) {
mDalUserCouponLog.CloseDb();
}
super.onDestroy();
}
private void initCamera(SurfaceHolder surfaceHolder) {
try {
CameraManager.get().openDriver(surfaceHolder);
} catch (IOException ioe) {
return;
} catch (RuntimeException e) {
return;
}
if (handler == null) {
handler = new CaptureActivityHandler(this, decodeFormats,
characterSet);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
public ViewfinderView getViewfinderView() {
return viewfinderView;
}
public Handler getHandler() {
return handler;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
showDialog(DIALOG_Exit);
} else {
super.onKeyDown(keyCode, event);
}
return true;
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_Exit:
dialog = mExitDialog = new AlertDialog.Builder(CaptureActivity.this)
.setTitle("提示")
.setMessage("确定要退出吗?")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
java.lang.System.exit(0);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
}).show();
break;
case 6548:
break;
}
return dialog;
}
public void drawViewfinder() {
viewfinderView.drawViewfinder();
}
public void handleDecode(Result obj, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String string = obj.getText();
}
private void initBeepSound() {
if (playBeep && mediaPlayer == null) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(beepListener);
AssetFileDescriptor file = getResources().openRawResourceFd(
R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
}
}
private static final long VIBRATE_DURATION = 200L;
private void playBeepSoundAndVibrate() {
if (playBeep && mediaPlayer != null) {
mediaPlayer.start();
}
if (vibrate) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
void ShowDialog(String titleString, String messString) {
new AlertDialog.Builder(this).setTitle(titleString)
.setMessage(messString)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
Message quit = Message.obtain(handler, R.id.restart_preview);
quit.sendToTarget();
}
@Override
public void onTick(long millisUntilFinished) {
}
}
/**
* When the beep has finished playing, rewind to queue up another one.
*/
private final OnCompletionListener beepListener = new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.seekTo(0);
}
};
}
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/preview_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<com.ejoyshop.android.ViewfinderView
android:id="@+id/viewfinder_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/transparent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="bottom|center_horizontal" >
<ImageView
android:id="@+id/capture_maucalcode"
android:layout_width="48.0dip"
android:layout_height="48.0dip"
android:layout_marginRight="3dip"
android:background="@drawable/capture_btn_bk"
android:src="@drawable/ic_edit" />
<ImageView
android:id="@+id/capture_sendcard"
android:layout_width="48.0dip"
android:layout_height="48.0dip"
android:layout_marginLeft="3dip"
android:background="@drawable/capture_btn_bk"
android:src="@drawable/ic_tag" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</RelativeLayout>