blob: bdd3672f607a49276a4e4d18a975276da3e4fb98 [file] [log] [blame]
/*
* Copyright (C) 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.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.content.LocalBroadcastManager;
class HiccupReportDB {
static final public String db_changed_action = "com.fairphone.hiccup.REPORT_DB_CHANGED";
private HiccupReportDBHelper reportHelper = null;
private final LocalBroadcastManager broadcastManager;
HiccupReportDB(Context ctx) {
reportHelper = new HiccupReportDBHelper(ctx);
broadcastManager = LocalBroadcastManager.getInstance(ctx);
}
private void sendBroadcast() {
Intent intent = new Intent(db_changed_action);
broadcastManager.sendBroadcast(intent);
}
public HiccupReport putReport(HiccupReport report) {
// Create a new map of values, where column names are the keys
SQLiteDatabase db = reportHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_BUILDFINGERPRINT, report.getBuildFingerPrint());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_DATE, new java.sql.Timestamp(report.getDate().getTime()).toString());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_LOGFILE_PATH, report.getLogFilesPath());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_BOOTREASON, report.getBootReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWERONREASON, report.getPowerOnReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWEROFFREASON, report.getPowerOffReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_AUX_DATA, report.getAuxData());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPTIME, report.getUptime());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE, report.isUploadCompleted());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE, report.getReportType());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_DEVICE_LOCAL_ID, report.getServerSideLocalId());
long id;
id = db.insert(HiccupReportDBContract.reportEntry.TABLE_NAME, null, values);
report.setId(id);
sendBroadcast();
db.close();
return report;
}
public HiccupReport getReport(long id) {
SQLiteDatabase db = reportHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
String[] projection = {
HiccupReportDBContract.reportEntry._ID,
HiccupReportDBContract.reportEntry.COLUMN_NAME_BUILDFINGERPRINT,
HiccupReportDBContract.reportEntry.COLUMN_NAME_LOGFILE_PATH,
HiccupReportDBContract.reportEntry.COLUMN_NAME_BOOTREASON,
HiccupReportDBContract.reportEntry.COLUMN_NAME_POWERONREASON,
HiccupReportDBContract.reportEntry.COLUMN_NAME_POWEROFFREASON,
HiccupReportDBContract.reportEntry.COLUMN_NAME_DATE,
HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE,
HiccupReportDBContract.reportEntry.COLUMN_NAME_AUX_DATA,
HiccupReportDBContract.reportEntry.COLUMN_NAME_UPTIME,
HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE,
HiccupReportDBContract.reportEntry.COLUMN_NAME_DEVICE_LOCAL_ID,
};
String selection = HiccupReportDBContract.reportEntry._ID + " LIKE ?";
String[] selectionArgs = {String.valueOf(id)};
Cursor c = db.query(HiccupReportDBContract.reportEntry.TABLE_NAME,
projection, selection, selectionArgs, null, null, null);
if (c.getCount() != 1) {
return null;
}
c.moveToNext();
HiccupReport ret = new HiccupReport();
ret.setId(c.getLong(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry._ID)));
ret.setBootReason(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_BOOTREASON)));
ret.setLogFilesPath(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_LOGFILE_PATH)));
ret.setDate(java.sql.Timestamp.valueOf(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_DATE))));
ret.setUploadCompleted(Boolean.valueOf(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE))));
ret.setBuildFingerPrint(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_BUILDFINGERPRINT)));
ret.setUptime(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPTIME)));
ret.setAuxData(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_AUX_DATA)));
ret.setPowerOnReason(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWERONREASON)));
ret.setPowerOffReason(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWEROFFREASON)));
ret.setReportType(c.getString(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE)));
ret.setServerSideLocalId(c.getInt(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry.COLUMN_NAME_DEVICE_LOCAL_ID)));
c.close();
sendBroadcast();
db.close();
return ret;
}
public HiccupReport updateReport(HiccupReport report) {
SQLiteDatabase db = reportHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_BUILDFINGERPRINT, report.getBuildFingerPrint());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_DATE, new java.sql.Timestamp(report.getDate().getTime()).toString());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_LOGFILE_PATH, report.getLogFilesPath());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_BOOTREASON, report.getBootReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_AUX_DATA, report.getAuxData());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPTIME, report.getUptime());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE, report.isUploadCompleted());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWERONREASON, report.getPowerOnReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_POWEROFFREASON, report.getPowerOffReason());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE, report.getReportType());
values.put(HiccupReportDBContract.reportEntry.COLUMN_NAME_DEVICE_LOCAL_ID, report.getServerSideLocalId());
String selection = HiccupReportDBContract.reportEntry._ID + " LIKE ?";
String[] selectionArgs = {String.valueOf(report.getId())};
db.update(HiccupReportDBContract.reportEntry.TABLE_NAME, values, selection, selectionArgs);
sendBroadcast();
db.close();
return report;
}
public void deleteReport(HiccupReport report) {
SQLiteDatabase db = reportHelper.getWritableDatabase();
String selection = HiccupReportDBContract.reportEntry._ID + " LIKE ?";
String[] selectionArgs = {String.valueOf(report.getId())};
db.delete(HiccupReportDBContract.reportEntry.TABLE_NAME,selection, selectionArgs);
sendBroadcast();
db.close();
}
public long[] getIdsOfNotUploadedReports() {
SQLiteDatabase db = reportHelper.getWritableDatabase();
String[] projection = {
HiccupReportDBContract.reportEntry._ID,
};
// Define 'where' part of query.
String selection = HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = {"0"};
Cursor c = db.query(HiccupReportDBContract.reportEntry.TABLE_NAME,
projection, selection, selectionArgs, null, null, null);
long ret[] = new long[c.getCount()];
int i = 0;
while (c.moveToNext()) {
ret[i++] = c.getLong(c.getColumnIndexOrThrow(HiccupReportDBContract.reportEntry._ID));
}
c.close();
db.close();
return ret;
}
public int getNumNotUploadedCrashReports() {
SQLiteDatabase db = reportHelper.getWritableDatabase();
String[] projection = {
HiccupReportDBContract.reportEntry._ID,
};
String selection = HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE + " LIKE ? AND " + HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE + " NOT LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs ={"0", "PING_REPORT"};
Cursor c = db.query(HiccupReportDBContract.reportEntry.TABLE_NAME,
projection, selection, selectionArgs, null, null, null);
int ret = c.getCount();
c.close();
db.close();
return ret;
}
public int getNumNotUploadedReports() {
SQLiteDatabase db = reportHelper.getWritableDatabase();
String[] projection = {
HiccupReportDBContract.reportEntry._ID,
};
String selection = HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs ={"0"};
Cursor c = db.query(HiccupReportDBContract.reportEntry.TABLE_NAME,
projection, selection, selectionArgs, null, null, null);
int ret = c.getCount();
c.close();
db.close();
return ret;
}
public int getNumUploadedCrashReports() {
SQLiteDatabase db = reportHelper.getWritableDatabase();
String[] projection = {
HiccupReportDBContract.reportEntry._ID,
};
// Define 'where' part of query.
String selection = HiccupReportDBContract.reportEntry.COLUMN_NAME_UPLOAD_COMPLETE + " LIKE ? AND "+ HiccupReportDBContract.reportEntry.COLUMN_NAME_REPORT_TYPE + " NOT LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = {"1", "PING_REPORT"};
Cursor c = db.query(HiccupReportDBContract.reportEntry.TABLE_NAME,
projection, selection, selectionArgs, null, null, null);
int ret = c.getCount();
c.close();
db.close();
return ret;
}
}