Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
package com.cesams.twogetskills.activity;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ProgressBar;import com.cesams.twogetskills.Configuration;import com.cesams.twogetskills.Constants;import com.cesams.twogetskills.R;import com.cesams.twogetskills.library.Functions;import com.cesams.twogetskills.library.Http;import com.cesams.twogetskills.library.MD5;import com.github.barteksc.pdfviewer.PDFView;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.Arrays;import java.util.Calendar;import java.util.Random;import java.util.TimeZone;import okhttp3.Cache;import okhttp3.Call;import okhttp3.Callback;import okhttp3.ConnectionSpec;import okhttp3.Interceptor;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;import okhttp3.logging.HttpLoggingInterceptor;public class PdfActivity extends AppCompatActivity {private final static String TAG = "C2GS - PdfActivity";private static final int BUFFER_SIZE = 4096;private File mOutputFile;private PDFView mPdfView;private ProgressBar mProgressBar;private String documentUrl;private String deviceId;private String password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pdf);documentUrl = getIntent().getStringExtra("documentUrl");deviceId = getIntent().getStringExtra("deviceId");password = getIntent().getStringExtra("password");mPdfView = (PDFView) findViewById(R.id.activity_pdf_document);mProgressBar = (ProgressBar ) findViewById(R.id.activity_pdf_progress_bar);try {TimeZone timeZone = TimeZone.getTimeZone("UTC");Calendar calendar = Calendar.getInstance(timeZone);TimeZone tz = calendar.getTimeZone();int created = (int) (calendar.getTimeInMillis() / 1000);Random random = new Random(created);int rand = 1000 + random.nextInt(8999);Log.d(TAG, "token = " + deviceId);Log.d(TAG, "created = " + created);Log.d(TAG, "rand = " + rand);Log.d(TAG, "calc = " + password + ':' + created + ':' + rand);String secret = MD5.generar(password + ':' + created + ':' + rand);Log.d(TAG, "secret = " + secret);Http http = new Http(getCacheDir(), deviceId, secret, created, rand);OkHttpClient client = http.getHttpClient(false);Log.d(TAG, "URL = " + documentUrl);Request request = new Request.Builder().url(documentUrl).build();Call call = client.newCall(request);call.enqueue(new Callback() {public void onResponse(Call call, Response response)throws IOException {File outputDir = getApplication().getCacheDir(); // context being the Activity pointermOutputFile = File.createTempFile("2getskills", ".pdf", outputDir);mProgressBar.setVisibility(View.VISIBLE);String fileName;String disposition = response.header("Content-Disposition");String contentType = response.header("Content-Type");int contentLength = Functions.Numero2Int(response.header("Content-Length"));System.out.println("Content-Type = " + contentType);System.out.println("Content-Disposition = " + disposition);System.out.println("Content-Length = " + contentLength);// opens input stream from the HTTP connectionInputStream inputStream = response.body().byteStream();// opens an output stream to save into fileFileOutputStream outputStream = new FileOutputStream(mOutputFile);int bytesRead = -1;byte[] buffer = new byte[BUFFER_SIZE];while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);Log.d(TAG, "bytesRead = " + bytesRead);}outputStream.close();inputStream.close();mPdfView.fromFile(mOutputFile).enableSwipe(true) // allows to block changing pages using swipe.swipeHorizontal(false).enableDoubletap(true).defaultPage(0)// allows to draw something on the current page, usually visible in the middle of the screen.enableAnnotationRendering(false) // render annotations (such as comments, colors or forms).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(0).load();}public void onFailure(Call call, IOException e) {Log.d(TAG, "Error : " + e.getMessage());}});File outputDir = getApplication().getCacheDir(); // context being the Activity pointermOutputFile = File.createTempFile("2getskills", ".pdf", outputDir);mProgressBar.setVisibility(View.VISIBLE);} catch(Exception e) {mOutputFile = null;}}@Overridepublic void onBackPressed() {if(mOutputFile != null) {if(mOutputFile.exists()) {mOutputFile.delete();mOutputFile = null;}}Intent intent = new Intent();intent.putExtra("completed", true);intent.putExtra("requestCode", Constants.REQUEST_CODE_PDF);setResult(RESULT_OK, intent);finish();}@Overrideprotected void onDestroy() {super.onDestroy();if(mOutputFile != null) {if(mOutputFile.exists()) {mOutputFile.delete();}}}}