Skip to content

Commit

Permalink
Merge branch 'crash-no-space'
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed May 28, 2016
2 parents 09d459c + d83b32e commit ddc7c49
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 219 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

1.6.5 *(2016-05-28)*
--------------------

* Prevent a crash if the user does not have enough disk space.

1.6.4 *(2016-05-18)*
--------------------

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {
applicationId "ca.rmen.android.poetassistant"
minSdkVersion 16
targetSdkVersion 23
versionCode 164
versionName "1.6.4"
versionCode 165
versionName "1.6.5"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import android.databinding.DataBindingUtil;
import android.media.AudioManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
Expand All @@ -38,7 +40,6 @@

import ca.rmen.android.poetassistant.Constants;
import ca.rmen.android.poetassistant.R;
import ca.rmen.android.poetassistant.Theme;
import ca.rmen.android.poetassistant.about.AboutActivity;
import ca.rmen.android.poetassistant.databinding.ActivityMainBinding;
import ca.rmen.android.poetassistant.main.dictionaries.Search;
Expand All @@ -50,9 +51,10 @@
import ca.rmen.android.poetassistant.settings.SettingsActivity;


public class MainActivity extends AppCompatActivity implements OnWordClickedListener {
public class MainActivity extends AppCompatActivity implements OnWordClickedListener, WarningNoSpaceDialogFragment.WarningNoSpaceDialogListener {

private static final String TAG = Constants.TAG + MainActivity.class.getSimpleName();
private static final String DIALOG_TAG = "dialog";

private Search mSearch;
private ActivityMainBinding mBinding;
Expand Down Expand Up @@ -90,14 +92,27 @@ else if (Intent.ACTION_SEND.equals(intent.getAction()))
* can already be fast.
*/
private void loadDictionaries() {
new Thread() {
new AsyncTask<Void, Void, Boolean>() {
@Override
public void run() {
Rhymer.getInstance(getApplicationContext());
Thesaurus.getInstance(getApplicationContext());
Dictionary.getInstance(getApplicationContext());
protected Boolean doInBackground(Void... params) {
Rhymer rhymer = Rhymer.getInstance(getApplicationContext());
Thesaurus thesaurus = Thesaurus.getInstance(getApplicationContext());
Dictionary dictionary = Dictionary.getInstance(getApplicationContext());
return rhymer.isLoaded() && thesaurus.isLoaded() && dictionary.isLoaded();
}
}.start();

@Override
protected void onPostExecute(Boolean allDictionariesAreLoaded) {
Fragment warningNoSpaceDialogFragment = getSupportFragmentManager().findFragmentByTag(DIALOG_TAG);
if (!allDictionariesAreLoaded
&& warningNoSpaceDialogFragment == null) {
getSupportFragmentManager().beginTransaction().add(new WarningNoSpaceDialogFragment(), DIALOG_TAG).commit();
} else if (allDictionariesAreLoaded
&& warningNoSpaceDialogFragment != null){
getSupportFragmentManager().beginTransaction().remove(warningNoSpaceDialogFragment).commit();
}
}
}.execute();
}

@Override
Expand Down Expand Up @@ -172,6 +187,12 @@ public void onWordClicked(String word, Tab tab) {
mSearch.search(word, tab);
}

@Override
public void onWarningNoSpaceDialogDismissed() {
Log.v(TAG, "onWarningNoSpaceDialogDismissed");
finish();
}

// Hide the keyboard when we navigate to any tab other than the reader tab.
private final ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2016 Carmen Alvarez
*
* This file is part of Poet Assistant.
*
* Poet Assistant is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Poet Assistant is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/

package ca.rmen.android.poetassistant.main;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import ca.rmen.android.poetassistant.Constants;
import ca.rmen.android.poetassistant.R;


/**
* Shows a dialog with a title, message, and ok button.
* The activity or fragment which adds this dialog should implement the
* WarningDialogListener interface.
*/
public class WarningNoSpaceDialogFragment extends DialogFragment {

private static final String TAG = Constants.TAG + WarningNoSpaceDialogFragment.class.getSimpleName();

public interface WarningNoSpaceDialogListener {
void onWarningNoSpaceDialogDismissed();
}

/**
* @return a Dialog with a title, message and ok button.
*/
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.v(TAG, "onCreateDialog: savedInstanceState = " + savedInstanceState);
Context context = getActivity();

OnClickListener positiveListener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notifyListener();
}
};

DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
notifyListener();
}
};

Dialog dialog = new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.warning_no_space_title))
.setMessage(context.getString(R.string.warning_no_space_message))
.setPositiveButton(android.R.string.ok, positiveListener)
.setOnDismissListener(dismissListener)
.create();
dialog.setOnDismissListener(dismissListener);
return dialog;
}

