Guest User

Untitled

a guest
Dec 29th, 2010
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package org.kman.AndroMail.data;
  2.  
  3. import java.io.File;
  4.  
  5. import android.content.Context;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.os.Environment;
  8.  
  9. /**
  10.  * Does not derive from SQLiteOpenHelper
  11.  */
  12.  
  13. public class MailDbOpenHelper {
  14.  
  15.     private static final String DIRECTORY_NAME = "AndroMail";
  16.     private static final String DATABASE_NAME = "Messages.sqldb";
  17.     private static final int DATABASE_VERSION = 1;
  18.  
  19.     private static final String FOLDER_TABLE_CREATE =
  20.             "CREATE TABLE " + MailConstants.FOLDER._TABLE_NAME + " ("
  21.             + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
  22.             + "name TEXT, "
  23.             + "type INTEGER, "
  24.             + "is_sync INTEGER, "
  25.             + "is_dead INTEGER, "
  26.             + "msg_count_total INTEGER, "
  27.             + "msg_count_unread INTEGER, "
  28.             + "imap_uid_validity INTEGER, "
  29.             + "imap_uid_next INTEGER, "
  30.             + "imap_uid_unseen INTEGER, "
  31.             + "account_id INTEGER "
  32.             + ");";
  33.  
  34.     public MailDbOpenHelper(Context context) {
  35.         // mContext = context;
  36.     }
  37.  
  38.     public static File getStoragePath() {
  39.         File d = Environment.getExternalStorageDirectory();
  40.         File f = new File(d, DIRECTORY_NAME);
  41.         return f;
  42.     }
  43.  
  44.     public static File getDatabaseFile() {
  45.         File d = getStoragePath();
  46.         File f = new File(d, DATABASE_NAME);
  47.         return f;
  48.     }
  49.  
  50.     public synchronized SQLiteDatabase getReadableDatabase() {
  51.         return getWritableDatabase();
  52.     }
  53.  
  54.     public synchronized SQLiteDatabase getWritableDatabase() {
  55.         if (mDatabase != null && mDatabase.isOpen()) {
  56.             return mDatabase;
  57.         }
  58.  
  59.         File dbPath = getStoragePath();
  60.         if (!dbPath.exists()) {
  61.             dbPath.mkdirs();
  62.         }
  63.        
  64.         File dbFile = getDatabaseFile();
  65.         SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
  66.        
  67.         int version = db.getVersion();
  68.         if (version != DATABASE_VERSION) {
  69.             db.beginTransaction();
  70.             try {
  71.                 if (version == 0) {
  72.                     onCreate(db);
  73.                 } else {
  74.                     onUpgrade(db, version, DATABASE_VERSION);
  75.                 }
  76.                 db.setVersion(DATABASE_VERSION);
  77.                 db.setTransactionSuccessful();
  78.             } finally {
  79.                 db.endTransaction();
  80.             }
  81.         }
  82.  
  83.         mDatabase = db;
  84.         return mDatabase;
  85.     }
  86.  
  87.     private void onCreate(SQLiteDatabase db) {
  88.         db.execSQL(FOLDER_TABLE_CREATE);
  89.     }
  90.  
  91.     private void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  92.     }
  93.  
  94.     // private Context mContext;
  95.     private SQLiteDatabase mDatabase;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment