Skip to content

Commit

Permalink
fix: share one tts instance among all text fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
renard314 committed Dec 27, 2016
1 parent 0ab0b4d commit c42eda2
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 204 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 57
versionCode 60
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public class DocumentActivity extends NewDocumentActivity implements LoaderManag
protected ViewSwitcher mViewModeSwitcher;

public interface DocumentContainerFragment {
String getLangOfCurrentlyShownDocument();


String getTextOfCurrentlyShownDocument();

int getDocumentCount();
Expand Down Expand Up @@ -357,14 +356,6 @@ void copyTextToClipboard() {
Toast.makeText(this, getString(R.string.text_was_copied_to_clipboard), Toast.LENGTH_LONG).show();
}

public String getCurrentDocumentText() {
return getDocumentContainer().getTextOfCurrentlyShownDocument();
}

String getLanguageOfDocument() {
return getDocumentContainer().getLangOfCurrentlyShownDocument();
}

String getPlainDocumentText() {
final String htmlText = getDocumentContainer().getTextOfAllDocuments();
if (htmlText != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
public class DocumentAdapter extends FragmentStatePagerAdapter {
private int mIndexLanguage;
private int mIndexImagePath;
private int mIndexTitle;
private int mIndexOCRText;
private int mIndexId;
private int mIndexTitle;
private int mIndexOCRText;
private int mIndexId;

private Cursor mCursor;
private Cursor mCursor;
private Map<Integer, DocumentTextFragment> mPageReferenceMap = new HashMap<>();
private boolean mShowText = true;

Expand All @@ -52,10 +52,10 @@ public DocumentAdapter(FragmentManager fm, final Cursor cursor) {
@Override
public Object instantiateItem(ViewGroup container, int position) {
final Object o = super.instantiateItem(container, position);
if (o instanceof DocumentTextFragment) {
if (o instanceof DocumentTextFragment) {
mPageReferenceMap.put(position, (DocumentTextFragment) o);
} else {
mPageReferenceMap.put(position,null);
mPageReferenceMap.put(position, null);
}
return o;

Expand All @@ -78,8 +78,8 @@ public Fragment getItem(int position) {
imagePath = mCursor.getString(mIndexImagePath);
}
if (mShowText) {
return DocumentTextFragment.newInstance(text, documentId, imagePath);
}else {
return DocumentTextFragment.newInstance(text, documentId, imagePath, getLanguage(position), position);
} else {
return DocumentImageFragment.newInstance(imagePath);

}
Expand All @@ -94,34 +94,34 @@ public void destroyItem(View container, int position, Object object) {
mPageReferenceMap.remove(position);
}

@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public int getCount() {
return mCursor.getCount();
}


public String getLanguage(int position){
if(mCursor.moveToPosition(position)){
public String getLanguage(int position) {
if (mCursor.moveToPosition(position)) {
return mCursor.getString(mIndexLanguage);
}
return null;
}

public int getId(int position){
if(mCursor.moveToPosition(position)){
public int getId(int position) {
if (mCursor.moveToPosition(position)) {
return mCursor.getInt(mIndexId);
}
return -1;
}

public String getLongTitle(int position) {
if (mCursor.moveToPosition(position)) {
return mCursor.getString(mIndexTitle);
}
return null;
}
public String getLongTitle(int position) {
if (mCursor.moveToPosition(position)) {
return mCursor.getString(mIndexTitle);
}
return null;
}

public String getText(int position){
public String getText(int position) {
boolean success = mCursor.moveToPosition(position);
if (success) {
return mCursor.getString(mIndexOCRText);
Expand All @@ -131,9 +131,9 @@ public String getText(int position){

@Override
public int getItemPosition(Object object) {
if(object instanceof DocumentTextFragment && !mShowText){
if (object instanceof DocumentTextFragment && !mShowText) {
return POSITION_NONE;
}else if(object instanceof DocumentImageFragment && mShowText){
} else if (object instanceof DocumentImageFragment && mShowText) {
return POSITION_NONE;
}
return POSITION_UNCHANGED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@
import com.renard.ocr.R;
import com.renard.ocr.documents.viewing.single.DocumentActivity.DocumentContainerFragment;
import com.renard.ocr.documents.viewing.single.tts.TextSpeaker;
import com.renard.ocr.documents.viewing.single.tts.TtsInitError;
import com.renard.ocr.documents.viewing.single.tts.TtsInitListener;
import com.renard.ocr.documents.viewing.single.tts.TtsInitSuccess;
import com.renard.ocr.documents.viewing.single.tts.TtsLanguageChoosen;
import com.renard.ocr.util.PreferencesUtils;
import com.renard.ocr.util.ResourceUtils;
import com.viewpagerindicator.CirclePageIndicator;

import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
Expand All @@ -44,11 +37,6 @@
import android.view.ViewGroup;
import android.widget.EditText;

import java.util.Locale;
import java.util.Map;

import de.greenrobot.event.EventBus;

public class DocumentPagerFragment extends Fragment implements DocumentContainerFragment {


Expand All @@ -59,73 +47,22 @@ public class DocumentPagerFragment extends Fragment implements DocumentContainer
private Cursor mCursor;
private DocumentAdapter mAdapter;
private int mLastPosition = -1;
static final int REQUEST_CODE_TTS_CHECK = 6;
private TextSpeaker mTextSpeaker;
private Map<String, String> hashMapResource;
private TextSpeaker mTextSpeaker = new TextSpeaker();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_document_pager, container, false);
mPager = (ViewPager) v.findViewById(R.id.document_pager);
mTitleIndicator = (CirclePageIndicator) v.findViewById(R.id.titles);
mLastPosition = 0;
hashMapResource = ResourceUtils.getHashMapResource(getContext(), R.xml.iso_639_mapping);
initTts();
initPager();
EventBus.getDefault().register(this);
return v;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mTextSpeaker.onDestroyView();
EventBus.getDefault().unregister(this);
}

private void initTts() {
mTextSpeaker = new TextSpeaker();
mTextSpeaker.createTts(getContext(), new TtsInitListener() {
@Override
public void onInitError() {
EventBus.getDefault().post(new TtsInitError());
}

@Override
public void onInitSuccess() {
final String langOfCurrentlyShownDocument = getLangOfCurrentlyShownDocument();
Locale documentLocale = mapTesseractLanguageToLocale(langOfCurrentlyShownDocument);

final boolean localeSupported = mTextSpeaker.isLocaleSupported(documentLocale);
if (localeSupported) {
mTextSpeaker.setTtsLocale(documentLocale);
}
EventBus.getDefault().post(new TtsInitSuccess());

}
});
}

public Locale mapTesseractLanguageToLocale(String ocrLanguage) {
final String s = hashMapResource.get(ocrLanguage);
if (s != null) {
return new Locale(s);
} else {
return null;
}
}

@SuppressWarnings("unused")
public void onEventMainThread(final TtsLanguageChoosen event) {
Locale documentLocale = event.getLocale();
mTextSpeaker.setTtsLocale(documentLocale);
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

boolean isKeyboardHidden = Configuration.KEYBOARDHIDDEN_YES == newConfig.hardKeyboardHidden;
boolean isKeyboardShown = Configuration.KEYBOARDHIDDEN_NO == newConfig.hardKeyboardHidden;
if (isKeyboardShown) {
Expand All @@ -135,7 +72,6 @@ public void onConfigurationChanged(Configuration newConfig) {
}
}


private void showTitleIndicator(final boolean show) {
if (mIsTitleIndicatorVisible) {
if (show) {
Expand All @@ -146,6 +82,12 @@ private void showTitleIndicator(final boolean show) {
}
}

@Override
public void onDestroyView() {
super.onDestroyView();
mTextSpeaker.onDestroyView();
}

public void applyTextPreferences() {
final int count = mPager.getChildCount();
for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -182,6 +124,7 @@ private void initPager() {
mPager.setAdapter(mAdapter);
}


if (mAdapter.getCount() > 1) {
mTitleIndicator.setViewPager(mPager);
mIsTitleIndicatorVisible = true;
Expand Down Expand Up @@ -215,12 +158,6 @@ public void onPageScrollStateChanged(int arg0) {
}
}

@Override
public String getLangOfCurrentlyShownDocument() {
DocumentAdapter adapter = (DocumentAdapter) mPager.getAdapter();
int currentItem = mPager.getCurrentItem();
return adapter.getLanguage(currentItem);
}

@Override
public String getTextOfCurrentlyShownDocument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import com.renard.ocr.R;
import com.renard.ocr.documents.creation.NewDocumentActivity;
import com.renard.ocr.documents.viewing.DocumentContentProvider;
import com.renard.ocr.documents.viewing.single.tts.TextSpeaker;
import com.renard.ocr.documents.viewing.single.tts.TextToSpeechControls;
import com.renard.ocr.documents.viewing.single.tts.TtsInitError;
import com.renard.ocr.documents.viewing.single.tts.TtsInitSuccess;
import com.renard.ocr.util.PreferencesUtils;

import android.net.Uri;
Expand All @@ -42,11 +41,12 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class DocumentTextFragment extends Fragment implements TextWatcher {

private final static String IS_STATE_SAVED = "is_state_saved";
public static final String EXTRA_LANG = "lang";
private static final String EXTRA_POSITION = "position";

@BindView(R.id.text_to_speech_controls)
protected TextToSpeechControls mTextToSpeechControls;
Expand All @@ -61,10 +61,12 @@ public class DocumentTextFragment extends Fragment implements TextWatcher {
private HtmlToSpannedAsyncTask mHtmlTask;
private boolean mIsInitialTextChange = true;

public static DocumentTextFragment newInstance(final String text, Integer documentId, final String imagePath) {
public static DocumentTextFragment newInstance(final String text, Integer documentId, final String imagePath, String lang, int position) {
DocumentTextFragment f = new DocumentTextFragment();
// Supply text input as an argument.
Bundle args = new Bundle();
args.putString(EXTRA_LANG, lang);
args.putInt(EXTRA_POSITION, position);
args.putString("text", text);
args.putInt("id", documentId);
args.putString("image_path", imagePath);
Expand All @@ -81,19 +83,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mDocumentId = getArguments().getInt("id");
View view = inflater.inflate(R.layout.fragment_document_text, container, false);
ButterKnife.bind(this, view);
EventBus.getDefault().register(this);

if (mHtmlTask != null) {
mHtmlTask.cancel(true);
}

DocumentPagerFragment pagerFragment = (DocumentPagerFragment) getParentFragment();

DocumentActivity documentActivity = (DocumentActivity) getActivity();
mTextToSpeechControls.onCreateView(getChildFragmentManager(), documentActivity.getAnaLytics());
if (pagerFragment.getTextSpeaker().isInitialized()) {
mTextToSpeechControls.onInitSuccess(pagerFragment.getTextSpeaker());
}
DocumentPagerFragment pagerFragment = (DocumentPagerFragment) getParentFragment();
TextSpeaker textSpeaker = pagerFragment.getTextSpeaker();
mTextToSpeechControls.onCreateView(getChildFragmentManager(), documentActivity.getAnaLytics(), textSpeaker);
PreferencesUtils.applyTextPreferences(mEditText, getActivity());

return view;
Expand All @@ -108,18 +106,6 @@ public void onDestroyView() {
}
mEditText.removeTextChangedListener(this);
mTextToSpeechControls.onDestroyView();
EventBus.getDefault().unregister(this);
}

@SuppressWarnings("unused")
public void onEventMainThread(final TtsInitError event) {
mTextToSpeechControls.onInitError();
}

@SuppressWarnings("unused")
public void onEventMainThread(final TtsInitSuccess event) {
DocumentPagerFragment pagerFragment = (DocumentPagerFragment) getParentFragment();
mTextToSpeechControls.onInitSuccess(pagerFragment.getTextSpeaker());
}

@Override
Expand All @@ -138,10 +124,18 @@ public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
} else {
mViewSwitcher.setDisplayedChild(1);
mEditText.addTextChangedListener(this);
mTextToSpeechControls.setCurrentText(mEditText.getText());
mTextToSpeechControls.setCurrentText(mEditText.getText(), getLangOfCurrentlyShownDocument(), getIndexOfCurrentlyShownDocument());
}
}

private int getIndexOfCurrentlyShownDocument() {
return getArguments().getInt(EXTRA_POSITION);
}

private String getLangOfCurrentlyShownDocument() {
return getArguments().getString(EXTRA_LANG);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -181,7 +175,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
} else {
mHasTextChanged = true;
}
mTextToSpeechControls.setCurrentText(s);
mTextToSpeechControls.setCurrentText(s, getLangOfCurrentlyShownDocument(), getIndexOfCurrentlyShownDocument());
}

@Override
Expand Down
Loading

0 comments on commit c42eda2

Please sign in to comment.