private void notifyListener() {
Fragment parentFragment = getParentFragment();
if (parentFragment instanceof WarningNoSpaceDialogListener) {
((WarningNoSpaceDialogListener) parentFragment).onWarningNoSpaceDialogDismissed();
} else if (getActivity() instanceof WarningNoSpaceDialogListener) {
((WarningNoSpaceDialogListener) getActivity()).onWarningNoSpaceDialogDismissed();
}
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
notifyListener();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2016 Carmen Alvarez
*
* This file is part of Poet Assistant.
*
* Poet Assistant is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Poet Assistant is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/

package ca.rmen.android.poetassistant.main.dictionaries;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import ca.rmen.android.poetassistant.Constants;

public class DbHelper {
private static final String TAG = Constants.TAG + DbHelper.class.getSimpleName();

private final Context mContext;
private final String mDbName;
private final int mVersion;
private SQLiteDatabase mDb;
private final Object mLock = new Object();

public DbHelper(Context context, String dbName, int version) {
mContext = context;
mDbName = dbName;
mVersion = version;
}

public SQLiteDatabase getDb() {
open();
return mDb;
}

private void open() {
synchronized (mLock) {
if (mDb == null) {
Log.v(TAG, "Open db " + mDbName + ":" + mVersion);
copyDb();
String dbFile = getDbFileName(mVersion);
File dbPath = new File(mContext.getDir("databases", Context.MODE_PRIVATE), dbFile);
try {
mDb = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
Log.w(TAG, "Could not open database " + mDbName + ":" + mVersion + ": " + e.getMessage(), e);
}
}
}
}

private void copyDb() {
String dbFileName = getDbFileName(mVersion);
File dbPath = getDbFile(dbFileName);
if (!dbPath.exists()) {
Log.v(TAG, dbPath + " not found");
for (int i = 0; i < mVersion; i++) {
deleteDb(i);
}

try {
InputStream is = mContext.getAssets().open(dbFileName);
FileOutputStream os = new FileOutputStream(dbPath);
byte[] buffer = new byte[1024];
int read = is.read(buffer);
while (read > 0) {
os.write(buffer, 0, read);
read = is.read(buffer);
}
Log.v(TAG, "wrote " + dbPath);
} catch (IOException e) {
Log.e(TAG, "Error writing to " + dbPath + ": " + e.getMessage(), e);
deleteDb(mVersion);
}
}
}

private String getDbFileName(int version) {
if (version == 1) return mDbName + ".db";
return mDbName + version + ".db";
}

private void deleteDb(int version) {
String dbFileName = getDbFileName(version);
File dbPath = getDbFile(dbFileName);
if (dbPath.exists()) {
boolean deleted = dbPath.delete();
Log.v(TAG, "dbDelete: deletion of " + dbPath.getAbsolutePath() + ": " + deleted);
}
}

private File getDbFile(String filename) {
File dbDir = mContext.getDir("databases", Context.MODE_PRIVATE);
return new File(dbDir, filename);
}

}
Loading

0 comments on commit ddc7c49

Please sign in to comment.