summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/LoginActivity.java
blob: 85092e9482fc3ed68a1027c0a73cf7402a8c6210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package net.brokenfox.vulpirc;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements Connection.LoginStateListener {
	private View mContainerLoginForm;
	private View mContainerLoginStatus;
	private TextView mLoginStatus;
	private EditText mInputUsername, mInputPassword;
	private EditText mInputHostname, mInputPort;
	private CheckBox mCheckUseTls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

	    mContainerLoginForm = findViewById(R.id.containerLoginForm);
	    mContainerLoginStatus = findViewById(R.id.containerLoginStatus);

	    mLoginStatus = (TextView)findViewById(R.id.loginStatus);

	    mInputUsername = (EditText)findViewById(R.id.inputUsername);
	    mInputPassword = (EditText)findViewById(R.id.inputPassword);
	    mInputHostname = (EditText)findViewById(R.id.inputHostname);
	    mInputPort = (EditText)findViewById(R.id.inputPort);

	    mCheckUseTls = (CheckBox)findViewById(R.id.checkUseTls);

	    findViewById(R.id.buttonConnect).setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View view) {
			    tryAndConnect();
		    }
	    });

	    findViewById(R.id.buttonCancel).setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View view) {
			    tryAndCancel();
		    }
	    });

	    doBindService();
    }

	@Override
	protected void onDestroy() {
		doUnbindService();
		super.onDestroy();
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }



	// Junk.

	private boolean mIsConnecting = false;

	private void tryAndConnect() {
		if (mIsConnecting)
			return;

		mInputUsername.setError(null);
		mInputPassword.setError(null);
		mInputHostname.setError(null);
		mInputPort.setError(null);

		String username = mInputUsername.getText().toString();
		String password = mInputPassword.getText().toString();
		String hostname = mInputHostname.getText().toString();
		String portStr = mInputPort.getText().toString();
		int port = -1;
		if (!TextUtils.isEmpty(portStr)) {
			port = Integer.parseInt(portStr);
		}

		boolean usesTls = mCheckUseTls.isChecked();

		View errorView = null;

		//if (TextUtils.isEmpty(password)) {
		//	mInputPassword.setError("Required");
		//	errorView = mInputPassword;
		//}
		if (TextUtils.isEmpty(username)) {
			mInputUsername.setError("Required");
			errorView = mInputUsername;
		}

		if (port < 0 || port > 65535) {
			mInputPort.setError("Invalid");
			errorView = mInputPort;
		}
		if (TextUtils.isEmpty(hostname)) {
			mInputHostname.setError("Required");
			errorView = mInputHostname;
		}


		if (errorView != null) {
			errorView.requestFocus();
		} else {
			mIsConnecting = true;

			mLoginStatus.setText("Starting up...");

			showProgress(true);

			// start service so it'll remain around...
			Intent i = new Intent(this, IRCService.class);
			startService(i);

			Connection.get().connect(hostname, port, usesTls, username, password);
		}
	}

	private void tryAndCancel() {
		Connection.get().disconnect();
	}



	public void handleLoginStateChanged() {
		switch (Connection.get().getSocketState()) {
			case DISCONNECTED:
				mLoginStatus.setText("Disconnected.");
				String details = Connection.get().getLoginError();
				if (details != null) {
					Toast.makeText(this, details, Toast.LENGTH_LONG).show();
					Connection.get().clearLoginError();
				}
				showProgress(false);
				mIsConnecting = false;
				break;
			case DISCONNECTING:
				mLoginStatus.setText("Disconnecting...");
				break;
			case CONNECTING:
				mLoginStatus.setText("Connecting...");
				break;
			case CONNECTED:
				if (Connection.get().getSessionActive()) {
					mLoginStatus.setText("Connected!");

					completeLoginScreen();
				} else {
					mLoginStatus.setText("Logging in...");
				}
				break;
		}
	}


	private void showProgress(final boolean show) {
		// Straight from the LoginActivity template!
		// .... Almost.

		int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

		mContainerLoginStatus.setVisibility(View.VISIBLE);
		mContainerLoginStatus.animate()
				.setDuration(shortAnimTime)
				.alpha(show ? 1 : 0)
				.setListener(new AnimatorListenerAdapter() {
					@Override
					public void onAnimationEnd(Animator animation) {
						mContainerLoginStatus.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
					}
				});

		mContainerLoginForm.setVisibility(View.VISIBLE);
		mContainerLoginForm.animate()
				.setDuration(shortAnimTime)
				.alpha(show ? 0 : 1)
				.setListener(new AnimatorListenerAdapter() {
					@Override
					public void onAnimationEnd(Animator animation) {
						mContainerLoginForm.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
					}
				});
	}


	private void completeLoginScreen() {
		if (!isFinishing()) {
			Intent i = new Intent(LoginActivity.this, MainActivity.class);
			//i.setFlags(i.getFlags() | 0x2000); // enables halo intent, maybe?
			startActivity(i);
			finish();
		}
	}


	// SERVICE JUNK
	// Not happy about having this both here and in MainActivity.
	// Moof.
	private IRCService mService = null;

	private final ServiceConnection mServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			Log.i("VulpIRC", "[LoginActivity connected to IRCService]");

			mService = ((IRCService.LocalBinder)iBinder).getService();
			Connection.get().registerLoginStateListener(LoginActivity.this);

			if (Connection.get().getSessionActive()) {
				// We have a session, just jump right into the client
				completeLoginScreen();
			} else {
				handleLoginStateChanged();
				if (Connection.get().getSocketState() == BaseConn.SocketState.DISCONNECTED) {
					mContainerLoginForm.setVisibility(View.VISIBLE);
				} else {
					mContainerLoginStatus.setVisibility(View.VISIBLE);
				}
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			Log.i("VulpIRC", "[LoginActivity disconnected from IRCService]");

			Connection.get().deregisterLoginStateListener(LoginActivity.this);
			mService = null;
		}
	};

	private void doBindService() {
		Log.i("VulpIRC", "[LoginActivity binding to IRCService...]");

		Intent i = new Intent(this, IRCService.class);
		bindService(i, mServiceConnection, BIND_AUTO_CREATE);
	}

	private void doUnbindService() {
		Log.i("VulpIRC", "[LoginActivity unbinding from IRCService...]");

		unbindService(mServiceConnection);
	}}