Search in sources :

Example 66 with TextToSpeech

use of android.speech.tts.TextToSpeech in project OpenCamera by ageback.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    long debug_time = 0;
    if (MyDebug.LOG) {
        Log.d(TAG, "onCreate: " + this);
        debug_time = System.currentTimeMillis();
    }
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // don't show orientation animations
        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
        getWindow().setAttributes(layout);
    }
    setContentView(R.layout.activity_main);
    // initialise any unset preferences to their default values
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting default preference values: " + (System.currentTimeMillis() - debug_time));
    if (getIntent() != null && getIntent().getExtras() != null) {
        // whether called from testing
        is_test = getIntent().getExtras().getBoolean("test_project");
        if (MyDebug.LOG)
            Log.d(TAG, "is_test: " + is_test);
    }
    if (getIntent() != null && getIntent().getExtras() != null) {
        // whether called from Take Photo widget
        if (MyDebug.LOG)
            Log.d(TAG, "take_photo?: " + getIntent().getExtras().getBoolean(TakePhoto.TAKE_PHOTO));
    }
    if (getIntent() != null && getIntent().getAction() != null) {
        // invoked via the manifest shortcut?
        if (MyDebug.LOG)
            Log.d(TAG, "shortcut: " + getIntent().getAction());
    }
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    // determine whether we should support "auto stabilise" feature
    // risk of running out of memory on lower end devices, due to manipulation of large bitmaps
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    if (MyDebug.LOG) {
        Log.d(TAG, "standard max memory = " + activityManager.getMemoryClass() + "MB");
        Log.d(TAG, "large max memory = " + activityManager.getLargeMemoryClass() + "MB");
    }
    large_heap_memory = activityManager.getLargeMemoryClass();
    if (large_heap_memory >= 128) {
        supports_auto_stabilise = true;
    }
    if (MyDebug.LOG)
        Log.d(TAG, "supports_auto_stabilise? " + supports_auto_stabilise);
    // also added the check for having 128MB standard heap, to support modded LG G2, which has 128MB standard, 256MB large - see https://sourceforge.net/p/opencamera/tickets/9/
    if (activityManager.getMemoryClass() >= 128 || activityManager.getLargeMemoryClass() >= 512) {
        supports_force_video_4k = true;
    }
    if (MyDebug.LOG)
        Log.d(TAG, "supports_force_video_4k? " + supports_force_video_4k);
    // set up components
    mainUI = new MainUI(this);
    applicationInterface = new MyApplicationInterface(this, savedInstanceState);
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after creating application interface: " + (System.currentTimeMillis() - debug_time));
    textFormatter = new TextFormatter(this);
    // determine whether we support Camera2 API
    initCamera2Support();
    // set up window flags for normal operation
    setWindowFlagsForCamera();
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting window flags: " + (System.currentTimeMillis() - debug_time));
    save_location_history = new SaveLocationHistory(this, "save_location_history", getStorageUtils().getSaveLocation());
    if (applicationInterface.getStorageUtils().isUsingSAF()) {
        if (MyDebug.LOG)
            Log.d(TAG, "create new SaveLocationHistory for SAF");
        save_location_history_saf = new SaveLocationHistory(this, "save_location_history_saf", getStorageUtils().getSaveLocationSAF());
    }
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after updating folder history: " + (System.currentTimeMillis() - debug_time));
    // set up sensors
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    // accelerometer sensor (for device orientation)
    if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
        if (MyDebug.LOG)
            Log.d(TAG, "found accelerometer");
        mSensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    } else {
        if (MyDebug.LOG)
            Log.d(TAG, "no support for accelerometer");
    }
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after creating accelerometer sensor: " + (System.currentTimeMillis() - debug_time));
    // magnetic sensor (for compass direction)
    if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null) {
        if (MyDebug.LOG)
            Log.d(TAG, "found magnetic sensor");
        mSensorMagnetic = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    } else {
        if (MyDebug.LOG)
            Log.d(TAG, "no support for magnetic sensor");
    }
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after creating magnetic sensor: " + (System.currentTimeMillis() - debug_time));
    // clear any seek bars (just in case??)
    mainUI.clearSeekBar();
    // set up the camera and its preview
    preview = new Preview(applicationInterface, ((ViewGroup) this.findViewById(R.id.preview)));
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after creating preview: " + (System.currentTimeMillis() - debug_time));
    // initialise on-screen button visibility
    View switchCameraButton = findViewById(R.id.switch_camera);
    switchCameraButton.setVisibility(preview.getCameraControllerManager().getNumberOfCameras() > 1 ? View.VISIBLE : View.GONE);
    View speechRecognizerButton = findViewById(R.id.audio_control);
    // disabled by default, until the speech recognizer is created
    speechRecognizerButton.setVisibility(View.GONE);
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting button visibility: " + (System.currentTimeMillis() - debug_time));
    View pauseVideoButton = findViewById(R.id.pause_video);
    pauseVideoButton.setVisibility(View.GONE);
    View takePhotoVideoButton = findViewById(R.id.take_photo_when_video_recording);
    takePhotoVideoButton.setVisibility(View.GONE);
    // We initialise optional controls to invisible/gone, so they don't show while the camera is opening - the actual visibility is
    // set in cameraSetup().
    // Note that ideally we'd set this in the xml, but doing so for R.id.zoom causes a crash on Galaxy Nexus startup beneath
    // setContentView()!
    // To be safe, we also do so for take_photo and zoom_seekbar (we already know we've had no reported crashes for focus_seekbar,
    // however).
    View takePhotoButton = findViewById(R.id.take_photo);
    takePhotoButton.setVisibility(View.INVISIBLE);
    View zoomControls = findViewById(R.id.zoom);
    zoomControls.setVisibility(View.GONE);
    View zoomSeekbar = findViewById(R.id.zoom_seekbar);
    zoomSeekbar.setVisibility(View.INVISIBLE);
    // listen for orientation event change
    orientationEventListener = new OrientationEventListener(this) {

        @Override
        public void onOrientationChanged(int orientation) {
            MainActivity.this.mainUI.onOrientationChanged(orientation);
        }
    };
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting orientation event listener: " + (System.currentTimeMillis() - debug_time));
    // set up gallery button long click
    View galleryButton = findViewById(R.id.gallery);
    galleryButton.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // preview.showToast(null, "Long click");
            longClickedGallery();
            return true;
        }
    });
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting gallery long click listener: " + (System.currentTimeMillis() - debug_time));
    // listen for gestures
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after creating gesture detector: " + (System.currentTimeMillis() - debug_time));
    // set up listener to handle immersive mode options
    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
            if (!usingKitKatImmersiveMode())
                return;
            if (MyDebug.LOG)
                Log.d(TAG, "onSystemUiVisibilityChange: " + visibility);
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                if (MyDebug.LOG)
                    Log.d(TAG, "system bars now visible");
                // The system bars are visible. Make any desired
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
                mainUI.setImmersiveMode(false);
                setImmersiveTimer();
            } else {
                if (MyDebug.LOG)
                    Log.d(TAG, "system bars now NOT visible");
                // The system bars are NOT visible. Make any desired
                // adjustments to your UI, such as hiding the action bar or
                // other navigational controls.
                mainUI.setImmersiveMode(true);
            }
        }
    });
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after setting immersive mode listener: " + (System.currentTimeMillis() - debug_time));
    // show "about" dialog for first time use; also set some per-device defaults
    boolean has_done_first_time = sharedPreferences.contains(PreferenceKeys.FirstTimePreferenceKey);
    if (MyDebug.LOG)
        Log.d(TAG, "has_done_first_time: " + has_done_first_time);
    if (!has_done_first_time) {
        setDeviceDefaults();
    }
    if (!has_done_first_time) {
        if (!is_test) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle(R.string.app_name);
            alertDialog.setMessage(R.string.intro_text);
            alertDialog.setPositiveButton(android.R.string.ok, null);
            alertDialog.setNegativeButton(R.string.preference_online_help, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (MyDebug.LOG)
                        Log.d(TAG, "online help");
                    launchOnlineHelp();
                }
            });
            alertDialog.show();
        }
        setFirstTimeFlag();
    }
    {
        // handle What's New dialog
        int version_code = -1;
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            version_code = pInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            if (MyDebug.LOG)
                Log.d(TAG, "NameNotFoundException exception trying to get version number");
            e.printStackTrace();
        }
        if (version_code != -1) {
            int latest_version = sharedPreferences.getInt(PreferenceKeys.LatestVersionPreferenceKey, 0);
            if (MyDebug.LOG) {
                Log.d(TAG, "version_code: " + version_code);
                Log.d(TAG, "latest_version: " + latest_version);
            }
            final boolean whats_new_enabled = false;
            if (whats_new_enabled) {
                final boolean force_whats_new = false;
                // don't show What's New if this is the first time the user has run
                if (has_done_first_time && (force_whats_new || version_code > latest_version)) {
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                    alertDialog.setTitle(R.string.whats_new);
                    alertDialog.setMessage(R.string.whats_new_text);
                    alertDialog.setPositiveButton(android.R.string.ok, null);
                    alertDialog.setNegativeButton(R.string.donate, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (MyDebug.LOG)
                                Log.d(TAG, "donate");
                            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(MainActivity.getDonateLink()));
                            startActivity(browserIntent);
                        }
                    });
                    alertDialog.show();
                }
            }
            // we set the latest_version whether or not the dialog is shown - if we showed the irst time dialog, we don't
            // want to then show the What's New dialog next time we run!
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt(PreferenceKeys.LatestVersionPreferenceKey, version_code);
            editor.apply();
        }
    }
    setModeFromIntents(savedInstanceState);
    // load icons
    preloadIcons(R.array.flash_icons);
    preloadIcons(R.array.focus_mode_icons);
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: time after preloading icons: " + (System.currentTimeMillis() - debug_time));
    // initialise text to speech engine
    textToSpeechSuccess = false;
    // run in separate thread so as to not delay startup time
    new Thread(new Runnable() {

        public void run() {
            textToSpeech = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int status) {
                    if (MyDebug.LOG)
                        Log.d(TAG, "TextToSpeech initialised");
                    if (status == TextToSpeech.SUCCESS) {
                        textToSpeechSuccess = true;
                        if (MyDebug.LOG)
                            Log.d(TAG, "TextToSpeech succeeded");
                    } else {
                        if (MyDebug.LOG)
                            Log.d(TAG, "TextToSpeech failed");
                    }
                }
            });
        }
    }).start();
    if (MyDebug.LOG)
        Log.d(TAG, "onCreate: total time for Activity startup: " + (System.currentTimeMillis() - debug_time));
}
Also used : MainUI(net.sourceforge.opencamera.UI.MainUI) AlertDialog(android.app.AlertDialog) OrientationEventListener(android.view.OrientationEventListener) DialogInterface(android.content.DialogInterface) GestureDetector(android.view.GestureDetector) ActivityManager(android.app.ActivityManager) TextToSpeech(android.speech.tts.TextToSpeech) WindowManager(android.view.WindowManager) SharedPreferences(android.content.SharedPreferences) PackageInfo(android.content.pm.PackageInfo) Preview(net.sourceforge.opencamera.Preview.Preview) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) View(android.view.View) SuppressLint(android.annotation.SuppressLint)

