All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.hockeyapp.android.tasks.SendFeedbackTask Maven / Gradle / Ivy

Go to download

HockeySDK-Android implements support for using HockeyApp in your Android application. The following features are currently supported: Collect crash reports:If your app crashes, a crash log is written to the device's storage. If the user starts the app again, they will be asked asked to submit the crash report to HockeyApp. This works for both beta and live apps, i.e. those submitted to Google Play or other app stores. Crash logs contain viable information for you to help resolve the issue. Furthermore, you as a developer can add additional information to the report as well. Update Alpha/Beta apps: The app will check with HockeyApp if a new version for your alpha/beta build is available. If yes, it will show a dialog to users and let them see the release notes, the version history and start the installation process right away. You can even force the installation of certain updates. User Metrics: Understand user behavior to improve your app. Track usage through daily and monthly active users. Monitor crash impacted users. Measure customer engagement through session count. Add custom tracking calls to learn which features your users are actually using. This feature requires a minimum API level of 14 (Android 4.x Ice Cream Sandwich). Feedback: Besides crash reports, collecting feedback from your users from within your app is a great option to help with improving your app. You act on and answer feedback directly from the HockeyApp backend. Authenticate: Identify and authenticate users against your registered testers with the HockeyApp backend.

There is a newer version: 5.2.0
Show newest version
package net.hockeyapp.android.tasks;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import net.hockeyapp.android.Constants;
import net.hockeyapp.android.utils.HttpURLConnectionBuilder;
import net.hockeyapp.android.utils.SimpleMultipartEntity;
import net.hockeyapp.android.utils.Util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 

Description

* * Internal helper class. Sends feedback to server. * *

License

