Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ie.appz.youtubevideostestapp;
- import android.app.Activity;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.app.ListFragment;
- import android.support.v4.app.LoaderManager;
- import android.support.v4.content.CursorLoader;
- import android.support.v4.content.Loader;
- import android.support.v4.widget.CursorAdapter;
- import android.support.v4.widget.SimpleCursorAdapter;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListView;
- import android.widget.SearchView;
- import java.lang.ref.WeakReference;
- import java.util.ArrayList;
- import ie.appz.youtubevideostestapp.adapters.TwitterSearchResultAdapter;
- import ie.appz.youtubevideostestapp.persistence.UsersOpenHelper;
- import ie.appz.youtubevideostestapp.persistence.UsersProvider;
- import twitter4j.ResponseList;
- import twitter4j.Twitter;
- import twitter4j.TwitterException;
- import twitter4j.TwitterFactory;
- import twitter4j.User;
- import twitter4j.conf.ConfigurationBuilder;
- public class FeedListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
- /**
- * The serialization (saved instance state) Bundle key representing the
- * activated item position. Only used on tablets.
- */
- private static final String STATE_ACTIVATED_POSITION = "activated_position";
- // Identifies a particular Loader being used in this component
- private static final int URL_LOADER = 0;
- TwitterSearchHandler mTwitterSearchHandler;
- private OnUserSelectedListener onUserSelectedListener;
- /**
- * The current activated item position. Only used on tablets.
- */
- private int mActivatedPosition = ListView.INVALID_POSITION;
- private SimpleCursorAdapter adapter;
- private ListView searchResultsListView;
- private ListView listView;
- private TwitterSearchResultAdapter twitterSearchResultAdapter;
- private ResponseList<User> mFoundUsers;
- /**
- * Mandatory empty constructor for the fragment manager to instantiate the
- * fragment (e.g. upon screen orientation changes).
- */
- public FeedListFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
- super.onCreateView(layoutInflater, container, savedInstanceState);
- View view = layoutInflater.inflate(R.layout.fragment_feed_list,container,false);
- searchResultsListView = (ListView) view.findViewById(R.id.searchResultListView);
- twitterSearchResultAdapter = new TwitterSearchResultAdapter(getActivity(), mFoundUsers);
- searchResultsListView.setAdapter(twitterSearchResultAdapter);
- return view;
- }
- @Override
- public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
- String[] projection = {UsersOpenHelper.COLUMN_ID, UsersOpenHelper.SCREENNAME};
- return new CursorLoader(getActivity(), UsersProvider.CONTENT_URI, projection, null, null, null);
- }
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
- menuInflater.inflate(ie.appz.youtubevideostestapp.R.menu.activity_feed_list, menu);
- SearchView avSearch = (SearchView) menu.findItem(R.id.action_search).getActionView();
- if (avSearch != null) {
- avSearch.setIconifiedByDefault(true);
- avSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- @Override
- public boolean onQueryTextChange(String query) {
- if (query.length() == 0)
- searchResultsListView.setVisibility(View.GONE);
- else {
- searchResultsListView.setVisibility(View.VISIBLE);
- submitLocationQuery(query);
- }
- return true;
- }
- @Override
- public boolean onQueryTextSubmit(String query) {
- if (query.length() >= 1) {
- searchResultsListView.setVisibility(View.VISIBLE);
- submitLocationQuery(query);
- }
- return true;
- }
- });
- }
- }
- private void submitLocationQuery(final String query) {
- Thread thrd = new Thread() {
- public void run() {
- Message threadMessage = new Message();
- ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
- configurationBuilder.setDebugEnabled(true)
- .setOAuthConsumerKey(getResources().getString(ie.appz.youtubevideostestapp.R.string.consumer_key))
- .setOAuthConsumerSecret(getResources().getString(ie.appz.youtubevideostestapp.R.string.consumer_secret))
- .setOAuthAccessToken(getResources().getString(ie.appz.youtubevideostestapp.R.string.access_token))
- .setOAuthAccessTokenSecret(getResources().getString(ie.appz.youtubevideostestapp.R.string.access_token_secret));
- TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build());
- Twitter twitter = twitterFactory.getInstance();
- //twitterFactory.getInstance(new AccessToken(getResources().getString(R.string.access_token),getResources().getString(R.string.access_token_secret)));
- try {
- mFoundUsers = twitter.searchUsers(query, 1);
- threadMessage.arg1 = 1;
- } catch (TwitterException e) {
- e.printStackTrace();
- }
- mTwitterSearchHandler.sendMessage(threadMessage);
- }
- };
- thrd.start();
- }
- @Override
- public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
- adapter.swapCursor(cursor);
- }
- @Override
- public void onLoaderReset(Loader<Cursor> cursorLoader) {
- adapter.swapCursor(null);
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- String[] uiBindFrom = {UsersOpenHelper.SCREENNAME};
- int[] uiBindTo = {android.R.id.text1};
- getLoaderManager().initLoader(URL_LOADER, null, this);
- adapter = new SimpleCursorAdapter(
- getActivity().getApplicationContext(), android.R.layout.simple_list_item_activated_1,
- null, uiBindFrom, uiBindTo,
- CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
- setListAdapter(adapter);
- setHasOptionsMenu(true);
- mTwitterSearchHandler = new TwitterSearchHandler(this);
- }
- public interface OnUserSelectedListener {
- /**
- * Callback for when an item has been selected.
- */
- public void onItemSelected(String id);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- onUserSelectedListener = (OnUserSelectedListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString() + " must implement OnUserSelectedListener");
- }
- }
- @Override
- public void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- if (mActivatedPosition != ListView.INVALID_POSITION) {
- // Serialize and persist the activated item position.
- outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
- }
- }
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- String projection[] = { };
- Cursor tutorialCursor = getActivity().getContentResolver().query(
- Uri.withAppendedPath(UsersProvider.CONTENT_URI,
- String.valueOf(id)), projection, null, null, null);
- if (tutorialCursor.moveToFirst()) {
- String tutorialUrl = tutorialCursor.getString(0);
- onUserSelectedListener.onItemSelected(tutorialUrl);
- }
- tutorialCursor.close();
- l.setItemChecked(position, true);
- }
- /**
- * Turns on activate-on-click mode. When this mode is on, list items will be
- * given the 'activated' state when touched.
- */
- public void setActivateOnItemClick(boolean activateOnItemClick) {
- // When setting CHOICE_MODE_SINGLE, ListView will automatically
- // give items the 'activated' state when touched.
- getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
- }
- private void setActivatedPosition(int position) {
- if (position == ListView.INVALID_POSITION) {
- getListView().setItemChecked(mActivatedPosition, false);
- } else {
- getListView().setItemChecked(position, true);
- }
- mActivatedPosition = position;
- }
- /**
- * A callback interface that all activities containing this fragment must
- * implement. This mechanism allows activities to be notified of item
- * selections.
- */
- static public class TwitterSearchHandler extends Handler {
- // private Handler gcCallbackHandler = new Handler() {
- private WeakReference<FeedListFragment> activityReference;
- public TwitterSearchHandler(FeedListFragment mainActivity) {
- super();
- this.activityReference = new WeakReference<FeedListFragment>(
- mainActivity);
- }
- @Override
- public void handleMessage(Message msg) {
- FeedListFragment activity = activityReference.get();
- if (activity != null) {
- ArrayList<String> stringArray = new ArrayList<String>();
- if (msg.arg1 == 1) {
- if (activity.mFoundUsers != null
- && !activity.mFoundUsers.isEmpty()) {
- activity.twitterSearchResultAdapter.changeDataSet(activity.mFoundUsers);
- } else {
- stringArray.add("Address Not Found");
- }
- } else if (msg.arg1 == 0) {
- stringArray.add("Failed to connect to network.");
- stringArray
- .add("Please check your network connection and retry.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement