· 8 years ago · Dec 06, 2017, 06:36 AM
1package gymhelp.feup.org.pushupfit;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.content.Intent;
6import android.database.Cursor;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10
11import java.util.ArrayList;
12import java.util.List;
13
14/**
15 * Created by Paulo Maia on 18/11/2017.
16 */
17
18public class DatabaseHandler extends SQLiteOpenHelper {
19
20 // All Static variables
21 // Database Version
22 private static final int DATABASE_VERSION = 1;
23 public Boolean isLogin;
24
25 // Database Name
26 private static final String DATABASE_NAME = "userManager";
27
28 // Contacts table name
29 private static final String TABLE_CONTACTS = "users";
30 private static final String TABLE_ACTIVITY="activities";
31
32
33 // Contacts Table Columns names
34 private static final String USER_ID = "user_id";
35 private static final String NAME = "name";
36 private static final String PW = "password";
37 private static final String AGE = "age";
38 private static final String GENDER = "gender";
39 private static final String UP_NO = "up_number";
40 private static final String XP = "experience";
41
42 //Activities Table Column names
43 private static final String ACT_ID="activity_id";
44 private static final String ACT_NAME="activity_name";
45
46 private static final String CREATE_CONTACTS_TABLE="CREATE TABLE " + TABLE_CONTACTS + "("
47 + USER_ID + " INTEGER PRIMARY KEY," + NAME + " TEXT,"
48 + UP_NO + " TEXT," + PW + " TEXT," + XP + " INTEGER," + GENDER +" TEXT," + AGE +" AGE"+ ")";
49
50 private static final String CREATE_ACTIVITIES_TABLE= "CREATE TABLE " + TABLE_ACTIVITY + "("
51 + ACT_ID + " INTEGER PRIMARY KEY," + ACT_NAME + "TEXT," + USER_ID + " INTEGER, FOREIGN KEY ("+USER_ID+") REFERENCES "+TABLE_CONTACTS+"("+TABLE_CONTACTS+") ON DELETE CASCADE" +")";
52
53 Context _context;
54
55 public DatabaseHandler(Context context) {
56 super(context, DATABASE_NAME, null, DATABASE_VERSION);
57 this._context=context;
58 }
59
60 // Creating Tables
61 @Override
62 public void onCreate(SQLiteDatabase db) {
63 db.execSQL(CREATE_CONTACTS_TABLE);
64 db.execSQL(CREATE_ACTIVITIES_TABLE);
65 }
66
67 // Upgrading database
68 @Override
69 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
70 // Drop older table if existed
71 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
72 db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACTIVITY);
73
74
75 // Create tables again
76 onCreate(db);
77 }
78// activity functions
79
80 //adding activity
81 public void addActivity(Phys_Activity activity){
82 SQLiteDatabase db=this.getWritableDatabase();
83
84 ContentValues values = new ContentValues();
85 values.put(ACT_NAME,activity.getName());
86 values.put(USER_ID, activity.getUser_id());
87
88 // Inserting Row
89 db.insert(TABLE_ACTIVITY, null, values);
90 db.close(); // Closing database connection
91
92 };
93
94
95 public Phys_Activity getActivity(int user_id) {
96 SQLiteDatabase db = this.getReadableDatabase();
97
98 Cursor cursor = db.query(TABLE_ACTIVITY, new String[] { ACT_ID, ACT_NAME, USER_ID
99 }, USER_ID + "=?",
100 new String[] { String.valueOf(user_id) }, null, null, null, null);
101 if (cursor != null)
102 cursor.moveToFirst();
103
104 Phys_Activity activity = new Phys_Activity(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
105 Integer.parseInt(cursor.getString(2)));
106
107 return activity;
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121// user functions
122
123
124
125 //adding user
126
127 public void addUser(User user) {
128 SQLiteDatabase db = this.getWritableDatabase();
129
130 ContentValues values = new ContentValues();
131 values.put(NAME, user.getName()); // Contact Name
132 values.put(UP_NO, user.getupNumber()); // Contact Phone Number
133 values.put(PW, user.getPassword());
134 values.put(XP, user.getExperience());
135 values.put(GENDER, user.getGender());
136 values.put(AGE, user.getAge());
137
138 // Inserting Row
139 db.insert(TABLE_CONTACTS, null, values);
140 db.close(); // Closing database connection
141 }
142
143
144
145 // Updating single user
146 public int updateUser(User user) {
147 SQLiteDatabase db = this.getWritableDatabase();
148
149 ContentValues values = new ContentValues();
150 values.put(NAME, user.getName());
151 values.put(PW, user.getPassword());
152 values.put(UP_NO, user.getupNumber());
153 values.put(XP, user.getExperience());
154 values.put(GENDER, user.getGender());
155 values.put(AGE, user.getAge());
156
157 // updating row
158 return db.update(TABLE_CONTACTS, values, USER_ID + " = ?",
159 new String[] { String.valueOf(user.getId()) });
160 }
161
162 // Getting single contact
163 public User getUser(int id) {
164 SQLiteDatabase db = this.getReadableDatabase();
165
166 Cursor cursor = db.query(TABLE_CONTACTS, new String[] { USER_ID,
167 NAME, UP_NO, PW, XP, GENDER, AGE }, USER_ID + "=?",
168 new String[] { String.valueOf(id) }, null, null, null, null);
169 if (cursor != null)
170 cursor.moveToFirst();
171
172 User user = new User(Integer.parseInt(cursor.getString(0)),
173 cursor.getString(1), cursor.getString(2), cursor.getString(3), Integer.parseInt(cursor.getString(4)), cursor.getString(5), Integer.parseInt(cursor.getString(6)));
174 // return user
175 return user;
176 }
177
178 // Getting All Contacts
179 public List<User> getAllUsers() {
180 List<User> userList = new ArrayList<User>();
181 // Select All Query
182 String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
183
184 SQLiteDatabase db = this.getWritableDatabase();
185 Cursor cursor = db.rawQuery(selectQuery, null);
186
187 // looping through all rows and adding to list
188 if (cursor.moveToFirst()) {
189 do {
190 User user = new User();
191 user.setId(Integer.parseInt(cursor.getString(0)));
192 user.setName(cursor.getString(1));
193 user.setUpNumber(cursor.getString(2));
194 user.setPassword(cursor.getString(3));
195 user.setExperience(Integer.parseInt(cursor.getString(4)));
196 user.setGender(cursor.getString(5));
197 user.setAge(Integer.parseInt(cursor.getString(6)));
198
199 // Adding user to list
200 userList.add(user);
201 } while (cursor.moveToNext());
202 }
203
204 // return user list
205 return userList;
206 }
207
208 // Getting users Count
209 public int getUsersCount() {
210 String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
211 SQLiteDatabase db = this.getReadableDatabase();
212 Cursor cursor = db.rawQuery(countQuery, null);
213 cursor.close();
214
215 // return count
216 return cursor.getCount();
217 }
218
219
220 // Deleting single contact
221 public void deleteContact(User user) {
222 SQLiteDatabase db = this.getWritableDatabase();
223 db.delete(TABLE_CONTACTS, USER_ID + " = ?",
224 new String[] { String.valueOf(user.getId()) });
225 db.close();
226 }
227
228 // public User findLoggedUserInfo()
229 // {
230
231 // }
232
233 public void createLoginSession(){
234 // Storing login value as TRUE
235 isLogin = true;
236
237 }
238
239 public void checkLogin() {
240 // Check login status
241 if (isLogin!=null) {
242
243 // If the user is not logged in, redirect him to Login Activity
244 Intent i = new Intent(_context, LoginActivity.class);
245 // Closing all the Activities
246 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
247
248 // Add new Flag to start new Activity
249 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
250
251 // Staring Login Activity
252 _context.startActivity(i);
253 }
254 }
255
256 public void logoutUser(){
257 // Clearing all data from Shared Preferences
258 // editor.clear();
259 // editor.commit();
260
261 isLogin=false;
262
263 // After logout redirect user to Login Activity
264 Intent i = new Intent(_context, LoginActivity.class);
265 // Closing all the Activities
266 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
267
268 // Add new Flag to start new Activity
269 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
270
271 // Staring Login Activity
272 _context.startActivity(i);
273 }
274
275
276}