Skip to content
This repository has been archived by the owner on Aug 7, 2020. It is now read-only.

Commit

Permalink
Completely rewrite login process & UI, fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Mar 1, 2017
1 parent 0e649b1 commit f133c5d
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 153 deletions.
89 changes: 46 additions & 43 deletions app/src/main/java/io/homeassistant/android/HassActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.homeassistant.android;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
Expand All @@ -17,7 +16,7 @@
import android.support.customtabs.CustomTabsIntent;
import android.support.customtabs.CustomTabsServiceConnection;
import android.support.customtabs.CustomTabsSession;
import android.support.v7.app.AlertDialog;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -26,10 +25,12 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;

import io.homeassistant.android.ui.LoginView;

import static io.homeassistant.android.Common.PREF_HASS_URL_KEY;


Expand All @@ -42,6 +43,8 @@ public class HassActivity extends AppCompatActivity {
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((HassService.HassBinder) binder).getService();
service.setActivityHandler(communicationHandler);
// Make sure that service is connected, if not it'll re-attempt
service.connect();
}

@Override
Expand All @@ -51,8 +54,6 @@ public void onServiceDisconnected(ComponentName name) {
}
};
private SharedPreferences prefs;

private ViewAdapter viewAdapter = new ViewAdapter(this);
private CustomTabsSession customTabsSession;
private final CustomTabsServiceConnection chromeConnection = new CustomTabsServiceConnection() {
@Override
Expand All @@ -77,6 +78,11 @@ public void onServiceDisconnected(ComponentName name) {
}
};

private ViewAdapter viewAdapter = new ViewAdapter(this);
private FrameLayout rootView;
private CoordinatorLayout mainLayout;
private LoginView loginLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -92,13 +98,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
viewRecycler.setLayoutManager(new LinearLayoutManager(this));
viewRecycler.setItemAnimator(new DefaultItemAnimator());
viewRecycler.setAdapter(viewAdapter);

rootView = (FrameLayout) findViewById(R.id.root);
mainLayout = (CoordinatorLayout) findViewById(R.id.main_coordinator);

if (Utils.getUrl(this).isEmpty() || Utils.getPassword(this).isEmpty()) {
mainLayout.setVisibility(View.GONE);
addLoginLayout();
}
}

private void addLoginLayout() {
loginLayout = (LoginView) getLayoutInflater().inflate(R.layout.custom_login, rootView, true).findViewById(R.id.login_layout);
}

@Override
protected void onStart() {
super.onStart();
if (service != null) {
// Check connection / Reconnect when activity was hidden/inactive for a short time
// Make sure that service is connected, if not it'll re-attempt
service.connect();
}
}
Expand Down Expand Up @@ -140,39 +158,24 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

private void obtainURL() {
final View dialogView = getLayoutInflater().inflate(R.layout.dialog_ip, null);
final EditText input = (EditText) dialogView.findViewById(R.id.ip_input);
input.setText(prefs.getString(PREF_HASS_URL_KEY, ""));
new AlertDialog.Builder(HassActivity.this)
.setView(dialogView)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().putString(PREF_HASS_URL_KEY, input.getText().toString()).apply();
service.connect();
}
})
.create()
.show();
public void attemptLogin() {
service.connect();
}

private void obtainPassword() {
final View dialogView = getLayoutInflater().inflate(R.layout.dialog_password, null);
final EditText input = (EditText) dialogView.findViewById(R.id.password_input);
new AlertDialog.Builder(HassActivity.this)
.setView(dialogView)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().putString(Common.PREF_HASS_PASSWORD_KEY, input.getText().toString()).apply();
service.authenticate();
}
})
.create()
.show();
public void loginSuccess() {
if (loginLayout != null) {
rootView.removeView(loginLayout);
loginLayout = null;
mainLayout.setVisibility(View.VISIBLE);
}
}

public void loginFailed() {
if (loginLayout != null) {
loginLayout.showLoginError();
} else {
addLoginLayout();
}
}

private void updateStates() {
Expand All @@ -189,8 +192,8 @@ public boolean send(String message) {

public static class CommunicationHandler extends Handler {

public static final int MESSAGE_OBTAIN_URL = 0x04;
public static final int MESSAGE_OBTAIN_PASSWORD = 0x08;
public static final int MESSAGE_LOGIN_SUCCESS = 0x04;
public static final int MESSAGE_LOGIN_FAILED = 0x08;
public static final int MESSAGE_STATES_AVAILABLE = 0x10;

private final WeakReference<HassActivity> activity;
Expand All @@ -205,11 +208,11 @@ public void handleMessage(Message msg) {
return;
}
switch (msg.what) {
case MESSAGE_OBTAIN_URL:
activity.get().obtainURL();
case MESSAGE_LOGIN_SUCCESS:
activity.get().loginSuccess();
break;
case MESSAGE_OBTAIN_PASSWORD:
activity.get().obtainPassword();
case MESSAGE_LOGIN_FAILED:
activity.get().loginFailed();
break;
case MESSAGE_STATES_AVAILABLE:
activity.get().updateStates();
Expand Down
Loading

0 comments on commit f133c5d

Please sign in to comment.