Search in sources :

Example 66 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class ChoosenListActivity method getLists.

public void getLists() {
    canRefresh = false;
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                ResponseList<twitter4j.Status> lists = twitter.getUserListStatuses(listId, paging);
                paging.setPage(paging.getPage() + 1);
                for (Status status : lists) {
                    statuses.add(status);
                }
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (adapter == null) {
                            adapter = new TimelineArrayAdapter(context, statuses);
                            listView.setAdapter(adapter);
                            listView.setVisibility(View.VISIBLE);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                        spinner.setVisibility(View.GONE);
                        canRefresh = true;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                    }
                });
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                    }
                });
            }
        }
    }).start();
}
Also used : Status(twitter4j.Status) Twitter(twitter4j.Twitter) LoginActivity(com.klinker.android.twitter.activities.setup.LoginActivity) Activity(android.app.Activity) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter)

Example 67 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class ChoosenListActivity method onRefreshStarted.

public void onRefreshStarted() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                paging.setPage(1);
                ResponseList<twitter4j.Status> lists = twitter.getUserListStatuses(listId, paging);
                statuses.clear();
                for (Status status : lists) {
                    statuses.add(status);
                }
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, statuses);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
            ((Activity) context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mPullToRefreshLayout.setRefreshing(false);
                }
            });
        }
    }).start();
}
Also used : Status(twitter4j.Status) Twitter(twitter4j.Twitter) LoginActivity(com.klinker.android.twitter.activities.setup.LoginActivity) Activity(android.app.Activity) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter)

Example 68 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class SavedSearchFragment method getCursorAdapter.

@Override
public void getCursorAdapter(boolean showSpinner) {
    if (showSpinner) {
        listView.setVisibility(View.GONE);
        spinner.setVisibility(View.VISIBLE);
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            final long topId;
            if (tweets.size() > 0) {
                topId = tweets.get(0).getId();
            } else {
                topId = 0;
            }
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                query = new Query(search);
                QueryResult result = twitter.search(query);
                tweets.clear();
                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }
                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }
                try {
                    ((Activity) context).runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            int top = 0;
                            for (int i = 0; i < tweets.size(); i++) {
                                if (tweets.get(i).getId() == topId) {
                                    top = i;
                                    break;
                                }
                            }
                            adapter = new TimelineArrayAdapter(context, tweets);
                            listView.setAdapter(adapter);
                            listView.setVisibility(View.VISIBLE);
                            listView.setSelection(top);
                            spinner.setVisibility(View.GONE);
                            refreshLayout.setRefreshing(false);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    ((Activity) context).runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            spinner.setVisibility(View.GONE);
                            refreshLayout.setRefreshing(false);
                        }
                    });
                } catch (Exception x) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}
Also used : QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Status(twitter4j.Status) Twitter(twitter4j.Twitter) Activity(android.app.Activity) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter)

Example 69 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class SavedSearchFragment method getMore.

public void getMore() {
    if (hasMore) {
        canRefresh = false;
        refreshLayout.setRefreshing(true);
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    QueryResult result = twitter.search(query);
                    for (twitter4j.Status status : result.getTweets()) {
                        tweets.add(status);
                    }
                    if (result.hasNext()) {
                        query = result.nextQuery();
                        hasMore = true;
                    } else {
                        hasMore = false;
                    }
                    try {
                        ((Activity) context).runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                                refreshLayout.setRefreshing(false);
                                canRefresh = true;
                            }
                        });
                    } catch (Exception e) {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        ((Activity) context).runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                refreshLayout.setRefreshing(false);
                                canRefresh = true;
                            }
                        });
                    } catch (Exception x) {
                    }
                }
            }
        }).start();
    }
}
Also used : QueryResult(twitter4j.QueryResult) Status(twitter4j.Status) Twitter(twitter4j.Twitter) Activity(android.app.Activity)

