Search in sources :

Example 1 with DMDataSource

use of com.klinker.android.twitter.data.sq_lite.DMDataSource in project Talon-for-Twitter by klinker24.

the class SecondDMRefreshService 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;
        }
        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.notifySecondDMs(context, currentAccount);
        }
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}
Also used : Context(android.content.Context) AppSettings(com.klinker.android.twitter.settings.AppSettings) DMDataSource(com.klinker.android.twitter.data.sq_lite.DMDataSource)

Example 2 with DMDataSource

use of com.klinker.android.twitter.data.sq_lite.DMDataSource in project Talon-for-Twitter by klinker24.

the class DMFragment method onRefreshStarted.

@Override
public void onRefreshStarted() {
    new AsyncTask<Void, Void, Void>() {

        private boolean update;

        private int numberNew;

        @Override
        protected void onPreExecute() {
            DrawerActivity.canSwitch = false;
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                twitter = Utils.getTwitter(context, DrawerActivity.settings);
                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();
                    update = true;
                    numberNew = dm.size();
                } else {
                    update = false;
                    numberNew = 0;
                }
                DMDataSource dataSource = DMDataSource.getInstance(context);
                for (DirectMessage directMessage : dm) {
                    try {
                        dataSource.createDirectMessage(directMessage, currentAccount);
                    } catch (IllegalStateException e) {
                        dataSource = DMDataSource.getInstance(context);
                        dataSource.createDirectMessage(directMessage, currentAccount);
                    }
                }
                for (DirectMessage directMessage : sent) {
                    try {
                        dataSource.createDirectMessage(directMessage, currentAccount);
                    } catch (Exception e) {
                        dataSource = DMDataSource.getInstance(context);
                        dataSource.createDirectMessage(directMessage, currentAccount);
                    }
                }
            } catch (TwitterException e) {
                // Error in updating status
                Log.d("Twitter Update Error", e.getMessage());
            }
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            long now = new Date().getTime();
            long alarm = now + DrawerActivity.settings.dmRefresh;
            Log.v("alarm_date", "direct message " + new Date(alarm).toString());
            PendingIntent pendingIntent = PendingIntent.getService(context, DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
            if (DrawerActivity.settings.dmRefresh != 0)
                am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, DrawerActivity.settings.dmRefresh, pendingIntent);
            else
                am.cancel(pendingIntent);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            try {
                if (update) {
                    getCursorAdapter(false);
                    CharSequence text = numberNew == 1 ? numberNew + " " + getResources().getString(R.string.new_direct_message) : numberNew + " " + getResources().getString(R.string.new_direct_messages);
                    showToastBar(text + "", jumpToTop, 400, true, toTopListener);
                    int size = toDP(5) + mActionBarSize + (DrawerActivity.translucent ? DrawerActivity.statusBarHeight : 0);
                    listView.setSelectionFromTop(numberNew + (MainActivity.isPopup || landscape || MainActivity.settings.jumpingWorkaround ? 1 : 2), size);
                } else {
                    getCursorAdapter(false);
                    CharSequence text = getResources().getString(R.string.no_new_direct_messages);
                    showToastBar(text + "", allRead, 400, true, toTopListener);
                }
                refreshLayout.setRefreshing(false);
            } catch (IllegalStateException e) {
            // fragment not attached to activity
            }
            DrawerActivity.canSwitch = true;
        }
    }.execute();
}
Also used : DirectMessage(twitter4j.DirectMessage) User(twitter4j.User) Paging(twitter4j.Paging) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) DMDataSource(com.klinker.android.twitter.data.sq_lite.DMDataSource) TwitterException(twitter4j.TwitterException) Date(java.util.Date) AlarmManager(android.app.AlarmManager) ArrayList(java.util.ArrayList) List(java.util.List) PendingIntent(android.app.PendingIntent) TwitterException(twitter4j.TwitterException)

Example 3 with DMDataSource

use of com.klinker.android.twitter.data.sq_lite.DMDataSource in project Talon-for-Twitter by klinker24.

