Proyectos de Subversion Android Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.inconcert.activity;
2
 
3
import androidx.appcompat.app.AppCompatActivity;
4
 
5
import android.content.Intent;
6
import android.os.Bundle;
7
import android.util.Log;
8
import android.view.View;
9
import android.widget.ProgressBar;
10
 
11
import com.cesams.twogetskills.inconcert.Constants;
12
import com.cesams.twogetskills.inconcert.R;
13
import com.cesams.twogetskills.inconcert.library.Functions;
14
import com.cesams.twogetskills.inconcert.library.Http;
15
import com.cesams.twogetskills.inconcert.library.MD5;
16
import com.github.barteksc.pdfviewer.PDFView;
17
 
18
import java.io.File;
19
import java.io.FileOutputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.util.Calendar;
23
import java.util.Random;
24
import java.util.TimeZone;
25
 
26
import okhttp3.Call;
27
import okhttp3.Callback;
28
import okhttp3.OkHttpClient;
29
import okhttp3.Request;
30
import okhttp3.Response;
31
 
32
public class PdfActivity extends AppCompatActivity  {
33
    private final static String TAG = "C2GS - PdfActivity";
34
    private static final int BUFFER_SIZE = 4096;
35
    private File mOutputFile;
36
    private PDFView mPdfView;
37
    private ProgressBar mProgressBar;
38
    private String documentUrl;
39
    private String deviceId;
40
    private String password;
41
 
42
    @Override
43
    protected void onCreate(Bundle savedInstanceState) {
44
        super.onCreate(savedInstanceState);
45
        setContentView(R.layout.activity_pdf);
46
 
47
        documentUrl = getIntent().getStringExtra("documentUrl");
48
        deviceId = getIntent().getStringExtra("deviceId");
49
        password = getIntent().getStringExtra("password");
50
 
51
        mPdfView = (PDFView) findViewById(R.id.activity_pdf_document);
52
        mProgressBar = (ProgressBar ) findViewById(R.id.activity_pdf_progress_bar);
53
 
54
        try {
55
            TimeZone timeZone = TimeZone.getTimeZone("UTC");
56
            Calendar calendar = Calendar.getInstance(timeZone);
57
            TimeZone tz = calendar.getTimeZone();
58
            int created =  (int) (calendar.getTimeInMillis() / 1000);
59
 
60
            Random random = new Random(created);
61
            int rand = 1000 + random.nextInt(8999);
62
 
63
 
64
            Log.d(TAG, "token = " + deviceId);
65
            Log.d(TAG, "created = " + created);
66
            Log.d(TAG, "rand = " + rand);
67
            Log.d(TAG, "calc = " + password + ':' +  created + ':' + rand);
68
 
69
            String secret = MD5.generar(password + ':' +  created + ':' + rand);
70
 
71
            Log.d(TAG, "secret = " + secret);
72
 
73
            Http http = new Http(getCacheDir(), deviceId, secret, created, rand);
74
            OkHttpClient client = http.getHttpClient(false);
75
 
76
            Log.d(TAG, "URL = " + documentUrl);
77
            Request request = new Request.Builder()
78
                    .url(documentUrl)
79
                    .build();
80
 
81
            Call call = client.newCall(request);
82
            call.enqueue(new Callback() {
83
                public void onResponse(Call call, Response response)
84
                        throws IOException {
85
 
86
                    File outputDir = getApplication().getCacheDir(); // context being the Activity pointer
87
                    mOutputFile = File.createTempFile("2getskills", ".pdf", outputDir);
88
 
89
                    mProgressBar.setVisibility(View.VISIBLE);
90
 
91
 
92
                    String fileName;
93
                    String disposition = response.header("Content-Disposition");
94
                    String contentType = response.header("Content-Type");
95
                    int contentLength = Functions.Numero2Int(response.header("Content-Length"));
96
 
97
                    System.out.println("Content-Type = " + contentType);
98
                    System.out.println("Content-Disposition = " + disposition);
99
                    System.out.println("Content-Length = " + contentLength);
100
 
101
 
102
 
103
 
104
                    // opens input stream from the HTTP connection
105
                    InputStream inputStream = response.body().byteStream();
106
 
107
                    // opens an output stream to save into file
108
                    FileOutputStream outputStream = new FileOutputStream(mOutputFile);
109
 
110
                    int bytesRead = -1;
111
                    byte[] buffer = new byte[BUFFER_SIZE];
112
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
113
                        outputStream.write(buffer, 0, bytesRead);
114
 
115
                        Log.d(TAG, "bytesRead = " + bytesRead);
116
                    }
117
 
118
                    outputStream.close();
119
                    inputStream.close();
120
 
121
 
122
                    mPdfView.fromFile(mOutputFile)
123
                            .enableSwipe(true) // allows to block changing pages using swipe
124
                            .swipeHorizontal(false)
125
                            .enableDoubletap(true)
126
                            .defaultPage(0)
127
                            // allows to draw something on the current page, usually visible in the middle of the screen
128
 
129
                            .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
130
                            .password(null)
131
                            .scrollHandle(null)
132
                            .enableAntialiasing(true) // improve rendering a little bit on low-res screens
133
                            // spacing between pages in dp. To define spacing color, set view background
134
                            .spacing(0)
135
                            .load();
136
 
137
                }
138
 
139
                public void onFailure(Call call, IOException e) {
140
                    Log.d(TAG, "Error :  " +  e.getMessage());
141
                }
142
            });
143
 
144
            File outputDir = getApplication().getCacheDir(); // context being the Activity pointer
145
            mOutputFile = File.createTempFile("2getskills", ".pdf", outputDir);
146
 
147
            mProgressBar.setVisibility(View.VISIBLE);
148
 
149
        } catch(Exception e) {
150
            mOutputFile = null;
151
        }
152
    }
153
 
154
 
155
    @Override
156
    public void onBackPressed() {
157
        if(mOutputFile != null) {
158
            if(mOutputFile.exists()) {
159
                mOutputFile.delete();
160
                mOutputFile = null;
161
            }
162
        }
163
 
164
        Intent intent = new Intent();
165
        intent.putExtra("completed", true);
166
        intent.putExtra("requestCode", Constants.REQUEST_CODE_PDF);
167
        setResult(RESULT_OK, intent);
168
        finish();
169
    }
170
 
171
    @Override
172
    protected void onDestroy() {
173
        super.onDestroy();
174
 
175
        if(mOutputFile != null) {
176
            if(mOutputFile.exists()) {
177
                mOutputFile.delete();
178
            }
179
        }
180
    }
181
 
182
    @Override
183
    protected void onPause() {
184
        super.onPause();
185
 
186
        finish();
187
 
188
    }
189
}