Skip to content

Commit

Permalink
Support database on external storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Behrens committed Jan 6, 2024
1 parent c34279f commit 440ebc0
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ShoppingList/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {

defaultConfig {
applicationId "org.openintents.shopping"
versionName "2.2.1"
versionName "2.2.1a"
versionCode 100221
minSdkVersion 16
targetSdkVersion rootProject.ext.targetSdkVersion
Expand Down
1 change: 1 addition & 0 deletions ShoppingList/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

<uses-permission android:name="org.openintents.shopping.READ_PERMISSION" />
<uses-permission android:name="org.openintents.shopping.WRITE_PERMISSION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<application
android:name=".ShoppingApplication"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.openintents.shopping.provider;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.io.File;
import java.io.IOException;

import org.openintents.shopping.ui.PreferenceActivity;


public class DatabaseContext extends ContextWrapper {
private static String TAG = "DatabaseContext";

public DatabaseContext(Context context) {
super(context);
}

@Override
public File getDatabasePath(String name) {
// Parameter name="shopping.db" as passed as DB_NAME to super constructor in ShoppingDatabase constructor
SharedPreferences sp = getSharedPreferences(
"org.openintents.shopping_preferences", MODE_PRIVATE);
final Boolean externalDbUsed = sp.getBoolean(
PreferenceActivity.PREFS_EXTERNALDB_USED,
PreferenceActivity.PREFS_EXTERNALDB_USED_DEFAULT);
final String dirPath = sp.getString(
PreferenceActivity.PREFS_EXTERNALDB_PATH,
PreferenceActivity.PREFS_EXTERNALDB_PATH_DEFAULT);
if (externalDbUsed &&
!dirPath.equalsIgnoreCase(PreferenceActivity.PREFS_EXTERNALDB_PATH_DEFAULT)) {
String path = null;
File parentFile=new File(dirPath);
if (!parentFile.exists()){
parentFile.mkdirs();
}
String parentPath=parentFile.getAbsolutePath();
if (parentPath.lastIndexOf("\\/")!=-1){
path = dirPath + name;
} else {
path = dirPath + File.separator + name;
}
File dbFile = new File(path);
try {
if(!dbFile.exists()){
dbFile.createNewFile();
}
} catch (IOException e) {
Log.e(TAG,"getDatabasePath", e);
}
return dbFile;
} else {
return super.getDatabasePath(name);
}
}

@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
}

@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name).getAbsolutePath(), factory, errorHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ShoppingDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 13;

ShoppingDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(new DatabaseContext(context), DATABASE_NAME, null, DATABASE_VERSION);
}

/**
Expand Down
Loading

0 comments on commit 440ebc0

Please sign in to comment.