Skip to content

Commit

Permalink
fix: don't use tts install intent
Browse files Browse the repository at this point in the history
  • Loading branch information
renard314 committed Dec 23, 2016
1 parent 884df03 commit 0ab0b4d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 64 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 56
versionCode 57
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/renard/ocr/TextFairyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

public class TextFairyApplication extends Application {

private static final String LOG_TAG = TextFairyApplication.class.getSimpleName();
private Analytics mAnalytics;

public void onCreate() {
Expand All @@ -43,6 +42,16 @@ public void onCreate() {
initTextPreferences();
enableStrictMode();
alwaysShowOverflowButton();
startLeakCanary();
}

private void startLeakCanary() {
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
}

private void initTextPreferences() {
Expand All @@ -63,7 +72,6 @@ private void trackCrashes() {

private void enableStrictMode() {
if (BuildConfig.DEBUG) {
LeakCanary.install(this);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ public void run() {
i.putExtra(DocumentActivity.EXTRA_ACCURACY, accuracy);
i.putExtra(DocumentActivity.EXTRA_LANGUAGE, mOcrLanguage);
i.setData(documentUri);
i.putExtra(DocumentGridActivity.EXTRA_NATIVE_PIX, pix.getNativePix());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
break;
}
}
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import com.viewpagerindicator.CirclePageIndicator;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
Expand All @@ -41,12 +39,10 @@
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;
import java.util.Map;
Expand All @@ -65,7 +61,6 @@ public class DocumentPagerFragment extends Fragment implements DocumentContainer
private int mLastPosition = -1;
static final int REQUEST_CODE_TTS_CHECK = 6;
private TextSpeaker mTextSpeaker;
private boolean mHasTts;
private Map<String, String> hashMapResource;

@Override
Expand All @@ -74,9 +69,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mPager = (ViewPager) v.findViewById(R.id.document_pager);
mTitleIndicator = (CirclePageIndicator) v.findViewById(R.id.titles);
mLastPosition = 0;
mTextSpeaker = new TextSpeaker();
hashMapResource = ResourceUtils.getHashMapResource(getContext(), R.xml.iso_639_mapping);
mHasTts = checkTtsData();
initTts();
initPager();
EventBus.getDefault().register(this);
return v;
Expand All @@ -89,50 +83,27 @@ public void onDestroyView() {
EventBus.getDefault().unregister(this);
}

private boolean checkTtsData() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
ResolveInfo resolveInfo = getContext().getPackageManager().resolveActivity(checkIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo != null) {
getActivity().startActivityForResult(checkIntent, REQUEST_CODE_TTS_CHECK);
return true;
} else {
Toast.makeText(getContext(), R.string.tts_not_available, Toast.LENGTH_LONG).show();
return false;
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_TTS_CHECK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
mTextSpeaker.createTts(getContext(), new TtsInitListener() {
@Override
public void onInitError() {
EventBus.getDefault().post(new TtsInitError());
}
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);
@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());
final boolean localeSupported = mTextSpeaker.isLocaleSupported(documentLocale);
if (localeSupported) {
mTextSpeaker.setTtsLocale(documentLocale);
}
EventBus.getDefault().post(new TtsInitSuccess());

}
});
} else {
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}

});
}

public Locale mapTesseractLanguageToLocale(String ocrLanguage) {
Expand Down Expand Up @@ -315,10 +286,6 @@ public boolean getShowText() {
return mAdapter.getShowText();
}

public boolean hasTts() {
return mHasTts;
}

public TextSpeaker getTextSpeaker() {
return mTextSpeaker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

DocumentPagerFragment pagerFragment = (DocumentPagerFragment) getParentFragment();

final boolean hasTts = pagerFragment.hasTts();
DocumentActivity documentActivity = (DocumentActivity) getActivity();
mTextToSpeechControls.onCreateView(getChildFragmentManager(), documentActivity.getAnaLytics());
if (!hasTts) {
mTextToSpeechControls.setVisibility(View.GONE);
} else if (pagerFragment.getTextSpeaker().isInitialized()) {
if (pagerFragment.getTextSpeaker().isInitialized()) {
mTextToSpeechControls.onInitSuccess(pagerFragment.getTextSpeaker());
}
PreferencesUtils.applyTextPreferences(mEditText, getActivity());
Expand Down

0 comments on commit 0ab0b4d

Please sign in to comment.