Search in sources :

Example 6 with Paging

use of twitter4j.Paging in project twicalico by moko256.

the class BaseTweetListFragment method onUpdateList.

@Override
protected void onUpdateList() {
    subscription.add(getResponseSingle(new Paging(list.get(list.size() >= 2 ? 1 : 0)).count(GlobalApplication.statusLimit)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
        if (result.size() > 0) {
            List<Long> ids = Observable.from(result).map(Status::getId).toList().toSingle().toBlocking().value();
            if (ids.get(ids.size() - 1).equals(list.get(0))) {
                ids.remove(ids.size() - 1);
            } else {
                ids.add(-1L);
            }
            if (ids.size() > 0) {
                list.addAll(0, ids);
                statusIdsDatabase.insertIds(0, ids);
                adapter.notifyItemRangeInserted(0, ids.size());
                TypedValue value = new TypedValue();
                Toast t = Toast.makeText(getContext(), R.string.new_post, Toast.LENGTH_SHORT);
                t.setGravity(Gravity.TOP | Gravity.CENTER, 0, getContext().getTheme().resolveAttribute(R.attr.actionBarSize, value, true) ? TypedValue.complexToDimensionPixelOffset(value.data, getResources().getDisplayMetrics()) : 0);
                t.show();
            }
        }
        setRefreshing(false);
    }, e -> {
        e.printStackTrace();
        Snackbar.make(getSnackBarParentContainer(), TwitterStringUtils.convertErrorToText(e), Snackbar.LENGTH_INDEFINITE).setAction(R.string.retry, v -> {
            setRefreshing(true);
            onUpdateList();
        }).show();
        setRefreshing(false);
    }));
}
Also used : Context(android.content.Context) Rect(android.graphics.Rect) Arrays(java.util.Arrays) Bundle(android.os.Bundle) WindowManager(android.view.WindowManager) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) NonNull(android.support.annotation.NonNull) TwitterStringUtils(com.github.moko256.twicalico.text.TwitterStringUtils) ArrayList(java.util.ArrayList) Observable(rx.Observable) CachedIdListSQLiteOpenHelper(com.github.moko256.twicalico.database.CachedIdListSQLiteOpenHelper) Single(rx.Single) Toast(android.widget.Toast) Schedulers(rx.schedulers.Schedulers) Display(android.view.Display) View(android.view.View) TwitterException(twitter4j.TwitterException) ResponseList(twitter4j.ResponseList) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) LayoutInflater(android.view.LayoutInflater) Point(android.graphics.Point) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) Gravity(android.view.Gravity) List(java.util.List) TypedValue(android.util.TypedValue) Status(twitter4j.Status) Paging(twitter4j.Paging) Snackbar(android.support.design.widget.Snackbar) ArrayUtils(com.github.moko256.twicalico.array.ArrayUtils) Toast(android.widget.Toast) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) ResponseList(twitter4j.ResponseList) List(java.util.List) TypedValue(android.util.TypedValue)

Example 7 with Paging

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

the class DirectMessageRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);
    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;
    }
    boolean update = false;
    int numberNew = 0;
    try {
        Twitter twitter = Utils.getTwitter(context, settings);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        User user = twitter.verifyCredentials();
        long lastId = sharedPrefs.getLong("last_direct_message_id_" + currentAccount, 0);
        Paging paging;
        if (lastId != 0) {
            paging = new Paging(1).sinceId(lastId);
        } else {
            paging = new Paging(1, 500);
        }
        List<DirectMessage> dm = twitter.getDirectMessages(paging);
        List<DirectMessage> sent = twitter.getSentDirectMessages(paging);
        if (dm.size() != 0) {
            sharedPrefs.edit().putLong("last_direct_message_id_" + currentAccount, dm.get(0).getId()).commit();
            numberNew = dm.size();
        } else {
            numberNew = 0;
        }
        DMDataSource dataSource = DMDataSource.getInstance(context);
        int inserted = 0;
        for (DirectMessage directMessage : dm) {
            try {
                dataSource.createDirectMessage(directMessage, currentAccount);
            } catch (Exception e) {
                dataSource = DMDataSource.getInstance(context);
                dataSource.createDirectMessage(directMessage, currentAccount);
            }
            inserted++;
        }
        for (DirectMessage directMessage : sent) {
            try {
                dataSource.createDirectMessage(directMessage, currentAccount);
            } catch (Exception e) {
                dataSource = DMDataSource.getInstance(context);
                dataSource.createDirectMessage(directMessage, currentAccount);
            }
        }
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_dm", true).commit();
        if (settings.notifications && settings.dmsNot && inserted > 0) {
            int currentUnread = sharedPrefs.getInt("dm_unread_" + currentAccount, 0);
            sharedPrefs.edit().putInt("dm_unread_" + currentAccount, numberNew + currentUnread).commit();
            NotificationUtils.refreshNotification(context);
        }
        if (settings.syncSecondMentions) {
            startService(new Intent(context, SecondDMRefreshService.class));
        }
        sendBroadcast(new Intent("com.klinker.android.twitter.NEW_DIRECT_MESSAGE"));
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}
Also used : Context(android.content.Context) DirectMessage(twitter4j.DirectMessage) User(twitter4j.User) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) Twitter(twitter4j.Twitter) Intent(android.content.Intent) DMDataSource(com.klinker.android.twitter.data.sq_lite.DMDataSource) TwitterException(twitter4j.TwitterException) TwitterException(twitter4j.TwitterException)

