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);
}
}
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();
});
}
}
Aggregations