Example 70 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class TimelineRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    if (!MainActivity.canSwitch || CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning) {
        return;
    }
    if (MainActivity.canSwitch) {
        TimelineRefreshService.isRunning = true;
        sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
        Context context = getApplicationContext();
        int numberNew = 0;
        AppSettings settings = AppSettings.getInstance(context);
        // if they have mobile data on and don't want to sync over mobile data
        if (intent.getBooleanExtra("on_start_refresh", false)) {
        } else if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
            return;
        }
        Twitter twitter = Utils.getTwitter(context, settings);
        HomeDataSource dataSource = HomeDataSource.getInstance(context);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
        boolean foundStatus = false;
        Paging paging = new Paging(1, 200);
        long[] lastId = null;
        long id;
        try {
            lastId = dataSource.getLastIds(currentAccount);
            id = lastId[1];
        } catch (Exception e) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException i) {
            }
            TimelineRefreshService.isRunning = false;
            return;
        }
        if (id == 0) {
            id = 1;
        }
        try {
            paging.setSinceId(id);
        } catch (Exception e) {
            paging.setSinceId(1l);
        }
        for (int i = 0; i < settings.maxTweetsRefresh; i++) {
            try {
                if (!foundStatus) {
                    paging.setPage(i + 1);
                    List<Status> list = twitter.getHomeTimeline(paging);
                    statuses.addAll(list);
                    if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
                        Log.v("talon_inserting", "found status");
                        foundStatus = true;
                    } else {
                        Log.v("talon_inserting", "haven't found status");
                        foundStatus = false;
                    }
                }
            } catch (Exception e) {
                // the page doesn't exist
                foundStatus = true;
            } catch (OutOfMemoryError o) {
            // don't know why...
            }
        }
        Log.v("talon_pull", "got statuses, new = " + statuses.size());
        // hash set to check for duplicates I guess
        HashSet hs = new HashSet();
        hs.addAll(statuses);
        statuses.clear();
        statuses.addAll(hs);
        Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
        lastId = dataSource.getLastIds(currentAccount);
        Long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - sharedPrefs.getLong("last_timeline_insert", 0l) < 10000) {
            Log.v("talon_refresh", "don't insert the tweets on refresh");
            sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", 0));
            TimelineRefreshService.isRunning = false;
            context.getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
            return;
        } else {
            sharedPrefs.edit().putLong("last_timeline_insert", currentTime).commit();
        }
        int inserted = HomeDataSource.getInstance(context).insertTweets(statuses, currentAccount, lastId);
        if (inserted > 0 && statuses.size() > 0) {
            sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
        }
        if (!intent.getBooleanExtra("on_start_refresh", false)) {
            sharedPrefs.edit().putBoolean("refresh_me", true).commit();
            if (settings.notifications && (settings.timelineNot || settings.favoriteUserNotifications) && inserted > 0 && !intent.getBooleanExtra("from_launcher", false)) {
                NotificationUtils.refreshNotification(context, !settings.timelineNot);
            }
            if (settings.preCacheImages) {
                startService(new Intent(this, PreCacheService.class));
            }
        } else {
            Log.v("talon_refresh", "sending broadcast to fragment");
            sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", inserted));
        }
        sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
        getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
        TimelineRefreshService.isRunning = false;
    }
}
Also used : Context(android.content.Context) Status(twitter4j.Status) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Intent(android.content.Intent) HomeDataSource(com.klinker.android.twitter.data.sq_lite.HomeDataSource) HashSet(java.util.HashSet)

Aggregations

Twitter (twitter4j.Twitter)127 TwitterException (twitter4j.TwitterException)76 TwitterFactory (twitter4j.TwitterFactory)60 Status (twitter4j.Status)44 Activity (android.app.Activity)35 QueryResult (twitter4j.QueryResult)17 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)16 Intent (android.content.Intent)13 ArrayList (java.util.ArrayList)13 User (twitter4j.User)12 Query (twitter4j.Query)11 IDs (twitter4j.IDs)8 LinearLayout (android.widget.LinearLayout)7 IOException (java.io.IOException)7 Context (android.content.Context)6 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)6 LoginActivity (com.klinker.android.twitter.activities.setup.LoginActivity)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)5 GeoLocation (twitter4j.GeoLocation)5 Paging (twitter4j.Paging)5