From 718274ecc762003044c00d791ee7dc7ad5eb20a0 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 7 Sep 2021 21:06:17 +0200 Subject: [PATCH] Prevent crash --- .../eu/faircode/email/FragmentCompose.java | 33 ++++++++++++------- .../eu/faircode/email/FragmentFolders.java | 7 ++-- .../eu/faircode/email/FragmentMessages.java | 6 ++-- app/src/main/res/layout/action_button.xml | 3 +- .../main/res/layout/action_button_text.xml | 5 +-- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 1a9d7b2d7e..7086f6b0c1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1477,8 +1477,11 @@ public class FragmentCompose extends FragmentBase { PopupMenuLifecycle.insertIcons(getContext(), menu); - menu.findItem(R.id.menu_encrypt).setActionView(R.layout.action_button_text); - ImageButton ib = menu.findItem(R.id.menu_encrypt).getActionView().findViewById(R.id.button); + LayoutInflater infl = LayoutInflater.from(getContext()); + + View v = infl.inflate(R.layout.action_button_text, null); + v.setId(View.generateViewId()); + ImageButton ib = v.findViewById(R.id.button); ib.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -1498,26 +1501,31 @@ public class FragmentCompose extends FragmentBase { return true; } }); + menu.findItem(R.id.menu_encrypt).setActionView(v); - menu.findItem(R.id.menu_translate).setActionView(R.layout.action_button); - ImageButton ibTranslate = (ImageButton) menu.findItem(R.id.menu_translate).getActionView(); + ImageButton ibTranslate = (ImageButton) infl.inflate(R.layout.action_button, null); + ibTranslate.setId(View.generateViewId()); ibTranslate.setImageResource(R.drawable.twotone_translate_24); + ib.setContentDescription(getString(R.string.title_translate)); ibTranslate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onTranslate(vwAnchorMenu); } }); + menu.findItem(R.id.menu_translate).setActionView(ibTranslate); - menu.findItem(R.id.menu_zoom).setActionView(R.layout.action_button); - ImageButton ibZoom = (ImageButton) menu.findItem(R.id.menu_zoom).getActionView(); + ImageButton ibZoom = (ImageButton) infl.inflate(R.layout.action_button, null); + ibZoom.setId(View.generateViewId()); ibZoom.setImageResource(R.drawable.twotone_format_size_24); + ib.setContentDescription(getString(R.string.title_legend_zoom)); ibZoom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onMenuZoom(); } }); + menu.findItem(R.id.menu_zoom).setActionView(ibZoom); MenuCompat.setGroupDividerEnabled(menu, true); @@ -1528,9 +1536,11 @@ public class FragmentCompose extends FragmentBase { public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); + final Context context = getContext(); + menu.findItem(R.id.menu_encrypt).setEnabled(state == State.LOADED); menu.findItem(R.id.menu_translate).setEnabled(state == State.LOADED); - menu.findItem(R.id.menu_translate).setVisible(DeepL.isAvailable(getContext())); + menu.findItem(R.id.menu_translate).setVisible(DeepL.isAvailable(context)); menu.findItem(R.id.menu_zoom).setEnabled(state == State.LOADED); menu.findItem(R.id.menu_media).setEnabled(state == State.LOADED); menu.findItem(R.id.menu_compact).setEnabled(state == State.LOADED); @@ -1540,7 +1550,8 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_answer_create).setEnabled(state == State.LOADED); menu.findItem(R.id.menu_clear).setEnabled(state == State.LOADED); - int colorEncrypt = Helper.resolveColor(getContext(), R.attr.colorEncrypt); + int colorEncrypt = Helper.resolveColor(context, R.attr.colorEncrypt); + int colorActionForeground = Helper.resolveColor(context, R.attr.colorActionForeground); View v = menu.findItem(R.id.menu_encrypt).getActionView(); ImageButton ib = v.findViewById(R.id.button); @@ -1551,7 +1562,7 @@ public class FragmentCompose extends FragmentBase { if (EntityMessage.PGP_SIGNONLY.equals(encrypt) || EntityMessage.SMIME_SIGNONLY.equals(encrypt)) { ib.setImageResource(R.drawable.twotone_gesture_24); - ib.setImageTintList(null); + ib.setImageTintList(ColorStateList.valueOf(colorActionForeground)); tv.setText(EntityMessage.PGP_SIGNONLY.equals(encrypt) ? "P" : "S"); } else if (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt) || EntityMessage.SMIME_SIGNENCRYPT.equals(encrypt)) { ib.setImageResource(R.drawable.twotone_lock_24); @@ -1559,11 +1570,11 @@ public class FragmentCompose extends FragmentBase { tv.setText(EntityMessage.PGP_SIGNENCRYPT.equals(encrypt) ? "P" : "S"); } else { ib.setImageResource(R.drawable.twotone_lock_open_24); - ib.setImageTintList(null); + ib.setImageTintList(ColorStateList.valueOf(colorActionForeground)); tv.setText(null); } - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean save_drafts = prefs.getBoolean("save_drafts", true); boolean send_dialog = prefs.getBoolean("send_dialog", true); boolean image_dialog = prefs.getBoolean("image_dialog", true); diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index d46b7b9b73..fc16a98116 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -498,9 +498,11 @@ public class FragmentFolders extends FragmentBase { } }); - menu.findItem(R.id.menu_search).setActionView(R.layout.action_button); - ImageButton ibSearch = (ImageButton) menu.findItem(R.id.menu_search).getActionView(); + LayoutInflater infl = LayoutInflater.from(getContext()); + ImageButton ibSearch = (ImageButton) infl.inflate(R.layout.action_button, null); + ibSearch.setId(View.generateViewId()); ibSearch.setImageResource(R.drawable.twotone_search_24); + ibSearch.setContentDescription(getString(R.string.title_legend_search)); ibSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -514,6 +516,7 @@ public class FragmentFolders extends FragmentBase { return true; } }); + menu.findItem(R.id.menu_search).setActionView(ibSearch); MenuCompat.setGroupDividerEnabled(menu, true); super.onCreateOptionsMenu(menu, inflater); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 08fbc4a54b..d7eec1a13a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -4269,8 +4269,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_messages, menu); - menu.findItem(R.id.menu_folders).setActionView(R.layout.action_button); - ImageButton ib = (ImageButton) menu.findItem(R.id.menu_folders).getActionView(); + LayoutInflater infl = LayoutInflater.from(getContext()); + ImageButton ib = (ImageButton) infl.inflate(R.layout.action_button, null); + ib.setId(View.generateViewId()); ib.setImageResource(R.drawable.twotone_folder_24); ib.setContentDescription(getString(R.string.title_legend_section_folders)); ib.setOnClickListener(new View.OnClickListener() { @@ -4289,6 +4290,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. return true; } }); + menu.findItem(R.id.menu_folders).setActionView(ib); MenuCompat.setGroupDividerEnabled(menu, true); diff --git a/app/src/main/res/layout/action_button.xml b/app/src/main/res/layout/action_button.xml index 0ef5a2a490..f43b763f12 100644 --- a/app/src/main/res/layout/action_button.xml +++ b/app/src/main/res/layout/action_button.xml @@ -4,4 +4,5 @@ style="@style/Widget.AppCompat.Toolbar.Button.Navigation" android:layout_width="match_parent" android:layout_height="match_parent" - app:srcCompat="@mipmap/ic_launcher" /> + app:srcCompat="@mipmap/ic_launcher" + app:tint="?colorActionForeground" /> diff --git a/app/src/main/res/layout/action_button_text.xml b/app/src/main/res/layout/action_button_text.xml index fb160e6374..0505fe4d92 100644 --- a/app/src/main/res/layout/action_button_text.xml +++ b/app/src/main/res/layout/action_button_text.xml @@ -12,7 +12,8 @@ android:background="@android:color/transparent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:srcCompat="@drawable/twotone_lock_24" /> + app:srcCompat="@drawable/twotone_lock_24" + app:tint="?colorActionForeground" />