Search in sources :

Example 1 with MainUI

use of net.sourceforge.opencamera.UI.MainUI 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)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 ActivityManager (android.app.ActivityManager)1 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageInfo (android.content.pm.PackageInfo)1 RecognizerIntent (android.speech.RecognizerIntent)1 TextToSpeech (android.speech.tts.TextToSpeech)1 GestureDetector (android.view.GestureDetector)1 OrientationEventListener (android.view.OrientationEventListener)1 View (android.view.View)1 WindowManager (android.view.WindowManager)1 Preview (net.sourceforge.opencamera.Preview.Preview)1 MainUI (net.sourceforge.opencamera.UI.MainUI)1