Android Open Source - VideoExtand Read Card Activity






From Project

Back to project page VideoExtand.

License

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.

Java Source Code

/*
 * Copyright (C) 2014 The Android Open Source Project
 *//  w w  w  . ja v a  2  s.  com
 * 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.
 *
 * Auther?yinglovezhuzhu@gmail.com
 * File: ReadCardActivity.java
 * Date?2014?1?3?
 * Version?v1.0
 */  
package com.yuninfo.videoextand;

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.ProgressDialog;
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.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn21.openapi.util.helper.ReqeustParasParseHelper;
import com.cn21.openapi.util.helper.RequestSignatureHelper;
import com.cn21.openapi.util.helper.StringUtil;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.opensource.bitmapfun.util.ImageCache;
import com.opensource.bitmapfun.util.ImageFetcher;
import com.opensource.bitmapfun.util.Utils;
import com.opensource.bitmapfun.util.ImageCache.ImageCacheParams;
import com.opensource.bitmapfun.util.ImageWorker;
import com.yuninfo.videoextand.bean.CommonReq;
import com.yuninfo.videoextand.bean.ReadVideoResp;
import com.yuninfo.videoextand.bean.SendVideoResp;
import com.yuninfo.videoextand.player.VideoPlayerActivity;
import com.yuninfo.videoextand.utils.HttpUtil;
import com.yuninfo.videoextand.utils.LogUtil;
import com.yuninfo.videoextand.widget.TitleBar;

/**
 * ???
 * @author yinglovezhuzhu@gmail.com
 */
public class ReadCardActivity extends BaseActivity {
  
  private static final String TAG = ReadCardActivity.class.getSimpleName();

  private TitleBar mTitleBar;
  private ImageView mIvVideoThumb;
  private ImageView mIvVideoPlay;
  private TextView mTvMsg;
  private EditText mEtMsgFeedBack;
  private Button mBtnOpenVideo;
  
  private String mAppId;
  private String mAppSecret;
  
  private String mUserId; //?????id
  private String mMsgId;
  
  private String mVideoUrl;
  private String mThumbUrl;
  private String mMsg;
  private boolean mHasGetCoin = false;
  
  private ImageWorker mImageWorker;

  private Resources mResources;
  private String mPackageName;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    mResources = getResources();
    mPackageName = getPackageName();
    
    if(!HttpUtil.isNetworkAvairable(this)) {
      setResult(RESULT_CANCELED);
      finish();
    }
    
    initData();
    
    int layoutId = mResources.getIdentifier("yuninfo_activity_read_card", "layout", mPackageName);
    setContentView(layoutId);
    
    initView();
  }
  
  private void initData() {
    if(hasExtra("yuninfo_appId")) {
      mAppId = getStringExtra("yuninfo_appId");
    } else {
      showShortToast(mResources.getIdentifier("yuninfo_appid_null", "string", mPackageName));
      setResult(RESULT_CANCELED);
      finish();
    }
    if(hasExtra("yuninfo_appSecret")) {
      mAppSecret = getStringExtra("yuninfo_appSecret");
    } else {
      showShortToast(mResources.getIdentifier("yuninfo_appsecret_null", "string", mPackageName));
      setResult(RESULT_CANCELED);
      finish();
    }
    if(hasExtra("yuninfo_userId")) {
      mUserId = getStringExtra("yuninfo_userId");
    } else {
      showShortToast(mResources.getIdentifier("yuninfo_appid_null", "string", mPackageName));
      setResult(RESULT_CANCELED);
      finish();
    }
    if(hasExtra("yuninfo_msgId")) {
      mMsgId = getStringExtra("yuninfo_msgId");
    } else {
      showShortToast("msgId?????");
      setResult(RESULT_CANCELED);
      finish();
    }
    if(hasExtra("yuninfo_url")) {
      String url = getStringExtra("yuninfo_url");
      if(StringUtil.isEmpty(url)) {
        showShortToast("url?????");
        setResult(RESULT_CANCELED);
        finish();
        return;
      }
      String [] urls = url.split(",");
      if(urls.length < 1) {
        showShortToast("url??????");
        setResult(RESULT_CANCELED);
        finish();
        return;
      }
      mVideoUrl = urls[0];
      if(urls.length > 1) {
        mThumbUrl = urls[1];
      }
    } else {
      showShortToast("url?????");
      setResult(RESULT_CANCELED);
      finish();
    }
    if(hasExtra("yuninfo_msg")) {
      mMsg = getStringExtra("yuninfo_msg");
    } else {
      
    }
    if(hasExtra("yuninfo_state")) {
      mHasGetCoin = getBooleanExtra("yuninfo_state");
    } else {
      
    }
  }
  
  @SuppressLint("NewApi")
  private void initView() {
    initTitle();
    
    mTvMsg = (TextView) findViewById(mResources.getIdentifier("yuninfo_tv_card_msg", "id", mPackageName));
    mBtnOpenVideo = (Button) findViewById(mResources.getIdentifier("yuninfo_btn_see_video", "id", mPackageName));
    mIvVideoPlay = (ImageView) findViewById(mResources.getIdentifier("yuninfo_iv_video_view_icon", "id", mPackageName));
    mIvVideoThumb = (ImageView) findViewById(mResources.getIdentifier("yuninfo_iv_video_thumb", "id", mPackageName));
    mEtMsgFeedBack = (EditText) findViewById(mResources.getIdentifier("yuninfo_et_card_feedbak_msg", "id", mPackageName));
    mTvMsg.setText(mMsg);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
      Bitmap bm = ThumbnailUtils.createVideoThumbnail(mVideoUrl, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
      mIvVideoThumb.setImageBitmap(bm);
    }
    mIvVideoPlay.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        playVideo(mVideoUrl);
      }
    });
    if(mHasGetCoin) {
      mBtnOpenVideo.setText("???????");
    } else {
      mBtnOpenVideo.setText("????");
    }
    mBtnOpenVideo.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        playVideo(mVideoUrl);
