eu.codeplumbers.cosi.api.tasks.DeleteDocumentTask.java Source code

Java tutorial

Introduction

Here is the source code for eu.codeplumbers.cosi.api.tasks.DeleteDocumentTask.java

Source

/*
 *     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.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Base64;
import android.widget.Toast;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
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 eu.codeplumbers.cosi.activities.NoteDetailsActivity;
import eu.codeplumbers.cosi.db.models.Device;

/**
 * Created by thor on 10/2/16.
 */

public class DeleteDocumentTask extends AsyncTask<Void, Void, String> {

    private final String url;
    private final String authHeader;
    private final String remoteId;
    private Activity activity;
    private String result;
    private String errorMessage;
    private ProgressDialog dialog;
    private long objectId;

    public DeleteDocumentTask(String remoteId, Activity activity) {
        this.activity = activity;
        this.remoteId = remoteId;
        result = "";

        Device device = Device.registeredDevice();

        // 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(activity);
        dialog.setCancelable(false);
        dialog.setMessage("Please wait");
        dialog.setIndeterminate(true);
        dialog.show();

    }

    @Override
    protected String doInBackground(Void... voids) {
        URL urlO = null;
        try {
            urlO = new URL(url + remoteId + "/");
            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 = "error";
            e.printStackTrace();
            errorMessage = e.getLocalizedMessage();
        } catch (ProtocolException e) {
            result = "error";
            errorMessage = e.getLocalizedMessage();
            e.printStackTrace();
        } catch (IOException e) {
            result = "error";
            errorMessage = e.getLocalizedMessage();
            e.printStackTrace();
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        dialog.cancel();

        if (result != "error") {
            if (activity instanceof NoteDetailsActivity) {
                ((NoteDetailsActivity) activity).onNoteDeleted(result);
            }

        } else {
            Toast.makeText(activity, errorMessage, Toast.LENGTH_LONG).show();
        }
    }
}