-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
408 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/ca/rmen/android/poetassistant/main/WarningNoSpaceDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
app/src/main/java/ca/rmen/android/poetassistant/main/dictionaries/DbHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.