//        playVideo("/sdcard/Video_2014_01_04_213238.mp4");
      }
    });
    
    ImageCacheParams cacheParams = new ImageCache.ImageCacheParams("video_thumb");
    cacheParams.compressFormat = CompressFormat.JPEG;
    cacheParams.memCacheSize = 1024 * 1024 * Utils.getMemoryClass(this) / 3;
    ImageCache imageCache = new ImageCache(this, cacheParams);
    mImageWorker = new ImageFetcher(this, mResources.getDisplayMetrics().widthPixels);
    mImageWorker.setImageCache(imageCache);
    mImageWorker.setImageFadeIn(true);
    mImageWorker.setLoadFailedImage(null);
    if(!StringUtil.isEmpty(mThumbUrl)) {
      mImageWorker.loadImage(mThumbUrl, mIvVideoThumb);
    }
  }
  
  private void initTitle() {
    mTitleBar = (TitleBar) findViewById(mResources.getIdentifier("yuninfo_tb_read_card", "id", mPackageName));
    mTitleBar.setLeftButton(mResources.getIdentifier("yuninfo_ic_title_back", "drawable", mPackageName), new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        setResult(RESULT_CANCELED);
        finish();
      }
    });
    mTitleBar.setTitleText("????????");
  }
  
  private void playVideo(String url) {
    Intent intent = new Intent(this, VideoPlayerActivity.class);
    intent.putExtra(Config.YUNINFO_EXTRA_URL, url);
        startActivityForResult(intent, Config.YUNINFO_REQUEST_CODE_PLAY_VIDEO);  
  }
  
  private void getCoin() {
    String msg = mEtMsgFeedBack.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 + "&msgId=" + mMsgId + "&url=" 
        + mVideoUrl + "&msg=" + msg;
    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 ReadVideoTask().execute(req);
  }
  
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == Config.YUNINFO_REQUEST_CODE_PLAY_VIDEO) {
      if(resultCode == RESULT_OK) {
        if(mHasGetCoin) {
          return;
        }
        mEtMsgFeedBack.setVisibility(View.VISIBLE);
        mBtnOpenVideo.setText("??????????");
        mBtnOpenVideo.setOnClickListener(new View.OnClickListener() {
          
          @Override
          public void onClick(View v) {
            getCoin();
          }
        });
      } else {
        showShortToast("??????????????????????????");
      }
    }
  }
  
  
  private class ReadVideoTask extends AsyncTask<CommonReq, Long, ReadVideoResp> {

    private ProgressDialog mmProgressDialog;
    
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      mmProgressDialog = ProgressDialog.show(ReadCardActivity.this, null, "??????????,????...");
      mmProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        
        @Override
        public void onCancel(DialogInterface dialog) {
          ReadVideoTask.this.cancel(true);
        }
      });
    }
    
    @Override
    protected ReadVideoResp doInBackground(CommonReq... params) {
      try {
        HttpResponse resp = HttpUtil.doPost(Config.YUNINFO_READ_VIDEO_URL, null, params[0]);
        if(HttpUtil.getStatusCode(resp) == HttpUtil.OK) {
          ReadVideoResp result = new Gson().fromJson(EntityUtils.toString(resp.getEntity(), "UTF-8"), ReadVideoResp.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(ReadVideoResp result) {
      super.onPostExecute(result);
      if(result == null) {
        showShortToast("??????");
      } else {
        if(result.getResult() == 0) {
          LogUtil.i(TAG, result);
          showShortToast("?????????????????????????????");
          mmProgressDialog.cancel();
          Intent data = new Intent();
          data.putExtra(Config.YUNINFO_RESULT_DATA, true);
          setResult(RESULT_OK, data);
          finish();
          return;
        } else {
          showShortToast(result.getMsg());
        }
      }
      mmProgressDialog.cancel();
    }
    
  }
}




Java Source Code List

com.example.androidtest.MainActivity.java
com.yuninfo.videoextand.BaseActivity.java
com.yuninfo.videoextand.Config.java
com.yuninfo.videoextand.ReadCardActivity.java
com.yuninfo.videoextand.SendCardActivity.java
com.yuninfo.videoextand.bean.CommonReq.java
com.yuninfo.videoextand.bean.ReadVideoResp.java
com.yuninfo.videoextand.bean.SendVideoResp.java
com.yuninfo.videoextand.bean.UploadReq.java
com.yuninfo.videoextand.bean.UploadResp.java
com.yuninfo.videoextand.player.VideoPlayerActivity.java
com.yuninfo.videoextand.recorder.RecorderActivity.java
com.yuninfo.videoextand.uploader.ProgressListener.java
com.yuninfo.videoextand.uploader.ProgressMultipartEntity.java
com.yuninfo.videoextand.uploader.ProgressOutputStream.java
com.yuninfo.videoextand.uploader.UploadHandler.java
com.yuninfo.videoextand.uploader.UploadThread.java
com.yuninfo.videoextand.utils.BeanRefUtil.java
com.yuninfo.videoextand.utils.DateUtil.java
com.yuninfo.videoextand.utils.FileUtil.java
com.yuninfo.videoextand.utils.HttpUtil.java
com.yuninfo.videoextand.utils.LogUtil.java
com.yuninfo.videoextand.utils.StringUtil.java
com.yuninfo.videoextand.widget.CustomPopupWindow.java
com.yuninfo.videoextand.widget.TitleBar.java
com.yuninfo.videoextand.widget.dialog.MenuDialog.java