Android Open Source - gasp-gcm-client Places Activity






From Project

Back to project page gasp-gcm-client.

License

The source code is released under:

Apache License

If you think the Android project gasp-gcm-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cloudbees.gasp.activity;
//from   w w w .  j av  a 2s. com
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.cloudbees.gasp.R;
import com.cloudbees.gasp.fragment.AddEventFragment;
import com.cloudbees.gasp.fragment.DeleteEventFragment;
import com.cloudbees.gasp.fragment.LocationFragment;
import com.cloudbees.gasp.fragment.NearbySearchFragment;
import com.cloudbees.gasp.fragment.PlaceDetailsFragment;
import com.cloudbees.gasp.model.EventResponse;
import com.cloudbees.gasp.model.Place;
import com.cloudbees.gasp.model.PlaceDetails;
import com.cloudbees.gasp.model.Places;
import com.cloudbees.gasp.model.Query;

import java.util.ArrayList;

/**
 * Copyright (c) 2013 Mark Prichard, CloudBees
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.
 */

public class PlacesActivity extends Activity {
    private static final String TAG = PlacesActivity.class.getName();

    private ArrayAdapter<String> mAdapter;
    private ListView mListView;
    private final ArrayList<String> mList = new ArrayList<String>();
    private final ArrayList<String> mReferenceList = new ArrayList<String>();
    private String mReference;

    private NearbySearchFragment mSearchFragment;
    private PlaceDetailsFragment mDetailsFragment;
    private AddEventFragment mAddEventFragment;
    private DeleteEventFragment mDeleteEventFragment;

    private static double lat = 37.3750274;
    private static double lng = -122.1142916;
    private static int radius = 500;
    private static String token = "";

    public static void setLocation(double latitude, double longitude) {
        lat = latitude;
        lng = longitude;
    }

    private void showLocations(Places places) {
        for (Place place : places.getResults()) {
            Log.d(TAG, place.getName() + " " + place.getReference());
            mAdapter.add(place.getName());
            mReferenceList.add(place.getReference());
        }
    }

