Advertisement
Guest User

Untitled

a guest
May 6th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. package com.achal089.pestcontrol;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6.  
  7. import java.util.HashMap;
  8.  
  9. /**
  10. * Created by as on 5/7/2016.
  11. */
  12. public class UserSession {
  13.  
  14. SharedPreferences pref;
  15.  
  16. // Editor reference for Shared preferences
  17. SharedPreferences.Editor editor;
  18.  
  19. // Context
  20. Context _context;
  21.  
  22. // Shared preferences mode
  23. int PRIVATE_MODE = 0;
  24.  
  25. // Shared preferences file name
  26. public static final String PREFER_NAME = "Register";
  27.  
  28. // All Shared Preferences Keys
  29. public static final String IS_USER_LOGIN = "IsUserLoggedIn";
  30.  
  31. // User name (make variable public to access from outside)
  32. public static final String Email = "Email";
  33.  
  34. // Email address (make variable public to access from outside)
  35. public static final String Password = "Password";
  36.  
  37.  
  38. // Constructor
  39. public UserSession(Context context){
  40. this._context = context;
  41. pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
  42. editor = pref.edit();
  43. }
  44.  
  45. //Create login session
  46. public void createUserLoginSession(String uName, String uPassword){
  47. // Storing login value as TRUE
  48. editor.putBoolean(IS_USER_LOGIN, true);
  49.  
  50. // Storing name in preferences
  51. editor.putString(Email, uName);
  52.  
  53. // Storing email in preferences
  54. editor.putString(Password, uPassword);
  55.  
  56. // commit changes
  57. editor.commit();
  58. }
  59.  
  60. /**
  61. * Check login method will check user login status
  62. * If false it will redirect user to login page
  63. * Else do anything
  64. * */
  65. public boolean checkLogin(){
  66. // Check login status
  67. if(!this.isUserLoggedIn()){
  68.  
  69. // user is not logged in redirect him to Login Activity
  70. Intent i = new Intent(_context, Login.class);
  71.  
  72. // Closing all the Activities from stack
  73. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  74.  
  75. // Add new Flag to start new Activity
  76. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  77.  
  78. // Staring Login Activity
  79. _context.startActivity(i);
  80.  
  81. return true;
  82. }
  83. return false;
  84. }
  85.  
  86.  
  87.  
  88. /**
  89. * Get stored session data
  90. * */
  91. public HashMap<String, String> getUserDetails(){
  92.  
  93. //Use hashmap to store user credentials
  94. HashMap<String, String> user = new HashMap<String, String>();
  95.  
  96. // user name
  97. user.put(Email, pref.getString(Email, null));
  98.  
  99. // user email id
  100. user.put(Password, pref.getString(Password, null));
  101.  
  102. // return user
  103. return user;
  104. }
  105.  
  106. /**
  107. * Clear session details
  108. * */
  109. public void logoutUser(){
  110.  
  111. // Clearing all user data from Shared Preferences
  112. editor.clear();
  113. editor.commit();
  114.  
  115. // After logout redirect user to MainActivity
  116. Intent i = new Intent(_context, MainActivity.class);
  117.  
  118. // Closing all the Activities
  119. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  120.  
  121. // Add new Flag to start new Activity
  122. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  123.  
  124. // Staring Login Activity
  125. _context.startActivity(i);
  126. }
  127.  
  128.  
  129. // Check for login
  130. public boolean isUserLoggedIn(){
  131. return pref.getBoolean(IS_USER_LOGIN, false);
  132. }
  133. }
  134.  
  135. package com.achal089.pestcontrol;
  136.  
  137. import android.content.Context;
  138. import android.content.Intent;
  139. import android.content.SharedPreferences;
  140.  
  141. import java.util.HashMap;
  142.  
  143. /**
  144. * Created by as on 5/7/2016.
  145. */
  146. public class UserSession {
  147.  
  148. SharedPreferences pref;
  149.  
  150. // Editor reference for Shared preferences
  151. SharedPreferences.Editor editor;
  152.  
  153. // Context
  154. Context _context;
  155.  
  156. // Shared preferences mode
  157. int PRIVATE_MODE = 0;
  158.  
  159. // Shared preferences file name
  160. public static final String PREFER_NAME = "Register";
  161.  
  162. // All Shared Preferences Keys
  163. public static final String IS_USER_LOGIN = "IsUserLoggedIn";
  164.  
  165. // User name (make variable public to access from outside)
  166. public static final String Email = "Email";
  167.  
  168. // Email address (make variable public to access from outside)
  169. public static final String Password = "Password";
  170.  
  171.  
  172. // Constructor
  173. public UserSession(Context context){
  174. this._context = context;
  175. pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
  176. editor = pref.edit();
  177. }
  178.  
  179. //Create login session
  180. public void createUserLoginSession(String uName, String uPassword){
  181. // Storing login value as TRUE
  182. editor.putBoolean(IS_USER_LOGIN, true);
  183.  
  184. // Storing name in preferences
  185. editor.putString(Email, uName);
  186.  
  187. // Storing email in preferences
  188. editor.putString(Password, uPassword);
  189.  
  190. // commit changes
  191. editor.commit();
  192. }
  193.  
  194. /**
  195. * Check login method will check user login status
  196. * If false it will redirect user to login page
  197. * Else do anything
  198. * */
  199. public boolean checkLogin(){
  200. // Check login status
  201. if(!this.isUserLoggedIn()){
  202.  
  203. // user is not logged in redirect him to Login Activity
  204. Intent i = new Intent(_context, Login.class);
  205.  
  206. // Closing all the Activities from stack
  207. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  208.  
  209. // Add new Flag to start new Activity
  210. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  211.  
  212. // Staring Login Activity
  213. _context.startActivity(i);
  214.  
  215. return true;
  216. }
  217. return false;
  218. }
  219.  
  220.  
  221.  
  222. /**
  223. * Get stored session data
  224. * */
  225. public HashMap<String, String> getUserDetails(){
  226.  
  227. //Use hashmap to store user credentials
  228. HashMap<String, String> user = new HashMap<String, String>();
  229.  
  230. // user name
  231. user.put(Email, pref.getString(Email, null));
  232.  
  233. // user email id
  234. user.put(Password, pref.getString(Password, null));
  235.  
  236. // return user
  237. return user;
  238. }
  239.  
  240. /**
  241. * Clear session details
  242. * */
  243. public void logoutUser(){
  244.  
  245. // Clearing all user data from Shared Preferences
  246. editor.clear();
  247. editor.commit();
  248.  
  249. // After logout redirect user to MainActivity
  250. Intent i = new Intent(_context, MainActivity.class);
  251.  
  252. // Closing all the Activities
  253. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  254.  
  255. // Add new Flag to start new Activity
  256. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  257.  
  258. // Staring Login Activity
  259. _context.startActivity(i);
  260. }
  261.  
  262.  
  263. // Check for login
  264. public boolean isUserLoggedIn(){
  265. return pref.getBoolean(IS_USER_LOGIN, false);
  266. }
  267. }
  268.  
  269. import android.app.Activity;
  270. import android.content.Context;
  271. import android.content.Intent;
  272. import android.content.SharedPreferences;
  273. import android.os.Bundle;
  274. import android.view.View;
  275. import android.widget.TextView;
  276.  
  277. public class Register extends Activity {
  278.  
  279. UserSession session;
  280. SharedPreferences sharedpreferences;
  281. TextView name;
  282. TextView address;
  283. TextView email;
  284. TextView phone;
  285. TextView occupation;
  286. TextView password;
  287. public static final String mypreference = "mypref";
  288. public static final String Name = "nameKey";
  289. public static final String Email = "emailKey";
  290. public static final String Phone = "Phone";
  291. public static final String Address = "Address";
  292. public static final String Occupation = "Occupation";
  293. public static final String Password = "Password";
  294.  
  295. @Override
  296. protected void onCreate(Bundle savedInstanceState) {
  297. super.onCreate(savedInstanceState);
  298. setContentView(R.layout.activity_register);
  299.  
  300. name = (TextView) findViewById(R.id.editText6);
  301. address = (TextView) findViewById(R.id.editText7);
  302. email = (TextView) findViewById(R.id.editText9);
  303. phone = (TextView) findViewById(R.id.editText8);
  304. password = (TextView)findViewById(R.id.editText11);
  305. occupation = (TextView)findViewById(R.id.editText10);
  306. sharedpreferences = getSharedPreferences(mypreference,
  307. Context.MODE_PRIVATE);
  308. if (sharedpreferences.contains(Name)) {
  309. name.setText(sharedpreferences.getString(Name, ""));
  310. }
  311. if (sharedpreferences.contains(Email)) {
  312. email.setText(sharedpreferences.getString(Email, ""));
  313.  
  314. }
  315.  
  316. if (sharedpreferences.contains(Occupation)) {
  317. occupation.setText(sharedpreferences.getString(Occupation, ""));
  318. }
  319.  
  320. if (sharedpreferences.contains(Password)) {
  321. password.setText(sharedpreferences.getString(Password, ""));
  322. }
  323.  
  324.  
  325. if (sharedpreferences.contains(Address)) {
  326. address.setText(sharedpreferences.getString(Address, ""));
  327.  
  328. }
  329.  
  330. if (sharedpreferences.contains(Phone)) {
  331. phone.setText(sharedpreferences.getString(Phone, ""));
  332.  
  333. }
  334.  
  335.  
  336.  
  337. }
  338.  
  339.  
  340. public void Save(View view) {
  341. String n = name.getText().toString();
  342. String e = email.getText().toString();
  343. String w = phone.getText().toString();
  344. String m = address.getText().toString();
  345. String p = password.getText().toString();
  346. String v = occupation.getText().toString();
  347. SharedPreferences.Editor editor = sharedpreferences.edit();
  348. editor.putString(Name, n);
  349. editor.putString(Email, e);
  350. editor.putString(Phone, w);
  351. editor.putString(Address,m);
  352. editor.putString(Occupation,v);
  353. editor.putString(Password,p);
  354. editor.commit();
  355.  
  356.  
  357. Intent a = new Intent(Register.this, Login.class);
  358. startActivity(a);
  359. }
  360.  
  361. public void Login (View view)
  362. {
  363. Intent a = new Intent(Register.this, Login.class);
  364. startActivity(a);
  365. }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement