Android Open Source - WarDroid Badlands Fragment






From Project

Back to project page WarDroid.

License

The source code is released under:

MIT License

If you think the Android project WarDroid 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 com.deathsnacks.wardroid.fragments;
/*  w  w  w .  j  a va 2  s.  co  m*/
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.deathsnacks.wardroid.R;
import com.deathsnacks.wardroid.adapters.BadlandsListViewAdapter;
import com.deathsnacks.wardroid.adapters.SeparatedListAdapter;
import com.deathsnacks.wardroid.gson.badlands.BadlandNode;
import com.deathsnacks.wardroid.utils.Http;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Admin on 23/01/14.
 */
public class BadlandsFragment extends Fragment {
    private static final String TAG = "BadlandsFragment";
    private View mRefreshView;
    private ListView mListView;
    private BadlandsRefresh mTask;
    private /*BadlandsListViewAdapter*/SeparatedListAdapter mAdapter;
    private Handler mHandler;
    private boolean mUpdate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_badlands, container, false);
        mRefreshView = rootView.findViewById(R.id.refresh);
        mListView = (ListView) rootView.findViewById(R.id.list);
        mHandler = new Handler();
        setHasOptionsMenu(true);
        mUpdate = true;
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (view.getTag() == null)
                    return;
                showDialog(((BadlandsListViewAdapter.GroupViewHolder)view.getTag()).bl_node);
            }
        });
        if (savedInstanceState != null) {
            String pc = savedInstanceState.getString("bl_pc");
            String ps4 = savedInstanceState.getString("bl_ps4");
            String xbox = savedInstanceState.getString("bl_xbox");
            long time = savedInstanceState.getLong("time");
            if (pc != null || ps4 != null || xbox != null) {
                mUpdate = false;
                Log.d(TAG, "saved instance");
                Type collectionType = new TypeToken<List<BadlandNode>>() {
                }.getType();
                mAdapter = new SeparatedListAdapter(getActivity(), null);
                if (pc != null) {
                    List<BadlandNode> data = (new GsonBuilder().create()).fromJson(pc, collectionType);
                    mAdapter.addSection("PC", new BadlandsListViewAdapter(getActivity(), data));
                }
                if (ps4 != null) {
                    List<BadlandNode> data = (new GsonBuilder().create()).fromJson(ps4, collectionType);
                    mAdapter.addSection("PS4", new BadlandsListViewAdapter(getActivity(), data));
                }
                if (xbox != null) {
                    List<BadlandNode> data = (new GsonBuilder().create()).fromJson(xbox, collectionType);
                    mAdapter.addSection("Xbox One", new BadlandsListViewAdapter(getActivity(), data));
                }
                mListView.setAdapter(mAdapter);
                mListView.onRestoreInstanceState(savedInstanceState.getParcelable("bl_lv"));
                if (System.currentTimeMillis() - time > 120 * 1000) {
                    refresh(false);
                }
            }
        }
        return rootView;
    }

    private void showDialog(BadlandNode node) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        BadlandDialogFragment blDialog = new BadlandDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("node", (new Gson()).toJson(node));
        blDialog.setArguments(bundle);
        blDialog.show(fm, "dialog");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mAdapter == null)
            return;
        outState.putParcelable("bl_lv", mListView.onSaveInstanceState());
        BadlandsListViewAdapter pc = (BadlandsListViewAdapter) mAdapter.getSectionAdapter("PC");
        BadlandsListViewAdapter ps4 = (BadlandsListViewAdapter) mAdapter.getSectionAdapter("PS4");
        BadlandsListViewAdapter xbox = (BadlandsListViewAdapter) mAdapter.getSectionAdapter("Xbox One");
        if (pc != null)
            outState.putString("bl_pc", pc.getOriginalValues());
        if (ps4 != null)
            outState.putString("bl_ps4", ps4.getOriginalValues());
        if (xbox != null)
            outState.putString("bl_xbox", xbox.getOriginalValues());
        outState.putLong("time", System.currentTimeMillis());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_refresh, menu);
    }

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

    private void refresh(Boolean show) {
        showProgress(show);
        if (mTask == null) {
            mTask = new BadlandsRefresh(getActivity());
            mTask.execute();
        }
    }

    private final Runnable mTimer = new Runnable() {
        @Override
        public void run() {
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }
            mHandler.postDelayed(this, 1000);
        }
    };

    private final Runnable mRefreshTimer = new Runnable() {
        @Override
        public void run() {
            refresh(false);
            mHandler.postDelayed(this, 5 * 60 * 1000);
        }
    };

    @Override
    public void onDestroy() {
        mHandler.removeCallbacksAndMessages(mTimer);
        mHandler.removeCallbacksAndMessages(mRefreshTimer);
        super.onDestroy();
    }

    @Override
    public void onPause() {
        mHandler.removeCallbacksAndMessages(mTimer);
        mHandler.removeCallbacksAndMessages(mRefreshTimer);
        super.onPause();
    }

    @Override
    public void onResume() {
        mHandler.postDelayed(mRefreshTimer, 60 * 1000);
        mTimer.run();
        super.onResume();
    }

    @Override
    public void onStart() {
        if (mUpdate)
            refresh(true);
        mUpdate = true;
        super.onStart();
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    private void showProgress(final Boolean show) {
        if (!isAdded())
            return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
            mRefreshView.setVisibility(View.VISIBLE);
            mRefreshView.animate()
                    .setDuration(shortAnimTime)
                    .alpha(show ? 1 : 0)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mRefreshView.setVisibility(show ? View.VISIBLE : View.GONE);
                        }
                    });

            mListView.setVisibility(View.VISIBLE);
            mListView.animate()
                    .setDuration(shortAnimTime)
                    .alpha(show ? 0 : 1)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mListView.setVisibility(show ? View.GONE : View.VISIBLE);
                        }
                    });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            mRefreshView.setVisibility(show ? View.VISIBLE : View.GONE);
            mListView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    }

    public class BadlandsRefresh extends AsyncTask<Void, Void, Boolean> {
        private static final String KEY = "BADLANDS";
        private Activity activity;
        private List<BadlandNode> data;
        private List<BadlandNode> data_ps4;
        private List<BadlandNode> data_xbox;
        private boolean error;

        public BadlandsRefresh(Activity activity) {
            this.activity = activity;
            error = false;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            if (activity == null)
                return false;
            try {
                SharedPreferences preferences = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE);
                String cache = preferences.getString(KEY + "_cache", "_ded");
                String response;
                try {
                    response = Http.get("http://deathsnacks.com/wf/data/currentbadlands_2.json", preferences, KEY);
                } catch (IOException ex) {
                    //We failed to update, but we still have a cache, hopefully.
                    ex.printStackTrace();
                    //If no cache, proceed to normally handling an exception.
                    if (cache.equals("_ded"))
                        throw ex;
                    response = cache;
                    error = true;
                }
                Type collectionType = new TypeToken<List<BadlandNode>>() {
                }.getType();
                data = (new GsonBuilder().create()).fromJson(response, collectionType);

                String cache_ps4 = preferences.getString(KEY + "_ps4_cache", "_ded");
                String response_ps4;
                try {
                    response_ps4 = Http.get("http://deathsnacks.com/wf/data/ps4/currentbadlands_2.json", preferences, KEY + "_ps4");
                } catch (IOException ex) {
                    //We failed to update, but we still have a cache, hopefully.
                    ex.printStackTrace();
                    //If no cache, proceed to normally handling an exception.
                    if (cache_ps4.equals("_ded"))
                        throw ex;
                    response_ps4 = cache_ps4;
                    error = true;
                }
                data_ps4 = (new GsonBuilder().create()).fromJson(response_ps4, collectionType);

                String cache_xbox = preferences.getString(KEY + "_xbox_cache", "_ded");
                String response_xbox;
                try {
                    response_xbox = Http.get("http://deathsnacks.com/wf/data/xbox/currentbadlands_2.json", preferences, KEY + "_xbox");
                } catch (IOException ex) {
                    //We failed to update, but we still have a cache, hopefully.
                    ex.printStackTrace();
                    //If no cache, proceed to normally handling an exception.
                    if (cache_xbox.equals("_ded"))
                        throw ex;
                    response_xbox = cache_xbox;
                    error = true;
                }
                data_xbox = (new GsonBuilder().create()).fromJson(response_xbox, collectionType);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {
            mTask = null;
            if (activity == null)
                return;
            showProgress(false);
            SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
            if (success) {
                mAdapter = new SeparatedListAdapter(activity, null);
                if (mPreferences.getString("platform", "pc|ps4|xbox").contains("pc")) {
                    Collections.sort(data,
                            new Comparator<BadlandNode>() {
                                @Override
                                public int compare(BadlandNode bl1, BadlandNode bl2) {
                                    String a = bl1.getNodeRegionName() + bl1.getNode();
                                    String b = bl2.getNodeRegionName() + bl2.getNode();
                                    return a.compareTo(b);
                                }
                            });
                    mAdapter.addSection("PC", new BadlandsListViewAdapter(activity, data));
                }
                if (mPreferences.getString("platform", "pc|ps4|xbox").contains("ps4")) {
                    Collections.sort(data_ps4,
                            new Comparator<BadlandNode>() {
                                @Override
                                public int compare(BadlandNode bl1, BadlandNode bl2) {
                                    String a = bl1.getNodeRegionName() + bl1.getNode();
                                    String b = bl2.getNodeRegionName() + bl2.getNode();
                                    return a.compareTo(b);
                                }
                            });
                    mAdapter.addSection("PS4", new BadlandsListViewAdapter(activity, data_ps4));
                }
                if (mPreferences.getString("platform", "pc|ps4|xbox").contains("xbox")) {
                    Collections.sort(data_xbox,
                            new Comparator<BadlandNode>() {
                                @Override
                                public int compare(BadlandNode bl1, BadlandNode bl2) {
                                    String a = bl1.getNodeRegionName() + bl1.getNode();
                                    String b = bl2.getNodeRegionName() + bl2.getNode();
                                    return a.compareTo(b);
                                }
                            });
                    mAdapter.addSection("Xbox One", new BadlandsListViewAdapter(activity, data_xbox));
                }
                mListView.setAdapter(mAdapter);
                if (error) {
                    Toast.makeText(activity, R.string.error_error_occurred, Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(activity.getApplicationContext(), R.string.error_error_occurred, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onCancelled() {
            mTask = null;
            showProgress(false);
        }
    }
}




Java Source Code List

afzkl.development.colorpickerview.dialog.ColorPickerDialog.java
afzkl.development.colorpickerview.drawable.AlphaPatternDrawable.java
afzkl.development.colorpickerview.preference.ColorPickerPreference.java
afzkl.development.colorpickerview.view.ColorPanelView.java
afzkl.development.colorpickerview.view.ColorPickerView.java
android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
com.deathsnacks.wardroid.Constants.java
com.deathsnacks.wardroid.activities.CustomFilterActivity.java
com.deathsnacks.wardroid.activities.MainActivity.java
com.deathsnacks.wardroid.activities.SettingsActivity.java
com.deathsnacks.wardroid.adapters.AlertsListViewAdapter.java
com.deathsnacks.wardroid.adapters.BadlandsListViewAdapter.java
com.deathsnacks.wardroid.adapters.InvasionListViewAdapter.java
com.deathsnacks.wardroid.adapters.NewsListViewAdapter.java
com.deathsnacks.wardroid.adapters.SeparatedListAdapter.java
com.deathsnacks.wardroid.fragments.AlertsFragment.java
com.deathsnacks.wardroid.fragments.BadlandDialogFragment.java
com.deathsnacks.wardroid.fragments.BadlandsFragment.java
com.deathsnacks.wardroid.fragments.InvasionFragment.java
com.deathsnacks.wardroid.fragments.NewsFragment.java
com.deathsnacks.wardroid.fragments.SettingsFragment.java
com.deathsnacks.wardroid.gson.alert.Activation.java
com.deathsnacks.wardroid.gson.alert.Alert.java
com.deathsnacks.wardroid.gson.alert.CountedItem.java
com.deathsnacks.wardroid.gson.alert.Expiry.java
com.deathsnacks.wardroid.gson.alert.MissionInfo.java
com.deathsnacks.wardroid.gson.alert.MissionReward.java
com.deathsnacks.wardroid.gson.alert._id.java
com.deathsnacks.wardroid.gson.badlands.AttId.java
com.deathsnacks.wardroid.gson.badlands.AttackerInfo.java
com.deathsnacks.wardroid.gson.badlands.BadlandNode.java
com.deathsnacks.wardroid.gson.badlands.ConflictExpiration.java
com.deathsnacks.wardroid.gson.badlands.CountedItem.java
com.deathsnacks.wardroid.gson.badlands.DefId.java
com.deathsnacks.wardroid.gson.badlands.DefenderInfo.java
com.deathsnacks.wardroid.gson.badlands.DeploymentActivationTime.java
com.deathsnacks.wardroid.gson.badlands.End.java
com.deathsnacks.wardroid.gson.badlands.History.java
com.deathsnacks.wardroid.gson.badlands.Id.java
com.deathsnacks.wardroid.gson.badlands.LastHealTime.java
com.deathsnacks.wardroid.gson.badlands.MissionInfo.java
com.deathsnacks.wardroid.gson.badlands.MissionRewardExtra.java
com.deathsnacks.wardroid.gson.badlands.MissionReward.java
com.deathsnacks.wardroid.gson.badlands.Start.java
com.deathsnacks.wardroid.gson.badlands.TaxChangeAllowedTime.java
com.deathsnacks.wardroid.gson.badlands.WinId.java
com.deathsnacks.wardroid.gson.badlands._id.java
com.deathsnacks.wardroid.services.GcmBroadcastReceiver.java
com.deathsnacks.wardroid.services.NotificationsUpdateReceiver.java
com.deathsnacks.wardroid.services.PollingAlarmReceiver.java
com.deathsnacks.wardroid.services.UpdateReceiver.java
com.deathsnacks.wardroid.utils.GlobalApplication.java
com.deathsnacks.wardroid.utils.Http.java
com.deathsnacks.wardroid.utils.Names.java
com.deathsnacks.wardroid.utils.Platform.java
com.deathsnacks.wardroid.utils.PreferenceActivityCallbacks.java
com.deathsnacks.wardroid.utils.PreferenceUtils.java
com.deathsnacks.wardroid.utils.UnifiedPreferenceUtils.java
com.deathsnacks.wardroid.utils.Utils.java
com.deathsnacks.wardroid.utils.gcmclasses.Alert.java
com.deathsnacks.wardroid.utils.gcmclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.Alert.java
com.deathsnacks.wardroid.utils.httpclasses.InvasionMini.java
com.deathsnacks.wardroid.utils.httpclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.News.java
com.deathsnacks.wardroid.utils.httpclasses.Sale.java
com.deathsnacks.wardroid.utils.preferences.IntEditTextPreference.java
com.deathsnacks.wardroid.utils.preferences.MultiSelectListPreference.java