-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginActivity.java
More file actions
158 lines (116 loc) · 5.42 KB
/
LoginActivity.java
File metadata and controls
158 lines (116 loc) · 5.42 KB
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
//
//
// Copyright 2017 Kii Corporation
// http://kii.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
package com.kii.world;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.kii.cloud.storage.KiiUser;
import com.kii.cloud.storage.callback.KiiUserCallBack;
import com.kii.cloud.storage.exception.app.AppException;
public class LoginActivity extends Activity {
private static final String TAG = "LoginActivity";
// Define the UI elements.
private TextView mUsernameField;
private TextView mPasswordField;
private ProgressDialog mProgress;
// Called by the "Log In" button.
public void handleLogin(View v) {
// Show a progress dialog.
mProgress = ProgressDialog.show(LoginActivity.this, "", "Signing in...", true);
// Get the username and password from the UI.
String username = mUsernameField.getText().toString();
String password = mPasswordField.getText().toString();
Log.v(TAG, "Logging in: " + username + ":" + password);
// Authenticate the user asynchronously.
KiiUser.logIn(new KiiUserCallBack() {
// Catch the result from the callback method.
public void onLoginCompleted(int token, KiiUser user, Exception e) {
// Hide the progress dialog.
mProgress.cancel();
// Check for an exception. The request was successfully processed if e==null.
if(e == null) {
// Tell the console and the user that the login was successful.
Log.v(TAG, "Logged in: " + user.toString());
Toast.makeText(LoginActivity.this, "User authenticated!", Toast.LENGTH_SHORT).show();
// Go to the main screen.
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
// A failure occurred when processing the request.
else {
// Tell the console and the user that the login failed.
Log.v(TAG, "Error authenticating: " + e.getLocalizedMessage());
Toast.makeText(LoginActivity.this, "Error authenticating: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
}, username, password);
}
// Called by the "Sign Up" button.
public void handleSignUp(View v) {
// Show a progress dialog.
mProgress = ProgressDialog.show(LoginActivity.this, "", "Signing up...", true);
// Get the username and password from the UI.
String username = mUsernameField.getText().toString();
String password = mPasswordField.getText().toString();
Log.v(TAG, "Registering: " + username + ":" + password);
// Create a KiiUser object.
try {
KiiUser user = KiiUser.createWithUsername(username);
// Register the user asynchronously.
user.register(new KiiUserCallBack() {
// Catch the result from the callback method.
public void onRegisterCompleted(int token, KiiUser user, Exception e) {
// Hide the progress dialog.
mProgress.cancel();
// Check for an exception. The request was successfully processed if e==null.
if(e == null) {
// Tell the console and the user that the registration was successful.
Log.v(TAG, "Registered: " + user.toString());
Toast.makeText(LoginActivity.this, "User registered!", Toast.LENGTH_SHORT).show();
// Go to the main screen.
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
// A failure occurred when processing the request.
else {
// Tell the console and the user that the registration failed.
Log.v(TAG, "Error registering: " + e.getLocalizedMessage());
Toast.makeText(LoginActivity.this, "Error Registering: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
}, password);
} catch(Exception e) {
mProgress.cancel();
Toast.makeText(this, "Error signing up: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Link the variables to the UI elements.
mUsernameField = (TextView) findViewById(R.id.username_field);
mPasswordField = (TextView) findViewById(R.id.password_field);
}
}