Last active
August 29, 2015 14:21
-
-
Save maksudc/7316ab818ccf137bddba to your computer and use it in GitHub Desktop.
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
package com.lantas.postmortem.db; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.text.TextUtils; | |
import com.lantas.contracts.db.MigrationPreferenceHelper; | |
import com.lantas.phonebookbeta.R; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class NativeDatabaseHelper extends SQLiteOpenHelper { | |
private Context mContext; | |
public static String DATABASE_NAME = "phonebook.db"; | |
public static int DATABASE_VERSION = 30; | |
public NativeDatabaseHelper(Context context){ | |
super(context , DATABASE_NAME , null , DATABASE_VERSION); | |
mContext = context; | |
} | |
@Override | |
public void onConfigure(SQLiteDatabase db) | |
{ | |
db.enableWriteAheadLogging(); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db){ | |
// TODO Auto-generated method stub | |
InputStream sqlInputStream = getContext().getResources().openRawResource(R.raw.initial_create); | |
InputStreamReader sqlInputReader = new InputStreamReader(sqlInputStream); | |
BufferedReader sqlBufferedReader = new BufferedReader(sqlInputReader); | |
try{ | |
int c = -1; | |
String line = null; | |
String command = ""; | |
StringBuilder commandBuilder = new StringBuilder(); | |
while( (c=sqlBufferedReader.read()) != -1 ){ | |
if( String.valueOf((char) c).equals(";")){ | |
db.execSQL(commandBuilder.toString()); | |
commandBuilder = new StringBuilder(); | |
}else{ | |
commandBuilder.append((char)c); | |
} | |
} | |
}catch(IOException ex){ | |
/** | |
* We should not catch any other exception without IOException | |
* Speciall SQlException should be visible from the outer layer so that it can automatically roll back the database operations | |
*/ | |
ex.printStackTrace(); | |
}finally{ | |
FileOperationUtils.close(sqlBufferedReader); | |
} | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// TODO Auto-generated method stub | |
for(int VN=oldVersion ; VN < newVersion ; VN++){ | |
int NVN = VN + 1; | |
String VERSION_MIGRATION_NAME = "from"+"_"+VN+"_"+"to"+"_"+ NVN; | |
int VERSION_MIGRATION_RESOURCE_ID = getContext().getResources().getIdentifier(VERSION_MIGRATION_NAME, "raw", getContext().getPackageName()); | |
InputStream resourceInputStream = getContext().getResources().openRawResource(VERSION_MIGRATION_RESOURCE_ID); | |
InputStreamReader resourceInputStreamReader = new InputStreamReader(resourceInputStream); | |
BufferedReader resourceBufferedReader = new BufferedReader(resourceInputStreamReader); | |
try{ | |
int c = -1; | |
String line = null; | |
String command = ""; | |
StringBuilder commandBuilder = new StringBuilder(); | |
while( (c=resourceBufferedReader.read()) != -1 ){ | |
if( String.valueOf((char) c).equals(";")){ | |
if(!TextUtils.isEmpty( commandBuilder.toString() )){ | |
db.execSQL(commandBuilder.toString()); | |
} | |
commandBuilder = new StringBuilder(); | |
}else{ | |
commandBuilder.append((char)c); | |
} | |
} | |
}catch(IOException ex){ | |
ex.printStackTrace(); | |
}finally{ | |
FileOperationUtils.close(resourceBufferedReader); | |
} | |
if(VN==29 && NVN==30){ | |
SharedPreferences sharedPref = getContext().getSharedPreferences( | |
MigrationPreferenceHelper.KEY_MIGRATION_PREFERNECES, Context.MODE_MULTI_PROCESS); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putBoolean(MigrationPreferenceHelper.PREF_MIGRATION_29_30_NEED_ACCOUNT_SYNC , true); | |
editor.commit(); | |
} | |
} | |
} | |
public Context getContext() { | |
return mContext; | |
} | |
public void setContext(Context mContext) { | |
this.mContext = mContext; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment