Search in sources :

Example 1 with ServerDiscovery

use of de.vier_bier.habpanelviewer.openhab.ServerDiscovery in project habpanelviewer by vbier.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventBus.getDefault().register(this);
    try {
        ConnectionUtil.initialize(this);
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, R.string.sslFailed, Toast.LENGTH_LONG).show();
    }
    setContentView(R.layout.activity_main);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    // inflate navigation header to make sure the textview holding the connection text is created
    NavigationView navigationView = findViewById(R.id.nav_view);
    LinearLayout navHeader = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
    navigationView.addHeaderView(navHeader);
    navigationView.setNavigationItemSelectedListener(this);
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    if (mServerConnection == null) {
        mServerConnection = new ServerConnection(this);
        mServerConnection.addConnectionListener(this);
    }
    if (mConnections == null) {
        mConnections = new ConnectionStatistics(this);
    }
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mFlashService == null) {
            try {
                mFlashService = new FlashHandler(this, (CameraManager) getSystemService(Context.CAMERA_SERVICE));
            } catch (CameraAccessException | IllegalAccessException e) {
                Log.d(TAG, "Could not create flash controller");
            }
        }
        if (mCam == null) {
            mCam = new Camera(this, findViewById(R.id.previewView), prefs);
        }
        if (mMotionVisualizer == null) {
            int scaledSize = getResources().getDimensionPixelSize(R.dimen.motionFontSize);
            final SurfaceView motionView = findViewById(R.id.motionView);
            mMotionVisualizer = new MotionVisualizer(motionView, navigationView, prefs, mCam.getSensorOrientation(), scaledSize);
            mMotionDetector = new MotionDetector(this, mCam, mMotionVisualizer, mServerConnection);
        }
    }
    if (mDiscovery == null) {
        mDiscovery = new ServerDiscovery((NsdManager) getSystemService(Context.NSD_SERVICE));
    }
    if (prefs.getBoolean("pref_first_start", true)) {
        SharedPreferences.Editor editor1 = prefs.edit();
        editor1.putBoolean("pref_first_start", false);
        editor1.putString("pref_app_version", BuildConfig.VERSION_NAME);
        editor1.apply();
        final String startText = ResourcesUtil.fetchFirstStart(this);
        UiUtil.showScrollDialog(this, "HABPanelViewer", getString(R.string.welcome), startText);
        if (prefs.getString("pref_server_url", "").isEmpty()) {
            mDiscovery.discover(new ServerDiscovery.DiscoveryListener() {

                @Override
                public void found(String serverUrl) {
                    SharedPreferences.Editor editor1 = prefs.edit();
                    editor1.putString("pref_server_url", serverUrl);
                    editor1.apply();
                }

                @Override
                public void notFound() {
                }
            }, true, true);
        }
    } else {
        // make sure old server url preference is used in case it is still set
        if (prefs.getString("pref_server_url", "").isEmpty()) {
            final String oldUrl = prefs.getString("pref_url", "");
            if (!oldUrl.isEmpty()) {
                SharedPreferences.Editor editor1 = prefs.edit();
                editor1.putString("pref_server_url", oldUrl);
                editor1.remove("pref_url");
                editor1.apply();
            }
        }
        String lastVersion = prefs.getString("pref_app_version", "0.9.2");
        if (!BuildConfig.VERSION_NAME.equals(lastVersion)) {
            SharedPreferences.Editor editor1 = prefs.edit();
            editor1.putString("pref_app_version", BuildConfig.VERSION_NAME);
            editor1.apply();
            final String relText = ResourcesUtil.fetchReleaseNotes(this, lastVersion);
            UiUtil.showScrollDialog(this, getString(R.string.updated), getString(R.string.updatedText), relText);
        }
    }
    if (mBatteryMonitor == null) {
        mBatteryMonitor = new BatteryMonitor(this, mServerConnection);
    }
    if (mVolumeMonitor == null) {
        mVolumeMonitor = new VolumeMonitor(this, (AudioManager) getSystemService(Context.AUDIO_SERVICE), mServerConnection);
    }
    if (mConnectedReporter == null) {
        mConnectedReporter = new ConnectedIndicator(this, mServerConnection);
    }
    SensorManager m = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (mProximityMonitor == null && getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_PROXIMITY)) {
        try {
            mProximityMonitor = new ProximityMonitor(this, m, mServerConnection);
        } catch (SensorMissingException e) {
            Log.d(TAG, "Could not create proximity monitor");
        }
    }
    if (mBrightnessMonitor == null && getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT)) {
        try {
            mBrightnessMonitor = new BrightnessMonitor(this, m, mServerConnection);
        } catch (SensorMissingException e) {
            Log.d(TAG, "Could not create brightness monitor");
        }
    }
    if (mPressureMonitor == null && getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_BAROMETER)) {
        try {
            mPressureMonitor = new PressureMonitor(this, m, mServerConnection);
        } catch (SensorMissingException e) {
            Log.d(TAG, "Could not create pressure monitor");
        }
    }
    if (mTemperatureMonitor == null && getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_AMBIENT_TEMPERATURE)) {
        try {
            mTemperatureMonitor = new TemperatureMonitor(this, m, mServerConnection);
        } catch (SensorMissingException e) {
            Log.d(TAG, "Could not create temperature monitor");
        }
    }
    if (mCommandQueue == null) {
        ScreenHandler mScreenHandler = new ScreenHandler((PowerManager) getSystemService(POWER_SERVICE), this);
        mCommandQueue = new CommandQueue(this, mServerConnection);
        mCommandQueue.addHandler(new InternalCommandHandler(this, mMotionDetector, mServerConnection));
        mCommandQueue.addHandler(new AdminHandler(this));
        mCommandQueue.addHandler(new BluetoothHandler(this, (BluetoothManager) getSystemService(BLUETOOTH_SERVICE)));
        mCommandQueue.addHandler(mScreenHandler);
        mCommandQueue.addHandler(new VolumeHandler(this, (AudioManager) getSystemService(Context.AUDIO_SERVICE)));
        if (mFlashService != null) {
            mCommandQueue.addHandler(mFlashService);
        }
    }
    mRestartCount = getIntent().getIntExtra("restartCount", 0);
    showInitialToastMessage(mRestartCount);
    mTextView = navHeader.findViewById(R.id.textView);
    mWebView = findViewById(R.id.activity_main_webview);
    mWebView.initialize(new IConnectionListener() {

        @Override
        public void connected(String url) {
        }

        @Override
        public void disconnected() {
            if (prefs.getBoolean("pref_track_browser_connection", false)) {
                mServerConnection.reconnect();
            }
        }
    });
    mCommandQueue.addHandler(new WebViewHandler(mWebView));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(projectionManager.createScreenCaptureIntent(), ScreenCapturer.REQUEST_MEDIA_PROJECTION);
    }
}
Also used : NavigationView(android.support.design.widget.NavigationView) FlashHandler(de.vier_bier.habpanelviewer.command.FlashHandler) ConnectedIndicator(de.vier_bier.habpanelviewer.reporting.ConnectedIndicator) MotionVisualizer(de.vier_bier.habpanelviewer.reporting.motion.MotionVisualizer) CommandQueue(de.vier_bier.habpanelviewer.command.CommandQueue) BluetoothManager(android.bluetooth.BluetoothManager) ServerDiscovery(de.vier_bier.habpanelviewer.openhab.ServerDiscovery) BatteryMonitor(de.vier_bier.habpanelviewer.reporting.BatteryMonitor) CameraAccessException(android.hardware.camera2.CameraAccessException) AudioManager(android.media.AudioManager) AdminHandler(de.vier_bier.habpanelviewer.command.AdminHandler) IMotionDetector(de.vier_bier.habpanelviewer.reporting.motion.IMotionDetector) MotionDetector(de.vier_bier.habpanelviewer.reporting.motion.MotionDetector) ScreenHandler(de.vier_bier.habpanelviewer.command.ScreenHandler) Camera(de.vier_bier.habpanelviewer.reporting.motion.Camera) NsdManager(android.net.nsd.NsdManager) SurfaceView(android.view.SurfaceView) VolumeHandler(de.vier_bier.habpanelviewer.command.VolumeHandler) WebViewHandler(de.vier_bier.habpanelviewer.command.WebViewHandler) SharedPreferences(android.content.SharedPreferences) BluetoothHandler(de.vier_bier.habpanelviewer.command.BluetoothHandler) BrightnessMonitor(de.vier_bier.habpanelviewer.reporting.BrightnessMonitor) ServerConnection(de.vier_bier.habpanelviewer.openhab.ServerConnection) CameraManager(android.hardware.camera2.CameraManager) ProximityMonitor(de.vier_bier.habpanelviewer.reporting.ProximityMonitor) SensorMissingException(de.vier_bier.habpanelviewer.reporting.SensorMissingException) CameraException(de.vier_bier.habpanelviewer.reporting.motion.CameraException) CameraAccessException(android.hardware.camera2.CameraAccessException) Point(android.graphics.Point) PressureMonitor(de.vier_bier.habpanelviewer.reporting.PressureMonitor) MediaProjectionManager(android.media.projection.MediaProjectionManager) SensorManager(android.hardware.SensorManager) IConnectionListener(de.vier_bier.habpanelviewer.openhab.IConnectionListener) TemperatureMonitor(de.vier_bier.habpanelviewer.reporting.TemperatureMonitor) SensorMissingException(de.vier_bier.habpanelviewer.reporting.SensorMissingException) VolumeMonitor(de.vier_bier.habpanelviewer.reporting.VolumeMonitor) InternalCommandHandler(de.vier_bier.habpanelviewer.command.InternalCommandHandler) LinearLayout(android.widget.LinearLayout)

