New Thread and additional ProgressBar for saving of wave file,

ProgressBarWrapper now with text
pull/3/head
Olga Miller 2017-01-09 22:37:17 +01:00
rodzic acaceef5b7
commit cf796fe695
5 zmienionych plików z 147 dodań i 31 usunięć

Wyświetl plik

@ -29,14 +29,19 @@ import om.sstvencoder.Output.OutputFactory;
// Creates IMode instance
class Encoder {
private final MainActivityMessenger mMessenger;
private final Thread mThread;
private Thread mSaveWaveThread;
private final List<IMode> mQueue;
private final ProgressBarWrapper mProgressBar;
private final ProgressBarWrapper mProgressBar, mProgressBar2;
private boolean mQuit, mStop;
private Class<?> mModeClass;
Encoder(ProgressBarWrapper progressBar) {
Encoder(MainActivityMessenger messenger,
ProgressBarWrapper progressBar, ProgressBarWrapper progressBar2) {
mMessenger = messenger;
mProgressBar = progressBar;
mProgressBar2 = progressBar2;
mQueue = new LinkedList<>();
mQuit = false;
mStop = false;
@ -61,7 +66,7 @@ class Encoder {
mode = mQueue.remove(0);
}
mode.init();
mProgressBar.begin(mode.getProcessCount());
mProgressBar.begin(mode.getProcessCount(), "Sending...");
while (mode.process()) {
mProgressBar.step();
@ -103,19 +108,37 @@ class Encoder {
enqueue(mode);
}
boolean save(Bitmap bitmap, File file) {
void save(Bitmap bitmap, File file) {
if (mSaveWaveThread != null && mSaveWaveThread.isAlive())
return;
IOutput output = OutputFactory.createOutputForSavingAsWave(file);
IMode mode = ModeFactory.CreateMode(mModeClass, bitmap, output);
if (mode != null) {
mode.init();
if (mode != null)
save(mode, file);
}
while (mode.process()) {
if (mQuit)
break;
private void save(final IMode mode, final File file) {
mSaveWaveThread = new Thread() {
@Override
public void run() {
mode.init();
mProgressBar2.begin(mode.getProcessCount(), file.getName() + " saving...");
while (mode.process()) {
mProgressBar2.step();
synchronized (this) {
if (mQuit)
break;
}
}
mode.finish(mQuit);
mProgressBar2.end();
if (!mQuit)
mMessenger.carrySaveAsWaveIsDoneMessage(file);
}
mode.finish(mQuit);
}
return !mQuit;
};
mSaveWaveThread.start();
}
void stop() {

Wyświetl plik

@ -40,6 +40,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
@ -65,7 +66,7 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCropView = (CropView) findViewById(R.id.cropView);
mEncoder = new Encoder(new ProgressBarWrapper((ProgressBar) findViewById(R.id.progressBar)));
mEncoder = new Encoder(new MainActivityMessenger(this), getProgressBar(), getProgressBar2());
IModeInfo mode = mEncoder.getModeInfo();
mCropView.setModeSize(mode.getModeSize());
setTitle(mode.getModeName());
@ -76,6 +77,18 @@ public class MainActivity extends AppCompatActivity {
loadImage(getIntent());
}
private ProgressBarWrapper getProgressBar() {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
TextView progressBarText = (TextView) findViewById(R.id.progressBarText);
return new ProgressBarWrapper(progressBar, progressBarText);
}
private ProgressBarWrapper getProgressBar2() {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
TextView progressBarText = (TextView) findViewById(R.id.progressBarText2);
return new ProgressBarWrapper(progressBar, progressBarText);
}
@Override
protected void onNewIntent(Intent intent) {
loadImage(intent);
@ -401,12 +414,17 @@ public class MainActivity extends AppCompatActivity {
private void save() {
File file = Utility.createWaveFilePath();
if (mEncoder.save(mCropView.getBitmap(), file)) {
ContentValues values = Utility.getWavContentValues(file);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
getContentResolver().insert(uri, values);
Toast.makeText(this, file.getName() + " saved.", Toast.LENGTH_SHORT).show();
}
mEncoder.save(mCropView.getBitmap(), file);
}
public void completeSaving(File file) {
addFileToContentResolver(file);
}
private void addFileToContentResolver(File file) {
ContentValues values = Utility.getWavContentValues(file);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
getContentResolver().insert(uri, values);
}
@Override

Wyświetl plik

@ -0,0 +1,39 @@
/*
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 java.io.File;
class MainActivityMessenger {
private final MainActivity mMainActivity;
private final Handler mHandler;
MainActivityMessenger(MainActivity activity) {
mMainActivity = activity;
mHandler = new Handler();
}
void carrySaveAsWaveIsDoneMessage(final File file) {
mHandler.post(new Runnable() {
@Override
public void run() {
mMainActivity.completeSaving(file);
}
});
}
}

Wyświetl plik

@ -18,37 +18,45 @@ package om.sstvencoder;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
class ProgressBarWrapper {
private final ProgressBar mProgressBar;
private final TextView mText;
private final Handler mHandler;
private final int mSteps;
private int mLastStep;
private int mPosition, mMaxPosition;
ProgressBarWrapper(ProgressBar progressBar) {
ProgressBarWrapper(ProgressBar progressBar, TextView text) {
mProgressBar = progressBar;
mProgressBar.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.GONE);
mText = text;
mText.setVisibility(View.GONE);
mHandler = new Handler();
mSteps = 10;
}
private void startProgressBar(final int max) {
private void startProgressBar(final int max, final String text) {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setMax(max);
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.VISIBLE);
if (text != null) {
mText.setText(text);
mText.setVisibility(View.VISIBLE);
}
}
});
}
private void stepProgressBar(final int state) {
private void stepProgressBar(final int progress) {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(state);
mProgressBar.setProgress(progress);
}
});
}
@ -57,16 +65,17 @@ class ProgressBarWrapper {
mHandler.post(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.GONE);
mText.setVisibility(View.GONE);
}
});
}
void begin(int max) {
void begin(int max, String text) {
mLastStep = 0;
mPosition = 0;
mMaxPosition = max;
startProgressBar(mSteps);
startProgressBar(mSteps, text);
}
void step() {

Wyświetl plik

@ -17,11 +17,38 @@
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<TextView
android:id="@+id/progressBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarText"
android:orientation="horizontal"/>
<TextView
android:id="@+id/progressBarText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar2"/>
</RelativeLayout>
</RelativeLayout>