Android Open Source - ean-android Hotel List






From Project

Back to project page ean-android.

License

The source code is released under:

Copyright (c) 2013, Expedia Affiliate Network All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that redistributions of sour...

If you think the Android project ean-android 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) 2013, Expedia Affiliate Network
 * All rights reserved.//from   w  ww.  j ava 2s . c  o  m
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that redistributions of source code
 * retain the above copyright notice, these conditions, and the following
 * disclaimer. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Expedia Affiliate Network or Expedia Inc.
 */

package com.ean.mobile.activity;

import java.text.NumberFormat;
import java.util.Currency;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ean.mobile.R;
import com.ean.mobile.app.ImageFetcher;
import com.ean.mobile.app.SampleApp;
import com.ean.mobile.app.SampleConstants;
import com.ean.mobile.app.StarRating;
import com.ean.mobile.exception.EanWsError;
import com.ean.mobile.exception.UrlRedirectionException;
import com.ean.mobile.hotel.Hotel;
import com.ean.mobile.hotel.request.ListRequest;
import com.ean.mobile.request.RequestProcessor;

/**
 * The code behind the HotelList layout.
 */
public class HotelList extends Activity {

    private Toast loadingMoreHotelsToast;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotellist);
        final ListView hotelListView = (ListView) findViewById(R.id.HotelList);
        hotelListView.setOnItemClickListener(new HotelListAdapterListener());

        loadingMoreHotelsToast = Toast.makeText(getApplicationContext(),
            getString(R.string.loading_more_hotels), Toast.LENGTH_LONG);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStart() {
        super.onStart();
        ((TextView) findViewById(R.id.searchQuery)).setText(SampleApp.searchQuery);
        final ListView hotelListView = (ListView) findViewById(R.id.HotelList);
        hotelListView.setAdapter(new HotelAdapter(getApplicationContext(), R.layout.hotellistlayout));
        hotelListView.setOnScrollListener(new HotelScrollListener());
    }

    private class HotelListAdapterListener implements AdapterView.OnItemClickListener {
        public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
            SampleApp.selectedHotel = (Hotel) parent.getItemAtPosition(position);
            startActivity(new Intent(HotelList.this, HotelInformation.class));
        }
    }

    private static final class HotelAdapter extends ArrayAdapter<Hotel> {

        private final LayoutInflater layoutInflater;

        /**
         * Overloads the constructor who takes the same parameters plus a list of objects.
         * Uses {@link SampleApp#FOUND_HOTELS} as the list of objects it will use.
         * @param context The context passed in by the calling class.
         * @param resource The resourceId of the resource this is being applied to.
         */
        private HotelAdapter(final Context context, final int resource) {
            super(context, resource, SampleApp.FOUND_HOTELS);
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = layoutInflater.inflate(R.layout.hotellistlayout, null);
            }

            //Get the hotel.
            final Hotel hotel = this.getItem(position);
            Log.d(SampleConstants.LOG_TAG, "name " + hotel.name);

            //Set the name field
            final TextView name = (TextView) view.findViewById(R.id.hotelName);
            name.setText(hotel.name);

            //Set the short location description
            final TextView locDesc = (TextView) view.findViewById(R.id.hotelLocationDesc);
            locDesc.setText(hotel.locationDescription);

            //Populate the star rating
            StarRating.populate((LinearLayout) view.findViewById(R.id.hotelStars), hotel.starRating);

            //Get and show the pricing information.
            fixUpPricesAndDrr(view, hotel);

            //Get and show the thumbnail image
            final ImageView thumb = (ImageView) view.findViewById(R.id.hotelThumb);
            // reset the thumb to eliminate possible flickering
            thumb.setImageResource(R.drawable.noimg_large);

            ImageFetcher.loadThumbnailIntoImageView(thumb, hotel.mainHotelImageTuple);

            return view;
        }

        private static void fixUpPricesAndDrr(final View view, final Hotel hotel) {
            final TextView highPrice = (TextView) view.findViewById(R.id.highPrice);
            final TextView lowPrice = (TextView) view.findViewById(R.id.lowPrice);
            final ImageView drrIcon = (ImageView) view.findViewById(R.id.drrPromoImg);
            final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
            currencyFormat.setCurrency(Currency.getInstance(hotel.currencyCode));
            lowPrice.setText(currencyFormat.format(hotel.lowPrice));
            if (hotel.lowPrice.equals(hotel.highPrice)) {
                highPrice.setVisibility(TextView.GONE);
                drrIcon.setVisibility(ImageView.GONE);
            } else {
                highPrice.setVisibility(TextView.VISIBLE);
                drrIcon.setVisibility(ImageView.VISIBLE);
                highPrice.setText(currencyFormat.format(hotel.highPrice));
                highPrice.setPaintFlags(highPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }
        }
    }


    private class HotelScrollListener implements AbsListView.OnScrollListener {

        private PerformLoadTask loadingTask;

        private final int distanceFromLastPositionToLoad = 7;

        /**
         * {@inheritDoc}
         */
        @Override
        public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
                final int totalItemCount) {
            if (view.getLastVisiblePosition() >= SampleApp.FOUND_HOTELS.size() - distanceFromLastPositionToLoad) {
                if (loadingTask == null || loadingTask.getStatus() == AsyncTask.Status.FINISHED) {
                    loadingTask = new PerformLoadTask((ArrayAdapter) view.getAdapter());
                }
                if (loadingTask.getStatus() == AsyncTask.Status.PENDING) {
                    loadingMoreHotelsToast.show();
                    loadingTask.execute((Void) null);
                }
            }
        }

        /**
         * no op, implemented for the interface.
         * {@inheritDoc}
         */
        @Override
        public void onScrollStateChanged(final AbsListView absListView, final int i) {
            // see javadoc.
        }
    }

    private final class PerformLoadTask extends AsyncTask<Void, Integer, Void> {

        private final ArrayAdapter adapter;

        private PerformLoadTask(final ArrayAdapter adapter) {
            this.adapter = adapter;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected Void doInBackground(final Void... voids) {
            try {
                final ListRequest request = new ListRequest(
                    SampleApp.cacheKey,
                    SampleApp.cacheLocation);

                SampleApp.updateFoundHotels(RequestProcessor.run(request));
            } catch (EanWsError ewe) {
                Log.d(SampleConstants.LOG_TAG, "An APILevel Exception occurred.", ewe);
            } catch (UrlRedirectionException ure) {
                SampleApp.sendRedirectionToast(getApplicationContext());
            }
            return null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void onPostExecute(final Void aVoid) {
            adapter.notifyDataSetChanged();
            loadingMoreHotelsToast.cancel();
        }
    }
}




Java Source Code List

android.text.Html.java
android.text.Spanned.java
android.util.Log.java
com.ean.mobile.Address.java
com.ean.mobile.Constants.java
com.ean.mobile.CustomerAddress.java
com.ean.mobile.Destination.java
com.ean.mobile.Individual.java
com.ean.mobile.LatLongAddress.java
com.ean.mobile.Name.java
com.ean.mobile.activity.BookingSummary.java
com.ean.mobile.activity.HotelInformation.java
com.ean.mobile.activity.HotelList.java
com.ean.mobile.activity.ReservationDisplay.java
com.ean.mobile.activity.StartupSearch.java
com.ean.mobile.activity.package-info.java
com.ean.mobile.app.HotelImageDrawableMap.java
com.ean.mobile.app.HotelImageDrawable.java
com.ean.mobile.app.ImageFetcher.java
com.ean.mobile.app.SampleApp.java
com.ean.mobile.app.SampleConstants.java
com.ean.mobile.app.StarRating.java
com.ean.mobile.app.package-info.java
com.ean.mobile.exception.CommonParameterValidationException.java
com.ean.mobile.exception.DataValidationException.java
com.ean.mobile.exception.EanWsError.java
com.ean.mobile.exception.UriCreationException.java
com.ean.mobile.exception.UrlRedirectionException.java
com.ean.mobile.exception.package-info.java
com.ean.mobile.hotel.CancellationPolicy.java
com.ean.mobile.hotel.Cancellation.java
com.ean.mobile.hotel.ConfirmationStatus.java
com.ean.mobile.hotel.HotelImageTuple.java
com.ean.mobile.hotel.HotelInformation.java
com.ean.mobile.hotel.HotelList.java
com.ean.mobile.hotel.HotelRoom.java
com.ean.mobile.hotel.Hotel.java
com.ean.mobile.hotel.Itinerary.java
com.ean.mobile.hotel.NightlyRate.java
com.ean.mobile.hotel.Rate.java
com.ean.mobile.hotel.ReservationRoom.java
com.ean.mobile.hotel.Reservation.java
com.ean.mobile.hotel.RoomOccupancy.java
com.ean.mobile.hotel.SupplierType.java
com.ean.mobile.hotel.request.BookingRequest.java
com.ean.mobile.hotel.request.CancellationRequest.java
com.ean.mobile.hotel.request.InformationRequest.java
com.ean.mobile.hotel.request.ItineraryRequest.java
com.ean.mobile.hotel.request.ListRequest.java
com.ean.mobile.hotel.request.RoomAvailabilityRequest.java
com.ean.mobile.hotel.request.package-info.java
com.ean.mobile.hotel.package-info.java
com.ean.mobile.request.CommonParameters.java
com.ean.mobile.request.DestinationRequest.java
com.ean.mobile.request.RequestProcessor.java
com.ean.mobile.request.Request.java
com.ean.mobile.request.package-info.java
com.ean.mobile.task.ImageDrawableLoaderTask.java
com.ean.mobile.task.SuggestionFactory.java
com.ean.mobile.task.package-info.java
com.ean.mobile.package-info.java