com.bidyut.app.wwquakes.QuakeDetailActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.bidyut.app.wwquakes.QuakeDetailActivity.java

Source

/*
 * Copyright (c) 2014 Saud Khan.
 *
 * I am not responsible for any damage caused by this code. This code is provided free
 * to use as long as the original author is attributed in derivatives and uses. All
 * derivatives of this project should be licensed similarly.
 */

package com.bidyut.app.wwquakes;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.bidyut.app.wwquakes.data.ContentInfo;
import com.bidyut.app.wwquakes.data.FeedFeature;
import com.bidyut.app.wwquakes.data.FeedProduct;
import com.bidyut.app.wwquakes.data.NearbyCity;
import com.bidyut.app.wwquakes.data.Quake;
import com.bidyut.app.wwquakes.service.UsgsQuakeService;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class QuakeDetailActivity extends FragmentActivity implements Callback, AdapterView.OnItemClickListener {
    public static final String EXTRA_QUAKE = "quake";

    private static final float ZOOM_LEVEL = 7f;

    private Quake mQuake;
    private GoogleMap mMap;
    private UsgsQuakeService mService;
    private QuakeInfoFragment mQuakeInfoFragment;
    private SupportWebViewFragment mTectonicFragment;
    private View mProgress;

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.activity_quake_detail);

        mQuake = getIntent().getParcelableExtra(EXTRA_QUAKE);

        mProgress = findViewById(R.id.progress);
        mQuakeInfoFragment = new QuakeInfoFragment();
        mTectonicFragment = new SupportWebViewFragment();
        final PageInfo[] infos = new PageInfo[] { new PageInfo(getString(R.string.quake_info), mQuakeInfoFragment),
                new PageInfo(getString(R.string.tectonic_summary), mTectonicFragment) };

        final int color = mQuake.getMagnitudeColor(this);

        final QuakeDetailAdapter adapter = new QuakeDetailAdapter(getSupportFragmentManager(), infos);
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setDividerColor(color);
        tabs.setIndicatorColor(color);
        tabs.setViewPager(pager);
    }

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

        final MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mMap = fragment.getMap();
        if (mMap != null) {
            mMap.addMarker(new MarkerOptions().position(this.mQuake.position));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mQuake.position, ZOOM_LEVEL));
        }

        mService = UsgsQuakeService.Builder.build();
        mService.getDetail(this.mQuake.id, this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            onBackPressed();
            return true;
        }
        default: {
            return super.onOptionsItemSelected(item);
        }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (position == QuakeInfoAdapter.POSITION_LOCATION) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mQuake.position, ZOOM_LEVEL));
        } else if (position >= QuakeInfoAdapter.POSITION_FIRST_CITY) {
            final NearbyCity city = mQuake.cities[position - QuakeInfoAdapter.POSITION_FIRST_CITY];
            final LatLng latLng = new LatLng(city.latitude, city.longitude);
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
        }
    }

    @Override
    public void success(Object object, Response response) {
        if (object instanceof FeedFeature) {
            mProgress.setVisibility(View.GONE);
            final FeedFeature feedFeature = (FeedFeature) object;
            if (feedFeature.properties.products.nearbyCities != null) {
                final ContentInfo info = feedFeature.properties.products.nearbyCities.get(0);
                mService.getNearbyCities(info.code, info.source, info.updateTime, this);
            }
            if (feedFeature.properties.products.tectonicSummary != null) {
                final ContentInfo info = feedFeature.properties.products.tectonicSummary.get(0);
                mTectonicFragment.getWebView().loadUrl(
                        FeedProduct.getProductHtmlLink(info.type, info.code, info.source, info.updateTime));
            } else {
                mTectonicFragment.getWebView().loadData(getString(R.string.empty_tectonic_info), "text/html",
                        "utf-8");
            }
        } else if (object instanceof List) {
            final List<NearbyCity> cities = (List<NearbyCity>) object;
            if (!cities.isEmpty()) {
                if (mMap != null) {
                    final BitmapDescriptor icon = BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
                    for (NearbyCity city : cities) {
                        mMap.addMarker(new MarkerOptions().position(new LatLng(city.latitude, city.longitude))
                                .icon(icon).title(city.name));
                    }
                }
                mQuake.cities = cities.toArray(new NearbyCity[cities.size()]);
                mQuakeInfoFragment.setQuake(mQuake, this);
            }
        }
    }

    @Override
    public void failure(RetrofitError error) {
        Toast.makeText(this, R.string.fetch_error, Toast.LENGTH_SHORT).show();
    }

    private static class QuakeDetailAdapter extends FragmentPagerAdapter {
        private final PageInfo[] mPageInfos;

        public QuakeDetailAdapter(FragmentManager fm, PageInfo[] infos) {
            super(fm);
            mPageInfos = infos;
        }

        @Override
        public Fragment getItem(int position) {
            return mPageInfos[position].fragment;
        }

        @Override
        public int getCount() {
            return mPageInfos.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mPageInfos[position].title;
        }
    }

    private static class PageInfo {
        public final String title;
        public final Fragment fragment;

        public PageInfo(String title, Fragment fragment) {
            this.title = title;
            this.fragment = fragment;
        }
    }
}