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/>.
|
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2019-05-04 20:49:22 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-03 16:59:27 +00:00
|
|
|
import android.content.Context;
|
2019-12-19 18:53:16 +00:00
|
|
|
import android.view.MenuItem;
|
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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|