* *
 * Copyright (c) 2011-2014 Bit Stadium GmbH
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
* * @author Bogdan Nistor **/ public class SendFeedbackTask extends ConnectionTask> { private Context context; private Handler handler; private String urlString; private String name; private String email; private String subject; private String text; private List attachmentUris; private String token; private boolean isFetchMessages; private ProgressDialog progressDialog; private boolean showProgressDialog; private int lastMessageId; private HttpURLConnection urlConnection; /** * Send feedback {@link AsyncTask}. * If the class is intended to send a simple feedback message, the a POST is made with the specific data * If the class is intended to fetch the messages by providing a token, a GET is made * * @param context {@link Context} object * @param urlString URL for sending feedback/fetching messages * @param name Name of the feedback sender * @param email Email of the feedback sender * @param subject Message subject * @param text The message * @param attachmentUris List of all attached files * @param token Token received after sending the first feedback. This should be stored in {@link SharedPreferences} * @param handler Handler object to send data back to the activity * @param isFetchMessages If true, the {@link AsyncTask} will perform a GET, fetching the messages. * If false, the {@link AsyncTask} will perform a POST, sending the feedback message */ public SendFeedbackTask(Context context, String urlString, String name, String email, String subject, String text, List attachmentUris, String token, Handler handler, boolean isFetchMessages) { this.context = context; this.urlString = urlString; this.name = name; this.email = email; this.subject = subject; this.text = text; this.attachmentUris = attachmentUris; this.token = token; this.handler = handler; this.isFetchMessages = isFetchMessages; this.showProgressDialog = true; this.lastMessageId = -1; if (context != null) { Constants.loadFromContext(context); } } public void setShowProgressDialog(boolean showProgressDialog) { this.showProgressDialog = showProgressDialog; } public void setLastMessageId(int lastMessageId) { this.lastMessageId = lastMessageId; } public void attach(Context context) { this.context = context; } public void detach() { context = null; if (progressDialog != null) { progressDialog.dismiss(); } progressDialog = null; } @Override protected void onPreExecute() { String loadingMessage = "Sending feedback.."; if (isFetchMessages) { loadingMessage = "Retrieving discussions..."; } if ((progressDialog == null || !progressDialog.isShowing()) && showProgressDialog) { progressDialog = ProgressDialog.show(context, "", loadingMessage, true, false); } } @Override protected HashMap doInBackground(Void... args) { if (isFetchMessages && token != null) { /** If we are fetching messages then do a GET */ return doGet(); } else if (!isFetchMessages) { /** * If we are sending a feedback do POST, and if we are sending a feedback * to an existing discussion do PUT */ if (attachmentUris.isEmpty()) { return doPostPut(); } else { HashMap result = doPostPutWithAttachments(); /** Clear temporary folder */ String status = result.get("status"); if ((status != null) && (status.startsWith("2")) && (context != null)) { File folder = new File(context.getCacheDir(), Constants.TAG); if (folder.exists()) { for (File file : folder.listFiles()) { file.delete(); } } } return result; } } return null; } @Override protected void onPostExecute(HashMap result) { if (progressDialog != null) { try { progressDialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } /** If the Handler object is not NULL, send a message to the Activity with the result */ if (handler != null) { Message msg = new Message(); Bundle bundle = new Bundle(); if (result != null) { bundle.putString("request_type", (String) result.get("type")); bundle.putString("feedback_response", (String) result.get("response")); bundle.putString("feedback_status", (String) result.get("status")); } else { bundle.putString("request_type", "unknown"); } msg.setData(bundle); handler.sendMessage(msg); } } /** * POST/PUT * * @return */ private HashMap doPostPut() { HashMap result = new HashMap(); result.put("type", "send"); HttpURLConnection urlConnection = null; try { Map parameters = new HashMap(); parameters.put("name", name); parameters.put("email", email); parameters.put("subject", subject); parameters.put("text", text); parameters.put("bundle_identifier", Constants.APP_PACKAGE); parameters.put("bundle_short_version", Constants.APP_VERSION_NAME); parameters.put("bundle_version", Constants.APP_VERSION); parameters.put("os_version", Constants.ANDROID_VERSION); parameters.put("oem", Constants.PHONE_MANUFACTURER); parameters.put("model", Constants.PHONE_MODEL); if (token != null) { urlString += token + "/"; } urlConnection = new HttpURLConnectionBuilder(urlString) .setRequestMethod(token != null ? "PUT" : "POST") .writeFormFields(parameters) .build(); urlConnection.connect(); result.put("status", String.valueOf(urlConnection.getResponseCode())); result.put("response", getStringFromConnection(urlConnection)); } catch (IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return result; } /** * POST/PUT with attachments * * @return */ private HashMap doPostPutWithAttachments() { HashMap result = new HashMap(); result.put("type", "send"); HttpURLConnection urlConnection = null; try { Map parameters = new HashMap(); parameters.put("name", name); parameters.put("email", email); parameters.put("subject", subject); parameters.put("text", text); parameters.put("bundle_identifier", Constants.APP_PACKAGE); parameters.put("bundle_short_version", Constants.APP_VERSION_NAME); parameters.put("bundle_version", Constants.APP_VERSION); parameters.put("os_version", Constants.ANDROID_VERSION); parameters.put("oem", Constants.PHONE_MANUFACTURER); parameters.put("model", Constants.PHONE_MODEL); if (token != null) { urlString += token + "/"; } urlConnection = new HttpURLConnectionBuilder(urlString) .setRequestMethod(token != null ? "PUT" : "POST") .writeMultipartData(parameters, context, attachmentUris) .build(); urlConnection.connect(); result.put("status", String.valueOf(urlConnection.getResponseCode())); result.put("response", getStringFromConnection(urlConnection)); } catch (IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return result; } /** * GET * * @return */ private HashMap doGet() { StringBuilder sb = new StringBuilder(); sb.append(urlString + Util.encodeParam(token)); if (lastMessageId != -1) { sb.append("?last_message_id=" + lastMessageId); } HashMap result = new HashMap(); HttpURLConnection urlConnection = null; try { urlConnection = new HttpURLConnectionBuilder(sb.toString()) .build(); result.put("type", "fetch"); urlConnection.connect(); result.put("status", String.valueOf(urlConnection.getResponseCode())); result.put("response", getStringFromConnection(urlConnection)); } catch (IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy