Search in sources :

Example 16 with AppSettings

use of com.klinker.android.twitter.settings.AppSettings in project Talon-for-Twitter by klinker24.

the class NotificationUtils method newInteractions.

// type is either " retweeted your status", " favorited your status", or " followed you"
public static void newInteractions(User interactor, Context context, SharedPreferences sharedPrefs, String type) {
    String title = "";
    String text = "";
    String smallText = "";
    Bitmap icon = null;
    AppSettings settings = AppSettings.getInstance(context);
    Intent resultIntent = new Intent(context, RedirectToDrawer.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
    int newFollowers = sharedPrefs.getInt("new_followers", 0);
    int newRetweets = sharedPrefs.getInt("new_retweets", 0);
    int newFavorites = sharedPrefs.getInt("new_favorites", 0);
    int newQuotes = sharedPrefs.getInt("new_quotes", 0);
    // set title
    if (newFavorites + newRetweets + newFollowers > 1) {
        title = context.getResources().getString(R.string.new_interactions);
    } else {
        title = context.getResources().getString(R.string.new_interaction_upper);
    }
    // set text
    String currText = sharedPrefs.getString("old_interaction_text", "");
    if (!currText.equals("")) {
        currText += "<br>";
    }
    if (settings.displayScreenName) {
        text = currText + "<b>" + interactor.getScreenName() + "</b> " + type;
    } else {
        text = currText + "<b>" + interactor.getName() + "</b> " + type;
    }
    sharedPrefs.edit().putString("old_interaction_text", text).commit();
    // set icon
    int types = 0;
    if (newFavorites > 0) {
        types++;
    }
    if (newFollowers > 0) {
        types++;
    }
    if (newRetweets > 0) {
        types++;
    }
    if (newQuotes > 0) {
        types++;
    }
    if (types > 1) {
        icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_icon);
    } else {
        if (newFavorites > 0) {
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart_dark);
        } else if (newRetweets > 0) {
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_repeat_dark);
        } else {
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
        }
    }
    // set shorter text
    int total = newFavorites + newFollowers + newRetweets + newQuotes;
    if (total > 1) {
        smallText = total + " " + context.getResources().getString(R.string.new_interactions_lower);
    } else {
        smallText = text;
    }
    Intent markRead = new Intent(context, ReadInteractionsService.class);
    PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);
    Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, TALON_SERVICE_CHANNEL_ID).setContentTitle(title).setContentText(Html.fromHtml(settings.addonTheme ? smallText.replaceAll("FF8800", settings.accentColor) : smallText)).setSmallIcon(R.drawable.ic_stat_icon).setLargeIcon(icon).setContentIntent(resultPendingIntent).setTicker(title).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH).setAutoCancel(true);
    if (context.getResources().getBoolean(R.bool.expNotifications)) {
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? text.replaceAll("FF8800", settings.accentColor) : text)));
    }
    if (settings.vibrate) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    if (settings.sound) {
        try {
            mBuilder.setSound(Uri.parse(settings.ringtone));
        } catch (Exception e) {
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
    }
    if (settings.led)
        mBuilder.setLights(0xFFFFFF, 1000, 1000);
    if (settings.notifications) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(4, mBuilder.build());
        // if we want to wake the screen on a new message
        if (settings.wakeScreen) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
            wakeLock.acquire(5000);
        }
        // Pebble notification
        if (sharedPrefs.getBoolean("pebble_notification", false)) {
            sendAlertToPebble(context, title, text);
        }
        // Light Flow notification
        sendToLightFlow(context, title, text);
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PowerManager(android.os.PowerManager) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 17 with AppSettings

use of com.klinker.android.twitter.settings.AppSettings in project Talon-for-Twitter by klinker24.

the class NotificationUtils method sendTestNotification.

public static void sendTestNotification(Context context) {
    if (!TEST_NOTIFICATION) {
        return;
    }
    AppSettings settings = AppSettings.getInstance(context);
    SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    Intent markRead = new Intent(context, MarkReadService.class);
    PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);
    String shortText = "Test Talon";
    String longText = "Here is a test for Talon's notifications";
    Intent resultIntent = new Intent(context, RedirectToMentions.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
    NotificationCompat.Builder mBuilder;
    Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);
    mBuilder = new NotificationCompat.Builder(context, TALON_SERVICE_CHANNEL_ID).setContentTitle(shortText).setContentText(longText).setSmallIcon(R.drawable.ic_stat_icon).setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)).setContentIntent(resultPendingIntent).setAutoCancel(true).setTicker(shortText).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
    // Pebble notification
    if (sharedPrefs.getBoolean("pebble_notification", false)) {
        sendAlertToPebble(context, shortText, shortText);
    }
    // Light Flow notification
    sendToLightFlow(context, shortText, shortText);
    if (settings.vibrate) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    if (settings.sound) {
        try {
            mBuilder.setSound(Uri.parse(settings.ringtone));
        } catch (Exception e) {
            e.printStackTrace();
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
    }
    if (settings.led)
        mBuilder.setLights(0xFFFFFF, 1000, 1000);
    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    Intent reply = new Intent(context, NotificationCompose.class);
    MentionsDataSource data = MentionsDataSource.getInstance(context);
    PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0);
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("@" + "lukeklinker" + " ").build();
    // Create the notification action
    NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply), replyPending).addRemoteInput(remoteInput).build();
    NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder(R.drawable.ic_action_read_dark, context.getResources().getString(R.string.mark_read), readPending);
    mBuilder.addAction(replyAction);
    mBuilder.addAction(action.build());
    // Build the notification and issues it with notification manager.
    notificationManager.notify(1, mBuilder.build());
    // if we want to wake the screen on a new message
    if (settings.wakeScreen) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
        wakeLock.acquire(5000);
    }
}
Also used : RemoteInput(android.support.v4.app.RemoteInput) AppSettings(com.klinker.android.twitter.settings.AppSettings) SharedPreferences(android.content.SharedPreferences) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PowerManager(android.os.PowerManager) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 18 with AppSettings

