Android Open Source - SeeKampf Ocean Fragment






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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

package net.avedo.seekampf.fragments;
/*from w w w .ja va  2  s  .c o  m*/
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import net.avedo.seekampf.R;
import net.avedo.seekampf.core.OceanView;
import net.avedo.seekampf.core.VolleyActivity;
import net.avedo.seekampf.models.Island;
import net.avedo.seekampf.utils.AuthGsonRequest;
import net.avedo.seekampf.utils.Constants;
import net.avedo.seekampf.utils.VolleyErrorHelper;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class OceanFragment extends Fragment {
  public static final String SERVICE_TAG = "OceanFragment";
  
  private SharedPreferences settings;
  private Resources res;

  private AuthGsonRequest<Island[]> request;
  private boolean isFetching = false;
  private int retryCount = 0;
  
  private OceanView oceanView;

  @Override
  public void onActivityCreated(Bundle paramBundle) {
    // Fetch the application preferences handle ...
    this.settings = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
    
    // ... and the application resources.
    this.res = getResources();
    
    super.onActivityCreated(paramBundle);
  }

  @Override
  public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
  }
    
    @Override
    public void onStart() {
      super.onStart();
      fetchData();
    }

    @Override
    public void onDestroyView() {
      VolleyActivity.getInstance().cancelPendingRequests(SERVICE_TAG);
      isFetching = false;
        super.onDestroyView();
    }
    
    protected void fetchData() {
      // Break if already fetching data or there is nothing left to load.
      if (isFetching) {
        return;
      }
      
      // Start fetching data ...
      isFetching = true;

    // ... and generate the service url.
    String serviceURL = fetchServiceUrl();
      
    // Setup the request object.
    request = new AuthGsonRequest<Island[]>(Request.Method.GET, serviceURL,
        Island[].class, null, new Response.Listener<Island[]>() {
            @Override
            public void onResponse(Island[] islands) {
                      if (OceanFragment.this.getView() != null && islands != null) {
                          // Reset the retry counter, ...
                          retryCount = 0;
                          
                          // ... assign the island to the ocean view ...
                          OceanFragment.this.oceanView.setIslands(islands);
                          
                          // ... and reset fetching status.
                          isFetching = false;
                      }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                    retryCount++;
                    
                    if (retryCount < Constants.MAXIMUM_RETRIES) {
                      VolleyActivity.getInstance().addToRequestQueue(request, SERVICE_TAG);
                    }
                        
                    Toast.makeText(getActivity(), 
                        VolleyErrorHelper.getMessage(error, getActivity()), 
                        Toast.LENGTH_LONG).show();
            }
        });
    
    VolleyActivity.getInstance().addToRequestQueue(request, SERVICE_TAG);
    }

  protected String fetchServiceUrl() {
    return Constants.API_URL + "?server="
    + settings.getString(res.getString(R.string.prefs_server_key), "1")
    + "&typ=inseln";
  }
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Fetch the main View, ...
      View view = inflater.inflate(R.layout.ocean_fragment, container, false);
      
      // ... extract the OceanView ...
      this.oceanView = ((OceanView) view.findViewById(R.id.oceanView));
      
        // ... and return the main view.
        return view;
    }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java