Example 67 with TextToSpeech

use of android.speech.tts.TextToSpeech in project android_packages_apps_Settings by LineageOS.

the class TextToSpeechSettings method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.tts_settings);
    addEngineSpecificSettings();
    mDemoStrings = getResources().getStringArray(R.array.tts_demo_strings);
    setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
    mEnableDemo = false;
    mTtsStarted = false;
    Locale currentLocale = Locale.getDefault();
    mDefaultLanguage = currentLocale.getISO3Language();
    mDefaultCountry = currentLocale.getISO3Country();
    mDefaultLocVariant = currentLocale.getVariant();
    mTts = new TextToSpeech(this, this);
}
Also used : Locale(java.util.Locale) TextToSpeech(android.speech.tts.TextToSpeech)

Example 68 with TextToSpeech

use of android.speech.tts.TextToSpeech in project aware-client by denzilferreira.

the class Aware_TTS method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_AWARE_TTS_SPEAK);
    registerReceiver(awareTTS, filter);
    tts = new TextToSpeech(this, this);
}
Also used : IntentFilter(android.content.IntentFilter) TextToSpeech(android.speech.tts.TextToSpeech)

Example 69 with TextToSpeech

use of android.speech.tts.TextToSpeech in project robolectric by robolectric.

the class ShadowTextToSpeechTest method speak_withUtteranceId_shouldReturnSpokenText.