the class IOUtils method trimDatabase.

public static boolean trimDatabase(Context context, int account) {
    try {
        AppSettings settings = AppSettings.getInstance(context);
        SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
        InteractionsDataSource interactions = InteractionsDataSource.getInstance(context);
        Cursor inters = interactions.getCursor(account);
        if (inters.getCount() > 50) {
            if (inters.moveToPosition(inters.getCount() - 50)) {
                do {
                    interactions.deleteInteraction(inters.getLong(inters.getColumnIndex(InteractionsSQLiteHelper.COLUMN_ID)));
                } while (inters.moveToPrevious());
            }
        }
        inters.close();
        HomeDataSource home = HomeDataSource.getInstance(context);
        home.deleteDups(settings.currentAccount);
        Cursor timeline = home.getTrimmingCursor(account);
        Log.v("trimming", "timeline size: " + timeline.getCount());
        Log.v("trimming", "timeline settings size: " + settings.timelineSize);
        if (timeline.getCount() > settings.timelineSize) {
            if (timeline.moveToPosition(timeline.getCount() - settings.timelineSize)) {
                Log.v("trimming", "in the trim section");
                do {
                    home.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
                } while (timeline.moveToPrevious());
            }
        }
        timeline.close();
        // trimming the lists
        ListDataSource lists = ListDataSource.getInstance(context);
        int account1List1 = sharedPrefs.getInt("account_" + account + "_list_1", 0);
        int account1List2 = sharedPrefs.getInt("account_" + account + "_list_2", 0);
        lists.deleteDups(account1List1);
        lists.deleteDups(account1List2);
        Cursor list1 = lists.getTrimmingCursor(account1List1);
        Log.v("trimming", "lists size: " + list1.getCount());
        Log.v("trimming", "lists settings size: " + 400);
        if (list1.getCount() > 400) {
            if (list1.moveToPosition(list1.getCount() - 400)) {
                Log.v("trimming", "in the trim section");
                do {
                    lists.deleteTweet(list1.getLong(list1.getColumnIndex(ListSQLiteHelper.COLUMN_TWEET_ID)));
                } while (list1.moveToPrevious());
            }
        }
        list1.close();
        Cursor list2 = lists.getTrimmingCursor(account1List2);
        Log.v("trimming", "lists size: " + list2.getCount());
        Log.v("trimming", "lists settings size: " + 400);
        if (list2.getCount() > 400) {
            if (list2.moveToPosition(list2.getCount() - 400)) {
                Log.v("trimming", "in the trim section");
                do {
                    lists.deleteTweet(list2.getLong(list2.getColumnIndex(ListSQLiteHelper.COLUMN_TWEET_ID)));
                } while (list2.moveToPrevious());
            }
        }
        list2.close();
        MentionsDataSource mentions = MentionsDataSource.getInstance(context);
        mentions.deleteDups(settings.currentAccount);
        timeline = mentions.getTrimmingCursor(account);
        Log.v("trimming", "mentions size: " + timeline.getCount());
        Log.v("trimming", "mentions settings size: " + settings.mentionsSize);
        if (timeline.getCount() > settings.mentionsSize) {
            if (timeline.moveToPosition(timeline.getCount() - settings.mentionsSize)) {
                do {
                    mentions.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
                } while (timeline.moveToPrevious());
            }
        }
        timeline.close();
        DMDataSource dm = DMDataSource.getInstance(context);
        dm.deleteDups(settings.currentAccount);
        timeline = dm.getCursor(account);
        Log.v("trimming", "dm size: " + timeline.getCount());
        Log.v("trimming", "dm settings size: " + settings.dmSize);
        if (timeline.getCount() > settings.dmSize) {
            if (timeline.moveToPosition(timeline.getCount() - settings.dmSize)) {
                do {
                    dm.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
                } while (timeline.moveToPrevious());
            }
        }
        timeline.close();
        HashtagDataSource hashtag = HashtagDataSource.getInstance(context);
        timeline = hashtag.getCursor("");
        Log.v("trimming", "hashtag size: " + timeline.getCount());
        if (timeline.getCount() > 300) {
            if (timeline.moveToPosition(timeline.getCount() - 300)) {
                do {
                    hashtag.deleteTag(timeline.getString(timeline.getColumnIndex(HashtagSQLiteHelper.COLUMN_TAG)));
                } while (timeline.moveToPrevious());
            }
        }
        timeline.close();
        ActivityDataSource activity = ActivityDataSource.getInstance(context);
        Cursor actCurs = activity.getCursor(account);
        Log.v("trimming", "activity size: " + actCurs.getCount());
        Log.v("trimming", "activity settings size: " + 200);
        if (actCurs.getCount() > 200) {
            int toDelete = actCurs.getCount() - 200;
            if (actCurs.moveToFirst()) {
                do {
                    activity.deleteItem(actCurs.getLong(actCurs.getColumnIndex(ActivitySQLiteHelper.COLUMN_ID)));
                    toDelete--;
                } while (timeline.moveToNext() && toDelete > 0);
            }
        }
        actCurs.close();
        FavoriteTweetsDataSource favtweets = FavoriteTweetsDataSource.getInstance(context);
        favtweets.deleteDups(settings.currentAccount);
        timeline = favtweets.getCursor(account);
        Log.v("trimming", "favtweets size: " + timeline.getCount());
        Log.v("trimming", "favtweets settings size: " + 200);
        if (timeline.getCount() > 200) {
            if (timeline.moveToPosition(timeline.getCount() - 200)) {
                do {
                    favtweets.deleteTweet(timeline.getLong(timeline.getColumnIndex(FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID)));
                } while (timeline.moveToPrevious());
            }
        }
        timeline.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : HashtagDataSource(com.klinker.android.twitter.data.sq_lite.HashtagDataSource) AppSettings(com.klinker.android.twitter.settings.AppSettings) SharedPreferences(android.content.SharedPreferences) Cursor(android.database.Cursor) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) ListDataSource(com.klinker.android.twitter.data.sq_lite.ListDataSource) DMDataSource(com.klinker.android.twitter.data.sq_lite.DMDataSource) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FavoriteTweetsDataSource(com.klinker.android.twitter.data.sq_lite.FavoriteTweetsDataSource) InteractionsDataSource(com.klinker.android.twitter.data.sq_lite.InteractionsDataSource) ActivityDataSource(com.klinker.android.twitter.data.sq_lite.ActivityDataSource) HomeDataSource(com.klinker.android.twitter.data.sq_lite.HomeDataSource)

Example 4 with DMDataSource

use of com.klinker.android.twitter.data.sq_lite.DMDataSource 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)

Aggregations

DMDataSource (com.klinker.android.twitter.data.sq_lite.DMDataSource)4 AppSettings (com.klinker.android.twitter.settings.AppSettings)3 Context (android.content.Context)2 Intent (android.content.Intent)2 DirectMessage (twitter4j.DirectMessage)2 Paging (twitter4j.Paging)2 TwitterException (twitter4j.TwitterException)2 User (twitter4j.User)2 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 ActivityDataSource (com.klinker.android.twitter.data.sq_lite.ActivityDataSource)1 FavoriteTweetsDataSource (com.klinker.android.twitter.data.sq_lite.FavoriteTweetsDataSource)1 HashtagDataSource (com.klinker.android.twitter.data.sq_lite.HashtagDataSource)1 HomeDataSource (com.klinker.android.twitter.data.sq_lite.HomeDataSource)1 InteractionsDataSource (com.klinker.android.twitter.data.sq_lite.InteractionsDataSource)1 ListDataSource (com.klinker.android.twitter.data.sq_lite.ListDataSource)1 MentionsDataSource (com.klinker.android.twitter.data.sq_lite.MentionsDataSource)1 FileNotFoundException (java.io.FileNotFoundException)1