SQLLiteDatabase error while using DetectedActivities API from google maps

323 Views Asked by At

I get an error from sqllite that says something like this. Now I checked the logs, and it always seems to appear random. It's never after the same funcion or log line. This is the error:

12-10 16:12:19.397: E/SQLiteDatabase(6496): Error inserting context_name=6 end_time=1449760334152 context_family=1 module_id=com.google.android.contextmanager.module.DetectedActivityProducer version=1 sync_state_mod_time_millis=1449760339393 start_time=1449760329012 sync_state=0 context_id=18d4afbc-7563-403a-84d5-95bab5c64304 time_type=3 proto_blob=[B@9fafac0
12-10 16:12:19.397: E/SQLiteDatabase(6496): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: context.context_id (code 2067)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.a(SourceFile:445)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.b(SourceFile:420)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.a(SourceFile:370)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.b(SourceFile:400)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.r.a(SourceFile:381)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.a.j.a(SourceFile:58)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.a.a.run(SourceFile:52)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.i.a(SourceFile:263)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.i.handleMessage(SourceFile:254)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.os.Looper.loop(Looper.java:148)
12-10 16:12:19.397: E/SQLiteDatabase(6496):     at android.os.HandlerThread.run(HandlerThread.java:61)
12-10 16:12:19.401: E/SQLiteDatabase(6496): Error inserting context_name=6 end_time=1449760339330 context_family=1 module_id=com.google.android.contextmanager.module.DetectedActivityProducer version=1 sync_state_mod_time_millis=1449760339399 start_time=1449760334152 sync_state=0 context_id=d6726de4-a445-421d-a437-e66bbdee5c2d time_type=3 proto_blob=[B@8c373f9
12-10 16:12:19.401: E/SQLiteDatabase(6496): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: context.context_id (code 2067)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.a(SourceFile:445)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.b(SourceFile:420)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.a(SourceFile:370)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.ak.b(SourceFile:400)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.q.r.a(SourceFile:381)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.a.j.a(SourceFile:58)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.a.a.run(SourceFile:52)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.i.a(SourceFile:263)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at com.google.android.contextmanager.g.i.handleMessage(SourceFile:254)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.os.Looper.loop(Looper.java:148)
12-10 16:12:19.401: E/SQLiteDatabase(6496):     at android.os.HandlerThread.run(HandlerThread.java:61)

This is my onReceive broadcast from the DetectedActivity API:

   @Override
public void onReceive(Context context, Intent intent) {
    ArrayList<DetectedActivity> updatedActivities =
            intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
    int type = 0x0;
    Integer confidence = 0;
    Utils.appendLog("MOTION ON RECEIVE BROADCAST RECEIVER",true);
    Log.i("","autopilot-----------------------------------------------");
    String rawData = "(";
    for (DetectedActivity detectedActivity : updatedActivities){
        if(detectedActivity.getConfidence() >= 25){
            rawData += detectedActivity.getType() +":" + detectedActivity.getConfidence() + ",";
            Log.i("", "autopilot detected activity: " + detectedActivity);
            if(confidence < detectedActivity.getConfidence()){
                confidence = detectedActivity.getConfidence();
            }
            if(detectedActivity.getType() == DetectedActivity.STILL){
                type |= PSMotionService.stationary;
            }else if(detectedActivity.getType() == DetectedActivity.IN_VEHICLE){
                type |= PSMotionService.automotive;
            }else if(detectedActivity.getType() == DetectedActivity.ON_BICYCLE){
                type |= PSMotionService.bicycling;
            }else if(detectedActivity.getType() == DetectedActivity.ON_FOOT || detectedActivity.getType() == DetectedActivity.WALKING || detectedActivity.getType() == DetectedActivity.TILTING){
                type |= PSMotionService.walking;
            }else if(detectedActivity.getType() == DetectedActivity.RUNNING){
                type |= PSMotionService.running;
            }else if(detectedActivity.getType() == DetectedActivity.UNKNOWN){
                type |= PSMotionService.unknown;
            }
        }
    }
    rawData += ")";
    Long timestamp = System.currentTimeMillis()/1000;
    Integer confidenceFlag = 0;
    if(confidence >= 25 && confidence < 75){
        confidenceFlag = 1;
    }else if(confidence >=75){
        confidenceFlag = 2;
    }
    Log.i("", "autopilot type is:" + type + "... timestamp: " + timestamp + "....confidence" + confidence + "...confidenceFlag:" + confidenceFlag);
    Log.i("", "autopilot-----------------------------------------------END");
    Motion activity = new Motion(timestamp, type, confidenceFlag);
    Utils.appendLog("NEW MOTION: [" + activity.getTimestamp() + ", " + activity.getType() + ", " + activity.getConfidence() + "]" + rawData, true);
    if(PSTripDBFactory.getInstance(context).getActiveTrip() != null){
        PSMotionService.getInstance(context).motionsTrip.add(activity);
        Log.i("", "autopilot added to trip: size is:" + PSMotionService.getInstance(context).motionsTrip.size());
        if(PSLocationCenter.getInstance().pref.getGeoEnabled(context)) {
            appendMotionActivity(context, type, activity);
        }
    }else{
        Log.i("", "autopilot test to add to buffer");
        appendMotionActivity(context, type, activity);
    }
    Log.i("", "autopilot-----------------------------------------------END AAAAALLLLLLL");
}

public void appendMotionActivity(Context context, int type, Motion activity) {
    if(type != 0){
        Log.i("", "autopilot appendMotionActivity type != 0");
        Motion lastActivity = null;
        int size = PSMotionService.getInstance(context).autoPilotPreCheckinMotionsBuffer.size();
        if(size > 0){
            lastActivity = PSMotionService.getInstance(context).autoPilotPreCheckinMotionsBuffer.get(size-1);
        }
        PSMotionService.getInstance(context).autoPilotPreCheckinMotionsBuffer.add(activity);
        if(lastActivity != null){
            Long duration = activity.getTimestamp() - lastActivity.getTimestamp();
            Utils.appendLog("MOTION autoPilotPreCheckinMotionsBuffer motion size" + PSMotionService.getInstance(context).autoPilotPreCheckinMotionsBuffer.size(),true);
            if(lastActivity != null && type * Math.min(activity.getConfidence(), 1) <= 1){
                Log.i("", "autopilot received either low-confidence motion or stationary motion");
                // received either low-confidence motion or stationary motion
                Integer lastType = lastActivity.getType();
                PSMotionService.getInstance(context).stationaryPeriod += duration;
                if(lastType == PSMotionService.stationary && PSMotionService.getInstance(context).stationaryPeriod > 10){
                    Log.i("","autopilot Stationary/unknown motion for "+ PSMotionService.getInstance(context).stationaryPeriod  +" secs > resetting auto-pilot motions buffer.");
                    Utils.appendLog("CHECKIN TIME STATIONARY for more than 10 seconds, checkin will be null", true);
                    PSMotionService.getInstance(context).autoPilotPreCheckinMotionsBuffer.clear();
                    PSMotionService.getInstance(context).stationaryPeriod = 0l;
                    PSMotionService.getInstance(context).movingPeriod = 0l;
                    PSMotionService.getInstance(context).checkinTime = null;
                    PSLocationCenter.getInstance().stopLocationClient();
                }
            }else{
                PSMotionService.getInstance(context).movingPeriod += duration;
            }
        }
        PSMotionService.getInstance(context).maybeTriggerAutoPilotFromMotionBuffer();
    }
}

This is how I call the activities:

   ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                    mGoogleApiClient,
                    Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
                    getActivityDetectionPendingIntent()
            ).setResultCallback(this);

Now I checked and I don't add locations to nothing that is in my database (I use REALM.io library for my database). So This error doesn't make sense for me at all. Did anyone else encountered this? Is there a way to fix this?

PS: I get this errors on a Nexus 5 with Android 6

0

There are 0 best solutions below