0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00

NF: embed progress bar in the layout

This commit is contained in:
Tarekk Mohamed Abdalla 2021-04-20 16:55:28 +02:00 committed by Mike Hardy
parent 9014ab35b3
commit af78fc4845
2 changed files with 57 additions and 50 deletions

View File

@ -31,6 +31,7 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.ichi2.anki.R;
import com.ichi2.anki.UIUtils;
@ -67,9 +68,6 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
private String mTranslationAddress;
@SuppressWarnings("deprecation") // tracked in github as #5020
private android.app.ProgressDialog mProgressDialog = null;
private String mPronunciationAddress;
private String mMp3Address;
@ -78,6 +76,11 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
private LanguageListerBeolingus mLanguageLister;
private Spinner mSpinnerFrom;
private LinearLayout mMainLayout;
private TextView mLoadingLayoutTitle;
private TextView mLoadingLayoutMessage;
private View mLoadingLayout;
private BackgroundPost mPostTranslation = null;
private BackgroundPost mPostPronunciation = null;
private DownloadFileTask mDownloadMp3Task = null;
@ -105,7 +108,10 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
setContentView(R.layout.activity_load_pronounciation);
mSource = getIntent().getExtras().getString(EXTRA_SOURCE);
LinearLayout linearLayout = findViewById(R.id.layoutInLoadPronActivity);
mMainLayout = findViewById(R.id.layoutInLoadPronActivity);
mLoadingLayout = findViewById(R.id.progress_bar_layout);
mLoadingLayoutTitle = findViewById(R.id.progress_bar_layout_title);
mLoadingLayoutMessage = findViewById(R.id.progress_bar_layout_message);
mLanguageLister = new LanguageListerBeolingus();
@ -114,11 +120,11 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
mLanguageLister.getLanguages());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerFrom.setAdapter(adapter);
linearLayout.addView(mSpinnerFrom);
mMainLayout.addView(mSpinnerFrom);
Button buttonLoadPronunciation = new Button(this);
buttonLoadPronunciation.setText(gtxt(R.string.multimedia_editor_pron_load));
linearLayout.addView(buttonLoadPronunciation);
mMainLayout.addView(buttonLoadPronunciation);
buttonLoadPronunciation.setOnClickListener(this::onLoadPronunciation);
Button mSaveButton = new Button(this);
mSaveButton.setText("Save");
@ -136,6 +142,23 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
}
private void showProgressBar(String message) {
showProgressBar(gtxt(R.string.multimedia_editor_progress_wait_title), message);
}
private void showProgressBar(CharSequence title, CharSequence message) {
mMainLayout.setVisibility(View.GONE);
mLoadingLayout.setVisibility(View.VISIBLE);
mLoadingLayoutTitle.setText(title);
mLoadingLayoutMessage.setText(message);
}
private void hideProgressBar() {
mLoadingLayout.setVisibility(View.GONE);
mMainLayout.setVisibility(View.VISIBLE);
}
/**
* @param v Start of the story.
*/
@ -147,7 +170,7 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
String message = gtxt(R.string.multimedia_editor_searching_word);
showProgressDialog(message);
showProgressBar(message);
mTranslationAddress = computeAddressOfTranslationPage();
@ -158,23 +181,12 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
mPostTranslation.execute();
} catch (Exception e) {
Timber.w(e);
mProgressDialog.dismiss();
hideProgressBar();
showToast(gtxt(R.string.multimedia_editor_something_wrong));
}
}
@SuppressWarnings("deprecation") // ProgressDialog change tracked in github as #5020
private void showProgressDialog(String message) {
dismissCarefullyProgressDialog();
mProgressDialog = android.app.ProgressDialog.show(this, gtxt(R.string.multimedia_editor_progress_wait_title), message, true,
false);
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(this);
}
/**
* @author zaur This class is used two times. First time from Beolingus it requests a page with the word
* translation. Second time it loads a page with the link to mp3 pronunciation file.
@ -285,13 +297,13 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
}
try {
showProgressDialog(gtxt(R.string.multimedia_editor_pron_looking_up));
showProgressBar(gtxt(R.string.multimedia_editor_pron_looking_up));
mPostPronunciation = new BackgroundPost();
mPostPronunciation.setAddress(mPronunciationAddress);
mPostPronunciation.execute();
} catch (Exception e) {
Timber.w(e);
mProgressDialog.dismiss();
hideProgressBar();
showToast(gtxt(R.string.multimedia_editor_something_wrong));
}
@ -314,13 +326,13 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
// Download MP3 file
try {
showProgressDialog(gtxt(R.string.multimedia_editor_general_downloading));
showProgressBar(gtxt(R.string.multimedia_editor_general_downloading));
mDownloadMp3Task = new DownloadFileTask();
mDownloadMp3Task.setAddress(mMp3Address);
mDownloadMp3Task.execute();
} catch (Exception e) {
Timber.w(e);
mProgressDialog.dismiss();
hideProgressBar();
showToast(gtxt(R.string.multimedia_editor_something_wrong));
}
}
@ -343,7 +355,7 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
return;
}
mProgressDialog.dismiss();
hideProgressBar();
showToast(gtxt(R.string.multimedia_editor_general_done));
Intent resultData = new Intent();
resultData.putExtra(EXTRA_PRONUNCIATION_FILE_PATH, result);
@ -367,7 +379,7 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
private void stop(String string) {
mProgressDialog.dismiss();
hideProgressBar();
showToast(string);
}
@ -415,26 +427,13 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
@Override
public void onCancel(DialogInterface dialog) {
mStopped = true;
dismissCarefullyProgressDialog();
hideProgressBar();
stopAllTasks();
Intent resultData = new Intent();
setResult(RESULT_CANCELED, resultData);
finish();
}
private void dismissCarefullyProgressDialog() {
try {
if ((mProgressDialog != null) && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
} catch (Exception e) {
Timber.w(e);
// nothing is done intentionally
}
}
private void stopAllTasks() {
AsyncTask<?, ?, ?> t = mPostTranslation;
TaskOperations.stopTaskGracefully(t);
@ -448,7 +447,7 @@ public class LoadPronounciationActivity extends Activity implements OnCancelList
@Override
protected void onPause() {
super.onPause();
dismissCarefullyProgressDialog();
hideProgressBar();
stopAllTasks();
}

View File

@ -1,8 +1,12 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutInLoadPronActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:orientation="vertical">
<com.ichi2.ui.FixedTextView
android:id="@+id/textViewPoweredBy"
@ -10,4 +14,8 @@
android:layout_height="wrap_content"
android:text="@string/multimedia_editor_poweredbeolingus" />
</LinearLayout>
</LinearLayout>
<include layout="@layout/progress_bar_layout" />
</FrameLayout>