Example 8 with Paging

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

the class SecondMentionsRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);
    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;
    }
    boolean update = false;
    int numberNew = 0;
    try {
        Twitter twitter = Utils.getSecondTwitter(context);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        if (currentAccount == 1) {
            currentAccount = 2;
        } else {
            currentAccount = 1;
        }
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
        long lastId = dataSource.getLastIds(currentAccount)[0];
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId > 0) {
            paging.sinceId(lastId);
        }
        List<Status> statuses = twitter.getMentionsTimeline(paging);
        numberNew = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);
        if (numberNew > 0) {
            sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
            if (settings.notifications && settings.mentionsNot) {
                NotificationUtils.notifySecondMentions(context, currentAccount);
            }
            sendBroadcast(new Intent("com.klinker.android.twitter.REFRESH_SECOND_MENTIONS"));
        }
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}
Also used : Context(android.content.Context) Status(twitter4j.Status) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) Twitter(twitter4j.Twitter) Intent(android.content.Intent) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) TwitterException(twitter4j.TwitterException)

Example 9 with Paging

use of twitter4j.Paging 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)

Example 10 with Paging

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

the class WidgetRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    // it is refreshing elsewhere, so don't start
    if (WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || CatchupPull.isRunning || !MainActivity.canSwitch) {
        return;
    }
    WidgetRefreshService.isRunning = true;
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, TALON_SERVICE_CHANNEL_ID).setSmallIcon(R.drawable.ic_stat_icon).setTicker(getResources().getString(R.string.refreshing) + "...").setContentTitle(getResources().getString(R.string.app_name)).setContentText(getResources().getString(R.string.refreshing_widget) + "...").setProgress(100, 100, true).setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.drawer_sync_dark));
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(6, mBuilder.build());
    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);
    // if they have mobile data on and don't want to sync over mobile data
    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;
    long id;
    try {
        lastId = dataSource.getLastIds(currentAccount);
        id = lastId[0];
    } catch (Exception e) {
        WidgetRefreshService.isRunning = false;
        return;
    }
    paging.setSinceId(id);
    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 remove 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);
    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 (settings.preCacheImages) {
        startService(new Intent(this, PreCacheService.class));
    }
    context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
    getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
    sharedPrefs.edit().putBoolean("refresh_me", true).commit();
    mNotificationManager.cancel(6);
    WidgetRefreshService.isRunning = false;
}
Also used : Context(android.content.Context) Status(twitter4j.Status) NotificationManager(android.app.NotificationManager) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Intent(android.content.Intent) NotificationCompat(android.support.v4.app.NotificationCompat) HomeDataSource(com.klinker.android.twitter.data.sq_lite.HomeDataSource) HashSet(java.util.HashSet)

Aggregations

Paging (twitter4j.Paging)13 ArrayList (java.util.ArrayList)10 Status (twitter4j.Status)10 Context (android.content.Context)9 TwitterException (twitter4j.TwitterException)9 Intent (android.content.Intent)8 AppSettings (com.klinker.android.twitter.settings.AppSettings)6 Twitter (twitter4j.Twitter)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Point (android.graphics.Point)3 Rect (android.graphics.Rect)3 Bundle (android.os.Bundle)3 NonNull (android.support.annotation.NonNull)3 Snackbar (android.support.design.widget.Snackbar)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 RecyclerView (android.support.v7.widget.RecyclerView)3 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)3 TypedValue (android.util.TypedValue)3 Display (android.view.Display)3