Guest User

Untitled

a guest
Jan 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. package com.tass.login_teste.database;
  2.  
  3. private static final String DB_NAME = "login";
  4. private static final int DB_VERSION = 1;
  5.  
  6. private static final String DB_TABLE = "create table user (id integer primary key autoincrement, " +
  7. "usuario text not null, " +
  8. "senha text not null, " +
  9. "nome_completo text not null, " +
  10. "matricula text not null);";
  11.  
  12.  
  13. public DatabaseHelper(Context context) {
  14. super(context, DB_NAME, null, DB_VERSION);
  15. }
  16.  
  17.  
  18. @Override
  19. public void onCreate(SQLiteDatabase database) {
  20. database.execSQL(DB_TABLE);
  21. }
  22.  
  23.  
  24.  
  25. @Override
  26. public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
  27. Log.w(DatabaseHelper.class.getName(),
  28. "Upgrading databse from version" + oldVersion + "to "
  29. + newVersion + ", which will destroy all old data");
  30. database.execSQL("DROP TABLE IF EXISTS user");
  31. onCreate(database);
  32. }
  33.  
  34. package com.tass.login_teste.database;
  35.  
  36. private Context context;
  37. private SQLiteDatabase database;
  38. private DatabaseHelper dbHelper;
  39.  
  40.  
  41.  
  42.  
  43. public DatabaseAdapter(Context context) {
  44. this.context = context;
  45. }
  46.  
  47.  
  48.  
  49. public DatabaseAdapter open() throws SQLException {
  50. dbHelper = new DatabaseHelper(context);
  51. database = dbHelper.getWritableDatabase();
  52. return this;
  53. }
  54.  
  55.  
  56.  
  57. public void close() {
  58. dbHelper.close();
  59. }
  60.  
  61.  
  62.  
  63. public long criarUsuario(String usuario, String senha, String nome_completo, String matricula) {
  64. ContentValues initialValues = createUserTableContentValues(usuario, senha, nome_completo, matricula);
  65. return database.insert(LOGIN_TABLE, null, initialValues);
  66. }
  67.  
  68.  
  69.  
  70.  
  71. public boolean deleteUser(long rowId) {
  72. return database.delete(LOGIN_TABLE, COL_ID + "=" + rowId, null) > 0;
  73. }
  74.  
  75.  
  76.  
  77. public boolean updateUserTable(long rowId, String usuario, String senha, String nome_completo, String matricula) {
  78. ContentValues updateValues = createUserTableContentValues(usuario, senha, nome_completo, matricula);
  79. return database.update(LOGIN_TABLE, updateValues, COL_ID + "=" + rowId, null) > 0;
  80. }
  81.  
  82.  
  83.  
  84. public Cursor fetchAllUsers() {
  85. return database.query(LOGIN_TABLE, new String[] { COL_ID, COL_USUARIO,
  86. COL_SENHA }, null, null, null, null, null);
  87. }
  88.  
  89.  
  90.  
  91. public Cursor fetchUser(String username, String password) {
  92. Cursor myCursor = database.query(LOGIN_TABLE,
  93. new String[] { COL_ID, COL_USUARIO, COL_SENHA },
  94. COL_USUARIO + "='" + username + "' AND " +
  95. COL_SENHA + "='" + password + "'", null, null, null, null);
  96.  
  97. if (myCursor != null) {
  98. myCursor.moveToFirst();
  99. }
  100. return myCursor;
  101. }
  102.  
  103.  
  104.  
  105.  
  106. public Cursor fetchUserById(long rowId) throws SQLException {
  107. Cursor myCursor = database.query(LOGIN_TABLE,
  108. new String[] { COL_ID, COL_USUARIO, COL_SENHA },
  109. COL_ID + "=" + rowId, null, null, null, null);
  110. if (myCursor != null) {
  111. myCursor.moveToFirst();
  112. }
  113. return myCursor;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. private ContentValues createUserTableContentValues(String usuario, String senha, String nome_completo, String matricula) {
  120. ContentValues values = new ContentValues();
  121. values.put(COL_USUARIO, usuario);
  122. values.put(COL_SENHA, senha);
  123. values.put(COL_NOME_COMPLETO, nome_completo);
  124. values.put(COL_MATRICULA, matricula);
  125. return values;
  126. }
  127.  
  128. public class login extends Activity {
  129.  
  130. public static final String MY_PREFS = "SharedPreferences";
  131. private DatabaseAdapter dbHelper;
  132. private EditText theUsername;
  133. private EditText thePassword;
  134. private Button loginButton;
  135. private Button registerButton;
  136. private Button clearButton;
  137. private Button exitButton;
  138. private CheckBox rememberDetails;
  139.  
  140.  
  141. @Override
  142. public void onCreate(Bundle savedInstanceState) {
  143. super.onCreate(savedInstanceState);
  144.  
  145. SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, 0);
  146. SharedPreferences.Editor editor = mySharedPreferences.edit();
  147. editor.putLong("uid", 0);
  148. editor.commit();
  149.  
  150. dbHelper = new DatabaseAdapter(this);
  151. dbHelper.open();
  152.  
  153. setContentView(R.layout.login);
  154. initControls();
  155. }
  156.  
  157. private void initControls() {
  158. //Set the activity layout.
  159. theUsername = (EditText) findViewById(R.id.Username);
  160. thePassword = (EditText) findViewById(R.id.Password);
  161. loginButton = (Button) findViewById(R.id.Login);
  162. registerButton = (Button) findViewById(R.id.Register);
  163. clearButton = (Button) findViewById(R.id.Clear);
  164. exitButton = (Button) findViewById(R.id.Exit);
  165. rememberDetails = (CheckBox) findViewById(R.id.RememberMe);
  166.  
  167. //Create touch listeners for all buttons.
  168. loginButton.setOnClickListener(new Button.OnClickListener() {
  169. public void onClick(View v) {
  170. LogMeIn(v);
  171. }
  172. });
  173.  
  174. registerButton.setOnClickListener(new Button.OnClickListener() {
  175. public void onClick(View v) {
  176. Register(v);
  177. }
  178. });
  179.  
  180. clearButton.setOnClickListener(new Button.OnClickListener() {
  181. public void onClick(View v) {
  182. ClearForm();
  183. }
  184. });
  185.  
  186. exitButton.setOnClickListener(new Button.OnClickListener() {
  187. public void onClick(View v) {
  188. Exit();
  189. }
  190. });
  191. //Create remember password check box listener.
  192. rememberDetails.setOnClickListener(new CheckBox.OnClickListener() {
  193. public void onClick(View v) {
  194. RememberMe();
  195. }
  196. });
  197.  
  198. //Handle remember password preferences.
  199. SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
  200. String thisUsername = prefs.getString("username", "");
  201. String thisPassword = prefs.getString("password", "");
  202. boolean thisRemember = prefs.getBoolean("remember", false);
  203. if (thisRemember) {
  204. theUsername.setText(thisUsername);
  205. thePassword.setText(thisPassword);
  206. rememberDetails.setChecked(thisRemember);
  207. }
  208.  
  209. }
  210.  
  211.  
  212. private void Exit() {
  213. finish();
  214. }
  215.  
  216.  
  217. private void ClearForm() {
  218. saveLoggedInUId(0, "", "");
  219. theUsername.setText("");
  220. thePassword.setText("");
  221. }
  222.  
  223.  
  224. private void RememberMe() {
  225. boolean thisRemember = rememberDetails.isChecked();
  226. SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
  227. SharedPreferences.Editor editor = prefs.edit();
  228. editor.putBoolean("remember", thisRemember);
  229. editor.commit();
  230. }
  231.  
  232.  
  233. private void LogMeIn(View v) {
  234. //Get the username and password
  235. String thisUsername = theUsername.getText().toString();
  236. String thisPassword = thePassword.getText().toString();
  237.  
  238. //Assign the hash to the password
  239. thisPassword = md5(thisPassword);
  240.  
  241.  
  242.  
  243. Cursor theUser = dbHelper.fetchUser(thisUsername, thisPassword);
  244. if (theUser != null) {
  245. startManagingCursor(theUser);
  246. if (theUser.getCount() > 0) {
  247. saveLoggedInUId(theUser.getLong(theUser.getColumnIndex(DatabaseAdapter.COL_ID)), thisUsername, thePassword.getText().toString());
  248. stopManagingCursor(theUser);
  249. theUser.close();
  250. Intent i = new Intent(v.getContext(), Menu.class);
  251. //Intent i = new Intent(v.getContext(), teste.class);
  252. startActivity(i);
  253. }
  254.  
  255. //Returns appropriate message if no match is made
  256. else {
  257. Toast.makeText(getApplicationContext(),
  258. "Você digitou um nome de usuário ou senha incorreto.",
  259. Toast.LENGTH_SHORT).show();
  260. saveLoggedInUId(0, "", "");
  261. }
  262. stopManagingCursor(theUser);
  263. theUser.close();
  264. } else {
  265. Toast.makeText(getApplicationContext(),
  266. "Erro de consulta de banco de dados",
  267. Toast.LENGTH_SHORT).show();
  268. }
  269.  
  270.  
  271. }
  272.  
  273.  
  274. private void Register(View v) {
  275. Intent i = new Intent(v.getContext(), Register.class);
  276. startActivity(i);
  277. }
  278.  
  279.  
  280.  
  281. private void saveLoggedInUId(long id, String username, String password) {
  282. SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
  283. SharedPreferences.Editor myEditor = settings.edit();
  284. myEditor.putLong("uid", id);
  285. myEditor.putString("username", username);
  286. myEditor.putString("password", password);
  287. boolean rememberThis = rememberDetails.isChecked();
  288. myEditor.putBoolean("rememberThis", rememberThis);
  289. myEditor.commit();
  290. }
  291.  
  292.  
  293.  
  294. private String md5(String s) {
  295. try {
  296. MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
  297. digest.update(s.getBytes());
  298. byte messageDigest[] = digest.digest();
  299.  
  300. StringBuffer hexString = new StringBuffer();
  301. for (int i = 0; i < messageDigest.length; i++)
  302. hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
  303.  
  304. return hexString.toString();
  305. } catch (NoSuchAlgorithmException e) {
  306. return s;
  307. }
  308. }
Add Comment
Please, Sign In to add comment