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

94 lines
3.1 KiB
Java
Raw Normal View History

2018-12-27 12:31:02 +00:00
package eu.faircode.email;
2018-12-31 08:04:33 +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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2018-12-31 08:04:33 +00:00
*/
2018-12-27 12:31:02 +00:00
import android.content.Context;
2021-05-08 07:25:29 +00:00
import android.content.res.TypedArray;
2018-12-27 12:31:02 +00:00
import android.util.AttributeSet;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class ContentLoadingProgressBar extends ProgressBar {
2020-08-29 16:40:29 +00:00
private int visibility = VISIBLE;
2021-03-22 08:03:26 +00:00
private boolean init = false;
2021-05-08 07:25:29 +00:00
private int delay = VISIBILITY_DELAY;
2020-08-29 16:40:29 +00:00
private boolean delaying = false;
2019-02-11 11:06:53 +00:00
2021-03-13 07:15:33 +00:00
private static final int VISIBILITY_DELAY = 1500; // milliseconds
2019-02-11 11:06:53 +00:00
2018-12-27 12:31:02 +00:00
public ContentLoadingProgressBar(@NonNull Context context) {
this(context, null);
}
public ContentLoadingProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs, 0);
2021-05-08 07:25:29 +00:00
getAttr(context, attrs);
2020-08-21 14:08:31 +00:00
}
public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
2021-05-08 07:25:29 +00:00
getAttr(context, attrs);
2020-08-21 14:08:31 +00:00
}
public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
2021-05-08 07:25:29 +00:00
getAttr(context, attrs);
}
private void getAttr(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ContentLoadingProgressBar, 0, 0);
this.delay = a.getInt(R.styleable.ContentLoadingProgressBar_show_delay, VISIBILITY_DELAY);
2018-12-27 12:31:02 +00:00
}
@Override
public void setVisibility(int visibility) {
2019-02-11 11:06:53 +00:00
this.visibility = visibility;
2020-03-24 10:09:17 +00:00
2018-12-27 12:31:02 +00:00
if (visibility == VISIBLE) {
2021-03-22 08:03:26 +00:00
if (delaying || (init && super.getVisibility() == VISIBLE))
2020-08-29 16:40:29 +00:00
return;
2021-03-22 08:03:26 +00:00
init = true;
2020-08-29 16:40:29 +00:00
delaying = true;
2018-12-27 12:31:02 +00:00
super.setVisibility(INVISIBLE);
2021-05-08 07:25:29 +00:00
ApplicationEx.getMainHandler().postDelayed(delayedShow, delay);
2020-08-29 16:40:29 +00:00
} else {
ApplicationEx.getMainHandler().removeCallbacks(delayedShow);
2020-08-29 19:56:02 +00:00
delaying = false;
2018-12-27 12:31:02 +00:00
super.setVisibility(visibility);
2020-08-29 16:40:29 +00:00
}
2018-12-27 12:31:02 +00:00
}
2020-03-24 22:08:03 +00:00
@Override
public int getVisibility() {
return this.visibility;
}
2018-12-27 12:31:02 +00:00
private final Runnable delayedShow = new Runnable() {
@Override
public void run() {
2020-08-29 16:40:29 +00:00
delaying = false;
2019-02-11 11:06:53 +00:00
if (visibility == VISIBLE)
ContentLoadingProgressBar.super.setVisibility(VISIBLE);
2018-12-27 12:31:02 +00:00
}
};
}