Skip to content

Instantly share code, notes, and snippets.

@ovicus
Last active August 29, 2015 14:10
Show Gist options
  • Save ovicus/e8e1d5161abcb89a9191 to your computer and use it in GitHub Desktop.
Save ovicus/e8e1d5161abcb89a9191 to your computer and use it in GitHub Desktop.
ParseSender for ACRA
/*
* Copyright 2014 Lester Sanchez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseUser;
/**
* This class extends the ParseObject type and acts as a container
* for the crash data to be stored on Parse.
*
* @author Lester Sanchez
*
*/
@ParseClassName("CrashReport")
public class CrashReportParseObject extends ParseObject {
public String getAndroidVersion(){
return this.getString("ANDROID_VERSION");
}
public String getAppVersionName(){
return this.getString("APP_VERSION_NAME");
}
public String getPhoneModel(){
return this.getString("PHONE_MODEL");
}
public String getProduct(){
return this.getString("PRODUCT");
}
public void setUser(ParseUser user){
this.put("user", user);
}
}
/*
* Copyright 2014 Lester Sanchez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Map;
import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.ReportField;
import org.acra.collector.CrashReportData;
import org.acra.sender.ReportSender;
import com.parse.ParseUser;
/**
* This class represents a custom ReportSender to be used with
* ACRA (https://github.com/ACRA/acra). It sends to Parse (http://www.parse.com)
* the data collected by ACRA about a crash. Parse should be properly initialized
* before using this sender.
*
* @author Lester Sanchez
*
*/
public class ParseSender implements ReportSender {
@Override
public void send(CrashReportData report) {
// Get the ParseObject to be send to Parse
CrashReportParseObject crashReport = getParseObject(report);
// Associate the current logged user with the crash report
ParseUser currentUser = ParseUser.getCurrentUser();
crashReport.setUser(currentUser);
// Send crash report to Parse
crashReport.saveInBackground();
}
/**
* Get a custom ParseObject for data collected by ACRA.
* @param report
* Report data collected by ACRA.
* @return
* A custom ParseObject containing relevant data.
*/
private CrashReportParseObject getParseObject(Map<ReportField, String> report) {
ReportField[] fields = ACRA.getConfig().customReportContent();
if (fields.length == 0) {
fields = ACRAConstants.DEFAULT_REPORT_FIELDS;
}
CrashReportParseObject finalReport = new CrashReportParseObject();
for (ReportField field : fields) {
finalReport.put(field.toString(), report.get(field));
}
return finalReport;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment