List of usage examples for android.media AudioManager RINGER_MODE_SILENT
int RINGER_MODE_SILENT
To view the source code for android.media AudioManager RINGER_MODE_SILENT.
Click Source Link
From source file:ir.besteveryeverapp.telegram.NotificationsController.java
public void playOutChatSound() { if (!inChatSoundEnabled || MediaController.getInstance().isRecordingAudio() || NightModeUtil.blockSound()) { return;/*from ww w .j ava2 s . c o m*/ } try { if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) { return; } } catch (Exception e) { FileLog.e("tmessages", e); } notificationsQueue.postRunnable(new Runnable() { @Override public void run() { try { if (Math.abs(System.currentTimeMillis() - lastSoundOutPlay) <= 100) { return; } lastSoundOutPlay = System.currentTimeMillis(); if (soundPool == null) { soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0); soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f); } } }); } if (soundOut == 0 && !soundOutLoaded) { soundOutLoaded = true; soundOut = soundPool.load(ApplicationLoader.applicationContext, R.raw.sound_out, 1); } if (soundOut != 0) { soundPool.play(soundOut, 1.0f, 1.0f, 1, 0, 1.0f); } } catch (Exception e) { FileLog.e("tmessages", e); } } }); }
From source file:com.ferdi2005.secondgram.NotificationsController.java
private void showOrUpdateNotification(boolean notifyAboutLast) { if (!UserConfig.isClientActivated() || pushMessages.isEmpty()) { dismissNotification();// w ww. j ava2 s . c o m return; } try { ConnectionsManager.getInstance().resumeNetworkMaybe(); MessageObject lastMessageObject = pushMessages.get(0); SharedPreferences preferences = ApplicationLoader.applicationContext .getSharedPreferences("Notifications", Context.MODE_PRIVATE); int dismissDate = preferences.getInt("dismissDate", 0); if (lastMessageObject.messageOwner.date <= dismissDate) { dismissNotification(); return; } long dialog_id = lastMessageObject.getDialogId(); long override_dialog_id = dialog_id; if (lastMessageObject.messageOwner.mentioned) { override_dialog_id = lastMessageObject.messageOwner.from_id; } int mid = lastMessageObject.getId(); int chat_id = lastMessageObject.messageOwner.to_id.chat_id != 0 ? lastMessageObject.messageOwner.to_id.chat_id : lastMessageObject.messageOwner.to_id.channel_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 = 0xff0000ff; boolean inAppSounds; boolean inAppVibrate; boolean inAppPreview = false; boolean inAppPriority; int priority = 0; int priorityOverride; int vibrateOverride; 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; int notifyDelay; if (preferences.getBoolean("custom_" + dialog_id, false)) { notifyMaxCount = preferences.getInt("smart_max_count_" + dialog_id, 2); notifyDelay = preferences.getInt("smart_delay_" + dialog_id, 3 * 60); } else { notifyMaxCount = 2; notifyDelay = 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); boolean custom; if (custom = preferences.getBoolean("custom_" + dialog_id, false)) { vibrateOverride = preferences.getInt("vibrate_" + dialog_id, 0); priorityOverride = preferences.getInt("priority_" + dialog_id, 3); choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null); } else { vibrateOverride = 0; priorityOverride = 3; choosenSoundPath = null; } boolean vibrateOnlyIfSilent = false; 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", 0xff0000ff); } 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", 0xff0000ff); } if (custom) { 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) || needVibrate != 2 && vibrateOverride == 2 || vibrateOverride != 0 && vibrateOverride != 4) { 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(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 != null) { 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); mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE); if (chat == null && user != null && user.phone != null && user.phone.length() > 0) { mBuilder.addPerson("tel:+" + user.phone); } int silent = 2; String lastMessage = null; boolean hasNewMessages = false; if (pushMessages.size() == 1) { MessageObject messageObject = pushMessages.get(0); String message = lastMessage = getStringForMessage(messageObject, false); silent = messageObject.messageOwner.silent ? 1 : 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++) { MessageObject messageObject = pushMessages.get(i); String message = getStringForMessage(messageObject, false); if (message == null || messageObject.messageOwner.date <= dismissDate) { continue; } if (silent == 2) { lastMessage = message; silent = messageObject.messageOwner.silent ? 1 : 0; } 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); } Intent dismissIntent = new Intent(ApplicationLoader.applicationContext, NotificationDismissReceiver.class); dismissIntent.putExtra("messageDate", lastMessageObject.messageOwner.date); mBuilder.setDeleteIntent(PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 1, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT)); if (photoPath != null) { BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50"); if (img != null) { mBuilder.setLargeIcon(img.getBitmap()); } else { try { float scaleFactor = 160.0f / AndroidUtilities.dp(50); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor; Bitmap bitmap = BitmapFactory .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options); if (bitmap != null) { mBuilder.setLargeIcon(bitmap); } } catch (Throwable e) { //ignore } } } if (!notifyAboutLast || silent == 1) { 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); } } if (silent != 1 && !notifyDisabled) { if (ApplicationLoader.mainInterfacePaused || inAppPreview) { if (lastMessage.length() > 100) { lastMessage = lastMessage.substring(0, 100).replace('\n', ' ').trim() + "..."; } mBuilder.setTicker(lastMessage); } if (!MediaController.getInstance().isRecordingAudio()) { 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 == 2 || MediaController.getInstance().isRecordingAudio()) { 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 }); } if (Build.VERSION.SDK_INT < 24 && UserConfig.passcodeHash.length() == 0 && hasMessagesToReply()) { Intent replyIntent = new Intent(ApplicationLoader.applicationContext, PopupReplyReceiver.class); if (Build.VERSION.SDK_INT <= 19) { mBuilder.addAction(R.drawable.ic_ab_reply2, LocaleController.getString("Reply", R.string.Reply), PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)); } else { mBuilder.addAction(R.drawable.ic_ab_reply, LocaleController.getString("Reply", R.string.Reply), PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)); } } showExtraNotifications(mBuilder, notifyAboutLast); notificationManager.notify(1, mBuilder.build()); scheduleNotificationRepeat(); } catch (Exception e) { FileLog.e(e); } }
From source file:com.ferdi2005.secondgram.NotificationsController.java
public void playOutChatSound() { if (!inChatSoundEnabled || MediaController.getInstance().isRecordingAudio()) { return;//from w ww .ja v a2s .c o m } try { if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) { return; } } catch (Exception e) { FileLog.e(e); } notificationsQueue.postRunnable(new Runnable() { @Override public void run() { try { if (Math.abs(System.currentTimeMillis() - lastSoundOutPlay) <= 100) { return; } lastSoundOutPlay = System.currentTimeMillis(); if (soundPool == null) { soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0); soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { try { soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f); } catch (Exception e) { FileLog.e(e); } } } }); } if (soundOut == 0 && !soundOutLoaded) { soundOutLoaded = true; soundOut = soundPool.load(ApplicationLoader.applicationContext, R.raw.sound_out, 1); } if (soundOut != 0) { try { soundPool.play(soundOut, 1.0f, 1.0f, 1, 0, 1.0f); } catch (Exception e) { FileLog.e(e); } } } catch (Exception e) { FileLog.e(e); } } }); }