com.adstrosoftware.gpsplayground.activityrecognizer.ActivityRecognizerFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.adstrosoftware.gpsplayground.activityrecognizer.ActivityRecognizerFragment.java

Source

/**
 * Copyright 2013 Adstro Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.adstrosoftware.gpsplayground.activityrecognizer;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.adstrosoftware.gpsplayground.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.ActivityRecognitionClient;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

/**
 * TODO
 *
 * @author Adam Stroud &#60;<a href="mailto:adstro@adstrosoftware.com">adstro@adstrosoftware.com</a>&#62;
 */
public class ActivityRecognizerFragment extends ListFragment implements
        GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

    private static final String ACTION_ACTIVITY_UPDATE = "com.adstrosoftware.gpsplayground.action.ACTIVITY_UPDATE";
    private static final long UPDATE_INTERVAL = 0;
    private static final int CONFIDENCE_THRESHOLD = 0;

    private ActivityRecognitionClient client;
    private PendingIntent updateIntent;
    private ActivityUpdateBroadcastReceiver activityUpdateBroadcastReceiver;
    private DetectedActivityAdapter adapter;

    /**
     * No-arg constructor needed by Android
     */
    public ActivityRecognizerFragment() {
        // no-op
    }

    public static ActivityRecognizerFragment newInstance() {
        return new ActivityRecognizerFragment();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        adapter = new DetectedActivityAdapter(getActivity());
        setListAdapter(adapter);
        getListView().setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        client = new ActivityRecognitionClient(getActivity(), this, this);
        updateIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(ACTION_ACTIVITY_UPDATE),
                PendingIntent.FLAG_UPDATE_CURRENT);
        activityUpdateBroadcastReceiver = new ActivityUpdateBroadcastReceiver();
    }

    @Override
    public void onResume() {
        super.onResume();

        getActivity().registerReceiver(activityUpdateBroadcastReceiver, new IntentFilter(ACTION_ACTIVITY_UPDATE));

        client.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(activityUpdateBroadcastReceiver);
        client.removeActivityUpdates(updateIntent);
    }

    @Override
    public void onConnected(Bundle bundle) {
        client.requestActivityUpdates(UPDATE_INTERVAL, updateIntent);
    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    private class ActivityUpdateBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ActivityRecognitionResult.hasResult(intent)) {
                ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

                DetectedActivity activity = result.getMostProbableActivity();

                if (activity.getConfidence() >= CONFIDENCE_THRESHOLD) {

                    adapter.add(activity);
                }
            }
        }
    }

    /**
     * TODO
     */
    private class DetectedActivityAdapter extends ArrayAdapter<DetectedActivity> {
        public DetectedActivityAdapter(Context context) {
            super(context, android.R.layout.simple_list_item_1);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) super.getView(position, convertView, parent);

            DetectedActivity activity = adapter.getItem(position);
            int activityNameResId = R.string.n_a;

            switch (activity.getType()) {
            case DetectedActivity.IN_VEHICLE:
                activityNameResId = R.string.inVehicle;
                break;
            case DetectedActivity.ON_BICYCLE:
                activityNameResId = R.string.onBicycle;
                break;
            case DetectedActivity.ON_FOOT:
                activityNameResId = R.string.onFoot;
                break;
            case DetectedActivity.STILL:
                activityNameResId = R.string.still;
                break;
            case DetectedActivity.TILTING:
                activityNameResId = R.string.tilting;
                break;
            case com.google.android.gms.location.DetectedActivity.UNKNOWN:
                activityNameResId = R.string.unknown;
                break;
            }

            textView.setText(getContext().getString(activityNameResId) + ":" + activity.getConfidence());

            if (position == (getCount() - 1)) {
                textView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
            } else {
                textView.setBackgroundColor(getResources().getColor(android.R.color.white));
            }

            return textView;
        }
    }
}