content: /*
   * Copyright (C) 2016 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.example.android.quakereport;
  
  import android.app.LoaderManager.LoaderCallbacks;
  import android.content.Context;
  import android.content.Intent;
  import android.content.Loader;
  import android.content.SharedPreferences;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.net.Uri;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class EarthquakeActivity extends AppCompatActivity implements LoaderCallbacks<List<Earthquake>> {
  
  
      public static final String LOG_TAG = EarthquakeActivity.class.getName();
      /**
       * Constant value for the earthquake loader ID. We can choose any integer.
       * This really only comes into play if you're using multiple loaders.
       */
      private static final int EARTHQUAKE_LOADER_ID = 1;
      /**
       * URL for recent earthquakes in the world with at least a magnitude of 6
       */
      private static final String USGS_REQUEST_URL =
              "https://earthquake.usgs.gov/fdsnws/event/1/query";
      // https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10
  
      private EarthquakeArrayAdapter mAdapter;
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int itemId = item.getItemId();
          if (itemId == R.id.action_settings) {
              Intent settingsIntent = new Intent(this, SettingsActivity.class);
              startActivity(settingsIntent);
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.earthquake_activity);
  
          ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
  
          ListView earthquakeListView = (ListView) findViewById(R.id.list);
  
          // Create a new {@link ArrayAdapter} of earthquakes
          mAdapter = new EarthquakeArrayAdapter(this, new ArrayList<Earthquake>());
  
          // Set the adapter on the {@link ListView}
          // so the list can be populated in the user interface
          earthquakeListView.setAdapter(mAdapter);
          earthquakeListView.setEmptyView(findViewById(R.id.empty_state));
  
          earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  Intent detailsPage = new Intent(Intent.ACTION_VIEW, Uri.parse(mAdapter.getItem(position).getUrl()));
                  startActivity(detailsPage);
              }
          });
  
          if (!isConnected) {
              findViewById(R.id.progress).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.empty_state)).setText(R.string.no_internet);
          } else {
              getLoaderManager().initLoader(EARTHQUAKE_LOADER_ID, null, this);
          }
      }
  
      @Override
      public Loader<List<Earthquake>> onCreateLoader(int id, Bundle bundle) {
          Log.i(LOG_TAG, "onCreateLoader()");
          SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
          String minMagnitude = sharedPrefs.getString(
                  getString(R.string.settings_min_magnitude_key),
                  getString(R.string.settings_min_magnitude_default));
  
          String orderBy = sharedPrefs.getString(
                  getString(R.string.settings_order_by_key),
                  getString(R.string.settings_order_by_default)
          );
  
          Uri baseUri = Uri.parse(USGS_REQUEST_URL);
          Uri.Builder uriBuilder = baseUri.buildUpon()
                  .appendQueryParameter("format", "geojson")
                  .appendQueryParameter("limit", "10")
                  .appendQueryParameter("minmag", minMagnitude)
                  .appendQueryParameter("orderby", orderBy);
  
          return new EarthquakeLoader(this, uriBuilder.toString());
      }
  
      @Override
      public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
          Log.i(LOG_TAG, "onLoadFinished()");
  
          findViewById(R.id.progress).setVisibility(View.GONE);
          ((TextView) findViewById(R.id.empty_state)).setText(R.string.no_earthquakes_found);
          // Clear the adapter of previous earthquake data
          mAdapter.clear();
  
          /// If there is a valid list of {@link Earthquake}s, then add them to the adapter's
          // data set. This will trigger the ListView to update.
          if (earthquakes != null && !earthquakes.isEmpty()) {
              mAdapter.addAll(earthquakes);
          }
      }
  
      @Override
      public void onLoaderReset(Loader<List<Earthquake>> loader) {
          Log.i(LOG_TAG, "onLoaderReset()");
          mAdapter.clear();
      }
  }
  