* #38608: first implementation of history database

History is now stored in a lightweight ORM database.
Corners cases are still not treated, but simple calls are saved properly.
diff --git a/src/org/sflphone/service/CallManagerCallBack.java b/src/org/sflphone/service/CallManagerCallBack.java
index 5ba0d67..0aeb137 100644
--- a/src/org/sflphone/service/CallManagerCallBack.java
+++ b/src/org/sflphone/service/CallManagerCallBack.java
@@ -2,7 +2,6 @@
 
 import android.content.Intent;
 import android.os.Bundle;
-import android.support.v4.content.LocalBroadcastManager;
 import android.util.Log;
 import org.sflphone.client.CallActivity;
 import org.sflphone.model.*;
@@ -39,36 +38,28 @@
         Intent intent = new Intent(CALL_STATE_CHANGED);
         intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
 
-        /*try {
-            if (mService.getCurrentCalls().get(callID) != null && mBinder.isConferenceParticipant(callID)) {
-                mService.getCurrentCalls().remove(callID);
-            }
-        } catch (RemoteException e1) {
-            e1.printStackTrace();
-        }*/
-
 
         if (newState.equals("INCOMING")) {
-            mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_INCOMING);
+            mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_INCOMING);
         } else if (newState.equals("RINGING")) {
             try {
-                mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_RINGING);
+                mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_RINGING);
             } catch (NullPointerException e) {
-                if (mService.getCurrentConfs() == null) {
+                if (mService.getConferences() == null) {
                     return;
                 }
-                if (mService.getCurrentConfs().get(callID) == null) {
+                if (mService.getConferences().get(callID) == null) {
                     Log.e(TAG, "call for " + callID + " is null");
                     return;
                 }
             }
 
         } else if (newState.equals("CURRENT")) {
-            if (mService.getCurrentConfs().get(callID) != null) {
-                mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
+            if (mService.getConferences().get(callID) != null) {
+                mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
             } else {
                 // Check if call is in a conference
-                Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
+                Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
                 while (it.hasNext()) {
                     Conference tmp = it.next().getValue();
                     for (SipCall c : tmp.getParticipants()) {
@@ -79,43 +70,43 @@
             }
 
         } else if (newState.equals("HUNGUP")) {
-
             Log.d(TAG, "Hanging up " + callID);
-            if (mService.getCurrentConfs().get(callID) != null) {
-                if (mService.getCurrentConfs().get(callID).isRinging()
-                        && mService.getCurrentConfs().get(callID).isIncoming())
-                    mService.notificationManager.publishMissedCallNotification(mService.getCurrentConfs().get(callID));
-                mService.getCurrentConfs().remove(callID);
+            if (mService.getConferences().get(callID) != null) {
+                if (mService.getConferences().get(callID).isRinging()
+                        && mService.getConferences().get(callID).isIncoming())
+                    mService.mNotificationManager.publishMissedCallNotification(mService.getConferences().get(callID));
+
+                mService.mHistoryManager.insertNewEntry(mService.getConferences().get(callID));
+                mService.getConferences().remove(callID);
             } else {
-                ArrayList<Conference> it = new ArrayList<Conference>(mService.getCurrentConfs().values());
+                ArrayList<Conference> it = new ArrayList<Conference>(mService.getConferences().values());
 
                 boolean found = false;
                 int i = 0;
                 while (!found && i < it.size()) {
                     Conference tmp = it.get(i);
 
-                    for (int j = 0; j < tmp.getParticipants().size(); ++j) {
-                        if (tmp.getParticipants().get(j).getCallId().contentEquals(callID)) {
-                            mService.getCurrentConfs().get(tmp.getId()).getParticipants().remove(tmp.getParticipants().get(j));
+                    for (SipCall call : tmp.getParticipants()) {
+                        if (call.getCallId().contentEquals(callID)) {
+                            mService.mHistoryManager.insertNewEntry(call);
+                            mService.getConferences().get(tmp.getId()).removeParticipant(call.getCallId());
                             found = true;
                         }
-
                     }
                     ++i;
-
                 }
             }
 
         } else if (newState.equals("BUSY")) {
-            mService.getCurrentConfs().remove(callID);
+            mService.getConferences().remove(callID);
         } else if (newState.equals("FAILURE")) {
-            mService.getCurrentConfs().remove(callID);
+            mService.getConferences().remove(callID);
         } else if (newState.equals("HOLD")) {
-            if (mService.getCurrentConfs().get(callID) != null) {
-                mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
+            if (mService.getConferences().get(callID) != null) {
+                mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
             } else {
                 // Check if call is in a conference
-                Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
+                Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
                 while (it.hasNext()) {
                     Conference tmp = it.next().getValue();
                     for (SipCall c : tmp.getParticipants()) {
@@ -126,11 +117,11 @@
             }
         } else if (newState.equals("UNHOLD")) {
 
-            if (mService.getCurrentConfs().get(callID) != null) {
-                mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
+            if (mService.getConferences().get(callID) != null) {
+                mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
             } else {
                 // Check if call is in a conference
-                Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
+                Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
                 while (it.hasNext()) {
                     Conference tmp = it.next().getValue();
                     for (SipCall c : tmp.getParticipants()) {
@@ -140,13 +131,9 @@
                 }
             }
         } else {
-            mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
+            mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
         }
-
-
-        Log.d(TAG, "Hanging up " + callID);
         mService.sendBroadcast(intent);
-
     }
 
     @Override
@@ -170,11 +157,11 @@
             toSend.putExtra("newcall", newCall);
             StringMap callDetails = mService.getCallManagerJNI().getCallDetails(callID);
 
-            newCall.setTimestamp_start(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
+            newCall.setTimestampStart_(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
 
             Conference toAdd = new Conference(newCall);
 
-            mService.getCurrentConfs().put(toAdd.getId(), toAdd);
+            mService.getConferences().put(toAdd.getId(), toAdd);
 
             Bundle bundle = new Bundle();
 
@@ -182,8 +169,8 @@
             toSend.putExtra("resuming", false);
             toSend.putExtras(bundle);
             mService.startActivity(toSend);
-            mService.mediaManager.startRing("");
-            mService.mediaManager.obtainAudioFocus(true);
+            mService.mMediaManager.startRing("");
+            mService.mMediaManager.obtainAudioFocus(true);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -203,24 +190,24 @@
         StringVect all_participants = mService.getCallManagerJNI().getParticipantList(confID);
         Log.w(TAG, "all_participants:" + all_participants.size());
         for (int i = 0; i < all_participants.size(); ++i) {
-            if (mService.getCurrentConfs().get(all_participants.get(i)) != null) {
-                created.addParticipant(mService.getCurrentConfs().get(all_participants.get(i)).getCallById(all_participants.get(i)));
-                mService.getCurrentConfs().remove(all_participants.get(i));
+            if (mService.getConferences().get(all_participants.get(i)) != null) {
+                created.addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
+                mService.getConferences().remove(all_participants.get(i));
             } else {
-                Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
+                Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
                 while (it.hasNext()) {
                     Conference tmp = it.next().getValue();
                     for (SipCall c : tmp.getParticipants()) {
                         if (c.getCallId().contentEquals(all_participants.get(i))) {
                             created.addParticipant(c);
-                            mService.getCurrentConfs().get(tmp.getId()).removeParticipant(c.getCallId());
+                            mService.getConferences().get(tmp.getId()).removeParticipant(c.getCallId());
                         }
                     }
                 }
             }
         }
         intent.putExtra("newconf", created);
-        mService.getCurrentConfs().put(created.getId(), created);
+        mService.getConferences().put(created.getId(), created);
         mService.sendBroadcast(intent);
     }
 
@@ -236,15 +223,15 @@
         intent.putExtra("com.savoirfairelinux.sflphone.service.newtext", bundle);
 
 
-        if (mService.getCurrentConfs().get(ID) != null) {
-            mService.getCurrentConfs().get(ID).addSipMessage(new SipMessage(true, msg));
+        if (mService.getConferences().get(ID) != null) {
+            mService.getConferences().get(ID).addSipMessage(new SipMessage(true, msg));
         } else {
-            Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
+            Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
             while (it.hasNext()) {
                 Conference tmp = it.next().getValue();
                 for (SipCall c : tmp.getParticipants()) {
                     if (c.getCallId().contentEquals(ID)) {
-                        mService.getCurrentConfs().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
+                        mService.getConferences().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
                     }
                 }
             }
@@ -259,12 +246,12 @@
         Intent intent = new Intent(CONF_REMOVED);
         intent.putExtra("confID", confID);
 
-        Conference toReInsert = mService.getCurrentConfs().get(confID);
+        Conference toReInsert = mService.getConferences().get(confID);
         /*for (int i = 0; i < toDestroy.getParticipants().size(); ++i) {
             mService.getCurrentCalls().put(toDestroy.getParticipants().get(i).getCallId(), toDestroy.getParticipants().get(i));
         }*/
-        mService.getCurrentConfs().remove(confID);
-        mService.getCurrentConfs().put(toReInsert.getId(), toReInsert);
+        mService.getConferences().remove(confID);
+        mService.getConferences().put(toReInsert.getId(), toReInsert);
         mService.sendBroadcast(intent);
 
     }
@@ -275,28 +262,22 @@
         Intent intent = new Intent(CONF_CHANGED);
         intent.putExtra("confID", confID);
         intent.putExtra("State", state);
-        ArrayList<String> all_participants;
 
-/*        try {
-            all_participants = (ArrayList<String>) mBinder.getParticipantList(intent.getStringExtra("confID"));
-            for (String participant : all_participants) {
-                if (mService.getCurrentConfs().get(confID).getParticipants().size() < all_participants.size()
-                        && mService.getCurrentCalls().get(participant) != null) { // We need to add the new participant to the conf
-                    mService.getCurrentConfs().get(confID).getParticipants()
-                            .add(mService.getCurrentCalls().get(participant));
-                    mService.getCurrentCalls().remove(participant);
-                    mService.getCurrentConfs().get(confID).setState(intent.getStringExtra("State"));
-                    mService.sendBroadcast(intent);
-                    return;
-                }
+        StringVect all_participants = mService.getCallManagerJNI().getParticipantList(intent.getStringExtra("confID"));
+        for (int i = 0; i < all_participants.size(); ++i) {
+            if (mService.getConferences().get(confID).getParticipants().size() < all_participants.size()
+                    && mService.getConferences().get(all_participants.get(i)) != null) { // We need to add the new participant to the conf
+                mService.getConferences().get(confID).addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
+                mService.getConferences().remove(all_participants.get(i));
+                mService.getConferences().get(confID).setCallState(confID, intent.getStringExtra("State"));
+                mService.sendBroadcast(intent);
+                return;
             }
-        } catch (RemoteException e) {
-            e.printStackTrace();
-        }*/
+        }
 
         Log.i(TAG, "Received" + intent.getAction());
-        if (mService.getCurrentConfs().get(confID) != null) {
-            mService.getCurrentConfs().get(confID).setCallState(confID, state);
+        if (mService.getConferences().get(confID) != null) {
+            mService.getConferences().get(confID).setCallState(confID, state);
             mService.sendBroadcast(intent);
         }
     }
@@ -306,7 +287,7 @@
         Intent intent = new Intent(RECORD_STATE_CHANGED);
         intent.putExtra("id", id);
         intent.putExtra("file", filename);
-        LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
+        mService.sendBroadcast(intent);
     }
 
 }
diff --git a/src/org/sflphone/service/SipService.java b/src/org/sflphone/service/SipService.java
index bfc6ac2..78221ba 100644
--- a/src/org/sflphone/service/SipService.java
+++ b/src/org/sflphone/service/SipService.java
@@ -24,7 +24,6 @@
  */
 package org.sflphone.service;
 
-import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -32,6 +31,7 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.sflphone.history.HistoryManager;
 import org.sflphone.model.Codec;
 import org.sflphone.model.Conference;
 import org.sflphone.model.SipCall;
@@ -73,20 +73,16 @@
     private ConfigurationManagerCallback configurationManagerCallback;
     private boolean isPjSipStackStarted = false;
 
-    public SipNotifications notificationManager;
-    public MediaManager mediaManager;
+    protected SipNotifications mNotificationManager;
+    protected HistoryManager mHistoryManager;
+    protected MediaManager mMediaManager;
 
-    /*private HashMap<String, SipCall> current_calls = new HashMap<String, SipCall>();*/
-    private HashMap<String, Conference> current_confs = new HashMap<String, Conference>();
+    private HashMap<String, Conference> mConferences = new HashMap<String, Conference>();
 
-    public HashMap<String, Conference> getCurrentConfs() {
-        return current_confs;
+    public HashMap<String, Conference> getConferences() {
+        return mConferences;
     }
 
-    /*public HashMap<String, SipCall> getCurrentCalls() {
-        return current_calls;
-    }*/
-
     @Override
     public boolean onUnbind(Intent i) {
         super.onUnbind(i);
@@ -107,11 +103,12 @@
 
         getExecutor().execute(new StartRunnable());
 
-        notificationManager = new SipNotifications(this);
-        mediaManager = new MediaManager(this);
+        mNotificationManager = new SipNotifications(this);
+        mMediaManager = new MediaManager(this);
+        mHistoryManager = new HistoryManager(this);
 
-        notificationManager.onServiceCreate();
-        mediaManager.startService();
+        mNotificationManager.onServiceCreate();
+        mMediaManager.startService();
 
     }
 
@@ -127,7 +124,7 @@
     public void onDestroy() {
         Log.i(TAG, "onDestroy");
         /* called once by stopService() */
-        notificationManager.onServiceDestroy();
+        mNotificationManager.onServiceDestroy();
 
         getExecutor().execute(new FinalizeRunnable());
         super.onDestroy();
@@ -320,10 +317,10 @@
 
                     HashMap<String, String> details = SwigNativeConverter.convertCallDetailsToNative(callManagerJNI.getCallDetails(call.getCallId()));
                     // watchout timestamp stored by sflphone is in seconds
-                    call.setTimestamp_start(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
+                    call.setTimestampStart_(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
                     Conference toAdd = new Conference(call);
-                    current_confs.put(toAdd.getId(), toAdd);
-                    mediaManager.obtainAudioFocus(false);
+                    mConferences.put(toAdd.getId(), toAdd);
+                    mMediaManager.obtainAudioFocus(false);
                 }
             });
         }
@@ -342,26 +339,26 @@
 
         @Override
         public void accept(final String callID) {
-            mediaManager.stopRing();
+            mMediaManager.stopRing();
             getExecutor().execute(new SipRunnable() {
                 @Override
                 protected void doRun() throws SameThreadException {
                     Log.i(TAG, "SipService.accept() thread running...");
                     callManagerJNI.accept(callID);
-                    mediaManager.RouteToInternalSpeaker();
+                    mMediaManager.RouteToInternalSpeaker();
                 }
             });
         }
 
         @Override
         public void hangUp(final String callID) {
-            mediaManager.stopRing();
+            mMediaManager.stopRing();
             getExecutor().execute(new SipRunnable() {
                 @Override
                 protected void doRun() throws SameThreadException {
                     Log.i(TAG, "SipService.hangUp() thread running...");
                     callManagerJNI.hangUp(callID);
-                    mediaManager.abandonAudioFocus();
+                    mMediaManager.abandonAudioFocus();
                 }
             });
         }
@@ -668,7 +665,7 @@
                 protected void doRun() throws SameThreadException, RemoteException {
                     Log.i(TAG, "SipService.addParticipant() thread running...");
                     callManagerJNI.addParticipant(call.getCallId(), confID);
-                    current_confs.get(confID).getParticipants().add(call);
+                    mConferences.get(confID).getParticipants().add(call);
                 }
             });
 
@@ -693,14 +690,14 @@
                 protected void doRun() throws SameThreadException, RemoteException {
                     Log.i(TAG, "SipService.detachParticipant() thread running...");
                     Log.i(TAG, "Detaching " + callID);
-                    Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
-                    Log.i(TAG, "current_confs size " + current_confs.size());
+                    Iterator<Entry<String, Conference>> it = mConferences.entrySet().iterator();
+                    Log.i(TAG, "mConferences size " + mConferences.size());
                     while (it.hasNext()) {
                         Conference tmp = it.next().getValue();
                         Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
                         if (tmp.contains(callID)) {
                             Conference toDetach = new Conference(tmp.getCallById(callID));
-                            current_confs.put(toDetach.getId(), toDetach);
+                            mConferences.put(toDetach.getId(), toDetach);
                             Log.i(TAG, "Call found and put in current_calls");
                         }
                     }
@@ -800,7 +797,7 @@
             // nativelist.add(swigvect.get(i));
             //
             // return nativelist;
-            return current_confs;
+            return mConferences;
         }
 
         @Override
@@ -885,10 +882,10 @@
                     Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
                     boolean result = callManagerJNI.toggleRecording(id);
 
-                    if (getCurrentConfs().containsKey(id)) {
-                        getCurrentConfs().get(id).setRecording(result);
+                    if (getConferences().containsKey(id)) {
+                        getConferences().get(id).setRecording(result);
                     } else {
-                        Iterator<Conference> it = getCurrentConfs().values().iterator();
+                        Iterator<Conference> it = getConferences().values().iterator();
                         while (it.hasNext()) {
                             Conference c = it.next();
                             if (c.getCallById(id) != null)
@@ -949,8 +946,8 @@
                 protected void doRun() throws SameThreadException, RemoteException {
                     Log.i(TAG, "SipService.sendTextMessage() thread running...");
                     callManagerJNI.sendTextMessage(callID, message.comment);
-                    if (getCurrentConfs().get(callID) != null)
-                        getCurrentConfs().get(callID).addSipMessage(message);
+                    if (getConferences().get(callID) != null)
+                        getConferences().get(callID).addSipMessage(message);
                 }
             });
 
@@ -1040,10 +1037,10 @@
 
 /*        @Override
         public Conference getCallById(String callID) throws RemoteException {
-            if(current_confs.containsKey(callID))
-                return current_confs.get(callID);
+            if(mConferences.containsKey(callID))
+                return mConferences.get(callID);
             else{
-                Iterator<Conference> it = getCurrentConfs().values().iterator();
+                Iterator<Conference> it = getConferences().values().iterator();
                 while (it.hasNext()) {
                     Conference c = it.next();
                     if (c.getCallById(callID) != null)
@@ -1068,12 +1065,12 @@
 
         @Override
         public Conference getCurrentCall() throws RemoteException {
-            for (Conference conf : current_confs.values()) {
+            for (Conference conf : mConferences.values()) {
                 if (conf.isIncoming())
                     return conf;
             }
 
-            for (Conference conf : current_confs.values()) {
+            for (Conference conf : mConferences.values()) {
                 if (conf.isOnGoing())
                     return conf;
             }
@@ -1094,12 +1091,12 @@
 
         @Override
         public List getConcurrentCalls() throws RemoteException {
-            return new ArrayList(current_confs.values());
+            return new ArrayList(mConferences.values());
         }
 
         @Override
         public Conference getConference(String id) throws RemoteException {
-            return current_confs.get(id);
+            return mConferences.get(id);
         }
 
         @Override
@@ -1183,9 +1180,9 @@
         @Override
         public void toggleSpeakerPhone(boolean toggle) throws RemoteException {
             if (toggle)
-                mediaManager.RouteToSpeaker();
+                mMediaManager.RouteToSpeaker();
             else
-                mediaManager.RouteToInternalSpeaker();
+                mMediaManager.RouteToInternalSpeaker();
         }
 
     };