forked from tus/tus-android-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
207 lines (171 loc) · 6.39 KB
/
MainActivity.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package io.tus.android.example;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.ContentLoadingProgressBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.material.button.MaterialButton;
import java.net.URL;
import io.tus.android.client.TusAndroidUpload;
import io.tus.android.client.TusPreferencesURLStore;
import io.tus.java.client.TusClient;
import io.tus.java.client.TusUpload;
import io.tus.java.client.TusUploader;
public class MainActivity extends AppCompatActivity {
private final int REQUEST_FILE_SELECT = 1;
private TusClient client;
private TextView status;
private MaterialButton pauseButton;
private MaterialButton resumeButton;
private UploadTask uploadTask;
private ContentLoadingProgressBar progressBar;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SharedPreferences pref = getSharedPreferences("tus", 0);
client = new TusClient();
client.setUploadCreationURL(new URL("https://tusd.tusdemo.net/files/"));
client.enableResuming(new TusPreferencesURLStore(pref));
} catch (Exception e) {
showError(e);
}
status = (TextView) findViewById(R.id.status);
progressBar = (ContentLoadingProgressBar) findViewById(R.id.progressBar);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file to upload"), REQUEST_FILE_SELECT);
}
});
pauseButton = (MaterialButton) findViewById(R.id.pause_button);
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pauseUpload();
}
});
resumeButton = (MaterialButton) findViewById(R.id.resume_button);
resumeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resumeUpload();
}
});
}
private void beginUpload(Uri uri) {
fileUri = uri;
resumeUpload();
}
public void setPauseButtonEnabled(boolean enabled) {
pauseButton.setEnabled(enabled);
resumeButton.setEnabled(!enabled);
}
public void pauseUpload() {
uploadTask.cancel(false);
}
public void resumeUpload() {
try {
TusUpload upload = new TusAndroidUpload(fileUri, this);
uploadTask = new UploadTask(this, client, upload);
uploadTask.execute(new Void[0]);
} catch (Exception e) {
showError(e);
}
}
private void setStatus(String text) {
status.setText(text);
}
private void setUploadProgress(int progress) {
progressBar.setProgress(progress);
}
private class UploadTask extends AsyncTask<Void, Long, URL> {
private MainActivity activity;
private TusClient client;
private TusUpload upload;
private Exception exception;
public UploadTask(MainActivity activity, TusClient client, TusUpload upload) {
this.activity = activity;
this.client = client;
this.upload = upload;
}
@Override
protected void onPreExecute() {
activity.setStatus("Upload selected...");
activity.setPauseButtonEnabled(true);
activity.setUploadProgress(0);
}
@Override
protected void onPostExecute(URL uploadURL) {
activity.setStatus("Upload finished!\n" + uploadURL.toString());
activity.setPauseButtonEnabled(false);
}
@Override
protected void onCancelled() {
if (exception != null) {
activity.showError(exception);
}
activity.setPauseButtonEnabled(false);
}
@Override
protected void onProgressUpdate(Long... updates) {
long uploadedBytes = updates[0];
long totalBytes = updates[1];
activity.setStatus("Uploaded " + (int) ((double) uploadedBytes / totalBytes * 100) + "% | " + String.format("%d/%d.", uploadedBytes, totalBytes));
activity.setUploadProgress((int) ((double) uploadedBytes / totalBytes * 100));
}
@Override
protected URL doInBackground(Void... params) {
try {
TusUploader uploader = client.resumeOrCreateUpload(upload);
long totalBytes = upload.getSize();
long uploadedBytes = uploader.getOffset();
// Upload file in 1MiB chunks
uploader.setChunkSize(1024 * 1024);
while (!isCancelled() && uploader.uploadChunk() > 0) {
uploadedBytes = uploader.getOffset();
publishProgress(uploadedBytes, totalBytes);
}
uploader.finish();
return uploader.getUploadURL();
} catch (Exception e) {
exception = e;
cancel(true);
}
return null;
}
}
private void showError(Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Internal error");
builder.setMessage(e.getMessage());
AlertDialog dialog = builder.create();
dialog.show();
e.printStackTrace();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == REQUEST_FILE_SELECT) {
Uri uri = data.getData();
beginUpload(uri);
}
}
}