FairEmail/app/src/main/java/eu/faircode/email/PopupMenuLifecycle.java

141 lines
4.8 KiB
Java
Raw Normal View History

2019-05-03 16:59:27 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00:00
/*
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 <http://www.gnu.org/licenses/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-05-03 16:59:27 +00:00
import android.content.Context;
2021-05-07 07:41:26 +00:00
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.view.Gravity;
2021-05-07 07:41:26 +00:00
import android.view.Menu;
2019-12-19 18:53:16 +00:00
import android.view.MenuItem;
2021-05-07 07:41:26 +00:00
import android.view.SubMenu;
2019-05-03 16:59:27 +00:00
import android.view.View;
import androidx.annotation.NonNull;
2019-12-19 18:53:16 +00:00
import androidx.annotation.Nullable;
2020-11-12 15:19:23 +00:00
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.view.menu.MenuPopupHelper;
2019-05-03 16:59:27 +00:00
import androidx.appcompat.widget.PopupMenu;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
public PopupMenuLifecycle(@NonNull Context context, LifecycleOwner owner, @NonNull View anchor) {
2020-11-12 15:19:23 +00:00
super(new ContextThemeWrapper(context, R.style.popupMenuStyle), anchor);
2019-09-20 06:39:31 +00:00
Log.i("Instantiate " + this);
2019-09-21 14:15:55 +00:00
2019-09-21 18:28:03 +00:00
owner.getLifecycle().addObserver(this);
2019-05-03 16:59:27 +00:00
}
2021-05-07 07:41:26 +00:00
public void insertIcons(Context context) {
insertIcons(new ContextThemeWrapper(context, R.style.popupMenuStyle), getMenu());
}
2019-05-03 16:59:27 +00:00
@Override
public void show() {
2019-09-20 06:39:31 +00:00
Log.i("Show " + this);
2019-08-04 18:00:20 +00:00
try {
super.show();
} catch (Throwable ex) {
Log.e(ex);
}
2019-05-03 16:59:27 +00:00
}
public void showWithIcons(Context context, View anchor) {
MenuPopupHelper menuHelper = new MenuPopupHelper(context, (MenuBuilder) getMenu(), anchor);
menuHelper.setForceShowIcon(true);
menuHelper.setGravity(Gravity.END);
menuHelper.show();
}
2019-12-19 18:53:16 +00:00
@Override
public void setOnMenuItemClickListener(@Nullable OnMenuItemClickListener listener) {
super.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
try {
// Handle click just before destroy
return listener.onMenuItemClick(item);
} catch (Throwable ex) {
Log.w(ex);
return false;
}
}
});
}
2019-05-03 16:59:27 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
2019-09-20 06:39:31 +00:00
Log.i("Destroy " + this);
2019-05-03 16:59:27 +00:00
this.dismiss();
}
2021-05-07 07:41:26 +00:00
2021-05-20 17:04:07 +00:00
static void insertIcons(Context context, Menu menu) {
2021-05-07 07:41:26 +00:00
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
CharSequence title = item.getTitle();
insertIcon(context, item);
if (item.hasSubMenu()) {
SubMenu sub = item.getSubMenu();
2021-05-11 08:03:28 +00:00
boolean has = false;
2021-05-07 07:41:26 +00:00
for (int j = 0; j < sub.size(); j++)
if (sub.getItem(j).getIcon() != null) {
2021-05-11 08:03:28 +00:00
has = true;
2021-05-07 07:41:26 +00:00
insertIcons(context, sub);
break;
}
2021-05-11 08:03:28 +00:00
if (has)
sub.setHeaderTitle(title);
2021-05-07 07:41:26 +00:00
}
}
}
private static void insertIcon(Context context, MenuItem menuItem) {
Drawable icon = menuItem.getIcon();
if (icon == null)
icon = new ColorDrawable(Color.TRANSPARENT);
2021-05-07 19:50:38 +00:00
else {
2021-07-31 16:39:46 +00:00
icon = icon.getConstantState().newDrawable().mutate();
2021-05-07 19:50:38 +00:00
int color = Helper.resolveColor(context, R.attr.colorAccent);
icon.setTint(color);
2021-07-31 16:39:46 +00:00
if (!menuItem.isEnabled())
2021-05-07 19:50:38 +00:00
icon.setAlpha(Math.round(Helper.LOW_LIGHT * 255));
}
2021-05-07 07:41:26 +00:00
int iconSize = context.getResources().getDimensionPixelSize(R.dimen.menu_item_icon_size);
icon.setBounds(0, 0, iconSize, iconSize);
2021-06-02 13:15:27 +00:00
ImageSpan imageSpan = new CenteredImageSpan(icon);
2021-05-07 07:41:26 +00:00
2021-09-09 10:52:10 +00:00
SpannableStringBuilder ssb = new SpannableStringBuilderEx(menuItem.getTitle());
2021-05-23 06:36:57 +00:00
ssb.insert(0, "\uFFFC\u2002"); // object replacement character, en space
2021-05-07 07:41:26 +00:00
ssb.setSpan(imageSpan, 0, 1, 0);
menuItem.setTitle(ssb);
menuItem.setIcon(null);
}
2019-05-03 16:59:27 +00:00
}