Example 2 with ServerDiscovery

use of de.vier_bier.habpanelviewer.openhab.ServerDiscovery in project habpanelviewer by vbier.

the class ServerPreference method onAddEditTextToDialogView.

@Override
protected void onAddEditTextToDialogView(View dialogView, final EditText editText) {
    super.onAddEditTextToDialogView(dialogView, editText);
    final ViewGroup container = (ViewGroup) editText.getParent();
    if (container != null) {
        final CheckBox cb = new CheckBox(getContext());
        cb.setText(R.string.discoverHttps);
        cb.setChecked(true);
        final Button b = new Button(getContext());
        b.setText(R.string.discoverServer);
        final TextView ex = new TextView(getContext());
        ex.setText("  " + getContext().getResources().getString(R.string.pref_url_example));
        ex.setTextColor(Color.GRAY);
        container.addView(ex, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        container.addView(b, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        container.addView(cb, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        b.setOnClickListener(view -> {
            b.setEnabled(false);
            ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
            AsyncTask discoveryTask = new AsyncTask() {

                @Override
                protected Object doInBackground(Object[] objects) {
                    ServerDiscovery mDiscovery = new ServerDiscovery((NsdManager) getContext().getSystemService(Context.NSD_SERVICE));
                    mDiscovery.discover(new ServerDiscovery.DiscoveryListener() {

                        @Override
                        public void found(final String serverUrl) {
                            b.post(() -> editText.setText(serverUrl));
                        }

                        @Override
                        public void notFound() {
                            b.post(() -> Toast.makeText(getContext(), getContext().getString(R.string.serverNotFound), Toast.LENGTH_LONG).show());
                        }
                    }, !cb.isChecked(), cb.isChecked());
                    return null;
                }

                @Override
                protected void onPostExecute(Object o) {
                    b.setEnabled(true);
                    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(true);
                }
            };
            discoveryTask.execute();
        });
    }
}
Also used : Button(android.widget.Button) ViewGroup(android.view.ViewGroup) CheckBox(android.widget.CheckBox) AsyncTask(android.os.AsyncTask) TextView(android.widget.TextView) ServerDiscovery(de.vier_bier.habpanelviewer.openhab.ServerDiscovery)

Aggregations

ServerDiscovery (de.vier_bier.habpanelviewer.openhab.ServerDiscovery)2 BluetoothManager (android.bluetooth.BluetoothManager)1 SharedPreferences (android.content.SharedPreferences)1 Point (android.graphics.Point)1 SensorManager (android.hardware.SensorManager)1 CameraAccessException (android.hardware.camera2.CameraAccessException)1 CameraManager (android.hardware.camera2.CameraManager)1 AudioManager (android.media.AudioManager)1 MediaProjectionManager (android.media.projection.MediaProjectionManager)1 NsdManager (android.net.nsd.NsdManager)1 AsyncTask (android.os.AsyncTask)1 NavigationView (android.support.design.widget.NavigationView)1 SurfaceView (android.view.SurfaceView)1 ViewGroup (android.view.ViewGroup)1 Button (android.widget.Button)1 CheckBox (android.widget.CheckBox)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 AdminHandler (de.vier_bier.habpanelviewer.command.AdminHandler)1 BluetoothHandler (de.vier_bier.habpanelviewer.command.BluetoothHandler)1