@Test
@Config(minSdk = LOLLIPOP)
public void speak_withUtteranceId_shouldReturnSpokenText() {
    TextToSpeech textToSpeech = new TextToSpeech(activity, result -> {
    });
    textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null, "TTSEnable");
    assertThat(shadowOf(textToSpeech).getLastSpokenText()).isEqualTo("Hello");
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 70 with TextToSpeech

use of android.speech.tts.TextToSpeech in project robolectric by robolectric.

the class ShadowTextToSpeechTest method getLastSpokenText_shouldReturnMostRecentText.

@Test
public void getLastSpokenText_shouldReturnMostRecentText() {
    TextToSpeech textToSpeech = new TextToSpeech(activity, result -> {
    });
    textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null);
    textToSpeech.speak("Hi", TextToSpeech.QUEUE_FLUSH, null);
    assertThat(shadowOf(textToSpeech).getLastSpokenText()).isEqualTo("Hi");
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech) Test(org.junit.Test)

Aggregations

TextToSpeech (android.speech.tts.TextToSpeech)103 Test (org.junit.Test)34 Locale (java.util.Locale)18 TtsEngines (android.speech.tts.TtsEngines)15 ActivityNotFoundException (android.content.ActivityNotFoundException)12 Intent (android.content.Intent)12 ContentResolver (android.content.ContentResolver)10 MissingResourceException (java.util.MissingResourceException)10 IntentFilter (android.content.IntentFilter)8 TextView (android.widget.TextView)7 Config (org.robolectric.annotation.Config)7 BluetoothAdapter (android.bluetooth.BluetoothAdapter)6 Handler (android.os.Handler)6 UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)6 ArrayAdapter (android.widget.ArrayAdapter)6 Context (android.content.Context)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Bundle (android.os.Bundle)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3