SSTVEncoder2/app/src/main/java/om/sstvencoder/ProgressBarWrapper.java

85 lines
2.2 KiB
Java
Raw Normal View History

2017-01-08 22:20:12 +01:00
/*
Copyright 2017 Olga Miller <olga.rgb@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package om.sstvencoder;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
class ProgressBarWrapper {
private final ProgressBar mProgressBar;
private final Handler mHandler;
private final int mSteps;
private int mLastStep;
private int mPosition, mMaxPosition;
ProgressBarWrapper(ProgressBar progressBar) {
mProgressBar = progressBar;
mProgressBar.setVisibility(View.INVISIBLE);
mHandler = new Handler();
mSteps = 10;
}
private void startProgressBar(final int max) {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setMax(max);
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.VISIBLE);
}
});
}
private void stepProgressBar(final int state) {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(state);
}
});
}
private void endProgressBar() {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}
void begin(int max) {
mLastStep = 0;
mPosition = 0;
mMaxPosition = max;
startProgressBar(mSteps);
}
void step() {
++mPosition;
int newStep = (mSteps * mPosition + mMaxPosition / 2) / mMaxPosition;
if (newStep != mLastStep) {
stepProgressBar(newStep);
mLastStep = newStep;
}
}
void end() {
endProgressBar();
}
}