Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-08-03 11:33:02 +02:00
parent 09088b097e
commit 77f8652177
8 changed files with 123 additions and 3 deletions

View file

@ -26,6 +26,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.USE_CREDENTIALS"

View file

@ -21,11 +21,17 @@
package com.nextcloud.talk.controllers;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
@ -136,6 +142,8 @@ public class CallNotificationController extends BaseController {
private MediaPlayer mediaPlayer;
private boolean leavingScreen = false;
private RenderScript renderScript;
private Vibrator vibrator;
private Handler handler;
public CallNotificationController(Bundle args) {
super(args);
@ -325,9 +333,38 @@ public class CallNotificationController extends BaseController {
if (ringtoneUri != null) {
mediaPlayer = MediaPlayer.create(getApplicationContext(), ringtoneUri);
mediaPlayer.setLooping(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.start();
}
}
if (DoNotDisturbUtils.shouldVibrate(appPreferences.getShouldVibrateSetting())) {
vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
long[] vibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
int[] amplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255};
VibrationEffect vibrationEffect;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (vibrator.hasAmplitudeControl()) {
vibrationEffect = VibrationEffect.createWaveform(vibratePattern, amplitudes, 0);
vibrator.vibrate(vibrationEffect);
} else {
vibrationEffect = VibrationEffect.createWaveform(vibratePattern, 0);
vibrator.vibrate(vibrationEffect);
}
} else {
vibrator.vibrate(vibratePattern, 0);
}
}
handler.postDelayed(() -> {
if (vibrator != null) {
vibrator.cancel();
}
}, 10000);
}
}
@ -429,7 +466,7 @@ public class CallNotificationController extends BaseController {
}
}
private void endMediaPlayer() {
private void endMediaAndVibratorNotifications() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
@ -438,14 +475,20 @@ public class CallNotificationController extends BaseController {
mediaPlayer.release();
mediaPlayer = null;
}
if (vibrator != null) {
vibrator.cancel();
}
}
@Override
public void onDestroy() {
AvatarStatusCodeHolder.getInstance().setStatusCode(0);
leavingScreen = true;
handler.removeCallbacksAndMessages(null);
handler = null;
dispose();
endMediaPlayer();
endMediaAndVibratorNotifications();
super.onDestroy();
}

View file

@ -54,17 +54,19 @@ import com.nextcloud.talk.jobs.AccountRemovalJob;
import com.nextcloud.talk.models.RingtoneSettings;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
import com.nextcloud.talk.utils.DoNotDisturbUtils;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.nextcloud.talk.utils.glide.GlideApp;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
import com.yarolegovich.mp.MaterialChoicePreference;
import com.yarolegovich.mp.MaterialEditTextPreference;
import com.yarolegovich.mp.MaterialPreferenceCategory;
import com.yarolegovich.mp.MaterialPreferenceScreen;
import com.yarolegovich.mp.MaterialStandardPreference;
import com.yarolegovich.mp.MaterialSwitchPreference;
import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
@ -146,6 +148,9 @@ public class SettingsController extends BaseController {
@BindView(R.id.settings_client_cert)
MaterialStandardPreference certificateSetup;
@BindView(R.id.settings_always_vibrate)
MaterialSwitchPreference shouldVibrateSwitchPreference;
@BindView(R.id.message_text)
TextView messageText;
@ -207,6 +212,10 @@ public class SettingsController extends BaseController {
licenceButton.setVisibility(View.GONE);
}
if (!DoNotDisturbUtils.hasVibrator()) {
shouldVibrateSwitchPreference.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(getResources().getString(R.string.nc_privacy_url))) {
privacyButton.addPreferenceClickListener(view12 -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().

View file

@ -27,10 +27,13 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Base64;
@ -349,9 +352,22 @@ public class NotificationJob extends Job {
if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() &&
DoNotDisturbUtils.shouldPlaySound()) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, soundUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(MediaPlayer::release);
}
if (DoNotDisturbUtils.shouldVibrate(appPreferences.getShouldVibrateSetting())) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(500);
}
}
}
}
}

View file

@ -24,6 +24,7 @@ import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.Vibrator;
import com.nextcloud.talk.application.NextcloudTalkApplication;
@ -49,6 +50,31 @@ public class DoNotDisturbUtils {
shouldPlaySound = false;
}
}
return shouldPlaySound;
}
public static boolean hasVibrator() {
Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
return (vibrator != null && vibrator.hasVibrator());
}
public static boolean shouldVibrate(boolean vibrate) {
if (hasVibrator()) {
Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
if (vibrate) {
return (audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT);
} else {
return (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
}
}
}
return false;
}
}

View file

@ -21,6 +21,7 @@
package com.nextcloud.talk.utils.preferences;
import net.orange_box.storebox.annotations.method.ClearMethod;
import net.orange_box.storebox.annotations.method.DefaultValue;
import net.orange_box.storebox.annotations.method.KeyByString;
import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod;
import net.orange_box.storebox.annotations.method.RemoveMethod;
@ -178,6 +179,17 @@ public interface AppPreferences {
@RemoveMethod
void removeMessagesNotificationChannelUpgradeToV3();
@KeyByString("notifications_vibrate")
@DefaultValue(1)
boolean getShouldVibrateSetting();
@KeyByString("notifications_vibrate")
void setVibrateSetting(boolean value);
@KeyByString("notifications_vibrate")
@RemoveMethod
void removeVibrateSetting();
@ClearMethod
void clear();
}

View file

@ -132,6 +132,15 @@
apc:mp_key="@string/nc_settings_message_ringtone_key"
apc:mp_title="@string/nc_settings_other_notifications_ringtone"/>
<com.yarolegovich.mp.MaterialSwitchPreference
android:id="@+id/settings_always_vibrate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apc:mp_key="@string/nc_settings_vibrate_key"
apc:mp_title="@string/nc_settings_vibrate"
apc:mp_summary="@string/nc_settings_vibrate_desc"
apc:mp_default_value="true"/>
</com.yarolegovich.mp.MaterialPreferenceCategory>

View file

@ -67,6 +67,10 @@
<string name="nc_settings_message_ringtone_key" translatable="false">message_ringtone</string>
<string name="nc_settings_default_ringtone" translatable="false">Librem by feandesign</string>
<string name="nc_settings_no_ringtone">No sound</string>
<string name="nc_settings_vibrate">Vibrate</string>
<string name="nc_settings_vibrate_desc">When enabled, phone will vibrate unless it\'s silenced</string>
<string name="nc_settings_vibrate_key" translatable="false">notifications_vibrate</string>
<string name="nc_no_proxy">No proxy</string>
<string name="nc_username">Username</string>