Back to project page VideoExtand.
The source code is released under:
Apache License
If you think the Android project VideoExtand listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yuninfo.videoextand; /*from w w w. j a va 2 s .com*/ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.util.EntityUtils; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; import com.cn21.openapi.util.helper.ReqeustParasParseHelper; import com.cn21.openapi.util.helper.RequestSignatureHelper; import com.google.gson.Gson; import com.google.gson.JsonParseException; import com.google.gson.JsonSyntaxException; import com.yuninfo.videoextand.bean.CommonReq; import com.yuninfo.videoextand.bean.SendVideoResp; import com.yuninfo.videoextand.bean.UploadReq; import com.yuninfo.videoextand.bean.UploadResp; import com.yuninfo.videoextand.recorder.RecorderActivity; import com.yuninfo.videoextand.uploader.UploadHandler; import com.yuninfo.videoextand.uploader.UploadThread; import com.yuninfo.videoextand.utils.FileUtil; import com.yuninfo.videoextand.utils.HttpUtil; import com.yuninfo.videoextand.utils.LogUtil; import com.yuninfo.videoextand.utils.StringUtil; import com.yuninfo.videoextand.widget.CustomPopupWindow; import com.yuninfo.videoextand.widget.TitleBar; import com.yuninfo.videoextand.widget.dialog.MenuDialog; public class SendCardActivity extends BaseActivity { private static String TAG = SendCardActivity.class.getSimpleName(); private TitleBar mTitleBar; private ImageView mIvVideoThumb; private ImageView mIvVideoView; private Button mBtnAddVideo; private ProgressBar mPbUploadProgress; private EditText mEtWishes; private ImageButton mIbtnWishShort; private EditText mEtContact; private ImageButton mIbtnChoose; private Button mBtnSend; private CustomPopupWindow mWishDrop = null; private MenuDialog mAddVideoMenuDialog = null; private MenuDialog mVideoMenuDialog = null; private File mVideoFile; private File mImgFile; private UploadThread mUploadThread; private State mUploadState = State.FINISHED; // private String mAppId = "test"; // private String mAppSecret = "test"; // // private String mUserId = "1000000000"; // private String mToMobile = "13352801008"; private String mAppId; private String mAppSecret; private String mUserId; private String mUserName; private String mToName; private String mToMobile; private int mRemaiCoin = -1; private UploadResp mUploadResult; private Resources mResources; private String mPackageName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mResources = getResources(); mPackageName = getPackageName(); LogUtil.w(TAG, mPackageName); if(!HttpUtil.isNetworkAvairable(this)) { finish(); } if(!initData()) { return; } int layoutId = mResources.getIdentifier("yuninfo_activity_send_card", "layout", mPackageName); setContentView(layoutId); initView(); } private boolean initData() { if(hasExtra("yuninfo_userId")) { mUserId = getStringExtra("yuninfo_userId"); } else { showShortToast(mResources.getIdentifier("yuninfo_appid_null", "string", mPackageName)); finish(); return false; } if(hasExtra("yuninfo_appId")) { mAppId = getStringExtra("yuninfo_appId"); } else { showShortToast(mResources.getIdentifier("yuninfo_appid_null", "string", mPackageName)); finish(); return false; } if(hasExtra("yuninfo_appSecret")) { mAppSecret = getStringExtra("yuninfo_appSecret"); } else { showShortToast(mResources.getIdentifier("yuninfo_appid_null", "string", mPackageName)); finish(); return false; } if(hasExtra("yuninfo_userName")) { mUserName = getStringExtra("yuninfo_userName"); } if(hasExtra("yuninfo_remainCoin")) { mRemaiCoin = getIntExtra("yuninfo_remainCoin"); } return true; } private void initView() { initTitle(); mIvVideoThumb = (ImageView) findViewById(mResources.getIdentifier("yuninfo_iv_video_thumb", "id", mPackageName)); mIvVideoView = (ImageView) findViewById(mResources.getIdentifier("yuninfo_iv_video_view_icon", "id", mPackageName)); mIvVideoView.setVisibility(View.INVISIBLE); mBtnAddVideo = (Button) findViewById(mResources.getIdentifier("yuninfo_btn_add_video", "id", mPackageName)); mBtnAddVideo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAddVideoMenuDialog.show(); } }); mPbUploadProgress = (ProgressBar) findViewById(mResources.getIdentifier("yuninfo_pb_upload_progress", "id", mPackageName)); mEtWishes = (EditText) findViewById(mResources.getIdentifier("yuninfo_et_wish_msg", "id", mPackageName)); mIbtnWishShort = (ImageButton) findViewById(mResources.getIdentifier("yuninfo_ibtn_wish_short", "id", mPackageName)); mEtContact = (EditText) findViewById(mResources.getIdentifier("yuninfo_et_contact_info", "id", mPackageName)); mEtContact.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { mToName = null; mToMobile = null; } }); mIbtnChoose = (ImageButton) findViewById(mResources.getIdentifier("yuninfo_ibtn_select_contact", "id", mPackageName)); mIbtnChoose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pickContact(); } }); mBtnSend = (Button) findViewById(mResources.getIdentifier("yuninfo_btn_send_video_card", "id", mPackageName)); mBtnSend.requestFocus(); mBtnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendVideoCard(); } }); initDropWindow(); mIbtnWishShort.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDropWindow(mEtWishes, mEtWishes.getWidth() + mIbtnWishShort.getWidth()); } }); initAddVideoMenuDialog(); initVideoMenuDialog(); } private void initTitle() { mTitleBar = (TitleBar) findViewById(mResources.getIdentifier("yuninfo_tb_send_card", "id", mPackageName)); mTitleBar.setLeftButton(mResources.getIdentifier("yuninfo_ic_title_back", "drawable", mPackageName), new View.OnClickListener() { @Override public void onClick(View v) { exit(); } }); mTitleBar.setTitleText(mResources.getIdentifier("yuninfo_video_card_title", "string", mPackageName)); } private void initDropWindow() { mWishDrop = new CustomPopupWindow(this); int itemsId = mResources.getIdentifier("yuninfo_default_wish_list", "array", mPackageName); mWishDrop.setItems(itemsId); mWishDrop.setOnItemClickListener(new CustomPopupWindow.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mEtWishes.setText(mWishDrop.getItems(position)); } }, true); } private void showDropWindow(View view, int width) { mWishDrop.setWidth(width); mWishDrop.showAsDropDown(view); } private void initAddVideoMenuDialog() { mAddVideoMenuDialog = new MenuDialog(this); int itemsId = mResources.getIdentifier("yuninfo_add_video_menu", "array", mPackageName); int nId = mResources.getIdentifier("yuninfo_cancel", "string", mPackageName); String [] items = mResources.getStringArray(itemsId); mAddVideoMenuDialog.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Intent intent = new Intent(SendCardActivity.this, RecorderActivity.class); startActivityForResult(intent, Config.YUNINFO_REQUEST_CODE_RECORD_VIDEO); break; case 1: //TODO Choose a video file showFileChooser(); break; default: break; } dialog.cancel(); } }) .setNegativeButton(nId, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); } private void initVideoMenuDialog() { mVideoMenuDialog = new MenuDialog(this); int itemsId = mResources.getIdentifier("yuninfo_video_menu", "array", mPackageName); int nId = mResources.getIdentifier("yuninfo_cancel", "string", mPackageName); String [] items = mResources.getStringArray(itemsId); mVideoMenuDialog.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: playVideo(mVideoFile); break; case 1: mAddVideoMenuDialog.show(); break; default: break; } dialog.cancel(); } }) .setNegativeButton(nId, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); } /** ??????????????? **/ private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("video/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(Intent.createChooser(intent, "?????????"), Config.YUNINFO_REQUEST_CODE_PICK_VIDEO); } catch (android.content.ActivityNotFoundException ex) { // Potentially direct the user to the Market with a Dialog showLongToast("???????????????????????????"); } } private void exit() { // if(mVideoFile == null) { // return; // } new AlertDialog.Builder(this) .setTitle("????") .setMessage("????????????????????") .setPositiveButton("??", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(mUploadThread != null) { mUploadThread.interrupt(); } finish(); } }) .setNegativeButton("????", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); } private void pickContact() { Intent intent = new Intent(); // ???????????????? ComponentName componetName = new ComponentName( "com.corp21cn.flowpay", "com.corp21cn.flowpay.activity.PhoneContactActivity"); intent.setComponent(componetName); intent.putExtra("flowpay.intent.extra.actionFlag","GET_CONTACT"); try { startActivityForResult(intent, Config.YUNINFO_REQUEST_CODE_PICK_CONTACT); } catch (Exception e) { showShortToast("??????????????????"); e.printStackTrace(); } } private void playVideo(File file) { Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); it.setDataAndType(uri , "video/*"); startActivity(it); } private void uploadVideo(File video, File img) { if(mUploadState != State.UPLOADING) { UploadReq param = new UploadReq(video, img); mUploadThread = new UploadThread(null, param, new UploadHandler(this)); mUploadThread.start(); } } private void sendVideoCard() { if(StringUtil.isEmpty(mEtWishes.getText().toString().trim())) { showShortToast("????????"); return; } if(StringUtil.isEmpty(mToMobile)) { String phone = mEtContact.getText().toString().trim(); if(StringUtil.isEmpty(phone)) { showShortToast("???????"); return; } else { mToName = null; mToMobile = phone; } } if(!StringUtil.isValidPhoneNumber(mToMobile)) { showShortToast("????????????"); return; } if(mUploadState == State.UPLOADING) { showShortToast("???????????..."); return; } if(mUploadResult == null) { showShortToast("??????????????"); return; } final UploadResp.UploadData video = mUploadResult.getVideo(); final UploadResp.UploadData img = mUploadResult.getImg(); if(video == null || StringUtil.isEmpty(video.getShortUrl())) { showShortToast("??????????????"); return; } final int fileSize = getFileSize(video.getFileSize()); if(mRemaiCoin != -1 && mRemaiCoin < fileSize + 1) { showShortToast("???????????????????????????"); return; } new AlertDialog.Builder(this) .setTitle("????") .setMessage("???????????" + formatNameAndPhone(mToName, mToMobile) + "?????????????????????????????" + fileSize + "????????????????????" + fileSize + "???") .setPositiveButton("???????", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String msg = "??????" + (StringUtil.isEmpty(mUserName) ? "" : mUserName) + "???????????????????????" + mEtWishes.getText().toString().trim() + "??,???????????????????????????????????"; CommonReq req = new CommonReq(mAppId, Config.YUNINFO_REQUEST_VERSION, Config.YUNINFO_REQUEST_CLIENT_TYPE, Config.YUNINFO_REQUEST_FORMAT); String paramsStr = "userId=" + mUserId + "&toMobile=" + mToMobile + "&url=" + video.getShortUrl() + "," + img.getShortUrl() + "&msg=" + msg + "&fileSize=" + fileSize; LogUtil.i(TAG, paramsStr); String params = ReqeustParasParseHelper.generateXXTeaParasData(paramsStr, mAppSecret); String signStr = mAppId + Config.YUNINFO_REQUEST_CLIENT_TYPE + Config.YUNINFO_REQUEST_FORMAT + Config.YUNINFO_REQUEST_VERSION + params; LogUtil.i(TAG, signStr); String sign = RequestSignatureHelper.generateBase64HmacSignature(signStr, mAppSecret); req.setParas(params); req.setSign(sign); new SendVideoTask().execute(req); } }) .setNegativeButton("?????????", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // finish(); } }).show(); } private int getFileSize(float fileSize) { float mbF = fileSize / 1000; int mbI = (int) mbF; if(mbI < mbF) { mbI++; } return mbI; } /*private String getMbSize(float kbSize) { float mbSize = kbSize / 1000; return new DecimalFormat("#0.00").format(mbSize); }*/ public void onTaskStarted() { mPbUploadProgress.setVisibility(View.VISIBLE); showShortToast(mResources.getIdentifier("yuninfo_upload_start", "string", mPackageName)); mUploadState = State.UPLOADING; } public void onTaskCanceled() { mPbUploadProgress.setVisibility(View.GONE); mUploadState = State.CANCELD; } public void onProgressChanged(Long... progress) { LogUtil.e(TAG, "Progress++++++++++++++" + ((int)(progress[0] * 100 / progress[1]))); mPbUploadProgress.setProgress((int)(progress[0] * 100 / progress[1])); } public void onTaskSuccess(UploadResp resp) { mPbUploadProgress.setVisibility(View.GONE); showShortToast(mResources.getIdentifier("yuninfo_upload_success", "string", mPackageName)); mUploadState = State.FINISHED; mUploadResult = resp; } public void onTaskFailed(String msg) { mPbUploadProgress.setVisibility(View.GONE); mUploadState = State.CANCELD; new AlertDialog.Builder(this) .setTitle("????") .setMessage("???????" + msg + "??????????") .setPositiveButton("??", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { uploadVideo(mVideoFile, mImgFile); } }) .setNegativeButton("????", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // finish(); } }) .show(); } private String formatNameAndPhone(String name, String phone) { if(StringUtil.isEmpty(phone)) { return null; } boolean noName = StringUtil.isEmpty(name); return (noName ? "" : (name + "(")) + phone + (noName ? "" : ")"); } @SuppressLint("NewApi") private void doUploadVideo(){ mImgFile = new File(mVideoFile.getParent(), "Thumb_" + mVideoFile.getName() + ".jpg"); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { Bitmap thumb = ThumbnailUtils.createVideoThumbnail(mVideoFile.getAbsolutePath(), MediaStore.Images.Thumbnails.MICRO_KIND); if(thumb == null) { return; } try { thumb.compress(CompressFormat.JPEG, 100, new FileOutputStream(mImgFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } mIvVideoThumb.setImageBitmap(thumb); } mBtnAddVideo.setVisibility(View.GONE); mIvVideoView.setVisibility(View.VISIBLE); mIvVideoThumb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mVideoMenuDialog.show(); } }); uploadVideo(mVideoFile, mImgFile); } @SuppressLint("NewApi") @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == Config.YUNINFO_REQUEST_CODE_RECORD_VIDEO) { if(resultCode == RESULT_OK) { if(data == null) { return; } if(data.hasExtra(Config.YUNINFO_RESULT_DATA)) { mVideoFile = (File) data.getSerializableExtra(Config.YUNINFO_RESULT_DATA); if(mVideoFile == null || !mVideoFile.exists()) { showLongToast("???????,???????????????"); return; } doUploadVideo(); } } } else if(requestCode == Config.YUNINFO_REQUEST_CODE_PICK_CONTACT) { if(resultCode == RESULT_OK) { //TODO Get contact success. if(data == null) { return; } String name = null; String phone = null; if(data.hasExtra("contactName")) { name = data.getStringExtra("contactName"); } if(data.hasExtra("contactMobil")) { phone = data.getStringExtra("contactMobil"); } mEtContact.setText(formatNameAndPhone(name, phone)); mToName = name; mToMobile = phone; } else if(requestCode == RESULT_ERROR) { finish(); } } else if(requestCode == Config.YUNINFO_REQUEST_CODE_PICK_VIDEO) { if(resultCode == RESULT_OK) { // Get the Uri of the selected file if(data == null) { showShortToast("????"); return; } Uri uri = data.getData(); if(uri == null) { showShortToast("????"); return; } String mimeType = FileUtil.getMimeType(this, uri); if(isVideo(mimeType)) { mVideoFile = FileUtil.parseUriToFile(this, uri); if(mVideoFile == null || !mVideoFile.exists()) { showShortToast("??????"); mVideoFile = null; return; } if(FileUtil.getFileSize(mVideoFile) > 5 * 1024 * 1024) { showShortToast("?????????5M"); mVideoFile = null; return; } doUploadVideo(); } else { //Not video file showShortToast("????????????????????"); } } } } private boolean isVideo(String mimeType) { if(StringUtil.isEmpty(mimeType)) { return false; } return mimeType.startsWith("video"); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) { exit(); return true; } return super.onKeyDown(keyCode, event); } /** * Upload state. * @author yinglovezhuzhu@gmail.com * */ public static enum State { UPLOADING, FINISHED, PAUSED, CANCELD, } private class SendVideoTask extends AsyncTask<CommonReq, Long, SendVideoResp> { private ProgressDialog mmProgressDialog; @Override protected void onPreExecute() { super.onPreExecute(); mmProgressDialog = ProgressDialog.show(SendCardActivity.this, null, "???????????"); } @Override protected SendVideoResp doInBackground(CommonReq... params) { try { HttpResponse resp = HttpUtil.doPost(Config.YUNINFO_SEND_VIDEO_URL, null, params[0]); if(HttpUtil.getStatusCode(resp) == HttpUtil.OK) { SendVideoResp result = new Gson().fromJson(EntityUtils.toString(resp.getEntity(), "UTF-8"), SendVideoResp.class); return result; } else { SendVideoResp result = new SendVideoResp(); result.setResult(HttpUtil.getStatusCode(resp)); result.setMsg(EntityUtils.toString(resp.getEntity(), "UTF-8")); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); //TODO ????? } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (JsonParseException e) { // TODO: handle exception e.printStackTrace(); } return null; } @Override protected void onPostExecute(SendVideoResp result) { super.onPostExecute(result); if(result == null) { showShortToast("???????"); } else { if(result.getResult() == 0) { LogUtil.i(TAG, result); showShortToast("?????????????????????????" + (StringUtil.isEmpty(mToName) ? mToMobile : formatNameAndPhone(mToName, mToMobile))); mmProgressDialog.cancel(); finish(); return; } else { showShortToast(result.getMsg()); } } mmProgressDialog.cancel(); } } }