|
|
@@ -5,6 +5,7 @@ import android.graphics.Canvas;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
|
import android.os.Message;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.LinearLayout;
|
|
|
@@ -12,7 +13,10 @@ import android.widget.ProgressBar;
|
|
|
import android.widget.RelativeLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.core.net.ProgressDownloader;
|
|
|
+import com.core.net.ProgressResponseBody;
|
|
|
import com.core.utils.CommonUtil;
|
|
|
+import com.core.utils.ToastUtil;
|
|
|
import com.github.barteksc.pdfviewer.PDFView;
|
|
|
import com.github.barteksc.pdfviewer.listener.OnDrawListener;
|
|
|
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
|
|
|
@@ -23,22 +27,39 @@ import com.github.barteksc.pdfviewer.listener.OnRenderListener;
|
|
|
import com.xzjmyk.pm.activity.R;
|
|
|
import com.xzjmyk.pm.activity.ui.base.BaseActivity;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.activity.datainquiry.Constants;
|
|
|
-import com.xzjmyk.pm.activity.ui.erp.activity.datainquiry.PDFUtils;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.net.ViewUtil;
|
|
|
+
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static com.xzjmyk.pm.activity.ui.erp.activity.datainquiry.Constants.CONSTANT.PDF_FILE_NAME;
|
|
|
+import static com.xzjmyk.pm.activity.ui.erp.activity.datainquiry.Constants.CONSTANT.PDF_FILE_PATH;
|
|
|
|
|
|
/**
|
|
|
* Created by RaoMeng on 2017/8/17.
|
|
|
* 报表统计PDF文件下载并展示页面
|
|
|
*/
|
|
|
-public class PDFDownloadActivity extends BaseActivity implements View.OnClickListener, OnDrawListener, OnLoadCompleteListener, OnPageChangeListener, OnPageScrollListener, OnErrorListener, OnRenderListener {
|
|
|
+public class PDFDownloadActivity extends BaseActivity implements View.OnClickListener, OnDrawListener, OnLoadCompleteListener, OnPageChangeListener, OnPageScrollListener, OnErrorListener, OnRenderListener, ProgressResponseBody.ProgressListener {
|
|
|
+ private final int OBATIN_PDF_PATH = 100;
|
|
|
+
|
|
|
private PDFView mPDFView;
|
|
|
- private TextView mStateTextView, mRemainTextView, mReloadTextView, mExitTextView;
|
|
|
+ private TextView mStateTextView, mRemainTextView, mReloadTextView, mExitTextView, mPauseTextView;
|
|
|
private ProgressBar mDownloadProgressBar;
|
|
|
private RelativeLayout mDownloadRelativeLayout;
|
|
|
private LinearLayout mErrorLinearLayout;
|
|
|
private String downloadUrl = "", mReportName = "";
|
|
|
private String mCondition, mTitle;
|
|
|
+ private ProgressDownloader mDownloader;
|
|
|
+ private long breakPoints;
|
|
|
+ private File file;
|
|
|
+ private long contentLength;
|
|
|
+ private long totalBytes;
|
|
|
|
|
|
private Handler mHandler = new Handler() {
|
|
|
@Override
|
|
|
@@ -47,17 +68,18 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
case Constants.CONSTANT.DOWNLOAD_PROGRESS:
|
|
|
try {
|
|
|
int progress = (int) msg.obj;
|
|
|
- Log.d("progress", progress + "");
|
|
|
+ Log.d("progress", progress + ":" + contentLength);
|
|
|
mStateTextView.setText(R.string.str_loading);
|
|
|
mDownloadProgressBar.setProgress(progress);
|
|
|
- mRemainTextView.setText((100 - progress) + "");
|
|
|
+ mRemainTextView.setText((100 - (((progress * 1024 * 100) / contentLength))) + "");
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
}
|
|
|
break;
|
|
|
case Constants.CONSTANT.DOWNLOAD_SUCCESS:
|
|
|
+ mPauseTextView.setVisibility(View.GONE);
|
|
|
mStateTextView.setText("报表文件下载成功!\n正在加载....");
|
|
|
- mDownloadProgressBar.setProgress(100);
|
|
|
+// mDownloadProgressBar.setProgress(100);
|
|
|
mRemainTextView.setText("0");
|
|
|
/*mDownloadRelativeLayout.postDelayed(new Runnable() {
|
|
|
@Override
|
|
|
@@ -65,15 +87,21 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
mDownloadRelativeLayout.setVisibility(View.GONE);
|
|
|
}
|
|
|
}, 1000);*/
|
|
|
- displayFromFile(new File(Constants.CONSTANT.PDF_FILE_PATH, Constants.CONSTANT.PDF_FILE_NAME));
|
|
|
+ displayFromFile(new File(PDF_FILE_PATH, PDF_FILE_NAME));
|
|
|
break;
|
|
|
case Constants.CONSTANT.PDF_OVERLOAD:
|
|
|
+ mPauseTextView.setVisibility(View.GONE);
|
|
|
mErrorLinearLayout.setVisibility(View.VISIBLE);
|
|
|
mStateTextView.setText("数据量过载,报表文件获取失败!");
|
|
|
mDownloadProgressBar.setProgress(0);
|
|
|
mRemainTextView.setText(100 + "");
|
|
|
break;
|
|
|
+ case OBATIN_PDF_PATH:
|
|
|
+ String result = msg.getData().getString("result");
|
|
|
+ analysisPdfPath(result);
|
|
|
+ break;
|
|
|
case Constants.CONSTANT.DOWNLOAD_FAILED:
|
|
|
+ mPauseTextView.setVisibility(View.GONE);
|
|
|
mErrorLinearLayout.setVisibility(View.VISIBLE);
|
|
|
String info = "";
|
|
|
if (msg.obj != null) {
|
|
|
@@ -83,10 +111,67 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
mDownloadProgressBar.setProgress(0);
|
|
|
mRemainTextView.setText(100 + "");
|
|
|
break;
|
|
|
+ case com.core.app.Constants.APP_SOCKETIMEOUTEXCEPTION:
|
|
|
+ result = msg.getData().getString("response");
|
|
|
+ analysisPdfPath(result);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ private void analysisPdfPath(String result) {
|
|
|
+ if (result != null) {
|
|
|
+ try {
|
|
|
+ JSONObject responseObject = new JSONObject(result);
|
|
|
+ if (!responseObject.isNull("success") && !responseObject.optBoolean("success")) {
|
|
|
+ Message message = Message.obtain();
|
|
|
+ message.what = Constants.CONSTANT.DOWNLOAD_FAILED;
|
|
|
+ message.obj = responseObject.optString("message");
|
|
|
+ mHandler.sendMessage(message);
|
|
|
+ } else {
|
|
|
+ boolean overload = responseObject.optBoolean("overload");
|
|
|
+ int pageSize = -1;
|
|
|
+ if (!responseObject.isNull("pageSize")) {
|
|
|
+ pageSize = responseObject.optInt("pageSize");
|
|
|
+ }
|
|
|
+ if (overload) {
|
|
|
+ mHandler.sendEmptyMessage(Constants.CONSTANT.PDF_OVERLOAD);
|
|
|
+ } else if (pageSize == 0) {
|
|
|
+ Message message = Message.obtain();
|
|
|
+ message.what = Constants.CONSTANT.DOWNLOAD_FAILED;
|
|
|
+ message.obj = "报表文件内容为空";
|
|
|
+ mHandler.sendMessage(message);
|
|
|
+ } else {
|
|
|
+ downloadUrl = responseObject.optString("path");
|
|
|
+ if (!TextUtils.isEmpty(downloadUrl)) {
|
|
|
+ downloadUrl = "http://print.ubtob.com/report/" + downloadUrl;
|
|
|
+ mPauseTextView.setVisibility(View.VISIBLE);
|
|
|
+ breakPoints = 0L;
|
|
|
+ File directory = new File(PDF_FILE_PATH);
|
|
|
+ if (!directory.exists() && !directory.isDirectory()) {
|
|
|
+ boolean mkdirs = directory.mkdirs();
|
|
|
+
|
|
|
+ } else {
|
|
|
+ CommonUtil.delAllFile(PDF_FILE_PATH);
|
|
|
+ }
|
|
|
+ file = new File(PDF_FILE_PATH, PDF_FILE_NAME);
|
|
|
+ try {
|
|
|
+ file.createNewFile();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ mDownloader = new ProgressDownloader(downloadUrl, file, PDFDownloadActivity.this);
|
|
|
+ mDownloader.download(0L);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void displayFromFile(File fileName) {
|
|
|
try {
|
|
|
mPDFView.fromFile(fileName)
|
|
|
@@ -95,9 +180,9 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
// .swipeHorizontal(true)
|
|
|
.enableDoubletap(true)
|
|
|
.defaultPage(0)
|
|
|
- // allows to draw something on the current page, usually visible in the middle of the screen
|
|
|
+ // allows to draw something on the current page, usually visible in the middle of the screen
|
|
|
.onDraw(this)
|
|
|
- // allows to draw something on all pages, separately for every page. Called only for visible pages
|
|
|
+ // allows to draw something on all pages, separately for every page. Called only for visible pages
|
|
|
.onDrawAll(this)
|
|
|
.onLoad(this) // called after document is loaded and starts to be rendered
|
|
|
.onPageChange(this)
|
|
|
@@ -108,10 +193,11 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
.password(null)
|
|
|
.scrollHandle(null)
|
|
|
.enableAntialiasing(true) // improve rendering a little bit on low-res screens
|
|
|
- // spacing between pages in dp. To define spacing color, set view background
|
|
|
+ // spacing between pages in dp. To define spacing color, set view background
|
|
|
.spacing(10)
|
|
|
.load();
|
|
|
} catch (Exception e) {
|
|
|
+ mPauseTextView.setVisibility(View.GONE);
|
|
|
mErrorLinearLayout.setVisibility(View.VISIBLE);
|
|
|
mStateTextView.setText("报表文件加载失败!");
|
|
|
mDownloadProgressBar.setProgress(0);
|
|
|
@@ -126,11 +212,20 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
|
|
|
initViews();
|
|
|
initEvents();
|
|
|
- downloadPDF();
|
|
|
+ obtainPdfPath();
|
|
|
}
|
|
|
|
|
|
- private void downloadPDF() {
|
|
|
- PDFUtils.downloadPDF(downloadUrl, mHandler);
|
|
|
+ private void obtainPdfPath() {
|
|
|
+ String url = "http://print.ubtob.com/report/pdf/path";
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("u", CommonUtil.getSharedPreferences(ct, "erp_master"));
|
|
|
+ params.put("pf", "phone");
|
|
|
+ params.put("r", mReportName);
|
|
|
+ params.put("w", "where " + mCondition);
|
|
|
+ LinkedHashMap<String, Object> headers = new LinkedHashMap<>();
|
|
|
+ headers.put("Cookie", "JSESSIONID=" + CommonUtil.getSharedPreferences(ct, "sessionId"));
|
|
|
+ ViewUtil.httpSendRequest(this, url, params, mHandler, headers, OBATIN_PDF_PATH, null, null, "get");
|
|
|
+// PDFUtils.downloadPDF(downloadUrl, mHandler);
|
|
|
}
|
|
|
|
|
|
private void initViews() {
|
|
|
@@ -144,15 +239,8 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
if (mTitle != null)
|
|
|
getSupportActionBar().setTitle(mTitle);
|
|
|
|
|
|
-// downloadUrl = com.xzjmyk.pm.activity.ui.erp.util.Constants.getAppBaseUrl(this)
|
|
|
-// + "report/pdf/data?u=" + CommonUtil.getSharedPreferences(ct, "erp_master")
|
|
|
-// + "&pf=phone&r=" + mReportName + "&w=" + mCondition;
|
|
|
-// downloadUrl = "http://192.168.253.60:8090/report/pdf/data?u=" + CommonUtil.getSharedPreferences(ct, "erp_master")
|
|
|
+// downloadUrl = "http://print.ubtob.com/report/pdf/data?u=" + CommonUtil.getSharedPreferences(ct, "erp_master")
|
|
|
// + "&pf=phone&r=" + mReportName + "&w=where " + mCondition;
|
|
|
- downloadUrl = "http://print.ubtob.com/report/pdf/data?u=" + CommonUtil.getSharedPreferences(ct, "erp_master")
|
|
|
- + "&pf=phone&r=" + mReportName + "&w=where " + mCondition;
|
|
|
-
|
|
|
- Log.d("downloadUrl", downloadUrl);
|
|
|
|
|
|
mPDFView = (PDFView) findViewById(R.id.pdf_download_pdfview);
|
|
|
mPDFView.useBestQuality(true);
|
|
|
@@ -164,12 +252,14 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
mReloadTextView = (TextView) findViewById(R.id.pdf_download_reload_tv);
|
|
|
mExitTextView = (TextView) findViewById(R.id.pdf_download_exit_tv);
|
|
|
mErrorLinearLayout = (LinearLayout) findViewById(R.id.pdf_download_error_menu_ll);
|
|
|
+ mPauseTextView = (TextView) findViewById(R.id.pdf_download_pause_tv);
|
|
|
}
|
|
|
|
|
|
private void initEvents() {
|
|
|
mExitTextView.setOnClickListener(this);
|
|
|
mReloadTextView.setOnClickListener(this);
|
|
|
mDownloadRelativeLayout.setOnClickListener(this);
|
|
|
+ mPauseTextView.setOnClickListener(this);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -181,7 +271,21 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
case R.id.pdf_download_reload_tv:
|
|
|
mStateTextView.setText(R.string.str_loading);
|
|
|
mErrorLinearLayout.setVisibility(View.GONE);
|
|
|
- PDFUtils.downloadPDF(downloadUrl, mHandler);
|
|
|
+// PDFUtils.downloadPDF(downloadUrl, mHandler);
|
|
|
+ obtainPdfPath();
|
|
|
+ break;
|
|
|
+ case R.id.pdf_download_pause_tv:
|
|
|
+ if (getString(R.string.pause_download).equals(mPauseTextView.getText().toString())) {
|
|
|
+ mPauseTextView.setText(R.string.continue_download);
|
|
|
+ mDownloader.pause();
|
|
|
+ ToastUtil.showToast(this, "下载暂停");
|
|
|
+ // 存储此时的totalBytes,即断点位置。
|
|
|
+ breakPoints = totalBytes;
|
|
|
+ } else if (getString(R.string.continue_download).equals(mPauseTextView.getText().toString())) {
|
|
|
+ mPauseTextView.setText(R.string.pause_download);
|
|
|
+ ToastUtil.showToast(this, "下载继续");
|
|
|
+ mDownloader.download(breakPoints);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -214,6 +318,7 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
|
|
|
@Override
|
|
|
public void onError(Throwable t) {
|
|
|
+ mPauseTextView.setVisibility(View.GONE);
|
|
|
mErrorLinearLayout.setVisibility(View.VISIBLE);
|
|
|
mStateTextView.setText("报表文件加载失败!");
|
|
|
mDownloadProgressBar.setProgress(0);
|
|
|
@@ -224,4 +329,27 @@ public class PDFDownloadActivity extends BaseActivity implements View.OnClickLis
|
|
|
public void onInitiallyRendered(int nbPages, float pageWidth, float pageHeight) {
|
|
|
mPDFView.fitToWidth();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPreExecute(long contentLength) {
|
|
|
+ // 文件总长只需记录一次,要注意断点续传后的contentLength只是剩余部分的长度
|
|
|
+ if (this.contentLength == 0L) {
|
|
|
+ this.contentLength = contentLength;
|
|
|
+ mDownloadProgressBar.setMax((int) (contentLength / 1024));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(long totalBytes, boolean done) {
|
|
|
+ // 注意加上断点的长度
|
|
|
+ this.totalBytes = totalBytes + breakPoints;
|
|
|
+ int progress = (int) (totalBytes + breakPoints) / 1024;
|
|
|
+ Message message = Message.obtain();
|
|
|
+ message.what = Constants.CONSTANT.DOWNLOAD_PROGRESS;
|
|
|
+ message.obj = progress;
|
|
|
+ mHandler.sendMessage(message);
|
|
|
+ if (done) {
|
|
|
+ mHandler.sendEmptyMessage(Constants.CONSTANT.DOWNLOAD_SUCCESS);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|