forked from voghDev/PdfViewPager
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLegacyPDFActivity.java
104 lines (89 loc) · 3.41 KB
/
LegacyPDFActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (C) 2016 Olmo Gallegos Hernández.
*
* 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.
*/
package es.voghdev.pdfviewpager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.github.barteksc.pdfviewpager.remote.DownloadFile;
import com.github.barteksc.pdfviewpager.remote.DownloadFileUrlConnectionImpl;
import java.io.File;
/**
* Sample Activity for API Levels under 21
*/
public class LegacyPDFActivity extends AppCompatActivity implements DownloadFile.Listener{
Button button;
ProgressBar progressBar;
TextView textView;
DownloadFile downloadFile;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle(R.string.legacy_pdf);
setContentView(R.layout.activity_legacy);
bindViews();
downloadFile = new DownloadFileUrlConnectionImpl(this, new Handler(), this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setVisibility(View.INVISIBLE);
downloadFile.download(getString(R.string.sample_pdf_url), new File(getExternalFilesDir("pdf"), "legacy.pdf").getAbsolutePath());
textView.setText(R.string.downloading);
}
});
}
private void bindViews() {
button = (Button) findViewById(R.id.btn_download);
textView = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
public static void open(Context context){
Intent i = new Intent(context, LegacyPDFActivity.class);
context.startActivity(i);
}
private void launchOpenPDFIntent(String destinationPath) {
File file = new File(destinationPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
@Override
public void onSuccess(String url, String destinationPath) {
button.setVisibility(View.VISIBLE);
progressBar.setProgress(100);
textView.setText(R.string.downloaded_wait);
launchOpenPDFIntent(destinationPath);
}
@Override
public void onFailure(Exception e) {
button.setVisibility(View.INVISIBLE);
progressBar.setProgress(0);
textView.setText( String.format("Download error: %s", e.getMessage()) );
}
@Override
public void onProgressUpdate(int progress, int total) {
if(progressBar.getMax() != total )
progressBar.setMax(total);
progressBar.setProgress(progress);
}
}