Search in sources :

Example 6 with DirectMessage

use of twitter4j.DirectMessage in project twitter4j by yusuke.

the class ShowDirectMessage method main.

/**
 * Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]
 *
 * @param args String[]
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]");
        System.exit(-1);
    }
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        DirectMessage message = twitter.showDirectMessage(Long.parseLong(args[0]));
        System.out.println("From: id:" + message.getId() + " - " + message.getText());
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get message: " + te.getMessage());
        System.exit(-1);
    }
}
Also used : DirectMessage(twitter4j.DirectMessage) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Example 7 with DirectMessage

use of twitter4j.DirectMessage 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 8 with DirectMessage

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

DirectMessage (twitter4j.DirectMessage)8 TwitterException (twitter4j.TwitterException)7 Twitter (twitter4j.Twitter)6 TwitterFactory (twitter4j.TwitterFactory)3 Intent (android.content.Intent)2 DMDataSource (com.klinker.android.twitter.data.sq_lite.DMDataSource)2 Paging (twitter4j.Paging)2 User (twitter4j.User)2 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 AppSettings (com.klinker.android.twitter.settings.AppSettings)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 ActionDoc (org.openhab.core.scriptengine.action.ActionDoc)1 DirectMessageList (twitter4j.DirectMessageList)1