Разработка Android-приложений с Augmented Reality - страница 15



import android.support.annotation.Nullable;

import android.support.v4.app.ActivityCompat;

import android.support.v4.app.FragmentActivity;

import android.view. Window;

import android.widget.Toast;


import com.beyondar.android.fragment.BeyondarFragmentSupport;

import com.beyondar.android. opengl. util. LowPassFilter;

import com.beyondar.android. world. World;

import com.google.android.gms.common.ConnectionResult;

import com.google.android.gms.common. api. GoogleApiClient;

import com.google.android.gms. location. LocationListener;

import com.google.android.gms. location. LocationRequest;

import com.google.android.gms. location. LocationServices;


public class SimpleCameraActivity extends FragmentActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient. OnConnectionFailedListener {


private BeyondarFragmentSupport mBeyondarFragment;

private World mWorld;

GoogleApiClient mGoogleApiClient;

Location mCurrentLocation;

LocationRequest mLocationRequest;


/** Called when the activity is first created. */

@Override

public void onCreate (Bundle savedInstanceState) {

super. onCreate (savedInstanceState);

// Hide the window title.

requestWindowFeature (Window. FEATURE_NO_TITLE);


setContentView(R.layout.simple_camera);


mBeyondarFragment = (BeyondarFragmentSupport) getSupportFragmentManager () .findFragmentById(R.id.beyondarFragment);


// We also can see the Frames per seconds

mBeyondarFragment.showFPS (false);


// We create the world and fill it…

mWorld = CustomWorldHelper.generateObjects (this, mCurrentLocation);

// … and send it to the fragment

mBeyondarFragment.setWorld (mWorld);

LowPassFilter.ALPHA = 0.003f;


buildGoogleApiClient ();

}


/**

* Builds a GoogleApiClient. Uses the {@code #addApi} method to request the

* LocationServices API.

*/

protected synchronized void buildGoogleApiClient () {

mGoogleApiClient = new GoogleApiClient. Builder (this)

.addConnectionCallbacks (this)

.addOnConnectionFailedListener (this)

.addApi (LocationServices. API)

.build ();

createLocationRequest ();

}


protected void createLocationRequest () {

mLocationRequest = LocationRequest.create ();


// Sets the desired interval for active location updates. This interval is

// inexact. You may not receive updates at all if no location sources are available, or

// you may receive them slower than requested. You may also receive updates faster than

// requested if other applications are requesting location at a faster interval.

mLocationRequest.setInterval (10000);


// Sets the fastest rate for active location updates. This interval is exact, and your

// application will never receive updates faster than this value.

mLocationRequest.setFastestInterval (5000);


mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

}


@Override

public void onStart () {

super. onStart ();

mGoogleApiClient.connect ();

}


@Override

public void onStop () {

super. onStop ();

mGoogleApiClient. disconnect ();

}


@Override

public void onResume () {

super. onResume ();

// Within {@code onPause ()}, we pause location updates, but leave the

// connection to GoogleApiClient intact. Here, we resume receiving

// location updates if the user has requested them.


if (mGoogleApiClient.isConnected ()) {

startLocationUpdates ();

}

}


@Override

protected void onPause () {

super. onPause ();

// Stop location updates to save battery, but don’t disconnect the GoogleApiClient object.