2019-07-13 06:58:56 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2020-07-06 16:30:10 +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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2019-07-13 06:58:56 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2020-09-22 17:44:53 +00:00
|
|
|
import android.graphics.Color;
|
2020-06-12 15:06:33 +00:00
|
|
|
import android.os.Looper;
|
2019-07-13 15:13:49 +00:00
|
|
|
import android.view.Gravity;
|
2019-07-13 06:58:56 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-07-13 15:13:49 +00:00
|
|
|
public class ToastEx extends Toast {
|
|
|
|
public ToastEx(Context context) {
|
2019-08-09 14:41:36 +00:00
|
|
|
super(context.getApplicationContext());
|
2019-07-13 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 08:04:56 +00:00
|
|
|
public static ToastEx makeText(Context context, int resId, int duration) throws Resources.NotFoundException {
|
2019-07-13 06:58:56 +00:00
|
|
|
return makeText(context, context.getText(resId), duration);
|
|
|
|
}
|
|
|
|
|
2019-08-01 08:04:56 +00:00
|
|
|
public static ToastEx makeText(Context context, CharSequence text, int duration) {
|
2019-07-13 15:13:49 +00:00
|
|
|
ToastEx toast = new ToastEx(context);
|
2019-07-13 06:58:56 +00:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
|
|
|
View view = inflater.inflate(R.layout.toast, null);
|
2019-09-12 20:40:44 +00:00
|
|
|
view.setBackground(context.getResources().getDrawable(R.drawable.toast_background, context.getTheme()));
|
|
|
|
|
2019-07-13 06:58:56 +00:00
|
|
|
TextView tv = view.findViewById(android.R.id.message);
|
2019-12-27 08:10:54 +00:00
|
|
|
tv.setTextColor(Helper.resolveColor(context, R.attr.colorInfoForeground));
|
2019-07-13 06:58:56 +00:00
|
|
|
tv.setText(text);
|
|
|
|
toast.setView(view);
|
|
|
|
toast.setDuration(duration);
|
2019-07-13 15:13:49 +00:00
|
|
|
// <dimen name="design_bottom_navigation_height">56dp</dimen>
|
|
|
|
int dp = Helper.dp2pixels(context, 2 * 56);
|
|
|
|
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, dp);
|
2019-07-13 06:58:56 +00:00
|
|
|
return toast;
|
|
|
|
}
|
2019-08-20 14:39:29 +00:00
|
|
|
|
2020-09-22 17:44:53 +00:00
|
|
|
public static ToastEx makeTextBw(Context context, CharSequence text, int duration) {
|
|
|
|
ToastEx toast = new ToastEx(context);
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
|
|
|
View view = inflater.inflate(R.layout.toast, null);
|
|
|
|
view.setBackground(context.getDrawable(android.R.drawable.toast_frame));
|
|
|
|
|
|
|
|
TextView tv = view.findViewById(android.R.id.message);
|
|
|
|
tv.setTextColor(Color.parseColor("#de000000")); // primary_text_default_material_light
|
|
|
|
tv.setText(text);
|
|
|
|
toast.setView(view);
|
|
|
|
toast.setDuration(duration);
|
|
|
|
return toast;
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:39:29 +00:00
|
|
|
@Override
|
|
|
|
public void show() {
|
2020-06-12 15:06:33 +00:00
|
|
|
// https://developer.android.com/preview/features/toasts
|
|
|
|
if (Looper.myLooper() != Looper.getMainLooper())
|
|
|
|
Log.e("Toast from background");
|
|
|
|
|
2019-08-20 14:39:29 +00:00
|
|
|
// https://stackoverflow.com/questions/56017928/toast-not-showing-in-android-q
|
|
|
|
super.show();
|
|
|
|
}
|
2019-07-13 06:58:56 +00:00
|
|
|
}
|