edu.northwestern.cbits.activitydetector.app.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for edu.northwestern.cbits.activitydetector.app.MainActivity.java

Source

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * 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 edu.northwestern.cbits.activitydetector.app;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import junit.framework.Test;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Sample application that demonstrates the use of
 * ActivityRecognitionClient}. It registers for activity detection updates
 * at a rate of 1 seconds, logs them to a file, and displays the detected
 * activities with their associated confidence levels.
 * An IntentService receives activity detection updates in the background
 * so that detection can continue even if the Activity is not visible.
 */
public class MainActivity extends Activity {

    // setup UI refresh handler
    int i = 0;
    TextView nameView;
    final Handler mHandler = new Handler();

    // Store the current request type (ADD or REMOVE)
    public ActivityUtils.REQUEST_TYPE mRequestType;

    // The activity recognition update request object
    public DetectionRequester mDetectionRequester;

    // The activity recognition update removal object
    public DetectionRemover mDetectionRemover;

    private void UpdateGUI() {
        i++;

        final MainActivity me = this;

        // Runs once a second
        mHandler.post(new Runnable() {
            public void run() {
                Log.d(getString(R.string.app_name), ActivityRecognitionIntentService.ACTIVITY_NAME);
                nameView.setText(
                        ActivityRecognitionIntentService.ACTIVITY_NAME + " " + System.currentTimeMillis() / 1000);

                //setting up tone for on_foot
                Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                MediaPlayer mediaPlayer = new MediaPlayer();

                try {
                    mediaPlayer.setDataSource(me, defaultRingtoneUri);
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                    mediaPlayer.prepare();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.release();
                        }
                    });

                    if (ActivityRecognitionIntentService.ACTIVITY_NAME == "on_foot") {
                        mediaPlayer.start();
                    } else {
                        mediaPlayer.release();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ;
            }
        });
    }

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

        // Get detection requester and remover objects
        mDetectionRequester = new DetectionRequester(this);
        mDetectionRemover = new DetectionRemover(this);

        mDetectionRequester.requestUpdates();

        // Set the main layout
        setContentView(R.layout.activity_main);

        nameView = (TextView) this.findViewById(R.id.activity);
        nameView.setText(ActivityRecognitionIntentService.ACTIVITY_NAME);

        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                UpdateGUI();
            }
        }, 0, 1000);

    }

    /*
     * Handle results returned to this Activity by other Activities started with
     * startActivityForResult(). In particular, the method onConnectionFailed() in
     * DetectionRemover and DetectionRequester may call startResolutionForResult() to
     * start an Activity that handles Google Play services problems. The result of this
     * call returns here, to onActivityResult.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        // Choose what to do based on the request code
        switch (requestCode) {

        // If the request code matches the code sent in onConnectionFailed
        case ActivityUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST:

            switch (resultCode) {
            // If Google Play services resolved the problem
            case Activity.RESULT_OK:

                // If the request was to start activity recognition updates
                if (ActivityUtils.REQUEST_TYPE.ADD == mRequestType) {

                    // Restart the process of requesting activity recognition updates
                    mDetectionRequester.requestUpdates();

                    // If the request was to remove activity recognition updates
                } else if (ActivityUtils.REQUEST_TYPE.REMOVE == mRequestType) {

                    /*
                     * Restart the removal of all activity recognition updates for the 
                     * PendingIntent.
                     */
                    mDetectionRemover.removeUpdates(mDetectionRequester.getRequestPendingIntent());

                }
                break;

            // If any other result was returned by Google Play services
            default:

                // Report that Google Play services was unable to resolve the problem.
                Log.d(ActivityUtils.APPTAG, getString(R.string.no_resolution));
            }

            // If any other request code was received
        default:
            // Report that this Activity received an unknown requestCode
            Log.d(ActivityUtils.APPTAG, getString(R.string.unknown_activity_request_code, requestCode));

            break;
        }
    }

    /*
     * Register the broadcast receiver and update the log of activity updates
     */
    @Override
    protected void onResume() {
        super.onResume();

        // Check for Google Play services
        if (!servicesConnected()) {

            return;
        }

        /*
         * Set the request type. If a connection error occurs, and Google Play services can
         * handle it, then onActivityResult will use the request type to retry the request
         */
        mRequestType = ActivityUtils.REQUEST_TYPE.ADD;

        // Pass the update request to the requester object
        mDetectionRequester.requestUpdates();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    /*
     * Unregister the receiver during a pause
     */
    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * Verify that Google Play services is available before making a request.
     *
     * @return true if Google Play services is available, otherwise false
     */
    private boolean servicesConnected() {

        // Check that Google Play services is available
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {

            // In debug mode, log the status
            Log.d(ActivityUtils.APPTAG, getString(R.string.play_services_available));

            // Continue
            return true;

            // Google Play services was not available for some reason
        } else {

            // Display an error dialog
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0).show();
            return false;
        }
    }

}