androidandroid-intentandroid-broadcastreceiveractivity-recognition

ActivityDetectionBroadcastReceiver does not compile after updating to GooglePlayServices 26.0.2


This is my onReceive function:

    @Override
public void onReceive(Context context, Intent intent) {
    ArrayList<DetectedActivity> updatedActivities =
            intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
    int type = 0x0;
    Integer confidence = 0;
    Collections.sort(updatedActivities, new Comparator<DetectedActivity>() {
        @Override
        public int compare(DetectedActivity lhs, DetectedActivity rhs) {
            if(lhs.getConfidence() > rhs.getConfidence()){
                return -1;
            }else if(lhs.getConfidence() > rhs.getConfidence()){
                return 1;
            }
            return 0;
        }
    });
    String rawData = "(";
    for (DetectedActivity detectedActivity : updatedActivities){
        if(detectedActivity.getConfidence() >= 25){
            if(type==PSMotionService.unknown || type==PSMotionService.stationary) {
                rawData += detectedActivity.getType() + ":" + detectedActivity.getConfidence() + ",";
                Log.i("", "autopilot type 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) {
                    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, "D", Constants.TRACKER);
    if(PSTrip.getActiveTrip() != null){
        PSMotionService.getInstance(context).motionsTrip.add(activity);
        Log.i("", "autopilot added to trip: size is:" + PSMotionService.getInstance(context).motionsTrip.size());
        if(PSApplicationClass.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");
}

And I would extract the DetectedActivity List from the intent like this:

        ArrayList<DetectedActivity> updatedActivities =
            intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);

But now I get this issue:

/Users/alinrosu/Workspace/passenger-android/passenger/src/main/java/nl/hgrams/passenger/listeners/ActivityDetectionBroadcastReceiver.java
Error:(31, 51) error: incompatible types: inferred type does not conform to upper bound(s)
inferred: DetectedActivity
upper bound(s): Parcelable
Error:Execution failed for task ':passenger:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

How can I extract the DetectedActivity list, without getting this issue?


Solution

  • The problem is that you are using the language level settings for Java 8, and this tightens some restrictions on automatic casting that were OK in Java 7 and lower. See Why does this Java 8 program not compile? for some more details.

    I don't have something to test this with, so I can't say for sure if this will work, but you could first try to cast it:

    ArrayList<DetectedActivity> updatedActivities = 
        (ArrayList<DetectedActivity>)intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
    

    If that doesn't work, do it this way:

    ArrayList<Parcelable> updatedActivities = intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
    

    Now, when you use the array, you'll need to cast each element, like this:

    for (Parcelable p : updatedActivities) {
        // cast to correct type
        DetectedActivity detectedActivity = (DetectedActivity)p;
        // do whatever with "detectedActivity"...
    }