FileDocCategorySizeDatePackage
BordeauxSessionManager.javaAPI DocAndroid 5.1 API7539Thu Mar 12 22:22:48 GMT 2015android.bordeaux.services

BordeauxSessionManager

public class BordeauxSessionManager extends Object

Fields Summary
private static final String
TAG
private BordeauxSessionStorage
mSessionStorage
PeriodicSave
mSavingThread
private ConcurrentHashMap
mSessions
Constructors Summary
public BordeauxSessionManager(android.content.Context context)


        
        mSessionStorage = new BordeauxSessionStorage(context);
        mSavingThread.start();
    
Methods Summary
public android.os.IBindergetSessionBinder(java.lang.Class learnerClass, android.bordeaux.services.BordeauxSessionManager$SessionKey key)

        if (mSessions.containsKey(key.value)) {
            return mSessions.get(key.value).learner.getBinder();
        }
        // not in memory cache
        try {
            // try to find it in the database
            Session stored = mSessionStorage.getSession(key.value);
            if (stored != null) {
                // set the callback, so that we can save the state
                stored.learner.setModelChangeCallback(new LearningUpdateCallback(key.value));
                // found session in the storage, put in the cache
                mSessions.put(key.value, stored);
                return stored.learner.getBinder();
            }

            // if session is not already stored, create a new one.
            Log.i(TAG, "create a new learning session: " + key.value);
            IBordeauxLearner learner =
                    (IBordeauxLearner) learnerClass.getConstructor().newInstance();
            // set the callback, so that we can save the state
            learner.setModelChangeCallback(new LearningUpdateCallback(key.value));
            Session session = new Session();
            session.learnerClass = learnerClass;
            session.learner = learner;
            mSessions.put(key.value, session);
            return learner.getBinder();
        } catch (Exception e) {
            throw new RuntimeException("Can't instantiate class: " +
                                       learnerClass.getName());
        }
    
public android.bordeaux.services.BordeauxSessionManager$SessionKeygetSessionKey(java.lang.String callingUid, java.lang.Class learnerClass, java.lang.String name)

        SessionKey key = new SessionKey();
        key.value = callingUid + "#" + "_" + name + "_" + learnerClass.getName();
        return key;
    
public voidloadSessions()

        synchronized(mSessions) {
            mSessionStorage.getAllSessions(mSessions);
            for (Map.Entry<String, Session> session : mSessions.entrySet()) {
                // set the callback, so that we can save the state
                session.getValue().learner.setModelChangeCallback(
                        new LearningUpdateCallback(session.getKey()));
            }
        }
    
public voidremoveAllSessionsFromCaller(java.lang.String callingUid)

        // remove in the hash table
        ArrayList<String> remove_keys = new ArrayList<String>();
        for (Map.Entry<String, Session> session : mSessions.entrySet()) {
            if (session.getKey().startsWith(callingUid + "#")) {
                remove_keys.add(session.getKey());
            }
        }
        for (String key : remove_keys) {
            mSessions.remove(key);
        }
        // remove all session data from the callingUid in database
        // % is used as wild match for the rest of the string in sql
        int nDeleted = mSessionStorage.removeSessions(callingUid + "#%");
        if (nDeleted > 0)
            Log.i(TAG, "Successfully deleted " + nDeleted + "sessions");
    
public booleansaveSession(android.bordeaux.services.BordeauxSessionManager$SessionKey key)

        Session session = mSessions.get(key.value);
        if (session != null) {
            synchronized(session) {
                byte[] model = session.learner.getModel();

                // write to database
                boolean res = mSessionStorage.saveSession(key.value, session.learnerClass, model);
                if (res)
                    session.modified = false;
                else {
                    Log.e(TAG, "Can't save session: " + key.value);
                }
                return res;
            }
        }
        Log.e(TAG, "Session not found: " + key.value);
        return false;
    
public voidsaveSessions()

        for (Map.Entry<String, Session> session : mSessions.entrySet()) {
            synchronized(session) {
                // Save the session if it's modified.
                if (session.getValue().modified) {
                    SessionKey skey = new SessionKey();
                    skey.value = session.getKey();
                    saveSession(skey);
                }
            }
        }