|
| 1 | +package net.gotev.uploadservicedemo; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.CheckBox; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.ProgressBar; |
| 13 | +import android.widget.TextView; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import net.gotev.uploadservice.BinaryUploadRequest; |
| 17 | +import net.gotev.uploadservice.MultipartUploadRequest; |
| 18 | +import net.gotev.uploadservice.ServerResponse; |
| 19 | +import net.gotev.uploadservice.UploadInfo; |
| 20 | +import net.gotev.uploadservice.UploadNotificationConfig; |
| 21 | +import net.gotev.uploadservice.UploadService; |
| 22 | +import net.gotev.uploadservice.UploadStatusDelegate; |
| 23 | +import net.gotev.uploadservice.ftp.FTPUploadRequest; |
| 24 | +import net.gotev.uploadservice.ftp.UnixPermissions; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileNotFoundException; |
| 28 | +import java.net.MalformedURLException; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Locale; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import butterknife.BindView; |
| 35 | +import butterknife.ButterKnife; |
| 36 | +import butterknife.OnClick; |
| 37 | + |
| 38 | +/** |
| 39 | + * Activity that demonstrates how to use Android Upload Service. |
| 40 | + * |
| 41 | + * @author gotev (Aleksandar Gotev) |
| 42 | + * @author mabdurrahman |
| 43 | + * |
| 44 | + */ |
| 45 | +public class MainActivity extends FilesPickerActivity implements UploadStatusDelegate { |
| 46 | + |
| 47 | + private static final String TAG = "UploadServiceDemo"; |
| 48 | + private static final String USER_AGENT = "UploadServiceDemo/" + BuildConfig.VERSION_NAME; |
| 49 | + |
| 50 | + private static final String FTP_USERNAME = "ftpuser"; |
| 51 | + private static final String FTP_PASSWORD = "testpassword"; |
| 52 | + private static final String FTP_REMOTE_BASE_PATH = "home/ftpuser/"; |
| 53 | + |
| 54 | + @BindView(R.id.container) ViewGroup container; |
| 55 | + @BindView(R.id.multipartUploadButton) Button multipartUploadButton; |
| 56 | + @BindView(R.id.binaryUploadButton) Button binaryUploadButton; |
| 57 | + @BindView(R.id.cancelAllUploadsButton) Button cancelAllUploadsButton; |
| 58 | + @BindView(R.id.serverURL) EditText serverUrl; |
| 59 | + @BindView(R.id.filesToUpload) EditText filesToUpload; |
| 60 | + @BindView(R.id.parameterName) EditText parameterName; |
| 61 | + @BindView(R.id.displayNotification) CheckBox displayNotification; |
| 62 | + @BindView(R.id.autoDeleteUploadedFiles) CheckBox autoDeleteUploadedFiles; |
| 63 | + @BindView(R.id.autoClearOnSuccess) CheckBox autoClearOnSuccess; |
| 64 | + @BindView(R.id.fixedLengthStreamingMode) CheckBox fixedLengthStreamingMode; |
| 65 | + @BindView(R.id.useUtf8) CheckBox useUtf8; |
| 66 | + |
| 67 | + private Map<String, UploadProgressViewHolder> uploadProgressHolders = new HashMap<>(); |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void onCreate(Bundle savedInstanceState) { |
| 71 | + super.onCreate(savedInstanceState); |
| 72 | + setContentView(R.layout.activity_main); |
| 73 | + } |
| 74 | + |
| 75 | + private void logSuccessfullyUploadedFiles(List<String> files) { |
| 76 | + for (String file : files) { |
| 77 | + Log.e(TAG, "Success: " + file); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void showToast(String message) { |
| 82 | + Toast.makeText(this, message, Toast.LENGTH_LONG).show(); |
| 83 | + } |
| 84 | + |
| 85 | + private UploadNotificationConfig getNotificationConfig(String filename) { |
| 86 | + if (!displayNotification.isChecked()) return null; |
| 87 | + |
| 88 | + return new UploadNotificationConfig() |
| 89 | + .setIcon(R.drawable.ic_upload) |
| 90 | + .setCompletedIcon(R.drawable.ic_upload_success) |
| 91 | + .setErrorIcon(R.drawable.ic_upload_error) |
| 92 | + .setTitle(filename) |
| 93 | + .setInProgressMessage(getString(R.string.uploading)) |
| 94 | + .setCompletedMessage(getString(R.string.upload_success)) |
| 95 | + .setErrorMessage(getString(R.string.upload_error)) |
| 96 | + .setAutoClearOnSuccess(autoClearOnSuccess.isChecked()) |
| 97 | + .setClickIntent(new Intent(this, MainActivity.class)) |
| 98 | + .setClearOnAction(true) |
| 99 | + .setRingToneEnabled(true); |
| 100 | + } |
| 101 | + |
| 102 | + private void addUploadToList(String uploadID, String filename) { |
| 103 | + View uploadProgressView = getLayoutInflater().inflate(R.layout.view_upload_progress, null); |
| 104 | + UploadProgressViewHolder viewHolder = new UploadProgressViewHolder(uploadProgressView, filename); |
| 105 | + viewHolder.uploadId = uploadID; |
| 106 | + container.addView(viewHolder.itemView, 0); |
| 107 | + uploadProgressHolders.put(uploadID, viewHolder); |
| 108 | + } |
| 109 | + |
| 110 | + @OnClick(R.id.multipartUploadButton) |
| 111 | + public void onMultipartUploadClick() { |
| 112 | + final String serverUrlString = serverUrl.getText().toString(); |
| 113 | + final String paramNameString = parameterName.getText().toString(); |
| 114 | + |
| 115 | + final String filesToUploadString = filesToUpload.getText().toString(); |
| 116 | + final String[] filesToUploadArray = filesToUploadString.split("\\|\\|"); |
| 117 | + |
| 118 | + for (String fileToUploadPath : filesToUploadArray) { |
| 119 | + try { |
| 120 | + final String filename = getFilename(fileToUploadPath); |
| 121 | + |
| 122 | + MultipartUploadRequest req = new MultipartUploadRequest(this, serverUrlString) |
| 123 | + .addFileToUpload(fileToUploadPath, paramNameString) |
| 124 | + .setNotificationConfig(getNotificationConfig(filename)) |
| 125 | + .setCustomUserAgent(USER_AGENT) |
| 126 | + .setAutoDeleteFilesAfterSuccessfulUpload(autoDeleteUploadedFiles.isChecked()) |
| 127 | + .setUsesFixedLengthStreamingMode(fixedLengthStreamingMode.isChecked()) |
| 128 | + .setMaxRetries(3); |
| 129 | + |
| 130 | + if (useUtf8.isChecked()) { |
| 131 | + req.setUtf8Charset(); |
| 132 | + } |
| 133 | + |
| 134 | + String uploadID = req.setDelegate(this).startUpload(); |
| 135 | + |
| 136 | + addUploadToList(uploadID,filename); |
| 137 | + |
| 138 | + // these are the different exceptions that may be thrown |
| 139 | + } catch (FileNotFoundException exc) { |
| 140 | + showToast(exc.getMessage()); |
| 141 | + } catch (IllegalArgumentException exc) { |
| 142 | + showToast("Missing some arguments. " + exc.getMessage()); |
| 143 | + } catch (MalformedURLException exc) { |
| 144 | + showToast(exc.getMessage()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @OnClick(R.id.binaryUploadButton) |
| 150 | + public void onUploadBinaryClick() { |
| 151 | + final String serverUrlString = serverUrl.getText().toString(); |
| 152 | + |
| 153 | + final String filesToUploadString = filesToUpload.getText().toString(); |
| 154 | + final String[] filesToUploadArray = filesToUploadString.split(","); |
| 155 | + |
| 156 | + for (String fileToUploadPath : filesToUploadArray) { |
| 157 | + try { |
| 158 | + final String filename = getFilename(fileToUploadPath); |
| 159 | + |
| 160 | + final String uploadID = new BinaryUploadRequest(this, serverUrlString) |
| 161 | + .addHeader("file-name", new File(fileToUploadPath).getName()) |
| 162 | + .setFileToUpload(fileToUploadPath) |
| 163 | + .setNotificationConfig(getNotificationConfig(filename)) |
| 164 | + .setCustomUserAgent(USER_AGENT) |
| 165 | + .setAutoDeleteFilesAfterSuccessfulUpload(autoDeleteUploadedFiles.isChecked()) |
| 166 | + .setUsesFixedLengthStreamingMode(fixedLengthStreamingMode.isChecked()) |
| 167 | + .setMaxRetries(2) |
| 168 | + .setDelegate(this) |
| 169 | + .startUpload(); |
| 170 | + |
| 171 | + addUploadToList(uploadID, filename); |
| 172 | + |
| 173 | + // these are the different exceptions that may be thrown |
| 174 | + } catch (FileNotFoundException exc) { |
| 175 | + showToast(exc.getMessage()); |
| 176 | + } catch (IllegalArgumentException exc) { |
| 177 | + showToast("Missing some arguments. " + exc.getMessage()); |
| 178 | + } catch (MalformedURLException exc) { |
| 179 | + showToast(exc.getMessage()); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @OnClick(R.id.ftpUploadButton) |
| 185 | + public void onUploadFTPClick() { |
| 186 | + String serverUrlString = serverUrl.getText().toString(); |
| 187 | + |
| 188 | + int ftpPort = 21; |
| 189 | + if (serverUrlString.contains(":")) { |
| 190 | + try { |
| 191 | + String[] tmp = serverUrlString.split(":"); |
| 192 | + serverUrlString = tmp[0]; |
| 193 | + ftpPort = Integer.parseInt(tmp[1]); |
| 194 | + } catch (Exception exc) { |
| 195 | + Log.e(getClass().getSimpleName(), "error while getting FTP port from: " + serverUrlString, exc); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + final String filesToUploadString = filesToUpload.getText().toString(); |
| 200 | + final String[] filesToUploadArray = filesToUploadString.split(","); |
| 201 | + |
| 202 | + FTPUploadRequest request = new FTPUploadRequest(this, serverUrlString, ftpPort) |
| 203 | + .setUsernameAndPassword(FTP_USERNAME, FTP_PASSWORD) |
| 204 | + .setMaxRetries(4) |
| 205 | + .setNotificationConfig(getNotificationConfig("FTP upload")) |
| 206 | + .useCompressedFileTransferMode(true) |
| 207 | + .setCreatedDirectoriesPermissions(new UnixPermissions("777")) |
| 208 | + .setAutoDeleteFilesAfterSuccessfulUpload(autoDeleteUploadedFiles.isChecked()); |
| 209 | + |
| 210 | + for (String fileToUploadPath : filesToUploadArray) { |
| 211 | + try { |
| 212 | + request.addFileToUpload(fileToUploadPath, FTP_REMOTE_BASE_PATH, new UnixPermissions("777")); |
| 213 | + } catch (FileNotFoundException exc) { |
| 214 | + showToast(exc.getMessage()); |
| 215 | + } catch (IllegalArgumentException exc) { |
| 216 | + showToast("Missing some arguments. " + exc.getMessage()); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + try { |
| 221 | + String uploadID = request.setDelegate(this).startUpload(); |
| 222 | + addUploadToList(uploadID, "FTP upload"); |
| 223 | + |
| 224 | + } catch (IllegalArgumentException exc) { |
| 225 | + showToast("Missing some arguments. " + exc.getMessage()); |
| 226 | + } catch (MalformedURLException exc) { |
| 227 | + showToast(exc.getMessage()); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + @OnClick(R.id.cancelAllUploadsButton) |
| 232 | + public void onCancelAllUploadsButtonClick() { |
| 233 | + UploadService.stopAllUploads(); |
| 234 | + } |
| 235 | + |
| 236 | + @OnClick(R.id.pickFile) |
| 237 | + public void onPickFileClick() { |
| 238 | + openFilePicker(false); |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + public void onPickedFiles(List<String> pickedFiles) { |
| 243 | + StringBuilder absolutePathsConcat = new StringBuilder(); |
| 244 | + for (String file : pickedFiles) { |
| 245 | + if (absolutePathsConcat.length() == 0) { |
| 246 | + absolutePathsConcat.append(new File(file).getAbsolutePath()); |
| 247 | + } else { |
| 248 | + absolutePathsConcat.append("||").append(new File(file).getAbsolutePath()); |
| 249 | + } |
| 250 | + } |
| 251 | + filesToUpload.setText(absolutePathsConcat.toString()); |
| 252 | + } |
| 253 | + |
| 254 | + private String getFilename(String filepath) { |
| 255 | + if (filepath == null) |
| 256 | + return null; |
| 257 | + |
| 258 | + final String[] filepathParts = filepath.split("/"); |
| 259 | + |
| 260 | + return filepathParts[filepathParts.length - 1]; |
| 261 | + } |
| 262 | + |
| 263 | + class UploadProgressViewHolder { |
| 264 | + View itemView; |
| 265 | + |
| 266 | + @BindView(R.id.uploadTitle) TextView uploadTitle; |
| 267 | + @BindView(R.id.uploadProgress) ProgressBar progressBar; |
| 268 | + |
| 269 | + String uploadId; |
| 270 | + |
| 271 | + UploadProgressViewHolder(View view, String filename) { |
| 272 | + itemView = view; |
| 273 | + ButterKnife.bind(this, itemView); |
| 274 | + |
| 275 | + progressBar.setMax(100); |
| 276 | + progressBar.setProgress(0); |
| 277 | + |
| 278 | + uploadTitle.setText(getString(R.string.upload_progress, filename)); |
| 279 | + } |
| 280 | + |
| 281 | + @OnClick(R.id.cancelUploadButton) |
| 282 | + public void onCancelUploadClick() { |
| 283 | + if (uploadId == null) |
| 284 | + return; |
| 285 | + |
| 286 | + UploadService.stopUpload(uploadId); |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + @Override |
| 291 | + public void onProgress(Context context, UploadInfo uploadInfo) { |
| 292 | + Log.i(TAG, String.format(Locale.getDefault(), "ID: %1$s (%2$d%%) at %3$.2f Kbit/s", |
| 293 | + uploadInfo.getUploadId(), uploadInfo.getProgressPercent(), |
| 294 | + uploadInfo.getUploadRate())); |
| 295 | + logSuccessfullyUploadedFiles(uploadInfo.getSuccessfullyUploadedFiles()); |
| 296 | + |
| 297 | + if (uploadProgressHolders.get(uploadInfo.getUploadId()) == null) |
| 298 | + return; |
| 299 | + |
| 300 | + uploadProgressHolders.get(uploadInfo.getUploadId()) |
| 301 | + .progressBar.setProgress(uploadInfo.getProgressPercent()); |
| 302 | + } |
| 303 | + |
| 304 | + @Override |
| 305 | + public void onError(Context context, UploadInfo uploadInfo, Exception exception) { |
| 306 | + Log.e(TAG, "Error with ID: " + uploadInfo.getUploadId() + ": " |
| 307 | + + exception.getLocalizedMessage(), exception); |
| 308 | + logSuccessfullyUploadedFiles(uploadInfo.getSuccessfullyUploadedFiles()); |
| 309 | + |
| 310 | + if (uploadProgressHolders.get(uploadInfo.getUploadId()) == null) |
| 311 | + return; |
| 312 | + |
| 313 | + container.removeView(uploadProgressHolders.get(uploadInfo.getUploadId()).itemView); |
| 314 | + uploadProgressHolders.remove(uploadInfo.getUploadId()); |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { |
| 319 | + Log.i(TAG, String.format(Locale.getDefault(), |
| 320 | + "ID %1$s: completed in %2$ds at %3$.2f Kbit/s. Response code: %4$d, body:[%5$s]", |
| 321 | + uploadInfo.getUploadId(), uploadInfo.getElapsedTime() / 1000, |
| 322 | + uploadInfo.getUploadRate(), serverResponse.getHttpCode(), |
| 323 | + serverResponse.getBodyAsString())); |
| 324 | + logSuccessfullyUploadedFiles(uploadInfo.getSuccessfullyUploadedFiles()); |
| 325 | + for (Map.Entry<String, String> header : serverResponse.getHeaders().entrySet()) { |
| 326 | + Log.i("Header", header.getKey() + ": " + header.getValue()); |
| 327 | + } |
| 328 | + |
| 329 | + Log.e(TAG, "Printing response body bytes"); |
| 330 | + byte[] ba = serverResponse.getBody(); |
| 331 | + for (int j = 0; j < ba.length; j++) { |
| 332 | + Log.e(TAG, String.format("%02X ", ba[j])); |
| 333 | + } |
| 334 | + |
| 335 | + if (uploadProgressHolders.get(uploadInfo.getUploadId()) == null) |
| 336 | + return; |
| 337 | + |
| 338 | + container.removeView(uploadProgressHolders.get(uploadInfo.getUploadId()).itemView); |
| 339 | + uploadProgressHolders.remove(uploadInfo.getUploadId()); |
| 340 | + } |
| 341 | + |
| 342 | + @Override |
| 343 | + public void onCancelled(Context context, UploadInfo uploadInfo) { |
| 344 | + Log.i(TAG, "Upload with ID " + uploadInfo.getUploadId() + " is cancelled"); |
| 345 | + logSuccessfullyUploadedFiles(uploadInfo.getSuccessfullyUploadedFiles()); |
| 346 | + |
| 347 | + if (uploadProgressHolders.get(uploadInfo.getUploadId()) == null) |
| 348 | + return; |
| 349 | + |
| 350 | + container.removeView(uploadProgressHolders.get(uploadInfo.getUploadId()).itemView); |
| 351 | + uploadProgressHolders.remove(uploadInfo.getUploadId()); |
| 352 | + } |
| 353 | +} |
0 commit comments