Allow selecting notification sound before Android Oreo

This commit is contained in:
M66B 2018-12-09 10:47:20 +01:00
parent cda73acb06
commit 4e16d46280
5 changed files with 77 additions and 9 deletions

View File

@ -40,10 +40,12 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
static final int REQUEST_PERMISSION = 1;
static final int REQUEST_CHOOSE_ACCOUNT = 2;
static final int REQUEST_EXPORT = 3;
static final int REQUEST_IMPORT = 4;
static final int REQUEST_SOUND = 3;
static final int REQUEST_ERROR = 5;
static final int REQUEST_EXPORT = 4;
static final int REQUEST_IMPORT = 5;
static final int REQUEST_ERROR = 6;
static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT";
static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY";

View File

@ -20,8 +20,12 @@ package eu.faircode.email;
*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
@ -29,6 +33,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Spinner;
@ -40,6 +45,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import static android.app.Activity.RESULT_OK;
public class FragmentOptions extends FragmentEx implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swEnabled;
private SwitchCompat swMetered;
@ -50,6 +57,7 @@ public class FragmentOptions extends FragmentEx implements SharedPreferences.OnS
private SwitchCompat swIdenticons;
private SwitchCompat swPreview;
private SwitchCompat swLight;
private Button btnSound;
private SwitchCompat swBrowse;
private SwitchCompat swSwipe;
private SwitchCompat swActionbar;
@ -78,6 +86,7 @@ public class FragmentOptions extends FragmentEx implements SharedPreferences.OnS
swIdenticons = view.findViewById(R.id.swIdenticons);
swPreview = view.findViewById(R.id.swPreview);
swLight = view.findViewById(R.id.swLight);
btnSound = view.findViewById(R.id.btnSound);
swBrowse = view.findViewById(R.id.swBrowse);
swSwipe = view.findViewById(R.id.swSwipe);
swActionbar = view.findViewById(R.id.swActionbar);
@ -200,6 +209,19 @@ public class FragmentOptions extends FragmentEx implements SharedPreferences.OnS
}
});
btnSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sound = prefs.getString("sound", null);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.title_advanced_sound));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, sound == null ? null : Uri.parse(sound));
startActivityForResult(intent, ActivitySetup.REQUEST_SOUND);
}
});
swBrowse.setChecked(prefs.getBoolean("browse", true));
swBrowse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
@ -293,13 +315,32 @@ public class FragmentOptions extends FragmentEx implements SharedPreferences.OnS
}
});
swLight.setVisibility(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
swLight.setVisibility(BuildConfig.DEBUG || Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
btnSound.setVisibility(BuildConfig.DEBUG || Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(Helper.TAG, "Result class=" + this.getClass().getSimpleName() +
" request=" + requestCode + " result=" + resultCode + " data=" + data);
if (requestCode == ActivitySetup.REQUEST_SOUND)
if (resultCode == RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Log.i(Helper.TAG, "Selected ringtone=" + uri);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (uri == null)
prefs.edit().remove("sound").apply();
else
prefs.edit().putString("sound", uri.toString()).apply();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("enabled".equals(key))

View File

@ -495,10 +495,22 @@ public class ServiceSynchronize extends LifecycleService {
else
builder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O &&
prefs.getBoolean("light", false)) {
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
builder.setLights(0xff00ff00, 1000, 1000);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
int defaults = Notification.DEFAULT_VIBRATE;
if (prefs.getBoolean("light", false)) {
defaults |= Notification.FLAG_SHOW_LIGHTS;
builder.setLights(0xff00ff00, 1000, 1000);
} else
defaults += Notification.DEFAULT_LIGHTS;
String sound = prefs.getString("sound", null);
if (sound == null)
defaults |= Notification.DEFAULT_SOUND;
else
builder.setSound(Uri.parse(sound));
builder.setDefaults(defaults);
}
if (pro) {

View File

@ -92,6 +92,18 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swPreview" />
<Button
android:id="@+id/btnSound"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="@string/title_advanced_sound"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swLight" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swBrowse"
android:layout_width="match_parent"
@ -99,7 +111,7 @@
android:layout_marginTop="12dp"
android:text="@string/title_advanced_browse"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swLight" />
app:layout_constraintTop_toBottomOf="@id/btnSound" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swSwipe"

View File

@ -105,6 +105,7 @@
<string name="title_advanced_identicons">Show identicons</string>
<string name="title_advanced_preview">Show message preview</string>
<string name="title_advanced_light">Use notification light</string>
<string name="title_advanced_sound">Select notification sound</string>
<string name="title_advanced_browse">Browse messages on the server</string>
<string name="title_advanced_swipe">Swipe actions</string>
<string name="title_advanced_actionbar">Conversation action bar</string>