Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. menu.add(0, sign_up, 0, R.string.signup);
  2. menu.add(0, exit, 0, R.string.exit);
  3.  
  4. return result;
  5. }
  6.  
  7. public class LogginIn extends Activity {
  8.  
  9.  
  10. public static final int Connected_to_service = 0;
  11. public static final int not_Connected_to_service = 4;
  12. public static final int FILL_BOTH_USERNAME_PASSWORD = 1;
  13. public static final String Authentical_Failed = "failed";
  14. public static final String Friend_List = "friend_list";
  15. public static final int Make_sure_username_password = 2;
  16. public static final int Connected_to_network = 3;
  17. public static final int not_connected_to_network = 0;
  18.  
  19. public EditText username;
  20. public EditText password;
  21.  
  22.  
  23.  
  24. public Manager serviceProvider;
  25.  
  26. public static final int sign_up = Menu.FIRST;
  27. public static final int exit = Menu.FIRST + 1;
  28.  
  29.  
  30.  
  31. private ServiceConnection mConnection = new ServiceConnection() {
  32. public void onServiceConnected(ComponentName className, IBinder service) {
  33.  
  34. serviceProvider = ((MessagingService.IMBinder)service).getService();
  35.  
  36. if(serviceProvider.isUserAuthenticated() == true)
  37. {
  38. Intent i = new Intent(LogginIn.this, ListOfFriends.class);
  39. startActivity(i);
  40. LogginIn.this.finish();
  41. }
  42. }
  43.  
  44. public void onServiceDisconnected(ComponentName className) {
  45.  
  46. serviceProvider = null;
  47. Toast.makeText(LogginIn.this, R.string.local_service_stopped,
  48. Toast.LENGTH_SHORT).show();
  49. }
  50.  
  51. };
  52. private int message;
  53.  
  54.  
  55.  
  56. @Override
  57. public void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59.  
  60.  
  61. startService(new Intent(LogginIn.this, MessagingService.class));
  62.  
  63.  
  64. setContentView(R.layout.loggin_in);
  65. setTitle("login");
  66.  
  67. ImageButton button= (ImageButton) findViewById(R.id.button);
  68.  
  69. username = (EditText) findViewById(R.id.username);
  70. password = (EditText) findViewById(R.id.password);
  71.  
  72. button.setOnClickListener(new OnClickListener() {
  73. @SuppressWarnings("unused")
  74. public void onClick1(View arg0) {
  75. if (serviceProvider == null) {
  76. Toast.makeText(getApplicationContext(), R.string.not_connected_to_service, Toast.LENGTH_LONG).show();
  77.  
  78. return;
  79. } else if (serviceProvider.isNetworkConnected() == false) {
  80. Toast.makeText(getApplicationContext(), R.string.not_connected_to_service, Toast.LENGTH_LONG).show();
  81. } else if (username.length() > 0 &&
  82. password.length() > 0) {
  83.  
  84. Thread loginThread = new Thread() {
  85. private Handler handler = new Handler();
  86.  
  87. @Override
  88. public void run() {
  89. String result = null;
  90. try {
  91. result = serviceProvider.authenticateUser(username.getText().toString(), password.getText().toString());
  92. } catch (UnsupportedEncodingException e) {
  93.  
  94. e.printStackTrace();
  95. }
  96. if (result == null || result.equals(Authentical_Failed)) {
  97.  
  98. handler.post(new Runnable() {
  99. public void run() {
  100. Toast.makeText(getApplicationContext(), R.string.not_connected_to_service, Toast.LENGTH_LONG).show();
  101.  
  102. }
  103.  
  104. });
  105. } else {
  106.  
  107.  
  108. handler.post(new Runnable() {
  109. public void run() {
  110. Intent i = new Intent(LogginIn.this, ListOfFriends.class);
  111.  
  112. startActivity(i);
  113. LogginIn.this.finish();
  114.  
  115. }
  116. });
  117.  
  118. }
  119.  
  120. }
  121.  
  122. };
  123. loginThread.start();
  124.  
  125.  
  126. } else {
  127.  
  128. Toast.makeText(getApplicationContext(), R.string.fill_both_username_password, Toast.LENGTH_LONG).show();
  129. }
  130.  
  131.  
  132. }
  133.  
  134. @Override
  135. public void onClick(View arg0) {
  136.  
  137.  
  138. }
  139.  
  140.  
  141. });
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. @Override
  151. protected Dialog onCreateDialog(int id)
  152. {
  153.  
  154. switch (id)
  155. {
  156.  
  157. case not_Connected_to_service:
  158. message = R.string.not_connected_to_service;
  159. break;
  160. case FILL_BOTH_USERNAME_PASSWORD:
  161. message = R.string.fill_both_username_password;
  162. break;
  163. case Make_sure_username_password:
  164. message = R.string.make_sure_username_password_correct;
  165. break;
  166. case not_connected_to_network:
  167. message = R.string.not_connected_to_service;
  168. break;
  169. default:
  170. break;
  171. }
  172. return null;
  173. }
  174.  
  175.  
  176. public boolean onMenuItemSelected(int featureId, Menu menu){
  177. boolean result = super.onCreateOptionsMenu(menu);
  178.  
  179. menu.add(0, sign_up, 0, R.string.signup);
  180. menu.add(0, exit, 0, R.string.exit);
  181.  
  182. return result;
  183. }
  184. @Override
  185. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  186. switch (item.getItemId())
  187. {
  188. case sign_up:
  189. Intent i = new Intent(LogginIn.this, SigningUp.class);
  190. startActivity(i);
  191. return true;
  192. case exit:
  193. return true;
  194. }
  195. return super.onMenuItemSelected(featureId, item);
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement