Okay, this is more of a speed issue. The problem is that I am fetching three fields from varied sources like Contacts, Facebook and Google plus. The three fields are a date, name and a contact image URi. The logic is such that each of the sources writes to a File, Contacts -- to file, Facebook - append to same file, Google Plus - append to same file. This operation is pretty fast and takes the normal time to execute. This creates my original Data source. Then from this file I fetch each record (Row with three fields) individually and do some calculations and write data pertaining to each record into three different database tables. While as this works pretty fast on a Quad Core phone, the process is too slow on a dual core phone. It takes around ten minutes to process 150 records on a dual core, takle four minutes to process 150 records on Quad core. I am perplexed as in how to increase the speed? Should I drop the idea of making a file as my original Data Source and instead switch to a Database table? What would be the fastest?
The code is as follows:
private class MagicCall extends AsyncTask<Void, String, String> {
int years;
long secon;
long min;
int hours;
int mon;
int days;
int weeks;
String CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI;
ProgressDialog Asycdialog = new ProgressDialog(LoaderClass.this);
@Override
protected void onPreExecute() {
boolean DIALOG_SHOW = true;
try{
BufferedReader br = null;
if(FLAG ==1){
br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt"));
}else{
br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/output.txt"));
}
if (br.readLine() == null) {
Toast.makeText(getApplicationContext(),"There are no contacts with Birthday details, try syncing with Facebook and/or Google", Toast.LENGTH_LONG).show();
DIALOG_SHOW = false;
}
}catch(Exception e){
System.out.println(e);
}
if(DIALOG_SHOW){
//Init the LoaderDialog
Asycdialog.setMessage("Working");
Asycdialog.getWindow().setGravity(Gravity.CENTER_VERTICAL);
Asycdialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
Asycdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Asycdialog.setCancelable(false);
//Dialog Show
Asycdialog.show();
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = prefs.edit();
editor.putBoolean("async", true);
editor.commit();
super.onPreExecute();
}
protected void onPostExecute(String result) {
// hide the dialog
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, h:mm a");
String date = df.format(Calendar.getInstance().getTime());
SharedPreferences prefsx = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editorx = prefsx.edit();
editorx.putString("refresh", date);
editorx.commit();
dbHelper.close();
Asycdialog.dismiss();
startActivity(new Intent(LoaderClass.this, MainActivity.class));
finish();
super.onPostExecute(result);
}
@Override
protected String doInBackground(Void... args) {
dbHelper = new DBAdapter(getApplicationContext());
dbHelper.open();
int i=0;
int lines = 0;
// int prog = fetchAdhoc();
String progress = null;
String dateToInsert;
try{
BufferedReader reader;
if(FLAG ==1){
reader = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt"));
}else{
reader = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/output.txt"));
}
while (reader.readLine() != null) lines++;
reader.close();
}catch(Exception e){
}
Cursor c = dbHelper.fetchAdhoc();
ADHOC_COUNT = (c.getCount());
lines=lines+ADHOC_COUNT;
dbHelper.NotificationDrop();
if (c.moveToFirst()) {
do {
try{
String name = c.getString(c.getColumnIndexOrThrow("Name"));
String displayBirthday = c.getString(c.getColumnIndexOrThrow("DOB"));
String imageData = c.getString(c.getColumnIndexOrThrow("ImageData"));
String primaryAdhoc = c.getString(c.getColumnIndexOrThrow("_id"));
//CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI
CONTACT_ID = primaryAdhoc;
CONTACT_NAME = toTitleCase(name);
CONTACT_IMAGE_URI = imageData;
dateToInsert = displayBirthday;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dateToInsert);
java.sql.Date dx = new java.sql.Date(date.getTime());
Date key = dx;
years = getDiffYear(key); // For years elapsed
secon = seconds(key); // for seconds elapsed
min = seconds(key) / 60; // For minutes elapsed
hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
mon = months(String.valueOf(key)); // for months elapsed
days = daysElapsed(key); // Days elapsed
weeks = daysElapsed(key) / 7; // For weeks
System.out.println(lines);
progress = ("" + (CONTACT_NAME) + "\n"+ i + " of " + lines + " Contacts");
dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));
int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table.
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row.
//=========================================================================
//**Database Insertions Notifications Table/ SpecialNotifications Table**
//=========================================================================
//=======================================================================================//
//Regular intervals DB Insertions:
//======================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//======================================================================================//
//==============================
//For Months
//==============================
intCal.monthsNotify(mon, dateToInsert);
int monSpecial = intCal.getMonthRegular();
Date dateMonReg = intCal.getMonRegDate();
dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Weeks
//===============================
intCal.weeksToNotify(weeks,dateToInsert);
int weekSpecial = intCal.getWeekRegular();
Date dateWeekReg =intCal.getWeekRegDate();
dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Days
//===============================
intCal.daysToNotify(days, dateToInsert);
int daysSpecial= intCal.getDaysRegular();
Date dateDaysReg = intCal.getDaysRegDate();
dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For minutes
//===============================
intCal.minutesToNotify(min,dateToInsert);
long minutesSpecial= intCal.getMinutesRegular();
Date dateMinsReg = intCal.getMinutesRegDate();
dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);
//==============================
//For Years
//==============================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Seconds
//=============================
intCal.secondsToNotify(secon, dateToInsert);
long secondsSpecial= intCal.getSecondsRegular();
Date dateSecondsReg = intCal.getSecondsRegDate();
dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Hours
//=============================
intCal.hoursToNotify(hours, dateToInsert);
int hoursSpecial= intCal.getHoursRegular();
Date dateHoursReg= intCal.getHoursRegDate();
dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);
//============================================================================================//
//Special Intervals
//============================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//For Years
intCal.specialIntervalYears(years, dateToInsert);
int yearsOnceSpecial =intCal.getYearsSpecial();
Date dateYearsSpecial = intCal.getYearsSpDate();
dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Months
intCal.specialIntervalMonths(mon,dateToInsert);
int monthsOnceSpecial= intCal.getMonthsSpecial();
Date dateMonthsSpecial = intCal.getMonthsSpDate();
dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Weeks
intCal.specialIntervalsWeeks(weeks,dateToInsert);
int weeksOnceSpecial= intCal.getWeeksSpecial();
Date dateWeeksSpecial = intCal.getWeeksSpDate();
dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Days
intCal.specialIntervalsDays(days, dateToInsert);
int daysOnceSpecial= intCal.getDaysSpecial();
Date dateDaysSpecial = intCal.getDaysSpDate();
dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Hours
intCal.specialIntervalsHours(hours,dateToInsert);
int hoursOnceSpecial= intCal.getHoursSpecial();
Date dateHoursSpecial = intCal.getHoursSpDate();
dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Minutes
intCal.specialIntervalMinutes(min,dateToInsert);
long minutesOnceSpecial= intCal.getMinutesSpecial();
Date dateMinutesSpecial= intCal.getMinutesSpDate();
dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Seconds
intCal.specialIntervalsSeconds(secon,dateToInsert);
long secondsOnceSpecial= intCal.getSecondsSpecial();
Date dateSecondsSpecial= intCal.getSecondsSpDate();
dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
i++;
}catch(Exception e){
}
publishProgress(progress);
Asycdialog.setMax(lines);
Asycdialog.incrementProgressBy(1);
}while(c.moveToNext());
}
//
// Dear maintainer:
//
// Once you are done trying to 'optimise' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
//
// total_hours_wasted_here = 0;
//
//int i=0;
File toRead = null;
try{
if(FLAG ==1){
toRead=new File(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt");
}else{
toRead=new File(getApplicationContext().getFilesDir()+"/xxxinders/output.txt");
}
FileInputStream fis=new FileInputStream(toRead);
Scanner sc=new Scanner(fis);
String currentLine;
while(sc.hasNextLine()){
currentLine=sc.nextLine();
StringTokenizer st=new StringTokenizer(currentLine,"=",false);
CONTACT_NAME = toTitleCase(st.nextToken());
if(CONTACT_NAME.contains("'")){
CONTACT_NAME = CONTACT_NAME.replace("'", "");
}
// *********
String listStr = st.nextToken();
String cut = listStr.substring(1, listStr.length() - 1);
String[] array = cut.split(",");
CONTACT_ID = (array[0].trim());
String dateStr = (array[1].trim());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dateStr);
java.sql.Date dx = new java.sql.Date(date.getTime());
Date key = dx;
dateToInsert = String.valueOf(dx);
CONTACT_IMAGE_URI = (array[2].trim());
if (isCancelled()) {
break;
}
progress = ("" + Character.toUpperCase(CONTACT_NAME.charAt(0)) + CONTACT_NAME.substring(1) + "\n"+i + " of " + lines + " Contacts"); // Progress displayed here.
years = getDiffYear(key); // For years elapsed
secon = seconds(key); // for seconds elapsed
min = seconds(key) / 60; // For minutes elapsed
hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
mon = months(String.valueOf(key)); // for months elapsed
days = daysElapsed(key); // Days elapsed
weeks = daysElapsed(key) / 7; // For weeks
//===============================================================================================================
if (dateToInsert.contains("0001-") == true){ //Special Case, we added 0001 to Birthdays Which Have NO Year field.
//===========================================================================================================
dbHelper.insert(dateToInsert, CONTACT_NAME, "","", CONTACT_IMAGE_URI, "", "", "", CONTACT_ID, "", ""); // All other fields will be empty, because we don't have a Year.
int PRIMARY_ID = dbHelper.getPrimaryId();
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID);
//=====================================================================================================
//In this case we are only interested in fetching the year alert for next birthday of this contact -->
//=====================================================================================================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
}
//=========================================================================
//Case when all the Date fields exist and we set up notifications --->
//=========================================================================
else if(dateToInsert != "null" && dateToInsert.contains("0001-") != true){
dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));
int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table.
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row.
//=========================================================================
//**Database Insertions Notifications Table/ SpecialNotifications Table**
//=========================================================================
//=======================================================================================//
//Regular intervals DB Insertions:
//======================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//======================================================================================//
//==============================
//For Months
//==============================
intCal.monthsNotify(mon, dateToInsert);
int monSpecial = intCal.getMonthRegular();
Date dateMonReg = intCal.getMonRegDate();
dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Weeks
//===============================
intCal.weeksToNotify(weeks,dateToInsert);
int weekSpecial = intCal.getWeekRegular();
Date dateWeekReg =intCal.getWeekRegDate();
dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Days
//===============================
intCal.daysToNotify(days, dateToInsert);
int daysSpecial= intCal.getDaysRegular();
Date dateDaysReg = intCal.getDaysRegDate();
dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For minutes
//===============================
intCal.minutesToNotify(min,dateToInsert);
long minutesSpecial= intCal.getMinutesRegular();
Date dateMinsReg = intCal.getMinutesRegDate();
dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);
//==============================
//For Years
//==============================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Seconds
//=============================
intCal.secondsToNotify(secon, dateToInsert);
long secondsSpecial= intCal.getSecondsRegular();
Date dateSecondsReg = intCal.getSecondsRegDate();
dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Hours
//=============================
intCal.hoursToNotify(hours, dateToInsert);
int hoursSpecial= intCal.getHoursRegular();
Date dateHoursReg= intCal.getHoursRegDate();
dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);
//============================================================================================//
//Special Intervals
//============================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//For Years
intCal.specialIntervalYears(years, dateToInsert);
int yearsOnceSpecial =intCal.getYearsSpecial();
Date dateYearsSpecial = intCal.getYearsSpDate();
dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Months
intCal.specialIntervalMonths(mon,dateToInsert);
int monthsOnceSpecial= intCal.getMonthsSpecial();
Date dateMonthsSpecial = intCal.getMonthsSpDate();
dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Weeks
intCal.specialIntervalsWeeks(weeks,dateToInsert);
int weeksOnceSpecial= intCal.getWeeksSpecial();
Date dateWeeksSpecial = intCal.getWeeksSpDate();
dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Days
intCal.specialIntervalsDays(days, dateToInsert);
int daysOnceSpecial= intCal.getDaysSpecial();
Date dateDaysSpecial = intCal.getDaysSpDate();
dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Hours
intCal.specialIntervalsHours(hours,dateToInsert);
int hoursOnceSpecial= intCal.getHoursSpecial();
Date dateHoursSpecial = intCal.getHoursSpDate();
dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Minutes
intCal.specialIntervalMinutes(min,dateToInsert);
long minutesOnceSpecial= intCal.getMinutesSpecial();
Date dateMinutesSpecial= intCal.getMinutesSpDate();
dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Seconds
intCal.specialIntervalsSeconds(secon,dateToInsert);
long secondsOnceSpecial= intCal.getSecondsSpecial();
Date dateSecondsSpecial= intCal.getSecondsSpDate();
dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
writeToSD();
}
publishProgress(progress);
Asycdialog.setMax(lines);
Asycdialog.incrementProgressBy(1);
i++;
}
writeToSD();
}catch (Exception e){
System.out.println(e);
} finally{
dbHelper.close();
}
return "";
}
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Asycdialog.setMessage("" + values[0]);
}
}