package eu.faircode.email; /* This file is part of FairEmail. FairEmail is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. FairEmail is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with FairEmail. If not, see . Copyright 2018-2019 by Marcel Bokhorst (M66B) */ import android.content.Intent; import android.content.SharedPreferences; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; import androidx.constraintlayout.widget.Group; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import static android.app.Activity.RESULT_OK; public class FragmentOptionsNotifications extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener { private SwitchCompat swNotifyGroup; private SwitchCompat swNotifyPreview; private CheckBox cbNotifyActionTrash; private CheckBox cbNotifyActionArchive; private CheckBox cbNotifyActionReply; private CheckBox cbNotifyActionFlag; private CheckBox cbNotifyActionSeen; private SwitchCompat swLight; private Button btnSound; private Group grpNotification; private final static String[] RESET_OPTIONS = new String[]{ "notify_group", "notify_preview", "notify_trash", "notify_archive", "notify_reply", "notify_flag", "notify_seen", "light", "sound" }; @Override @Nullable public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { setSubtitle(R.string.title_advanced); setHasOptionsMenu(true); View view = inflater.inflate(R.layout.fragment_options_notifications, container, false); // Get controls swNotifyGroup = view.findViewById(R.id.swNotifyGroup); swNotifyPreview = view.findViewById(R.id.swNotifyPreview); cbNotifyActionTrash = view.findViewById(R.id.cbNotifyActionTrash); cbNotifyActionArchive = view.findViewById(R.id.cbNotifyActionArchive); cbNotifyActionReply = view.findViewById(R.id.cbNotifyActionReply); cbNotifyActionFlag = view.findViewById(R.id.cbNotifyActionFlag); cbNotifyActionSeen = view.findViewById(R.id.cbNotifyActionSeen); swLight = view.findViewById(R.id.swLight); btnSound = view.findViewById(R.id.btnSound); grpNotification = view.findViewById(R.id.grpNotification); setOptions(); // Wire controls final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); swNotifyGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { prefs.edit().putBoolean("notify_group", checked).apply(); } }); swNotifyPreview.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { prefs.edit().putBoolean("notify_preview", checked).apply(); } }); cbNotifyActionTrash.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { setAction(buttonView, "notify_trash", checked); } }); cbNotifyActionArchive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { setAction(buttonView, "notify_archive", checked); } }); cbNotifyActionReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { setAction(buttonView, "notify_reply", checked); } }); cbNotifyActionFlag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { setAction(buttonView, "notify_flag", checked); } }); cbNotifyActionSeen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { setAction(buttonView, "notify_seen", checked); } }); swLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { prefs.edit().putBoolean("light", checked).apply(); } }); 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_SHOW_SILENT, false); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, sound == null ? null : Uri.parse(sound)); startActivityForResult(Helper.getChooser(getContext(), intent), ActivitySetup.REQUEST_SOUND); } }); PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this); return view; } @Override public void onDestroyView() { PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this); super.onDestroyView(); } @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { setOptions(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_options, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_default: onMenuDefault(); return true; default: return super.onOptionsItemSelected(item); } } private void onMenuDefault() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); for (String option : RESET_OPTIONS) editor.remove(option); editor.apply(); } private void setOptions() { boolean pro = Helper.isPro(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); swNotifyGroup.setChecked(prefs.getBoolean("notify_group", true)); swNotifyGroup.setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? View.VISIBLE : View.GONE); swNotifyPreview.setChecked(prefs.getBoolean("notify_preview", true)); cbNotifyActionTrash.setChecked(prefs.getBoolean("notify_trash", true)); cbNotifyActionArchive.setChecked(prefs.getBoolean("notify_archive", true)); cbNotifyActionReply.setChecked(prefs.getBoolean("notify_reply", false) && pro); cbNotifyActionFlag.setChecked(prefs.getBoolean("notify_flag", false) && pro); cbNotifyActionSeen.setChecked(prefs.getBoolean("notify_seen", true)); swLight.setChecked(prefs.getBoolean("light", false)); grpNotification.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE); } private void setAction(CompoundButton cb, String key, boolean checked) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); if (Helper.isPro(getContext())) prefs.edit().putBoolean(key, checked).apply(); else { cb.setChecked(!checked); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO)); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i("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("Selected ringtone=" + uri); if (uri != null && "file".equals(uri.getScheme())) uri = null; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); if (uri == null) prefs.edit().remove("sound").apply(); else prefs.edit().putString("sound", uri.toString()).apply(); } } }