Android Open Source - mobile-shopping-assistant-appengine Main Activity






From Project

Back to project page mobile-shopping-assistant-appengine.

License

The source code is released under:

Apache License

If you think the Android project mobile-shopping-assistant-appengine 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

/* Copyright (c) 2012 Google Inc.
 */*from  w w  w.j  a v  a  2s. c  om*/
 * 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 ch.yereaztian.mobileassistant;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.json.jackson2.JacksonFactory;
import ch.yereaztian.mobileassistant.checkinendpoint.Checkinendpoint;
import ch.yereaztian.mobileassistant.checkinendpoint.model.CheckIn;
import ch.yereaztian.mobileassistant.placeendpoint.Placeendpoint;
import ch.yereaztian.mobileassistant.placeendpoint.model.Place;
import ch.yereaztian.mobileassistant.placeendpoint.model.CollectionResponsePlace;

import android.app.Activity;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.content.Intent;

public class MainActivity extends Activity {

  private ListView placesList;
  private List<Place> places = null;
  

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    placesList = (ListView) findViewById(R.id.PlacesList);

    new CheckInTask().execute();

    // start retrieving the list of nearby places
    new ListOfPlacesAsyncRetriever().execute();
    
    placesList.setOnItemClickListener(placesListClickListener);
  }


  /**
   * AsyncTask for calling Mobile Assistant API for checking into a 
   * place (e.g., a store).
   */
  private class CheckInTask extends AsyncTask<Void, Void, Void> {

    /**
     * Calls appropriate CloudEndpoint to indicate that user checked into a place.
     *
     * @param params the place where the user is checking in.
     */
    @Override
    protected Void doInBackground(Void... params) {
      CheckIn checkin = new ch.yereaztian.mobileassistant.checkinendpoint.model.CheckIn();

      // Set the ID of the store where the user is.
      checkin.setPlaceId("StoreNo123");

      Checkinendpoint.Builder builder = new Checkinendpoint.Builder(
          AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);

      builder = CloudEndpointUtils.updateBuilder(builder);

      Checkinendpoint endpoint = builder.build();


      try {
        endpoint.insertCheckIn(checkin).execute();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      return null;
    }
  }


  /**
   * AsyncTask for retrieving the list of places (e.g., stores) and updating the
   * corresponding results list.
   */
  private class ListOfPlacesAsyncRetriever extends AsyncTask<Void, Void, CollectionResponsePlace> {

    @Override
    protected CollectionResponsePlace doInBackground(Void... params) {


      Placeendpoint.Builder endpointBuilder = new Placeendpoint.Builder(
          AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
     
      endpointBuilder = CloudEndpointUtils.updateBuilder(endpointBuilder);


      CollectionResponsePlace result;

      Placeendpoint endpoint = endpointBuilder.build();

      try {
        result = endpoint.listPlace().execute();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result = null;
      }
      return result;
    }

    @Override
    @SuppressWarnings("null")
    protected void onPostExecute(CollectionResponsePlace result) {
      ListAdapter placesListAdapter = createPlaceListAdapter(result.getItems());
      placesList.setAdapter(placesListAdapter);

      places = result.getItems();
    }
    
    private ListAdapter createPlaceListAdapter(List<Place> places) {
      final double kilometersInAMile = 1.60934;
      List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
      for (Place place : places) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("placeIcon", R.drawable.ic_launcher);
        map.put("placeName", place.getName());
        map.put("placeAddress", place.getAddress());
        String distance = "1.2";
        map.put("placeDistance", distance);
        data.add(map);
      }

      SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.place_item,
          new String[] {"placeIcon", "placeName", "placeAddress", "placeDistance"},
          new int[] {R.id.place_Icon, R.id.place_name, R.id.place_address, R.id.place_distance});

      return adapter;
    }
    
  }

  private OnItemClickListener placesListClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      Place selectedPlace = places.get((int) arg3);

      new CheckInTask().execute();
      
      PlaceDetailsActivity.currentPlace = selectedPlace;
      Intent i = new Intent(MainActivity.this, PlaceDetailsActivity.class);
      startActivity(i);
      }
  };  
  
  
}




Java Source Code List

ch.yereaztian.mobileassistant.CheckInEndpoint.java
ch.yereaztian.mobileassistant.CheckIn.java
ch.yereaztian.mobileassistant.CloudEndpointUtils.java
ch.yereaztian.mobileassistant.DeviceInfoEndpoint.java
ch.yereaztian.mobileassistant.DeviceInfo.java
ch.yereaztian.mobileassistant.EMF.java
ch.yereaztian.mobileassistant.GCMIntentService.java
ch.yereaztian.mobileassistant.ImageUrlViewBinder.java
ch.yereaztian.mobileassistant.MainActivity.java
ch.yereaztian.mobileassistant.MessageData.java
ch.yereaztian.mobileassistant.MessageEndpoint.java
ch.yereaztian.mobileassistant.OfferEndpoint.java
ch.yereaztian.mobileassistant.Offer.java
ch.yereaztian.mobileassistant.PlaceDetailsActivity.java
ch.yereaztian.mobileassistant.PlaceEndpoint.java
ch.yereaztian.mobileassistant.Place.java
ch.yereaztian.mobileassistant.RecommendationEndpoint.java
ch.yereaztian.mobileassistant.Recommendation.java
ch.yereaztian.mobileassistant.RegisterActivity.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointRequestInitializer.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointRequest.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointScopes.java
ch.yereaztian.mobileassistant.checkinendpoint.Checkinendpoint.java
ch.yereaztian.mobileassistant.checkinendpoint.model.CheckIn.java
ch.yereaztian.mobileassistant.checkinendpoint.model.CollectionResponseCheckIn.java
ch.yereaztian.mobileassistant.checkinendpoint.model.Key.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointRequestInitializer.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointRequest.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointScopes.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.Deviceinfoendpoint.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.model.CollectionResponseDeviceInfo.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.model.DeviceInfo.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointRequestInitializer.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointRequest.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointScopes.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpoint.java
ch.yereaztian.mobileassistant.messageEndpoint.model.CollectionResponseMessageData.java
ch.yereaztian.mobileassistant.messageEndpoint.model.Key.java
ch.yereaztian.mobileassistant.messageEndpoint.model.MessageData.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointRequestInitializer.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointRequest.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointScopes.java
ch.yereaztian.mobileassistant.offerendpoint.Offerendpoint.java
ch.yereaztian.mobileassistant.offerendpoint.model.CollectionResponseOffer.java
ch.yereaztian.mobileassistant.offerendpoint.model.Offer.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointRequestInitializer.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointRequest.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointScopes.java
ch.yereaztian.mobileassistant.placeendpoint.Placeendpoint.java
ch.yereaztian.mobileassistant.placeendpoint.model.CollectionResponsePlace.java
ch.yereaztian.mobileassistant.placeendpoint.model.GeoPt.java
ch.yereaztian.mobileassistant.placeendpoint.model.Key.java
ch.yereaztian.mobileassistant.placeendpoint.model.Place.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointRequestInitializer.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointRequest.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointScopes.java
ch.yereaztian.mobileassistant.recommendationendpoint.Recommendationendpoint.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.CollectionResponseRecommendation.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.Key.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.Recommendation.java
com.google.samplesolutions.mobileassistant.ImageUrlViewBinder.java