Android Open Source - crtaci Cartoons Activity






From Project

Back to project page crtaci.

License

The source code is released under:

GNU General Public License

If you think the Android project crtaci 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.github.gen2brain.crtaci.activities;
//from   w ww.j a  v  a2s .c  om
import android.os.AsyncTask;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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

import com.github.gen2brain.crtaci.fragments.CartoonsFragment;
import com.github.gen2brain.crtaci.R;
import com.github.gen2brain.crtaci.entities.Character;
import com.github.gen2brain.crtaci.entities.Cartoon;
import com.github.gen2brain.crtaci.utils.Connectivity;
import com.github.gen2brain.crtaci.utils.Utils;

import go.main.Main;


public class CartoonsActivity extends ActionBarActivity {

    public static final String TAG = "CartoonsActivity";

    private boolean twoPane;
    private Character character;
    private ArrayList<Cartoon> cartoons;
    private CartoonsTask cartoonsTask;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cartoons);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        if(findViewById(R.id.cartoons_container) != null) {
            twoPane = true;
        } else {
            twoPane = false;
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle bundle = getIntent().getExtras();
        character = (Character) bundle.get("character");

        String title = character.name;
        title = String.format(Utils.toTitleCase(title));

        toolbar.setSubtitle(title);
        toolbar.setLogo(R.drawable.ic_launcher);
        toolbar.setNavigationIcon(R.drawable.ic_chevron_left);

        if(savedInstanceState != null) {
            cartoons = (ArrayList<Cartoon>) savedInstanceState.getSerializable("cartoons");
            if(cartoons != null) {
                replaceFragment(cartoons);
            } else {
                startCartoonsTask();
            }
        } else {
            startCartoonsTask();
        }

    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(TAG, "onSaveInstanceState");
        super.onSaveInstanceState(outState);
        if(cartoons != null && !cartoons.isEmpty()) {
            outState.putSerializable("cartoons", cartoons);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.cartoons, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == android.R.id.home) {
            NavUtils.navigateUpFromSameTask(this);
            return true;
        } else if(id == R.id.action_about) {
            Utils.showAbout(this);
            return true;
        } else if(id == R.id.action_rate) {
            Utils.rateThisApp(this);
        } else if(id == R.id.action_refresh) {
            startCartoonsTask();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        NavUtils.navigateUpFromSameTask(this);
    }

    public void startCartoonsTask() {
        String query;
        if(!character.altname.isEmpty()) {
            query = character.altname;
        } else {
            query = character.name;
        }

        if(Connectivity.isConnected(this)) {
            cartoonsTask = new CartoonsTask();
            cartoonsTask.execute(query);
        } else {
            Toast.makeText(this, getString(R.string.error_network), Toast.LENGTH_LONG).show();
        }
    }

    public void replaceFragment(ArrayList<Cartoon> results) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentById(R.id.container);
        if (prev != null) {
            ft.remove(prev);
        }
        ft.replace(R.id.container, CartoonsFragment.newInstance(results, twoPane));
        ft.commit();
    }


    private class CartoonsTask extends AsyncTask<String, Void, ArrayList<Cartoon>> {

        protected void onPreExecute() {
            super.onPreExecute();
            if(progressBar != null) {
                progressBar.setVisibility(View.VISIBLE);
            }
        }

        protected ArrayList<Cartoon> doInBackground(String... params) {
            String query = params[0];

            String result = null;
            try {
                result = Main.Search(query);
            } catch(Exception e) {
                e.printStackTrace();
            }

            if(result == null || result.equals("empty")) {
                return null;
            }

            Type listType = new TypeToken<ArrayList<Cartoon>>(){}.getType();
            try {
                ArrayList<Cartoon> list = new Gson().fromJson(result, listType);
                return list;
            } catch(Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        protected void onPostExecute(ArrayList<Cartoon> results) {
            Log.d(TAG, "onPostExecute");
            if(progressBar != null) {
                progressBar.setVisibility(View.GONE);
            }
            if(results != null) {
                cartoons = results;
                try {
                    replaceFragment(results);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplication(), getString(R.string.error_network), Toast.LENGTH_LONG).show();
            }
        }

    }

}




Java Source Code List

com.github.gen2brain.crtaci.activities.CartoonsActivity.java
com.github.gen2brain.crtaci.activities.CharactersActivity.java
com.github.gen2brain.crtaci.activities.PlayerActivity.java
com.github.gen2brain.crtaci.entities.Cartoon.java
com.github.gen2brain.crtaci.entities.Character.java
com.github.gen2brain.crtaci.fragments.CartoonsFragment.java
com.github.gen2brain.crtaci.fragments.CharactersFragment.java
com.github.gen2brain.crtaci.utils.Connectivity.java
com.github.gen2brain.crtaci.utils.Update.java
com.github.gen2brain.crtaci.utils.Utils.java
com.github.gen2brain.crtaci.utils.VideoEnabledWebChromeClient.java
com.github.gen2brain.crtaci.utils.VideoEnabledWebView.java
go.Go.java
go.Seq.java
go.main.Main.java