com.foodie.app.fragment.LocationFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.foodie.app.fragment.LocationFragment.java

Source

/*
 * Copyright (C) 2015 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 com.foodie.app.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import com.foodie.app.Activity.MapActivity;
import com.foodie.app.Entity.JsonListResult;
import com.foodie.app.Entity.Result;
import com.foodie.app.R;
import com.foodie.app.model.RestaurantInfo;
import com.foodie.app.util.Constant;
import com.foodie.app.util.HttpUtils;
import com.foodie.app.util.PrefUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.lang.reflect.Type;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class LocationFragment extends Fragment {
    protected static final String TAG = "MessageFragment";
    private RecyclerView restaurantListView;
    private List<RestaurantInfo> restaurantList;

    private RestaurantListAdapter restaurantListAdapter;

    //?
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    public String mLocation;

    public TextView mLocationView;

    private SwipeRefreshLayout sr;
    private ProgressBar progressBar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_location, container, false);
        restaurantListView = (RecyclerView) view.findViewById(R.id.recyclerview);
        restaurantListView.setItemAnimator(new DefaultItemAnimator());
        progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        sr = (SwipeRefreshLayout) view.findViewById(R.id.sr);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        restaurantListView.setLayoutManager(new LinearLayoutManager(restaurantListView.getContext()));

        //?
        //?
        mLocationClient = new LocationClient(getActivity().getApplicationContext()); //LocationClient
        mLocationClient.registerLocationListener(myListener); //?
        initLocation();//?
        mLocationClient.start();
        //
        updateRestaurantInfo();
        sr.setColorSchemeResources(R.color.colorPrimary, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

        sr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                updateRestaurantInfo();
                sr.setRefreshing(false);
            }
        });

    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//???
        option.setCoorType("bd09ll");//?gcj02???
        int span = 2000;
        option.setScanSpan(0);//?0?????1000ms?
        option.setIsNeedAddress(true);//???????
        option.setOpenGps(true);//?false,?gps
        option.setLocationNotify(true);//?false?gps1S1GPS
        option.setIsNeedLocationDescribe(true);//?false????BDLocation.getLocationDescribe?
        option.setIsNeedLocationPoiList(true);//?false??POI?BDLocation.getPoiList
        option.setIgnoreKillProcess(false);//?true?SDKSERVICE?stop???
        option.SetIgnoreCacheException(false);//?false?CRASH?
        option.setEnableSimulateGps(false);//?false??gps?
        mLocationClient.setLocOption(option);
    }

    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            mLocation = location.getAddrStr();
            PrefUtils.set("user", "location", mLocation, getActivity().getBaseContext());
            //Receive Location
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS?
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());// ????
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\nheight : ");
                sb.append(location.getAltitude());// ??
                sb.append("\ndirection : ");
                sb.append(location.getDirection());// ??
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                sb.append("\ndescribe : ");
                sb.append("gps??");
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// ?
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                //???
                sb.append("\noperationers : ");
                sb.append(location.getOperators());
                sb.append("\ndescribe : ");
                sb.append("??");
            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// ?
                sb.append("\ndescribe : ");
                sb.append("???");
            } else if (location.getLocType() == BDLocation.TypeServerError) {
                sb.append("\ndescribe : ");
                sb.append(
                        "?????IMEI??loc-bugs@baidu.com");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                sb.append("\ndescribe : ");
                sb.append("????");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                sb.append("\ndescribe : ");
                sb.append(
                        "????????????");
            }
            sb.append("\nlocationdescribe : ");
            sb.append(location.getLocationDescribe());// ??
            List<Poi> list = location.getPoiList();// POI?
            if (list != null) {
                sb.append("\npoilist size = : ");
                sb.append(list.size());
                for (Poi p : list) {
                    sb.append("\npoi= : ");
                    sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
                }
            }
            Log.i("BaiduLocationApiDem", sb.toString());
        }
    }

    private void updateRestaurantInfo() {
        if (HttpUtils.isNetworkConnected(getActivity())) {
            HttpUtils.get(Constant.RESTAURANT_LIST, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    Gson gson = new Gson();
                    //Type type = new TypeToken<JsonResult<User>>(){}.getType();
                    Type type = new TypeToken<JsonListResult<RestaurantInfo>>() {
                    }.getType();
                    JsonListResult<RestaurantInfo> jsonListResult = gson.fromJson(new String(responseBody), type);
                    String status = jsonListResult.getStatus();
                    restaurantList = jsonListResult.getData();
                    //Log.v(TAG,data);
                    if (TextUtils.equals(status, Result.SUCCESS) && restaurantList != null) {
                        //Toast.makeText(getActivity(),"?"+restaurantList.size(),Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                        Log.i(TAG, new String(responseBody));
                        restaurantListAdapter = new RestaurantListAdapter(getActivity(), restaurantList, mLocation);
                        restaurantListAdapter
                                .setMyItemClickListener(new RestaurantListAdapter.MyItemClickListener() {
                                    @Override
                                    public void onItemClick(View view, int postion) {
                                        if (postion == 0) {
                                            Log.v(TAG, "");
                                            getActivity()
                                                    .startActivity(new Intent(getActivity(), MapActivity.class));
                                            return;
                                        }
                                        Log.v(TAG, "?");
                                        Intent intent = new Intent(getActivity(), MapActivity.class);
                                        intent.putExtra("restaurantInfo",
                                                restaurantListAdapter.getRestaurantInfo(postion));
                                        getActivity().startActivity(intent);
                                    }
                                });
                        restaurantListView.setAdapter(restaurantListAdapter);
                        return;
                    } else {
                        Toast.makeText(getActivity(), "??" + new String(responseBody),
                                Toast.LENGTH_LONG).show();
                    }

                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    Toast.makeText(getActivity(), "?", Toast.LENGTH_LONG).show();
                }
            });
        } else {
            Toast.makeText(getActivity(), "!", Toast.LENGTH_LONG).show();
        }
    }

    public static class RestaurantListAdapter extends RecyclerView.Adapter<RestaurantListAdapter.ViewHolder> {
        private List<RestaurantInfo> restaurantList;
        private String mLocation = "??...";
        private Context context;
        private static final int IS_HEADER = 2;
        private static final int IS_NORMAL = 1;

        private MyItemClickListener myItemClickListener;

        public void setMyItemClickListener(MyItemClickListener listener) {
            this.myItemClickListener = listener;
        }

        public RestaurantListAdapter(Context context, List<RestaurantInfo> restaurantList,
                MyItemClickListener myItemClickListener) {
            super();
            this.restaurantList = restaurantList;
            this.context = context;
            this.myItemClickListener = myItemClickListener;
        }

        public interface MyItemClickListener {
            void onItemClick(View view, int postion);
        }

        public void setWorks(List<RestaurantInfo> restaurantList) {
            this.restaurantList = restaurantList;
        }

        public RestaurantInfo getRestaurantInfo(int position) {
            if (position == 0) {
                return null;
            }
            return restaurantList.get(position - 1);
        }

        public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            public int mViewType;
            public View mView;
            public ImageView mRestaurantImageView;
            public TextView mRestaurantNameView;
            public RatingBar mScoreView;
            public TextView mCommentCountView;
            public TextView mAveragePriceView;
            public TextView mKeywordView;
            public TextView mAddressVview;
            public TextView mDistanceView;
            public MyItemClickListener mItemClickListener;

            public TextView headerView;

            public ViewHolder(View view, int viewType, MyItemClickListener myItemClickListener) {
                super(view);
                mView = view;
                mViewType = viewType;
                mItemClickListener = myItemClickListener;
                if (viewType == IS_HEADER) {
                    headerView = (TextView) view.findViewById(R.id.my_location);
                    headerView.setText("???...");
                    headerView.setOnClickListener(this);
                    return;
                }
                mRestaurantImageView = (ImageView) view.findViewById(R.id.restaurant_image);
                mRestaurantNameView = (TextView) view.findViewById(R.id.restaurant_name_text);
                mScoreView = (RatingBar) view.findViewById(R.id.rating_bar);
                mCommentCountView = (TextView) view.findViewById(R.id.comment_count);
                mAveragePriceView = (TextView) view.findViewById(R.id.average_price);
                mKeywordView = (TextView) view.findViewById(R.id.keyword);
                mAddressVview = (TextView) view.findViewById(R.id.location);
                mDistanceView = (TextView) view.findViewById(R.id.distance);
                mItemClickListener = myItemClickListener;
                view.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(v, getPosition());
                }
            }
        }

        public RestaurantListAdapter(Context context, List<RestaurantInfo> restaurantList, String mLocation) {
            super();
            this.context = context;
            this.restaurantList = restaurantList;
            this.mLocation = mLocation;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            //RecyclerViewHolder holder;
            //??flag??Holder
            if (viewType == IS_HEADER) {
                View view = LayoutInflater.from(context).inflate(R.layout.location_header, parent, false);
                return new ViewHolder(view, IS_HEADER, myItemClickListener);
            } else if (viewType == IS_NORMAL) {
                View view = LayoutInflater.from(context).inflate(R.layout.list_item_location, parent, false);
                return new ViewHolder(view, IS_NORMAL, myItemClickListener);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            if (position == 0) {
                holder.headerView.setText(mLocation);
                return;
            }
            position = position - 1;
            RestaurantInfo resaurantInfo = restaurantList.get(position);

            DisplayImageOptions RestaurantImageOptions = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.loading_large).showImageOnFail(R.drawable.recipe)
                    .cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
            ImageLoader.getInstance().displayImage(resaurantInfo.getPictureSmall(), holder.mRestaurantImageView,
                    RestaurantImageOptions);

            holder.mDistanceView.setText("1.1km");
            holder.mAddressVview.setText(resaurantInfo.getAddress());
            holder.mKeywordView.setText(resaurantInfo.getKeyWord());
            holder.mAveragePriceView.setText(resaurantInfo.getAveragePrice());
            holder.mCommentCountView.setText("212");
            holder.mRestaurantNameView.setText(resaurantInfo.getRestaurantName());
            holder.mScoreView.setRating(4);
        }

        @Override
        public int getItemCount() {
            return restaurantList.size() + 1;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return IS_HEADER;
            }
            return IS_NORMAL;
        }

    }
}