| /* |
| * Copyright 2016 Fairphone B.V. |
| * |
| * 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. |
| */ |
| |
| package com.fairphone.hiccup.app; |
| |
| import android.content.Context; |
| |
| import java.util.Date; |
| |
| |
| public class HiccupReport { |
| |
| static public final String TYPE_CRASH_REPORT = "CRASH_REPORT"; |
| static public final String TYPE_FAKE_REPORT = "FAKE_CRASH_REPORT"; |
| static public final String TYPE_HEARTBEAT = "PING_REPORT"; |
| |
| private long id; |
| private long serverSideLocalId; |
| private String logfilesPath; |
| private String bootReason; |
| private String powerOnReason; |
| private String powerOffReason; |
| private String buildFingerPrint; |
| private String uptime; |
| private Date date; |
| private boolean uploadCompleted; |
| private String auxData; |
| private String reportType; |
| |
| |
| public enum ReportType { |
| CRASH_REPORT, |
| FAKE_CRASH_REPORT, |
| PING_BACK_REPORT |
| } |
| |
| public static HiccupReport createReport(Context ctx, ReportType type) { |
| HiccupReport ret = new HiccupReport(); |
| ret.buildFingerPrint = HiccupUtil.getBuildFingerPrint(); |
| |
| switch (type) { |
| case CRASH_REPORT: ret.reportType = TYPE_CRASH_REPORT; break; |
| case FAKE_CRASH_REPORT: ret.reportType = TYPE_FAKE_REPORT; break; |
| case PING_BACK_REPORT: ret.reportType = TYPE_HEARTBEAT; break; |
| } |
| |
| ret.bootReason = HiccupUtil.getBootReason(); |
| ret.powerOnReason = HiccupUtil.getPowerOnReason(); |
| ret.powerOffReason = HiccupUtil.getPowerOffReason(); |
| |
| ret.date = new Date(); |
| ret.uploadCompleted = false; |
| |
| ret.setAuxData(HiccupSettings.getAuxData(ctx)); |
| ret.setUptime(HiccupUtil.getUptime()); |
| ret.logfilesPath = ""; |
| |
| getLogFiles(ctx, ret); |
| |
| HiccupReportDB db = new HiccupReportDB(ctx); |
| ret = db.putReport(ret); |
| HiccupUploader.trigger(ctx); |
| return ret; |
| } |
| |
| public void save(Context ctx) { |
| HiccupReportDB db = new HiccupReportDB(ctx); |
| db.updateReport(this); |
| } |
| |
| private static void getLogFiles(Context ctx, HiccupReport report) { |
| if ("PING_REPORT".equals(report.reportType)) |
| return; /* no logs for heartbeats */ |
| HiccupSettings.IncludeLogFilesConfiguration configuration = |
| HiccupSettings.getIncludeLogFilesConfiguration(ctx); |
| LogfileArchive logfiles; |
| switch (configuration) { |
| case NONE: |
| break; |
| case LAST_KMSG: |
| case ALL: |
| logfiles = LogfileArchive.fetchCrashReportLogfiles(ctx,configuration); |
| if (logfiles != null) |
| report.logfilesPath = logfiles.getCrashReportFile().getAbsolutePath(); |
| break; |
| } |
| } |
| |
| public static void createCrashReport(Context ctx) { |
| createReport(ctx, ReportType.CRASH_REPORT); |
| } |
| |
| public static void createFakeCrashReport(Context ctx) { |
| createReport(ctx, ReportType.FAKE_CRASH_REPORT); |
| } |
| |
| public static void createHeartbeatReport(Context ctx) { |
| createReport(ctx, ReportType.PING_BACK_REPORT); |
| } |
| |
| public String getReportType() { |
| return reportType; |
| } |
| |
| |
| public void setReportType(String reportType) { |
| this.reportType = reportType; |
| } |
| |
| public String getPowerOnReason() { |
| return powerOnReason; |
| } |
| |
| public void setPowerOnReason(String powerOnReason) { |
| this.powerOnReason = powerOnReason; |
| } |
| |
| public String getPowerOffReason() { |
| return powerOffReason; |
| } |
| |
| public void setPowerOffReason(String powerOffReason) { |
| this.powerOffReason = powerOffReason; |
| } |
| |
| public String getAuxData() { |
| return auxData; |
| } |
| |
| public void setAuxData(String auxData) { |
| this.auxData = auxData; |
| } |
| |
| public String getUptime() { |
| return uptime; |
| } |
| |
| public void setUptime(String uptime) { |
| this.uptime = uptime; |
| } |
| |
| public long getId() { |
| return id; |
| } |
| |
| public void setId(long id) { |
| this.id = id; |
| } |
| |
| public String getLogFilesPath() { |
| return logfilesPath; |
| } |
| |
| public void setLogFilesPath(String logfilesPath) { |
| this.logfilesPath = logfilesPath; |
| } |
| |
| public String getBootReason() { |
| return bootReason; |
| } |
| |
| public void setBootReason(String bootReason) { |
| this.bootReason = bootReason; |
| } |
| |
| public String getBuildFingerPrint() { |
| return buildFingerPrint; |
| } |
| |
| public void setBuildFingerPrint(String buildFingerPrint) { |
| this.buildFingerPrint = buildFingerPrint; |
| } |
| |
| public Date getDate() { |
| return date; |
| } |
| |
| public void setDate(Date date) { |
| this.date = date; |
| } |
| |
| public boolean isUploadCompleted() { |
| return uploadCompleted; |
| } |
| |
| public void setUploadCompleted(boolean uploadCompleted) { |
| this.uploadCompleted = uploadCompleted; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| return ((HiccupReport)o).getId()== getId(); |
| } |
| |
| public long getServerSideLocalId() { |
| return serverSideLocalId; |
| } |
| |
| public void setServerSideLocalId(long serverSideLocalId) { |
| this.serverSideLocalId = serverSideLocalId; |
| } |
| |
| public boolean existsOnServerSide() { |
| return serverSideLocalId > 0; |
| } |
| |
| public boolean hasLogFiles() { |
| return !"".equals(logfilesPath); |
| } |
| } |