Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deleting crashes in database. #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public static Sherlock getInstance() {
return instance;
}

public List<Crash> getAllCrashes() {
public static List<Crash> getAllCrashes() {
return getInstance().database.getCrashes();
}

public static void clearAllCrashes() { getInstance().database.clearCrashes(); }

private static void analyzeAndReportCrash(Throwable throwable) {
Log.d(TAG, "Analyzing Crash...");
CrashAnalyzer crashAnalyzer = new CrashAnalyzer(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CrashTable implements BaseColumns {
public static final String DROP_QUERY = "drop table " + TABLE_NAME;
public static final String TRUNCATE_QUERY = "delete from " + TABLE_NAME;
public static final String SELECT_ALL = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + _ID + " DESC";
public static final String DELETE_ALL = "DELETE FROM " + TABLE_NAME;

public static String selectById(int id) {
return "SELECT * FROM " + TABLE_NAME + " WHERE " + _ID + " = " + id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public List<Crash> getCrashes() {
return crashes;
}

public void clearCrashes() {
getWritableDatabase().execSQL(CrashTable.DELETE_ALL);
}

public Crash getCrashById(int id) {
SQLiteDatabase readableDatabase = getReadableDatabase();
Cursor cursor = readableDatabase.rawQuery(CrashTable.selectById(id), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.singhajit.sherlock.R;
Expand All @@ -18,15 +19,22 @@
import com.singhajit.sherlock.crashes.presenter.CrashListPresenter;
import com.singhajit.sherlock.crashes.viewmodel.CrashesViewModel;

public class CrashListActivity extends BaseActivity implements CrashListActions {
import static com.singhajit.sherlock.crashes.activity.CrashActivity.CRASH_ID;


public class CrashListActivity extends BaseActivity implements CrashListActions {
public static final String CLEAR_CRASHES = "com.singhajit.sherlock.CLEAR_CRASHES";
private CrashListPresenter presenter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crash_list_activity);

Intent intent = getIntent();
boolean clearCrashes = intent.getBooleanExtra(CLEAR_CRASHES, false);
((CheckBox) findViewById(R.id.erase_crashes)).setChecked(clearCrashes);

enableHomeButton((Toolbar) findViewById(R.id.toolbar));
setTitle(R.string.app_name);

Expand All @@ -35,6 +43,18 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
presenter.render(database);
}

@Override
protected void onDestroy()
{
CheckBox eraseAll = (CheckBox) findViewById(R.id.erase_crashes);
if (eraseAll != null && eraseAll.isChecked())
{
SherlockDatabaseHelper dbHelper = new SherlockDatabaseHelper(this);
dbHelper.clearCrashes();
}
super.onDestroy();
}

@Override
public void render(CrashesViewModel viewModel) {
RecyclerView crashList = (RecyclerView) findViewById(R.id.crash_list);
Expand Down Expand Up @@ -66,7 +86,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void openCrashDetails(int crashId) {
Intent intent = new Intent(this, CrashActivity.class);
intent.putExtra(CrashActivity.CRASH_ID, crashId);
intent.putExtra(CRASH_ID, crashId);

startActivity(intent);
}
Expand Down
13 changes: 12 additions & 1 deletion sherlock/src/main/res/layout/crash_list_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:layout_height="match_parent"
android:orientation="vertical">


<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand All @@ -22,6 +23,13 @@
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<CheckBox
android:id="@+id/erase_crashes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/erase_all_crashes" />

<LinearLayout
android:id="@+id/no_crash_found_layout"
android:layout_width="match_parent"
Expand Down Expand Up @@ -56,5 +64,8 @@
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:scrollbars="vertical"/>
android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

</LinearLayout>
1 change: 1 addition & 0 deletions sherlock/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<string name="version">Version</string>
<string name="not_found">404</string>
<string name="github_link">https://github.com/ajitsing/Sherlock</string>
<string name="erase_all_crashes">Clear all crashes upon leaving</string>
</resources>