Example usage for android.media AudioManager STREAM_NOTIFICATION

List of usage examples for android.media AudioManager STREAM_NOTIFICATION

Introduction

In this page you can find the example usage for android.media AudioManager STREAM_NOTIFICATION.

Prototype

int STREAM_NOTIFICATION

To view the source code for android.media AudioManager STREAM_NOTIFICATION.

Click Source Link

Document

Used to identify the volume of audio streams for notifications

Usage

From source file:org.mareatlantica.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();//from w ww.j a va2  s .co  m
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().users.get(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().chats.get(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        boolean needVibrate = false;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        int vibrate_override = 0;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notify_override = preferences.getInt("notify2_" + dialog_id, 0);
        if (!notifyAboutLast || notify_override == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notify_override == 0) {
            notifyDisabled = true;
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0);

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getBoolean("EnableVibrateGroup", true);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getBoolean("EnableVibrateAll", true);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (!needVibrate && vibrate_override == 1) {
                needVibrate = true;
            } else if (needVibrate && vibrate_override == 2) {
                needVibrate = false;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = false;
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (pushDialogs.size() == 1) {
                if (chat != null) {
                    if (chat.photo != null && chat.photo.photo_small != null
                            && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                        photoPath = chat.photo.photo_small;
                    }
                } else {
                    if (user.photo != null && user.photo.photo_small != null
                            && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                        photoPath = user.photo.photo_small;
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = Utilities.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = String.format("%s %s",
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromContacts", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true)
                        .setContentIntent(contentIntent);

        String lastMessage = null;
        if (pushMessages.size() == 1) {
            String message = lastMessage = getStringForMessage(pushMessages.get(0));
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i));
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessage = message;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            Bitmap img = FileLoader.getInstance().getImageFromMemory(photoPath, null, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img);
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else {
                mBuilder.setVibrate(new long[] { 0, 0 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessage);
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:org.connectbot.ConsoleFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    getActivity().setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

    final View view = findCurrentView(R.id.console_flip);
    final boolean activeTerminal = (view instanceof TerminalView);
    final boolean sessionOpen;
    final boolean disconnected;
    final boolean canForwardPorts;

    if (activeTerminal) {
        final TerminalBridge bridge = ((TerminalView) view).bridge;
        sessionOpen = bridge.isSessionOpen();
        disconnected = bridge.isDisconnected();
        canForwardPorts = bridge.canFowardPorts();
    } else {/*from  www.  j  a va 2s  .  c om*/
        sessionOpen = false;
        disconnected = false;
        canForwardPorts = false;
    }

    mMenuDisconnect.setEnabled(activeTerminal);
    if (sessionOpen || !disconnected)
        mMenuDisconnect.setTitle(R.string.list_host_disconnect);
    else
        mMenuDisconnect.setTitle(R.string.console_menu_close);

    mMenuCopy.setEnabled(activeTerminal);
    mMenuPaste.setEnabled(clipboard.hasText() && sessionOpen);
    mMenuPortForwards.setEnabled(sessionOpen && canForwardPorts);
    mMenuUrlScan.setEnabled(activeTerminal);
    mMenuResize.setEnabled(sessionOpen);
}

From source file:org.telepatch.android.NotificationsController.java

/**
 *  Qui lui crea le notifiche/*from w w w .  j  av a2  s . c o  m*/
 */
private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        int mid = lastMessageObject.messageOwner.id;
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        int vibrate_override = 0;

        int notify_override = preferences.getInt("notify2_" + dialog_id, 0);
        if (!notifyAboutLast || notify_override == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notify_override == 0) {
            notifyDisabled = true;
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0);

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (needVibrate == 2 && (vibrate_override == 1 || vibrate_override == 3 || vibrate_override == 5)
                    || needVibrate != 2 && vibrate_override == 2 || vibrate_override != 0) {
                needVibrate = vibrate_override;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (pushDialogs.size() == 1) {
                if (chat != null) {
                    if (chat.photo != null && chat.photo.photo_small != null
                            && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                        photoPath = chat.photo.photo_small;
                    }
                } else {
                    if (user.photo != null && user.photo.photo_small != null
                            && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                        photoPath = user.photo.photo_small;
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = ContactsController.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromContacts", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages")

                        .setGroupSummary(true);

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    //lastMessage = getStringForMessage(pushMessages.get(i), true);
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50", null);
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }
        showWearNotifications(notifyAboutLast);
        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:me.cpwc.nibblegram.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();/*from  ww  w. j  a  va  2  s.  c  o m*/
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        int mid = lastMessageObject.messageOwner.id;
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        boolean inAppPriority = false;
        int priority = 0;
        int priority_override = 0;
        int vibrate_override = 0;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notify_override = preferences.getInt("notify2_" + dialog_id, 0);
        if (!notifyAboutLast || notify_override == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notify_override == 0) {
            notifyDisabled = true;
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0);
            priority_override = preferences.getInt("priority_" + dialog_id, 3);

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (priority_override != 3) {
                priority = priority_override;
            }

            if (needVibrate == 2 && (vibrate_override == 1 || vibrate_override == 3 || vibrate_override == 5)
                    || needVibrate != 2 && vibrate_override == 2 || vibrate_override != 0) {
                needVibrate = vibrate_override;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (pushDialogs.size() == 1) {
                if (chat != null) {
                    if (chat.photo != null && chat.photo.photo_small != null
                            && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                        photoPath = chat.photo.photo_small;
                    }
                } else {
                    if (user.photo != null && user.photo.photo_small != null
                            && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                        photoPath = user.photo.photo_small;
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = ContactsController.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromContacts", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true);

        if (priority == 0) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else if (priority == 1) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        } else if (priority == 2) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }
        /*Bundle bundle = new Bundle();
        bundle.putString(NotificationCompat.EXTRA_PEOPLE, );
        mBuilder.setExtras()*/

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    //lastMessage = getStringForMessage(pushMessages.get(i), true);
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50", null);
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }
        showWearNotifications(notifyAboutLast);
        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:org.woltage.irssiconnectbot.ConsoleActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

    final View view = findCurrentView(R.id.console_flip);
    boolean activeTerminal = (view instanceof TerminalView);
    boolean sessionOpen = false;
    boolean disconnected = false;
    boolean canForwardPorts = false;

    if (activeTerminal) {
        TerminalBridge bridge = ((TerminalView) view).bridge;
        sessionOpen = bridge.isSessionOpen();
        disconnected = bridge.isDisconnected();
        canForwardPorts = bridge.canFowardPorts();
    }//  ww w  .  j  a  v  a 2  s  .c  o  m

    disconnect.setEnabled(activeTerminal);
    if (sessionOpen || !disconnected)
        disconnect.setTitle(R.string.list_host_disconnect);
    else
        disconnect.setTitle(R.string.console_menu_close);
    copy.setEnabled(activeTerminal);
    paste.setEnabled(clipboard.hasText() && sessionOpen);
    portForward.setEnabled(sessionOpen && canForwardPorts);
    urlscan.setEnabled(activeTerminal);
    resize.setEnabled(sessionOpen);

    return true;
}

From source file:org.connectbot.ConsoleActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

    final TerminalView view = adapter.getCurrentTerminalView();
    boolean activeTerminal = view != null;
    boolean sessionOpen = false;
    boolean disconnected = false;
    boolean canForwardPorts = false;

    if (activeTerminal) {
        TerminalBridge bridge = view.bridge;
        sessionOpen = bridge.isSessionOpen();
        disconnected = bridge.isDisconnected();
        canForwardPorts = bridge.canFowardPorts();
    }//from ww  w .  j  a va2  s .  co m

    disconnect.setEnabled(activeTerminal);
    if (sessionOpen || !disconnected)
        disconnect.setTitle(R.string.list_host_disconnect);
    else
        disconnect.setTitle(R.string.console_menu_close);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        copy.setEnabled(activeTerminal);
    }
    paste.setEnabled(activeTerminal);
    portForward.setEnabled(sessionOpen && canForwardPorts);
    urlscan.setEnabled(activeTerminal);
    resize.setEnabled(sessionOpen);

    return true;
}

From source file:com.negaheno.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();//  www.j ava2s  . c  o m
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        int mid = lastMessageObject.messageOwner.id;
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        boolean inAppPriority = false;
        int priority = 0;
        int priority_override = 0;
        int vibrate_override = 0;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notify_override = preferences.getInt("notify2_" + dialog_id, 0);
        if (notify_override == 3) {
            int mute_until = preferences.getInt("notifyuntil_" + dialog_id, 0);
            if (mute_until >= ConnectionsManager.getInstance().getCurrentTime()) {
                notify_override = 2;
            }
        }
        if (!notifyAboutLast || notify_override == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notify_override == 0) {
            notifyDisabled = true;
        }

        boolean use_smart_notify = preferences.getBoolean("smart_notify_" + dialog_id, false);
        Long smart_notify_timeframe = preferences.getLong("smart_notify_timeframe_" + dialog_id, 1);
        int smart_notify_max_count = preferences.getInt("smart_notify_max_count_" + dialog_id, 1);
        if (chat_id != 0 && use_smart_notify) {
            if (chat.sound_timestamps == null)
                chat.sound_timestamps = new LinkedList<>();
            boolean shouldAdd = true;
            Date firstNotification = chat.sound_timestamps.peek();
            Date currentDate = new Date();
            if (firstNotification != null) {
                if (currentDate.getTime() - firstNotification.getTime() < smart_notify_timeframe * 1000
                        && chat.sound_timestamps.size() >= smart_notify_max_count) {
                    shouldAdd = false;
                }
            }
            if (!shouldAdd) {
                notifyDisabled = true;
            } else {
                if (chat.sound_timestamps.size() >= smart_notify_max_count)
                    chat.sound_timestamps.poll();
                chat.sound_timestamps.add(currentDate);
            }
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0);
            priority_override = preferences.getInt("priority_" + dialog_id, 3);

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (priority_override != 3) {
                priority = priority_override;
            }

            if (needVibrate == 2 && (vibrate_override == 1 || vibrate_override == 3 || vibrate_override == 5)
                    || needVibrate != 2 && vibrate_override == 2 || vibrate_override != 0) {
                needVibrate = vibrate_override;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
                photoPath = null;
            } else {
                if (pushDialogs.size() == 1) {
                    if (chat != null) {
                        if (chat.photo != null && chat.photo.photo_small != null
                                && chat.photo.photo_small.volume_id != 0
                                && chat.photo.photo_small.local_id != 0) {
                            photoPath = chat.photo.photo_small;
                        }
                    } else {
                        if (user.photo != null && user.photo.photo_small != null
                                && user.photo.photo_small.volume_id != 0
                                && user.photo.photo_small.local_id != 0) {
                            photoPath = user.photo.photo_small;
                        }
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1 || AndroidUtilities.needShowPasscode(false)
                || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = ContactsController.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromChats", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true)
                        //.setColor(0xff2ca5e0);
                        .setColor(AndroidUtilities.getIntColor("themeColor"));

        if (priority == 0) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else if (priority == 1) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        } else if (priority == 2) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }
        /*Bundle bundle = new Bundle();
        bundle.putString(NotificationCompat.EXTRA_PEOPLE, );
        mBuilder.setExtras()*/

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    //lastMessage = getStringForMessage(pushMessages.get(i), true);
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }
        showWearNotifications(notifyAboutLast);
        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:org.hermes.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();//from  ww w  .  ja v  a  2  s .  c o m
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        long override_dialog_id = dialog_id;
        if ((lastMessageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_MENTION) != 0) {
            override_dialog_id = lastMessageObject.messageOwner.from_id;
        }
        int mid = lastMessageObject.getId();
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        boolean inAppPriority = false;
        int priority = 0;
        int priority_override = 0;
        int vibrate_override = 0;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notify_override = preferences.getInt("notify2_" + override_dialog_id, 0);
        if (notify_override == 3) {
            int mute_until = preferences.getInt("notifyuntil_" + override_dialog_id, 0);
            if (mute_until >= ConnectionsManager.getInstance().getCurrentTime()) {
                notify_override = 2;
            }
        }
        if (!notifyAboutLast || notify_override == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notify_override == 0) {
            notifyDisabled = true;
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            vibrate_override = preferences.getInt("vibrate_" + dialog_id, 0);
            priority_override = preferences.getInt("priority_" + dialog_id, 3);
            boolean vibrateOnlyIfSilent = false;

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (priority_override != 3) {
                priority = priority_override;
            }

            if (needVibrate == 4) {
                vibrateOnlyIfSilent = true;
                needVibrate = 0;
            }
            if (needVibrate == 2 && (vibrate_override == 1 || vibrate_override == 3 || vibrate_override == 5)
                    || needVibrate != 2 && vibrate_override == 2 || vibrate_override != 0) {
                needVibrate = vibrate_override;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
            if (vibrateOnlyIfSilent && needVibrate != 2) {
                try {
                    int mode = audioManager.getRingerMode();
                    if (mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
                        needVibrate = 2;
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
                photoPath = null;
            } else {
                if (pushDialogs.size() == 1) {
                    if (chat != null) {
                        if (chat.photo != null && chat.photo.photo_small != null
                                && chat.photo.photo_small.volume_id != 0
                                && chat.photo.photo_small.local_id != 0) {
                            photoPath = chat.photo.photo_small;
                        }
                    } else {
                        if (user.photo != null && user.photo.photo_small != null
                                && user.photo.photo_small.volume_id != 0
                                && user.photo.photo_small.local_id != 0) {
                            photoPath = user.photo.photo_small;
                        }
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1 || AndroidUtilities.needShowPasscode(false)
                || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = ContactsController.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromChats", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true)
                        .setColor(0xff2ca5e0);

        if (priority == 0) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else if (priority == 1) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        } else if (priority == 2) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }
        /*Bundle bundle = new Bundle();
        bundle.putString(NotificationCompat.EXTRA_PEOPLE, );
        mBuilder.setExtras()*/

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    //lastMessage = getStringForMessage(pushMessages.get(i), true);
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }
        showWearNotifications(notifyAboutLast);
        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:org.telegramsecureplus.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();/*ww w  . j a  va2s . c o  m*/
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        long override_dialog_id = dialog_id;
        if ((lastMessageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_MENTION) != 0) {
            override_dialog_id = lastMessageObject.messageOwner.from_id;
        }
        int mid = lastMessageObject.getId();
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds;
        boolean inAppVibrate;
        boolean inAppPreview = false;
        boolean inAppPriority;
        int priority = 0;
        int priorityOverride;
        int vibrateOverride;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notifyOverride = getNotifyOverride(preferences, override_dialog_id);
        if (!notifyAboutLast || notifyOverride == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notifyOverride == 0) {
            notifyDisabled = true;
        }

        if (!notifyDisabled && dialog_id == override_dialog_id && chat != null) {
            int notifyMaxCount = preferences.getInt("smart_max_count_" + dialog_id, 2);
            int notifyDelay = preferences.getInt("smart_delay_" + dialog_id, 3 * 60);
            if (notifyMaxCount != 0) {
                Point dialogInfo = smartNotificationsDialogs.get(dialog_id);
                if (dialogInfo == null) {
                    dialogInfo = new Point(1, (int) (System.currentTimeMillis() / 1000));
                    smartNotificationsDialogs.put(dialog_id, dialogInfo);
                } else {
                    int lastTime = dialogInfo.y;
                    if (lastTime + notifyDelay < System.currentTimeMillis() / 1000) {
                        dialogInfo.set(1, (int) (System.currentTimeMillis() / 1000));
                    } else {
                        int count = dialogInfo.x;
                        if (count < notifyMaxCount) {
                            dialogInfo.set(count + 1, (int) (System.currentTimeMillis() / 1000));
                        } else {
                            notifyDisabled = true;
                        }
                    }
                }
            }
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            vibrateOverride = preferences.getInt("vibrate_" + dialog_id, 0);
            priorityOverride = preferences.getInt("priority_" + dialog_id, 3);
            boolean vibrateOnlyIfSilent = false;

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (priorityOverride != 3) {
                priority = priorityOverride;
            }

            if (needVibrate == 4) {
                vibrateOnlyIfSilent = true;
                needVibrate = 0;
            }
            if (needVibrate == 2 && (vibrateOverride == 1 || vibrateOverride == 3 || vibrateOverride == 5)
                    || needVibrate != 2 && vibrateOverride == 2 || vibrateOverride != 0) {
                needVibrate = vibrateOverride;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
            if (vibrateOnlyIfSilent && needVibrate != 2) {
                try {
                    int mode = audioManager.getRingerMode();
                    if (mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
                        needVibrate = 2;
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
                photoPath = null;
            } else {
                if (pushDialogs.size() == 1) {
                    if (chat != null) {
                        if (chat.photo != null && chat.photo.photo_small != null
                                && chat.photo.photo_small.volume_id != 0
                                && chat.photo.photo_small.local_id != 0) {
                            photoPath = chat.photo.photo_small;
                        }
                    } else {
                        if (user.photo != null && user.photo.photo_small != null
                                && user.photo.photo_small.volume_id != 0
                                && user.photo.photo_small.local_id != 0) {
                            photoPath = user.photo.photo_small;
                        }
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1 || AndroidUtilities.needShowPasscode(false)
                || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
        }

        String detailText;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromChats", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true)
                        .setColor(0xff2ca5e0);

        if (!notifyAboutLast) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
        } else {
            if (priority == 0) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
            } else if (priority == 1) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
            } else if (priority == 2) {
                mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
            }
        }

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        showExtraNotifications(mBuilder, notifyAboutLast);
        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }

        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

From source file:org.vshgap.android.NotificationsController.java

private void showOrUpdateNotification(boolean notifyAboutLast) {
    if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) {
        dismissNotification();/* w ww  . ja  v a  2 s  . co  m*/
        return;
    }
    try {
        ConnectionsManager.getInstance().resumeNetworkMaybe();

        MessageObject lastMessageObject = pushMessages.get(0);

        long dialog_id = lastMessageObject.getDialogId();
        long override_dialog_id = dialog_id;
        if ((lastMessageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_MENTION) != 0) {
            override_dialog_id = lastMessageObject.messageOwner.from_id;
        }
        int mid = lastMessageObject.getId();
        int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
        int user_id = lastMessageObject.messageOwner.to_id.user_id;
        if (user_id == 0) {
            user_id = lastMessageObject.messageOwner.from_id;
        } else if (user_id == UserConfig.getClientUserId()) {
            user_id = lastMessageObject.messageOwner.from_id;
        }

        TLRPC.User user = MessagesController.getInstance().getUser(user_id);
        TLRPC.Chat chat = null;
        if (chat_id != 0) {
            chat = MessagesController.getInstance().getChat(chat_id);
        }
        TLRPC.FileLocation photoPath = null;

        boolean notifyDisabled = false;
        int needVibrate = 0;
        String choosenSoundPath = null;
        int ledColor = 0xff00ff00;
        boolean inAppSounds = false;
        boolean inAppVibrate = false;
        boolean inAppPreview = false;
        boolean inAppPriority = false;
        int priority = 0;
        int priorityOverride = 0;
        int vibrateOverride = 0;

        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notifyOverride = getNotifyOverride(preferences, override_dialog_id);
        if (!notifyAboutLast || notifyOverride == 2
                || (!preferences.getBoolean("EnableAll", true)
                        || chat_id != 0 && !preferences.getBoolean("EnableGroup", true))
                        && notifyOverride == 0) {
            notifyDisabled = true;
        }

        if (!notifyDisabled && dialog_id == override_dialog_id && chat != null) {
            int notifyMaxCount = preferences.getInt("smart_max_count_" + dialog_id, 2);
            int notifyDelay = preferences.getInt("smart_delay_" + dialog_id, 3 * 60);
            if (notifyMaxCount != 0) {
                Point dialogInfo = smartNotificationsDialogs.get(dialog_id);
                if (dialogInfo == null) {
                    dialogInfo = new Point(1, (int) (System.currentTimeMillis() / 1000));
                    smartNotificationsDialogs.put(dialog_id, dialogInfo);
                } else {
                    int lastTime = dialogInfo.y;
                    if (lastTime + notifyDelay < System.currentTimeMillis() / 1000) {
                        dialogInfo.set(1, (int) (System.currentTimeMillis() / 1000));
                    } else {
                        int count = dialogInfo.x;
                        if (count < notifyMaxCount) {
                            dialogInfo.set(count + 1, (int) (System.currentTimeMillis() / 1000));
                        } else {
                            notifyDisabled = true;
                        }
                    }
                }
            }
        }

        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
        if (!notifyDisabled) {
            inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
            inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
            inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
            inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
            vibrateOverride = preferences.getInt("vibrate_" + dialog_id, 0);
            priorityOverride = preferences.getInt("priority_" + dialog_id, 3);
            boolean vibrateOnlyIfSilent = false;

            choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
            if (chat_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_group", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("GroupLed", 0xff00ff00);
            } else if (user_id != 0) {
                if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
                    choosenSoundPath = null;
                } else if (choosenSoundPath == null) {
                    choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
                }
                needVibrate = preferences.getInt("vibrate_messages", 0);
                priority = preferences.getInt("priority_group", 1);
                ledColor = preferences.getInt("MessagesLed", 0xff00ff00);
            }
            if (preferences.contains("color_" + dialog_id)) {
                ledColor = preferences.getInt("color_" + dialog_id, 0);
            }

            if (priorityOverride != 3) {
                priority = priorityOverride;
            }

            if (needVibrate == 4) {
                vibrateOnlyIfSilent = true;
                needVibrate = 0;
            }
            if (needVibrate == 2 && (vibrateOverride == 1 || vibrateOverride == 3 || vibrateOverride == 5)
                    || needVibrate != 2 && vibrateOverride == 2 || vibrateOverride != 0) {
                needVibrate = vibrateOverride;
            }
            if (!ApplicationLoader.mainInterfacePaused) {
                if (!inAppSounds) {
                    choosenSoundPath = null;
                }
                if (!inAppVibrate) {
                    needVibrate = 2;
                }
                if (!inAppPriority) {
                    priority = 0;
                } else if (priority == 2) {
                    priority = 1;
                }
            }
            if (vibrateOnlyIfSilent && needVibrate != 2) {
                try {
                    int mode = audioManager.getRingerMode();
                    if (mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
                        needVibrate = 2;
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if ((int) dialog_id != 0) {
            if (pushDialogs.size() == 1) {
                if (chat_id != 0) {
                    intent.putExtra("chatId", chat_id);
                } else if (user_id != 0) {
                    intent.putExtra("userId", user_id);
                }
            }
            if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
                photoPath = null;
            } else {
                if (pushDialogs.size() == 1) {
                    if (chat != null) {
                        if (chat.photo != null && chat.photo.photo_small != null
                                && chat.photo.photo_small.volume_id != 0
                                && chat.photo.photo_small.local_id != 0) {
                            photoPath = chat.photo.photo_small;
                        }
                    } else {
                        if (user.photo != null && user.photo.photo_small != null
                                && user.photo.photo_small.volume_id != 0
                                && user.photo.photo_small.local_id != 0) {
                            photoPath = user.photo.photo_small;
                        }
                    }
                }
            }
        } else {
            if (pushDialogs.size() == 1) {
                intent.putExtra("encId", (int) (dialog_id >> 32));
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String name = null;
        boolean replace = true;
        if ((int) dialog_id == 0 || pushDialogs.size() > 1 || AndroidUtilities.needShowPasscode(false)
                || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
            replace = false;
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = ContactsController.formatName(user.first_name, user.last_name);
            }
        }

        String detailText = null;
        if (pushDialogs.size() == 1) {
            detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
        } else {
            detailText = LocaleController.formatString("NotificationMessagesPeopleDisplayOrder",
                    R.string.NotificationMessagesPeopleDisplayOrder,
                    LocaleController.formatPluralString("NewMessages", total_unread_count),
                    LocaleController.formatPluralString("FromChats", pushDialogs.size()));
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setAutoCancel(true).setNumber(total_unread_count)
                        .setContentIntent(contentIntent).setGroup("messages").setGroupSummary(true)
                        //.setColor(0xff2ca5e0);
                        .setColor(AndroidUtilities.getIntColor("themeColor")); //Plus

        if (priority == 0) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else if (priority == 1) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        } else if (priority == 2) {
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            mBuilder.addPerson("tel:+" + user.phone);
        }

        String lastMessage = null;
        String lastMessageFull = null;
        if (pushMessages.size() == 1) {
            String message = lastMessageFull = getStringForMessage(pushMessages.get(0), false);
            //lastMessage = getStringForMessage(pushMessages.get(0), true);
            lastMessage = lastMessageFull;
            if (message == null) {
                return;
            }
            if (replace) {
                if (chat != null) {
                    message = message.replace(" @ " + name, "");
                } else {
                    message = message.replace(name + ": ", "").replace(name + " ", "");
                }
            }
            mBuilder.setContentText(message);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        } else {
            mBuilder.setContentText(detailText);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(name);
            int count = Math.min(10, pushMessages.size());
            for (int i = 0; i < count; i++) {
                String message = getStringForMessage(pushMessages.get(i), false);
                if (message == null) {
                    continue;
                }
                if (i == 0) {
                    lastMessageFull = message;
                    lastMessage = lastMessageFull;
                }
                if (pushDialogs.size() == 1) {
                    if (replace) {
                        if (chat != null) {
                            message = message.replace(" @ " + name, "");
                        } else {
                            message = message.replace(name + ": ", "").replace(name + " ", "");
                        }
                    }
                }
                inboxStyle.addLine(message);
            }
            inboxStyle.setSummaryText(detailText);
            mBuilder.setStyle(inboxStyle);
        }

        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                mBuilder.setLargeIcon(img.getBitmap());
            }
        }

        if (!notifyDisabled) {
            if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
                if (lastMessage.length() > 100) {
                    lastMessage = lastMessage.substring(0, 100).replace("\n", " ").trim() + "...";
                }
                mBuilder.setTicker(lastMessage);
            }
            if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
                if (choosenSoundPath.equals(defaultPath)) {
                    /*MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
                    mediaPlayer.prepare();
                    mediaPlayer.start();*/
                    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                            AudioManager.STREAM_NOTIFICATION);
                } else {
                    mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
                }
            }
            if (ledColor != 0) {
                mBuilder.setLights(ledColor, 1000, 1000);
            }
            if (needVibrate == 2) {
                mBuilder.setVibrate(new long[] { 0, 0 });
            } else if (needVibrate == 1) {
                mBuilder.setVibrate(new long[] { 0, 100, 0, 100 });
            } else if (needVibrate == 0 || needVibrate == 4) {
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            } else if (needVibrate == 3) {
                mBuilder.setVibrate(new long[] { 0, 1000 });
            }
        } else {
            mBuilder.setVibrate(new long[] { 0, 0 });
        }

        notificationManager.notify(1, mBuilder.build());
        if (preferences.getBoolean("EnablePebbleNotifications", false)) {
            sendAlertToPebble(lastMessageFull);
        }
        showWearNotifications(notifyAboutLast);
        scheduleNotificationRepeat();
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}