· 9 years ago · Nov 25, 2016, 09:04 PM
1import android.content.ContentValues;
2 import android.content.Context;
3 import android.database.Cursor;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteOpenHelper;
6 import android.util.Log;
7
8 public class DatabaseHelper extends SQLiteOpenHelper{
9 // Database Version
10
11private static final int DATABASE_VERSION = 1;
12
13// Database Name
14private static final String DATABASE_NAME = "contacts.db";
15// Table name
16
17private static final String TABLE_NAME = "contacts"; //Table for Sign Up Activity
18
19
20// Sign Up Activity -> Table Columns names
21
22
23
24
25private static final String COLUMN_ID = "id";
26private static final String COLUMN_NAME = "name";
27private static final String COLUMN_UNAME = "uname";
28private static final String COLUMN_ADDRESS = "address";
29private static final String COLUMN_EMAIL = "email";
30private static final String COLUMN_MOBILE_NO= "mobile_no";
31private static final String COLUMN_PASSWORD= "password";
32private static final String COLUMN_RE_PASSWORD = "re_password";
33
34SQLiteDatabase db;
35
36//Create Query
37
38//Sign Up table create statement
39
40 private static final String TABLE_CREATE = "create table contacts(id int primary key not null,name text not null, uname text not null,address text not null,email text not null, mobile_no text not null, password text not null, re_password text not null);";
41//Create Database
42public DatabaseHelper(Context context) {
43 super(context, DATABASE_NAME, null, DATABASE_VERSION);
44 Log.d("Database Helper", "Database created");
45
46}
47
48//Create table
49@Override
50public void onCreate(SQLiteDatabase db){
51 db.execSQL(TABLE_CREATE);
52 Log.d("Database Helper", "Table created");
53 this.db = db;
54}
55
56//Insert data into tables.
57//Insert the detail of Sign Activity in contact table.
58public void insertContact(Contact c){
59
60 db = this.getWritableDatabase();
61 ContentValues values = new ContentValues();
62
63 String query = "select * from contacts";
64 Cursor cursor = db.rawQuery(query,null);
65 int count = cursor.getCount();
66 values.put(COLUMN_ID,count);
67 values.put(COLUMN_NAME,c.getName());
68 values.put(COLUMN_UNAME, c.getUname());
69 values.put(COLUMN_ADDRESS,c.getAddress());
70 values.put(COLUMN_EMAIL, c.getEmail());
71 values.put(COLUMN_MOBILE_NO,c.getMobile_no());
72 values.put(COLUMN_PASSWORD,c.getPassword());
73 values.put(COLUMN_RE_PASSWORD,c.getRe_password());
74 db.insert(TABLE_NAME, null, values);
75 //Log.v("Data: " , "id: " + COLUMN_ID + " name: " +COLUMN_NAME + " uname: " + COLUMN_UNAME + " Address: " +COLUMN_ADDRESS + " Email: " +COLUMN_ADDRESS +" Modile: " +COLUMN_MOBILE_NO +" Password: " +COLUMN_PASSWORD + " re_password : " +COLUMN_RE_PASSWORD );
76 Log.d("Database Helper", "One raw inserted");
77 db.close();
78}
79
80//Search the password
81public String searchPassword(String username){
82 Log.v("Detahelper", "uname = "+username);
83 db = this.getReadableDatabase();
84 String query = "select uname, password from " +TABLE_NAME;
85 Cursor cursor = db.rawQuery(query,null);
86 String usrname, pass;
87 pass = "not found";
88 if(cursor.moveToFirst()){
89 do{
90 usrname = cursor.getString(0); //0 represent the value in first column
91
92 if(usrname.equals(username)){
93 pass = cursor.getString(1); //1 represent the value in second column
94 }
95
96 }while (cursor.moveToNext());
97 }
98 return pass;
99}
100@Override
101public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
102 // Drop older table if existed
103 db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
104 // Creating tables again
105 onCreate(db);
106}
107
108public class Contact {
109
110//varible for Sign Up Activity
111String name,uname,address,email,mobile_no,password,re_password;
112
113
114//Constructor
115public Contact(){
116
117}
118
119public void Contact(String name, String uname, String address, String email, String mobile_no, String password, String re_password){
120 this.name = name;
121 this.uname = uname;
122 this.address = address;
123 this.email = email;
124 this.mobile_no = mobile_no;
125 this.password = password;
126 this.re_password = re_password;
127}
128
129//Setter and getter for Sign Up Activity
130
131public void setName(String name){
132 this.name = name;
133}
134
135public void setUname(String uname){
136 this.uname = uname;
137}
138
139public void setAddress(String address){
140 this.address = address;
141}
142
143public void setEmail(String email){
144 this.email = email;
145}
146
147public void setMobile_no(String mobile_no){
148 this.mobile_no = mobile_no;
149}
150
151public void setPassword(String password){
152 this.password = password;
153}
154
155public void setRe_password(String re_password){
156 this.re_password = re_password;
157}
158
159
160
161public String getName(){
162 return this.name;
163}
164
165public String getUname(){
166 return this.uname;
167}
168
169public String getEmail(){
170 return this.email;
171}
172
173public String getAddress(){
174 return this.address;
175}
176
177public String getMobile_no(){
178 return this.mobile_no;
179}
180
181public String getPassword(){
182 return this.password;
183}
184
185public String getRe_password(){
186 return this.re_password;
187}
188}`
189
190import android.support.v7.app.AppCompatActivity;
191import android.os.Bundle;
192import android.app.ProgressDialog;
193import android.util.Log;
194
195import android.content.Intent;
196import android.view.View;
197import android.widget.Button;
198import android.widget.EditText;
199import android.widget.TextView;
200import android.widget.Toast;
201
202import butterknife.ButterKnife;
203import butterknife.Bind;
204
205public class LoginActivity extends AppCompatActivity {
206private static final String TAG = "LoginActivity";
207private static final int REQUEST_SIGNUP = 0;
208DatabaseHelper databaseHelper = new DatabaseHelper(this);
209
210@Bind(R.id.input_user_name) EditText _UsernameText;
211@Bind(R.id.input_password) EditText _passwordText;
212@Bind(R.id.btn_login) Button _loginButton;
213@Bind(R.id.link_signup) TextView _signupLink;
214
215@Override
216public void onCreate(Bundle savedInstanceState) {
217 super.onCreate(savedInstanceState);
218 setContentView(R.layout.activity_login);
219 ButterKnife.bind(this);
220
221 _loginButton.setOnClickListener(new View.OnClickListener() {
222
223 @Override
224 public void onClick(View v) {
225 login();
226 }
227 });
228
229 _signupLink.setOnClickListener(new View.OnClickListener() {
230
231 @Override
232 public void onClick(View v) {
233 // Start the Signup activity
234 Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
235 startActivityForResult(intent, REQUEST_SIGNUP);
236 finish();
237 overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
238 }
239 });
240}
241public void login() {
242 Log.d(TAG, "Login");
243
244 if (!validate()) {
245 onLoginFailed();
246 return;
247 }
248
249 _loginButton.setEnabled(false);
250
251 final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
252 R.style.AppTheme_Dark_Dialog);
253 progressDialog.setIndeterminate(true);
254 progressDialog.setMessage("Authenticating...");
255 progressDialog.show();
256
257
258 new android.os.Handler().postDelayed(
259 new Runnable() {
260 public void run() {
261 // On complete call either onLoginSuccess or onLoginFailed
262 onLoginSuccess();
263 // onLoginFailed();
264 progressDialog.dismiss();
265 }
266 }, 3000);
267}
268
269
270@Override
271protected void onActivityResult(int requestCode, int resultCode, Intent data) {
272 if (requestCode == REQUEST_SIGNUP) {
273 if (resultCode == RESULT_OK) {
274
275 // TODO: Implement successful signup logic here
276 // By default we just finish the Activity and log them in automatically
277 this.finish();
278 }
279 }
280}
281
282@Override
283public void onBackPressed() {
284 // Disable going back to the MainActivity
285 moveTaskToBack(true);
286}
287
288public void onLoginSuccess() {
289 _loginButton.setEnabled(true);
290 Intent intent = new Intent(LoginActivity.this,SignupActivity.class);
291 startActivity(intent);
292 //finish();
293}
294
295public void onLoginFailed() {
296 Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
297
298 _loginButton.setEnabled(true);
299}
300
301public boolean validate() {
302 boolean valid = true;
303 String uname = _UsernameText.getText().toString();
304 String password = _passwordText.getText().toString();
305 String mainPassword = databaseHelper.searchPassword(uname);
306 Log.v("LoginActivity", " mainPassword: " +mainPassword);
307
308 if (uname.isEmpty() || uname.length() < 3) {
309 _UsernameText.setError("at least 3 characters");
310 valid = false;
311 } else {
312 _UsernameText.setError(null);
313 }
314
315 if (password.isEmpty() || password.length() < 4 || password.length() > 10 || !(password.equals(mainPassword))) {
316 _passwordText.setError("Username And Password Do not match");
317 valid = false;
318 } else {
319 _passwordText.setError(null);
320 }
321
322 return valid;
323}