Proyectos de Subversion Android Microlearning

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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