Java tutorial
/* * Cos android client for Cozy Cloud * * Copyright (C) 2016 Hamza Abdelkebir * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.codeplumbers.cosi.api.tasks; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Environment; import android.util.Base64; import com.activeandroid.query.Delete; import org.apache.commons.io.IOUtils; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.util.ArrayList; import java.util.List; import eu.codeplumbers.cosi.db.models.Device; import eu.codeplumbers.cosi.db.models.File; import eu.codeplumbers.cosi.db.models.Note; import eu.codeplumbers.cosi.fragments.FileManagerFragment; import eu.codeplumbers.cosi.utils.Constants; public class DeleteFileTask extends AsyncTask<File, String, String> { private final String url; private final String authHeader; private FileManagerFragment fileManagerFragment; private String errorMessage; private ProgressDialog dialog; public DeleteFileTask(FileManagerFragment fileManagerFragment) { this.fileManagerFragment = fileManagerFragment; Device device = Device.registeredDevice(); // delete all local notes new Delete().from(Note.class).execute(); // cozy register device url this.url = device.getUrl() + "/ds-api/data/"; // concatenate username and password with colon for authentication final String credentials = device.getLogin() + ":" + device.getPassword(); authHeader = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); dialog = new ProgressDialog(fileManagerFragment.getActivity()); dialog.setCancelable(false); dialog.setProgress(0); dialog.setMax(100); dialog.setMessage("Please wait"); dialog.setIndeterminate(false); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); } @Override protected String doInBackground(File... files) { for (int i = 0; i < files.length; i++) { File file = files[i]; String binaryRemoteId = file.getRemoteId(); if (!binaryRemoteId.isEmpty()) { publishProgress("Deleting file: " + file.getName(), "0", "100"); URL urlO = null; try { urlO = new URL(url + binaryRemoteId + "/binaries/file"); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoInput(true); conn.setRequestMethod("DELETE"); InputStream in = null; // read the response int status = conn.getResponseCode(); if (status >= HttpURLConnection.HTTP_BAD_REQUEST) { in = conn.getErrorStream(); } else { in = conn.getInputStream(); } StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); String result = writer.toString(); if (status >= HttpURLConnection.HTTP_BAD_REQUEST) { errorMessage = conn.getResponseMessage(); } else { errorMessage = deleteFileRequest(file); if (errorMessage == "") { java.io.File newFile = file.getLocalPath(); if (newFile.exists()) { newFile.delete(); } file.delete(); } else { return errorMessage; } } } catch (MalformedURLException e) { errorMessage = e.getLocalizedMessage(); } catch (ProtocolException e) { errorMessage = e.getLocalizedMessage(); } catch (IOException e) { errorMessage = e.getLocalizedMessage(); } } } return errorMessage; } private String deleteFileRequest(File file) { String result = ""; URL urlO = null; try { urlO = new URL(url + file.getRemoteId() + "/"); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoOutput(false); conn.setDoInput(true); conn.setRequestMethod("DELETE"); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); result = writer.toString(); in.close(); conn.disconnect(); } catch (MalformedURLException e) { result = e.getLocalizedMessage(); } catch (ProtocolException e) { result = e.getLocalizedMessage(); e.printStackTrace(); } catch (IOException e) { result = e.getLocalizedMessage(); } return result; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); dialog.setProgress(Integer.valueOf(values[1])); dialog.setMax(Integer.valueOf(values[2])); dialog.setMessage(values[0]); } @Override protected void onPostExecute(String apiResponse) { super.onPostExecute(apiResponse); dialog.cancel(); if (apiResponse == "") { fileManagerFragment.refreshList(); fileManagerFragment.onFileDeletedFromCozy(); } else { fileManagerFragment.onSyncFailure(apiResponse); } } }