Android Open Source - pypodroid Item List Activity






From Project

Back to project page pypodroid.

License

The source code is released under:

Apache License

If you think the Android project pypodroid 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 de.daxbau.pypodroid;
//from  w  ww. ja  va2s .  c o  m
import android.content.ClipData;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.daxbau.pypodroid.pypo.API;
import de.daxbau.pypodroid.pypo.Item;


/**
 * An activity representing a list of Items. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link ItemDetailActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link ItemListFragment} and the item details
 * (if present) is a {@link ItemDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link ItemListFragment.Callbacks} interface
 * to listen for item selections.
 */
public class ItemListActivity extends FragmentActivity
        implements ItemListFragment.Callbacks {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        ItemListFragment itemList = ((ItemListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.item_list));

        if (findViewById(R.id.item_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            itemList.setActivateOnItemClick(true);
        }

        Intent intent = getIntent();
        String action = intent.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {
            Uri data = intent.getData();
            try {
                addItem(data.getHost());
            } catch (NullPointerException e) {
                finish();
            }
        } else if (Intent.ACTION_SEND.equals(action)) {
            ClipData data = intent.getClipData();
            if (data.getItemCount() >= 1) {
                ClipData.Item item = data.getItemAt(0);
                String url = String.valueOf(item.getText());
                addItem(url);
            }

        } else {
            refreshItems();
        }
    }

    private void addItem(String url) {
        Log.d("Adding item:", url);

        AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                Log.d("API:", "success");
                try {
                    JSONObject jsonObject = new JSONObject(new String(responseBody));
                    Log.d("API:", jsonObject.toString());
                    Toast.makeText(getApplicationContext(),
                            jsonObject.getString("title"), Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            R.string.error_api, Toast.LENGTH_LONG).show();
                }
                refreshItems();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                if (responseBody != null) {
                    Log.d("API FAILURE:", new String(responseBody));
                } else {
                    Log.d("API FAILURE:", String.valueOf(statusCode));
                }
                Toast.makeText(getApplicationContext(),
                        R.string.error_api, Toast.LENGTH_LONG).show();
                showProgress(false);
            }

        };
        login();
        RequestParams params = new RequestParams();
        params.add("url", url);
        API.post("/api/items/", params, handler);
    }

    /**
     * Callback method from {@link ItemListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
            ItemDetailFragment fragment = new ItemDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, ItemDetailActivity.class);
            detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            case R.id.action_refresh:
                refreshItems();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void openSettings() {
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivity(settingsIntent);
    }

    private void refreshItems() {
        showProgress(true);
        AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                Log.d("API:", "success");
                try {
                    JSONArray jsonArray = new JSONArray(new String(responseBody));
                    Item.clearItems();
                    for (int i=0; i<jsonArray.length(); i++) {
                        JSONObject row = jsonArray.getJSONObject(i);
                        Log.d("API:", row.getString("title"));
                        // FIXME: bad usage of a global value
                        Item.addItem(new Item.PypoItem(
                                row.getString("id"),
                                row.getString("title"),
                                row.getString("url"),
                                row.getString("readable_article")));
                    }
                    ((ItemListFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.item_list)).refreshItems();
                } catch (JSONException e) {
                    Log.d("API FAILURE", e.toString());
                    String errorMsg = getResources().getString(R.string.error_api);
                    errorMsg += " Response: " + new String(responseBody);
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
                showProgress(false);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                if (responseBody != null) {
                    Log.d("API FAILURE", new String(responseBody));
                } else {
                    Log.d("API FAILURE", String.valueOf(statusCode));
                }
                String errorMsg = getResources().getString(R.string.error_api);
                if (error != null) {
                    Log.d("API FAILURE", error.toString());
                    errorMsg += " : " + error.toString();
                }
                Toast.makeText(getApplicationContext(),
                        errorMsg, Toast.LENGTH_LONG).show();
                showProgress(false);
            }

        };
        login();
        API.get("/api/items/", null, handler);
    }

    private void login() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String username = sharedPreferences.getString(SettingsActivity.KEY_PREF_USERNAME, "");;
        String password = sharedPreferences.getString(SettingsActivity.KEY_PREF_PASSWORD, "");
        String baseURL = sharedPreferences.getString(SettingsActivity.KEY_PREF_URL, "");
        API.login(username, password, baseURL);
    }

    private void showProgress(boolean b) {

    }

    private void openSearch() {

    }
}




Java Source Code List

de.daxbau.pypodroid.BuildConfig.java
de.daxbau.pypodroid.ItemDetailActivity.java
de.daxbau.pypodroid.ItemDetailFragment.java
de.daxbau.pypodroid.ItemListActivity.java
de.daxbau.pypodroid.ItemListFragment.java
de.daxbau.pypodroid.SettingsActivity.java
de.daxbau.pypodroid.SettingsFragment.java
de.daxbau.pypodroid.pypo.API.java
de.daxbau.pypodroid.pypo.Item.java