    private void checkToken(Places places) {
        try {
            Button placesButton = (Button) findViewById(R.id.places_button);
            if (places.getNext_page_token() == null) {
                token = "";
                Log.d(TAG, "No page token returned from Places API");
                placesButton.setEnabled(false);
            } else {
                token = places.getNext_page_token();
                Log.d(TAG, "Next page token: " + token);
                setButtonText(R.string.places_button_locations_next);
                placesButton.setEnabled(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showDetails(PlaceDetails placeDetails) {
        try {
            Intent intent = new Intent();
            intent.setClass(PlacesActivity.this, PlacesDetailActivity.class);
            intent.putExtra(PlacesDetailActivity.PLACES_DETAIL_SERIALIZED, placeDetails.getResult());
            intent.putExtra(PlacesDetailActivity.PLACES_DETAIL_REFERENCE, mReference);
            startActivityForResult(intent, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addPlacesFragments() {
        mSearchFragment = new NearbySearchFragment() {
            public void onSuccess(Places places) {
                showLocations(places);
                checkToken(places);
            }

            public void onFailure(String status) {
                Log.e(TAG, "Google Places API search failed: status = " + status);
            }
        };

        mDetailsFragment = new PlaceDetailsFragment() {
            @Override
            public void onSuccess(PlaceDetails placeDetails) {
                showDetails(placeDetails);
            }

            @Override
            public void onFailure(String status) {
                Log.e(TAG, "Google Places API search failed: status = " + status);
            }
        };

        mAddEventFragment = new AddEventFragment() {
            @Override
            public void onSuccess(EventResponse eventResponse) {
                Log.d(TAG, "Event Added: " + eventResponse.getEvent_id());
            }

            @Override
            public void onFailure(String status) {
                Log.e(TAG, "Google Places API search failed: status = " + status);
            }
        };

        mDeleteEventFragment = new DeleteEventFragment() {
            @Override
            public void onSuccess(EventResponse eventResponse) {
                Log.d(TAG, "Event Deleted");
            }

            @Override
            public void onFailure(String status) {
                Log.e(TAG, "Google Places API search failed: status = " + status);
            }
        };

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(mSearchFragment, getString(R.string.fragment_location_search));
        ft.add(mDetailsFragment, getString(R.string.fragment_place_details));
        ft.add(mAddEventFragment, getString(R.string.fragment_add_event));
        ft.add(mDeleteEventFragment, getString(R.string.fragment_delete_event));
        ft.commit();
    }

    private void getLocations() {
        try {
            SharedPreferences gaspSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            radius = Integer.valueOf(gaspSharedPreferences.getString(getString(R.string.places_search_radius_preferences), ""));
            Query query = new Query(lat, lng, radius, token);
            mSearchFragment.nearbySearch(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addButtonListener() {
        Button placesButton = (Button) findViewById(R.id.places_button);
        placesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getLocations();
            }
        });
    }

    private void addListViewAdapter() {
        try {
            // Use simple TextView layout for ArrayAdapter constructor and map to ListView
            setContentView(R.layout.gasp_places_layout);
            mListView = (ListView) findViewById(R.id.places_list);
            mAdapter = new ArrayAdapter<String>(this, R.layout.gasp_generic_textview, mList);
            mListView.setAdapter(mAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addItemClickListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "Clicked: item #" + id + ", "
                        + mList.get((int) id) + " ["
                        + mReferenceList.get((int) id) + "]");
                mReference = mReferenceList.get((int) id);
                Query query = new Query(mReferenceList.get((int) id));
                mDetailsFragment.placeDetails(query);
            }
        });
    }

    private void setButtonText(int resId) {
        Button placesButton = (Button) findViewById(R.id.places_button);
        placesButton.setText(resId);
    }

    private void checkLocation() {
        try {
            Location location = LocationFragment.getLocation(this);
            if (location != null) {
                setLocation(location.getLatitude(), location.getLongitude());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

        getActionBar().setDisplayHomeAsUpEnabled(true);

        try {
            checkLocation();
            addPlacesFragments();
            addListViewAdapter();
            addButtonListener();
            addItemClickListener();
            getLocations();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        token = "";
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;

            case R.id.gasp_settings:
                Intent intent = new Intent();
                intent.setClass(this, SetPreferencesActivity.class);
                startActivityForResult(intent, 0);
                return true;

            case R.id.options_exit:
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}




Java Source Code List

com.cloudbees.gasp.activity.ConsoleActivityTest.java
com.cloudbees.gasp.activity.ConsoleActivity.java
com.cloudbees.gasp.activity.PlacesActivityTest.java
com.cloudbees.gasp.activity.PlacesActivity.java
com.cloudbees.gasp.activity.PlacesDetailActivityTest.java
com.cloudbees.gasp.activity.PlacesDetailActivity.java
com.cloudbees.gasp.activity.RestaurantListActivityTest.java
com.cloudbees.gasp.activity.RestaurantListActivity.java
com.cloudbees.gasp.activity.ReviewActivity.java
com.cloudbees.gasp.activity.ReviewListActivityTest.java
com.cloudbees.gasp.activity.ReviewListActivity.java
com.cloudbees.gasp.activity.SetPreferencesActivity.java
com.cloudbees.gasp.activity.TestParams.java
com.cloudbees.gasp.activity.TwitterStreamActivityTest.java
com.cloudbees.gasp.activity.TwitterStreamActivity.java
com.cloudbees.gasp.activity.UserListActivityTest.java
com.cloudbees.gasp.activity.UserListActivity.java
com.cloudbees.gasp.adapter.DataAdapterQueryTest.java
com.cloudbees.gasp.adapter.DatabaseTest.java
com.cloudbees.gasp.adapter.GaspDataAdapter.java
com.cloudbees.gasp.adapter.GaspSQLiteHelper.java
com.cloudbees.gasp.adapter.RestaurantArrayAdapter.java
com.cloudbees.gasp.adapter.RestaurantDataAdapter.java
com.cloudbees.gasp.adapter.ReviewArrayAdapter.java
com.cloudbees.gasp.adapter.ReviewDataAdapter.java
com.cloudbees.gasp.adapter.UserArrayAdapter.java
com.cloudbees.gasp.adapter.UserDataAdapter.java
com.cloudbees.gasp.fragment.AddEventFragment.java
com.cloudbees.gasp.fragment.DeleteEventFragment.java
com.cloudbees.gasp.fragment.GaspDatabaseFragment.java
com.cloudbees.gasp.fragment.GaspRestaurantFragment.java
com.cloudbees.gasp.fragment.GaspReviewFragment.java
com.cloudbees.gasp.fragment.LocationFragment.java
com.cloudbees.gasp.fragment.NearbySearchFragment.java
com.cloudbees.gasp.fragment.PlaceDetailsFragment.java
com.cloudbees.gasp.fragment.PreferencesFragment.java
com.cloudbees.gasp.fragment.RESTResponderFragment.java
com.cloudbees.gasp.fragment.TwitterAuthenticationFragment.java
com.cloudbees.gasp.fragment.TwitterResponderFragment.java
com.cloudbees.gasp.gcm.GCMBroadcastReceiver.java
com.cloudbees.gasp.gcm.GCMIntentService.java
com.cloudbees.gasp.gcm.GCMRegistration.java
com.cloudbees.gasp.location.GooglePlacesClient.java
com.cloudbees.gasp.location.NearbySearchTest.java
com.cloudbees.gasp.location.PlaceDetailsTest.java
com.cloudbees.gasp.location.PlaceEventsTest.java
com.cloudbees.gasp.model.EventRequest.java
com.cloudbees.gasp.model.EventResponse.java
com.cloudbees.gasp.model.GaspDataObject.java
com.cloudbees.gasp.model.Geometry.java
com.cloudbees.gasp.model.Location.java
com.cloudbees.gasp.model.ModelObjectTest.java
com.cloudbees.gasp.model.PlaceDetail.java
com.cloudbees.gasp.model.PlaceDetails.java
com.cloudbees.gasp.model.PlaceEvent.java
com.cloudbees.gasp.model.Place.java
com.cloudbees.gasp.model.Places.java
com.cloudbees.gasp.model.Query.java
com.cloudbees.gasp.model.Restaurant.java
com.cloudbees.gasp.model.Review.java
com.cloudbees.gasp.model.TwitterStatus.java
com.cloudbees.gasp.model.TwitterStatuses.java
com.cloudbees.gasp.model.TwitterTokenResponse.java
com.cloudbees.gasp.model.User.java
com.cloudbees.gasp.robotium.NavigationTest.java
com.cloudbees.gasp.server.GaspEntityTest.java
com.cloudbees.gasp.server.GaspRestaurantTest.java
com.cloudbees.gasp.server.GaspReviewTest.java
com.cloudbees.gasp.server.GaspServerAPI.java
com.cloudbees.gasp.service.AsyncRESTClient.java
com.cloudbees.gasp.service.AsyncRestTestAll.java
com.cloudbees.gasp.service.AsyncRestTestIndex.java
com.cloudbees.gasp.service.IRESTListener.java
com.cloudbees.gasp.service.RESTIntentService.java
com.cloudbees.gasp.service.RestaurantSyncServiceTest.java
com.cloudbees.gasp.service.RestaurantSyncService.java
com.cloudbees.gasp.service.RestaurantUpdateServiceTest.java
com.cloudbees.gasp.service.RestaurantUpdateService.java
com.cloudbees.gasp.service.ReviewSyncServiceTest.java
com.cloudbees.gasp.service.ReviewSyncService.java
com.cloudbees.gasp.service.ReviewUpdateServiceTest.java
com.cloudbees.gasp.service.ReviewUpdateService.java
com.cloudbees.gasp.service.TwitterServiceTest.java
com.cloudbees.gasp.service.UserSyncServiceTest.java
com.cloudbees.gasp.service.UserSyncService.java
com.cloudbees.gasp.service.UserUpdateServiceTest.java
com.cloudbees.gasp.service.UserUpdateService.java
com.cloudbees.gasp.twitter.TwitterAPI.java
com.cloudbees.gasp.twitter.TwitterAuthServiceTest.java
com.cloudbees.gasp.twitter.TwitterAuthentication.java