From 008eb5ba4a3d578726dbeaf89f56630863955cf8 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Sat, 31 Oct 2020 11:58:33 +0100 Subject: [PATCH] Convert notification actions to a custom preference (#4652) --- .../settings/NotificationSettingsFragment.kt | 10 ++ .../NotificationActionsPreference.java} | 102 +++++++----------- .../main/res/layout/settings_notification.xml | 78 ++------------ .../main/res/xml/notification_settings.xml | 18 ++++ 4 files changed, 72 insertions(+), 136 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.kt rename app/src/main/java/org/schabi/newpipe/settings/{NotificationSettingsFragment.java => custom/NotificationActionsPreference.java} (74%) create mode 100644 app/src/main/res/xml/notification_settings.xml diff --git a/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.kt b/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.kt new file mode 100644 index 000000000..860968626 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.kt @@ -0,0 +1,10 @@ +package org.schabi.newpipe.settings + +import android.os.Bundle +import org.schabi.newpipe.R + +class NotificationSettingsFragment : BasePreferenceFragment() { + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + addPreferencesFromResource(R.xml.notification_settings) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java similarity index 74% rename from app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java rename to app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java index 13e500e12..1fe405552 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java @@ -1,10 +1,10 @@ -package org.schabi.newpipe.settings; +package org.schabi.newpipe.settings.custom; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.preference.PreferenceManager; +import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,18 +13,16 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; -import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; - import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.content.res.AppCompatResources; import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.widget.TextViewCompat; -import androidx.fragment.app.Fragment; - +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; +import java.util.List; import org.schabi.newpipe.R; import org.schabi.newpipe.player.MainPlayer; import org.schabi.newpipe.player.NotificationConstants; @@ -32,56 +30,35 @@ import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.views.FocusOverlayView; -import java.util.List; +public class NotificationActionsPreference extends Preference { + + public NotificationActionsPreference(final Context context, final AttributeSet attrs) { + super(context, attrs); + setLayoutResource(R.layout.settings_notification); + } -public class NotificationSettingsFragment extends Fragment { - private Switch scaleSwitch; private NotificationSlot[] notificationSlots; - private SharedPreferences pref; private List compactSlots; - private String scaleKey; //////////////////////////////////////////////////////////////////////////// // Lifecycle //////////////////////////////////////////////////////////////////////////// @Override - public void onCreate(@Nullable final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - pref = PreferenceManager.getDefaultSharedPreferences(requireContext()); - scaleKey = getString(R.string.scale_to_square_image_in_notifications_key); + public void onBindViewHolder(final PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + holder.itemView.setClickable(false); + setupActions(holder.itemView); } @Override - public View onCreateView(@NonNull final LayoutInflater inflater, - final ViewGroup container, - @Nullable final Bundle savedInstanceState) { - return inflater.inflate(R.layout.settings_notification, container, false); - } - - @Override - public void onViewCreated(@NonNull final View rootView, - @Nullable final Bundle savedInstanceState) { - super.onViewCreated(rootView, savedInstanceState); - - setupScaleSwitch(rootView); - setupActions(rootView); - } - - @Override - public void onResume() { - super.onResume(); - ThemeHelper.setTitleToAppCompatActivity(getActivity(), - getString(R.string.settings_category_notification_title)); - } - - @Override - public void onPause() { - super.onPause(); + public void onDetached() { + super.onDetached(); saveChanges(); - requireContext().sendBroadcast(new Intent(MainPlayer.ACTION_RECREATE_NOTIFICATION)); + getContext().sendBroadcast(new Intent(MainPlayer.ACTION_RECREATE_NOTIFICATION)); } @@ -89,17 +66,10 @@ public class NotificationSettingsFragment extends Fragment { // Setup //////////////////////////////////////////////////////////////////////////// - private void setupScaleSwitch(@NonNull final View view) { - scaleSwitch = view.findViewById(R.id.notificationScaleSwitch); - scaleSwitch.setChecked(pref.getBoolean(scaleKey, false)); - - view.findViewById(R.id.notificationScaleSwitchClickableArea) - .setOnClickListener(v -> scaleSwitch.toggle()); - } - private void setupActions(@NonNull final View view) { compactSlots = - NotificationConstants.getCompactSlotsFromPreferences(requireContext(), pref, 5); + NotificationConstants.getCompactSlotsFromPreferences( + getContext(), getSharedPreferences(), 5); notificationSlots = new NotificationSlot[5]; for (int i = 0; i < 5; i++) { notificationSlots[i] = new NotificationSlot(i, view); @@ -112,16 +82,15 @@ public class NotificationSettingsFragment extends Fragment { //////////////////////////////////////////////////////////////////////////// private void saveChanges() { - final SharedPreferences.Editor editor = pref.edit(); - editor.putBoolean(scaleKey, scaleSwitch.isChecked()); + final SharedPreferences.Editor editor = getSharedPreferences().edit(); for (int i = 0; i < 3; i++) { - editor.putInt(getString(NotificationConstants.SLOT_COMPACT_PREF_KEYS[i]), + editor.putInt(getContext().getString(NotificationConstants.SLOT_COMPACT_PREF_KEYS[i]), (i < compactSlots.size() ? compactSlots.get(i) : -1)); } for (int i = 0; i < 5; i++) { - editor.putInt(getString(NotificationConstants.SLOT_PREF_KEYS[i]), + editor.putInt(getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), notificationSlots[i].selectedAction); } @@ -183,7 +152,7 @@ public class NotificationSettingsFragment extends Fragment { } else if (compactSlots.size() < 3) { compactSlots.add(i); } else { - Toast.makeText(requireContext(), + Toast.makeText(getContext(), R.string.notification_actions_at_most_three, Toast.LENGTH_SHORT).show(); return; @@ -196,7 +165,8 @@ public class NotificationSettingsFragment extends Fragment { void setupSelectedAction(final View view) { icon = view.findViewById(R.id.notificationActionIcon); summary = view.findViewById(R.id.notificationActionSummary); - selectedAction = pref.getInt(getString(NotificationConstants.SLOT_PREF_KEYS[i]), + selectedAction = getSharedPreferences().getInt( + getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), NotificationConstants.SLOT_DEFAULTS[i]); updateInfo(); } @@ -205,20 +175,20 @@ public class NotificationSettingsFragment extends Fragment { if (NotificationConstants.ACTION_ICONS[selectedAction] == 0) { icon.setImageDrawable(null); } else { - icon.setImageDrawable(AppCompatResources.getDrawable(requireContext(), + icon.setImageDrawable(AppCompatResources.getDrawable(getContext(), NotificationConstants.ACTION_ICONS[selectedAction])); } - summary.setText(NotificationConstants.getActionName(requireContext(), selectedAction)); + summary.setText(NotificationConstants.getActionName(getContext(), selectedAction)); } void openActionChooserDialog() { - final LayoutInflater inflater = LayoutInflater.from(requireContext()); + final LayoutInflater inflater = LayoutInflater.from(getContext()); final LinearLayout rootLayout = (LinearLayout) inflater.inflate( R.layout.single_choice_dialog_view, null, false); final RadioGroup radioGroup = rootLayout.findViewById(android.R.id.list); - final AlertDialog alertDialog = new AlertDialog.Builder(requireContext()) + final AlertDialog alertDialog = new AlertDialog.Builder(getContext()) .setTitle(SLOT_TITLES[i]) .setView(radioGroup) .setCancelable(true) @@ -237,10 +207,10 @@ public class NotificationSettingsFragment extends Fragment { // if present set action icon with correct color if (NotificationConstants.ACTION_ICONS[action] != 0) { - Drawable drawable = AppCompatResources.getDrawable(requireContext(), + Drawable drawable = AppCompatResources.getDrawable(getContext(), NotificationConstants.ACTION_ICONS[action]); if (drawable != null) { - final int color = ThemeHelper.resolveColorFromAttr(requireContext(), + final int color = ThemeHelper.resolveColorFromAttr(getContext(), android.R.attr.textColorPrimary); drawable = DrawableCompat.wrap(drawable).mutate(); DrawableCompat.setTint(drawable, color); @@ -249,7 +219,7 @@ public class NotificationSettingsFragment extends Fragment { } } - radioButton.setText(NotificationConstants.getActionName(requireContext(), action)); + radioButton.setText(NotificationConstants.getActionName(getContext(), action)); radioButton.setChecked(action == selectedAction); radioButton.setId(id); radioButton.setLayoutParams(new RadioGroup.LayoutParams( @@ -259,7 +229,7 @@ public class NotificationSettingsFragment extends Fragment { } alertDialog.show(); - if (DeviceUtils.isTv(requireContext())) { + if (DeviceUtils.isTv(getContext())) { FocusOverlayView.setupFocusObserver(alertDialog); } } diff --git a/app/src/main/res/layout/settings_notification.xml b/app/src/main/res/layout/settings_notification.xml index dc329a78a..2ade057d1 100644 --- a/app/src/main/res/layout/settings_notification.xml +++ b/app/src/main/res/layout/settings_notification.xml @@ -1,78 +1,17 @@ - - + android:layout_height="wrap_content" + android:paddingTop="16dp"> - - - - - - - - - - - + app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toBottomOf="@+id/textView" /> - diff --git a/app/src/main/res/xml/notification_settings.xml b/app/src/main/res/xml/notification_settings.xml new file mode 100644 index 000000000..41659e7a3 --- /dev/null +++ b/app/src/main/res/xml/notification_settings.xml @@ -0,0 +1,18 @@ + + + + + + + + + + +