use of com.klinker.android.twitter.settings.AppSettings 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 19 with AppSettings

use of com.klinker.android.twitter.settings.AppSettings in project Talon-for-Twitter by klinker24.

the class ActivityRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    AppSettings settings = AppSettings.getInstance(this);
    ActivityUtils utils = new ActivityUtils(this, false);
    if (Utils.getConnectionStatus(this) && !settings.syncMobile) {
        return;
    }
    boolean newActivity = utils.refreshActivity();
    if (settings.notifications && settings.activityNot && newActivity) {
        utils.postNotification();
    }
    if (settings.syncSecondMentions) {
        Intent second = new Intent(this, SecondActivityRefreshService.class);
        startService(second);
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) Intent(android.content.Intent) ActivityUtils(com.klinker.android.twitter.utils.ActivityUtils)

Example 20 with AppSettings

use of com.klinker.android.twitter.settings.AppSettings in project Talon-for-Twitter by klinker24.

the class CatchupPull method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || !MainActivity.canSwitch) {
        return;
    }
    CatchupPull.isRunning = true;
    Log.v("talon_pull", "catchup pull started");
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    final Context context = getApplicationContext();
    int unreadNow = sharedPrefs.getInt("pull_unread", 0);
    // stop it just in case
    context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
    AppSettings settings = AppSettings.getInstance(context);
    if (settings.liveStreaming) {
        Log.v("talon_pull", "into the try for catchup service");
        Twitter twitter = Utils.getTwitter(context, settings);
        HomeDataSource dataSource = HomeDataSource.getInstance(context);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        List<Status> statuses = new ArrayList<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) {
            context.startService(new Intent(context, TalonPullNotificationService.class));
            CatchupPull.isRunning = false;
            return;
        }
        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;
                e.printStackTrace();
            } catch (OutOfMemoryError o) {
                // don't know why...
                o.printStackTrace();
            }
        }
        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 = dataSource.insertTweets(statuses, currentAccount, lastId);
        if (inserted > 0 && statuses.size() > 0) {
            sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
            unreadNow += statuses.size();
        }
        if (settings.preCacheImages) {
            // delay it 15 seconds so that we can finish checking mentions first
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    startService(new Intent(context, PreCacheService.class));
                }
            }, 15000);
        }
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
    }
    try {
        Twitter twitter = Utils.getTwitter(context, settings);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        User user = twitter.verifyCredentials();
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }
        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);
        int numNew = dataSource.insertTweets(statuses, currentAccount);
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
        if (settings.notifications && settings.mentionsNot && numNew > 0) {
            NotificationUtils.refreshNotification(context);
        }
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
    sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
    context.startService(new Intent(context, TalonPullNotificationService.class));
    context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
    getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
    Log.v("talon_pull", "finished with the catchup service");
    CatchupPull.isRunning = false;
}
Also used : Context(android.content.Context) Status(twitter4j.Status) User(twitter4j.User) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Handler(android.os.Handler) Intent(android.content.Intent) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) TwitterException(twitter4j.TwitterException) HomeDataSource(com.klinker.android.twitter.data.sq_lite.HomeDataSource) TwitterException(twitter4j.TwitterException) HashSet(java.util.HashSet)

Aggregations

AppSettings (com.klinker.android.twitter.settings.AppSettings)33 Intent (android.content.Intent)18 PendingIntent (android.app.PendingIntent)10 Context (android.content.Context)10 NotificationCompat (android.support.v4.app.NotificationCompat)9 SharedPreferences (android.content.SharedPreferences)6 Cursor (android.database.Cursor)6 Bitmap (android.graphics.Bitmap)6 PowerManager (android.os.PowerManager)6 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)6 Paging (twitter4j.Paging)6 ArrayList (java.util.ArrayList)5 Twitter (twitter4j.Twitter)5 HomeDataSource (com.klinker.android.twitter.data.sq_lite.HomeDataSource)4 MentionsDataSource (com.klinker.android.twitter.data.sq_lite.MentionsDataSource)4 Status (twitter4j.Status)4 AlarmManager (android.app.AlarmManager)3 Handler (android.os.Handler)3 RemoteInput (android.support.v4.app.RemoteInput)3 DMDataSource (com.klinker.android.twitter.data.sq_lite.DMDataSource)3