Advertisement
Ankhwatcher

FeedListFragment

Jul 17th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.77 KB | None | 0 0
  1. package ie.appz.youtubevideostestapp;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.support.v4.app.ListFragment;
  11. import android.support.v4.app.LoaderManager;
  12. import android.support.v4.content.CursorLoader;
  13. import android.support.v4.content.Loader;
  14. import android.support.v4.widget.CursorAdapter;
  15. import android.support.v4.widget.SimpleCursorAdapter;
  16. import android.view.LayoutInflater;
  17. import android.view.Menu;
  18. import android.view.MenuInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.ListView;
  22. import android.widget.SearchView;
  23.  
  24. import java.lang.ref.WeakReference;
  25. import java.util.ArrayList;
  26.  
  27. import ie.appz.youtubevideostestapp.adapters.TwitterSearchResultAdapter;
  28. import ie.appz.youtubevideostestapp.persistence.UsersOpenHelper;
  29. import ie.appz.youtubevideostestapp.persistence.UsersProvider;
  30. import twitter4j.ResponseList;
  31. import twitter4j.Twitter;
  32. import twitter4j.TwitterException;
  33. import twitter4j.TwitterFactory;
  34. import twitter4j.User;
  35. import twitter4j.conf.ConfigurationBuilder;
  36.  
  37.  
  38. public class FeedListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
  39.     /**
  40.      * The serialization (saved instance state) Bundle key representing the
  41.      * activated item position. Only used on tablets.
  42.      */
  43.     private static final String STATE_ACTIVATED_POSITION = "activated_position";
  44.     // Identifies a particular Loader being used in this component
  45.     private static final int URL_LOADER = 0;
  46.     TwitterSearchHandler mTwitterSearchHandler;
  47.     private OnUserSelectedListener onUserSelectedListener;
  48.     /**
  49.      * The current activated item position. Only used on tablets.
  50.      */
  51.     private int mActivatedPosition = ListView.INVALID_POSITION;
  52.     private SimpleCursorAdapter adapter;
  53.     private ListView searchResultsListView;
  54.     private ListView listView;
  55.     private TwitterSearchResultAdapter twitterSearchResultAdapter;
  56.     private ResponseList<User> mFoundUsers;
  57.  
  58.     /**
  59.      * Mandatory empty constructor for the fragment manager to instantiate the
  60.      * fragment (e.g. upon screen orientation changes).
  61.      */
  62.     public FeedListFragment() {
  63.     }
  64.  
  65.     @Override
  66.     public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
  67.         super.onCreateView(layoutInflater, container, savedInstanceState);
  68.         View view = layoutInflater.inflate(R.layout.fragment_feed_list,container,false);
  69.         searchResultsListView = (ListView) view.findViewById(R.id.searchResultListView);
  70.         twitterSearchResultAdapter = new TwitterSearchResultAdapter(getActivity(), mFoundUsers);
  71.         searchResultsListView.setAdapter(twitterSearchResultAdapter);
  72.         return view;
  73.     }
  74.  
  75.     @Override
  76.     public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
  77.         String[] projection = {UsersOpenHelper.COLUMN_ID, UsersOpenHelper.SCREENNAME};
  78.  
  79.         return new CursorLoader(getActivity(), UsersProvider.CONTENT_URI, projection, null, null, null);
  80.     }
  81.  
  82.     @Override
  83.     public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
  84.         menuInflater.inflate(ie.appz.youtubevideostestapp.R.menu.activity_feed_list, menu);
  85.  
  86.  
  87.         SearchView avSearch = (SearchView) menu.findItem(R.id.action_search).getActionView();
  88.         if (avSearch != null) {
  89.             avSearch.setIconifiedByDefault(true);
  90.  
  91.             avSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  92.  
  93.                 @Override
  94.                 public boolean onQueryTextChange(String query) {
  95.                     if (query.length() == 0)
  96.                         searchResultsListView.setVisibility(View.GONE);
  97.                     else {
  98.                         searchResultsListView.setVisibility(View.VISIBLE);
  99.                         submitLocationQuery(query);
  100.                     }
  101.                     return true;
  102.                 }
  103.  
  104.                 @Override
  105.                 public boolean onQueryTextSubmit(String query) {
  106.                     if (query.length() >= 1) {
  107.                         searchResultsListView.setVisibility(View.VISIBLE);
  108.                         submitLocationQuery(query);
  109.                     }
  110.                     return true;
  111.                 }
  112.             });
  113.         }
  114.  
  115.     }
  116.  
  117.     private void submitLocationQuery(final String query) {
  118.         Thread thrd = new Thread() {
  119.             public void run() {
  120.                 Message threadMessage = new Message();
  121.                 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
  122.                 configurationBuilder.setDebugEnabled(true)
  123.                         .setOAuthConsumerKey(getResources().getString(ie.appz.youtubevideostestapp.R.string.consumer_key))
  124.                         .setOAuthConsumerSecret(getResources().getString(ie.appz.youtubevideostestapp.R.string.consumer_secret))
  125.                         .setOAuthAccessToken(getResources().getString(ie.appz.youtubevideostestapp.R.string.access_token))
  126.                         .setOAuthAccessTokenSecret(getResources().getString(ie.appz.youtubevideostestapp.R.string.access_token_secret));
  127.                 TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build());
  128.                 Twitter twitter = twitterFactory.getInstance();
  129.                 //twitterFactory.getInstance(new AccessToken(getResources().getString(R.string.access_token),getResources().getString(R.string.access_token_secret)));
  130.  
  131.                 try {
  132.                     mFoundUsers = twitter.searchUsers(query, 1);
  133.                     threadMessage.arg1 = 1;
  134.                 } catch (TwitterException e) {
  135.                     e.printStackTrace();
  136.                 }
  137.                 mTwitterSearchHandler.sendMessage(threadMessage);
  138.             }
  139.         };
  140.         thrd.start();
  141.  
  142.     }
  143.  
  144.     @Override
  145.     public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
  146.         adapter.swapCursor(cursor);
  147.     }
  148.  
  149.     @Override
  150.     public void onLoaderReset(Loader<Cursor> cursorLoader) {
  151.         adapter.swapCursor(null);
  152.     }
  153.  
  154.     @Override
  155.     public void onCreate(Bundle savedInstanceState) {
  156.         super.onCreate(savedInstanceState);
  157.  
  158.  
  159.         String[] uiBindFrom = {UsersOpenHelper.SCREENNAME};
  160.         int[] uiBindTo = {android.R.id.text1};
  161.         getLoaderManager().initLoader(URL_LOADER, null, this);
  162.  
  163.         adapter = new SimpleCursorAdapter(
  164.                 getActivity().getApplicationContext(), android.R.layout.simple_list_item_activated_1,
  165.                 null, uiBindFrom, uiBindTo,
  166.                 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  167.         setListAdapter(adapter);
  168.         setHasOptionsMenu(true);
  169.  
  170.         mTwitterSearchHandler = new TwitterSearchHandler(this);
  171.     }
  172.     public interface OnUserSelectedListener {
  173.         /**
  174.          * Callback for when an item has been selected.
  175.          */
  176.         public void onItemSelected(String id);
  177.     }
  178.     @Override
  179.     public void onActivityCreated(Bundle savedInstanceState) {
  180.         super.onActivityCreated(savedInstanceState);
  181.  
  182.         getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  183.     }
  184.  
  185.     @Override
  186.     public void onAttach(Activity activity) {
  187.         super.onAttach(activity);
  188.  
  189.         try {
  190.             onUserSelectedListener = (OnUserSelectedListener) activity;
  191.         } catch (ClassCastException e) {
  192.             throw new ClassCastException(activity.toString() + " must implement OnUserSelectedListener");
  193.         }
  194.     }
  195.  
  196.     @Override
  197.     public void onSaveInstanceState(Bundle outState) {
  198.         super.onSaveInstanceState(outState);
  199.         if (mActivatedPosition != ListView.INVALID_POSITION) {
  200.             // Serialize and persist the activated item position.
  201.             outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
  202.         }
  203.     }
  204.  
  205.     @Override
  206.     public void onListItemClick(ListView l, View v, int position, long id) {
  207.         String projection[] = {  };
  208.         Cursor tutorialCursor = getActivity().getContentResolver().query(
  209.                 Uri.withAppendedPath(UsersProvider.CONTENT_URI,
  210.                         String.valueOf(id)), projection, null, null, null);
  211.         if (tutorialCursor.moveToFirst()) {
  212.             String tutorialUrl = tutorialCursor.getString(0);
  213.             onUserSelectedListener.onItemSelected(tutorialUrl);
  214.         }
  215.         tutorialCursor.close();
  216.         l.setItemChecked(position, true);
  217.     }
  218.  
  219.     /**
  220.      * Turns on activate-on-click mode. When this mode is on, list items will be
  221.      * given the 'activated' state when touched.
  222.             */
  223.      public void setActivateOnItemClick(boolean activateOnItemClick) {
  224.      // When setting CHOICE_MODE_SINGLE, ListView will automatically
  225.      // give items the 'activated' state when touched.
  226.      getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
  227.      }
  228.  
  229.      private void setActivatedPosition(int position) {
  230.      if (position == ListView.INVALID_POSITION) {
  231.      getListView().setItemChecked(mActivatedPosition, false);
  232.      } else {
  233.      getListView().setItemChecked(position, true);
  234.      }
  235.  
  236.      mActivatedPosition = position;
  237.      }
  238.  
  239.     /**
  240.      * A callback interface that all activities containing this fragment must
  241.      * implement. This mechanism allows activities to be notified of item
  242.      * selections.
  243.      */
  244.  
  245.  
  246.     static public class TwitterSearchHandler extends Handler {
  247.         // private Handler gcCallbackHandler = new Handler() {
  248.  
  249.         private WeakReference<FeedListFragment> activityReference;
  250.  
  251.         public TwitterSearchHandler(FeedListFragment mainActivity) {
  252.             super();
  253.             this.activityReference = new WeakReference<FeedListFragment>(
  254.                     mainActivity);
  255.         }
  256.  
  257.         @Override
  258.         public void handleMessage(Message msg) {
  259.             FeedListFragment activity = activityReference.get();
  260.             if (activity != null) {
  261.                 ArrayList<String> stringArray = new ArrayList<String>();
  262.                 if (msg.arg1 == 1) {
  263.                     if (activity.mFoundUsers != null
  264.                             && !activity.mFoundUsers.isEmpty()) {
  265.  
  266.                         activity.twitterSearchResultAdapter.changeDataSet(activity.mFoundUsers);
  267.                     } else {
  268.                         stringArray.add("Address Not Found");
  269.                     }
  270.  
  271.                 } else if (msg.arg1 == 0) {
  272.  
  273.                     stringArray.add("Failed to connect to network.");
  274.                     stringArray
  275.                             .add("Please check your network connection and retry.");
  276.  
  277.                 }
  278.  
  279.             }
  280.         }
  281.     }
  282.  
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement