Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. public class WLANService extends Service {
  2.  
  3. String username, password, ssid, url;
  4.  
  5. private static final String CREDENTIALS = "Credentials";
  6.  
  7. private final BroadcastReceiver receiver = new BroadcastReceiver() {
  8. @Override
  9. public void onReceive(Context context, Intent intent) {
  10.  
  11. SharedPreferences sharedPreferences = getSharedPreferences(CREDENTIALS, 0);
  12. if(sharedPreferences.contains("username")) {
  13. username = sharedPreferences.getString("username", "UNDEFINED");
  14.  
  15. }
  16. if(sharedPreferences.contains("password")) {
  17. password = sharedPreferences.getString("password", "UNDEFINED");
  18.  
  19. }
  20. if(sharedPreferences.contains("ssid")) {
  21. ssid = sharedPreferences.getString("ssid", "UNDEFINED");
  22.  
  23. }
  24. if(sharedPreferences.contains("url")) {
  25. url = sharedPreferences.getString("url", "UNDEFINED");
  26.  
  27. }
  28.  
  29. NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  30. boolean connected = info.isConnected();
  31. if(connected) {
  32. Toast.makeText(context, "WIFI CONNECTED!", Toast.LENGTH_LONG).show();
  33. Log.i("Wi-Fi-State", "Wi-Fi is On!");
  34. WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
  35. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  36.  
  37. if(wifiInfo.getSSID().contains(ssid) == true) {
  38. try {
  39. String output = new Connection().execute().get().toString();
  40. Log.i("LoginState", new Connection().execute().get().toString());
  41.  
  42. if(output.contains("Address")) {
  43. Toast.makeText(WLANService.this, "Login Success!", Toast.LENGTH_SHORT).show();
  44. Intent account_info_intent = new Intent(WLANService.this, AccountInfo.class);
  45. account_info_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  46. startActivity(account_info_intent);
  47. }else {
  48. if(output.contains("n/a")) {
  49. Toast.makeText(WLANService.this, "Login Failed!", Toast.LENGTH_SHORT).show();
  50. }
  51. }
  52.  
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. } catch (ExecutionException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. } else {
  60. Toast.makeText(context, "WIFI DISCONNECTED!", Toast.LENGTH_SHORT).show();
  61. //Log.i("Wi-Fi-State", "Wi-Fi is Off!");
  62. }
  63. }
  64. };
  65.  
  66. public WLANService() {
  67.  
  68. }
  69.  
  70. @Override
  71. public IBinder onBind(Intent intent) {
  72. return null;
  73. }
  74.  
  75. @Override
  76. public void onCreate() {
  77. super.onCreate();
  78. Toast.makeText(this, "Auto-Login Enabled!", Toast.LENGTH_SHORT).show();
  79. }
  80.  
  81. @Override
  82. public int onStartCommand(Intent intent, int flags, int startId) {
  83. // registering your receiver
  84. registerReceiver(receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
  85. return START_STICKY;
  86. }
  87.  
  88. @Override
  89. public void onDestroy() {
  90. Toast.makeText(this, "Auto-Login Disabled!", Toast.LENGTH_SHORT).show();
  91. unregisterReceiver(receiver);
  92. super.onDestroy();
  93. }
  94.  
  95. private class Connection extends AsyncTask {
  96.  
  97. @Override
  98. protected Object doInBackground(Object[] objects) {
  99. String formatted_url = url.replace("http://", "");
  100. String true_url;
  101. if(formatted_url.charAt((formatted_url.length()-1)) != '/') {
  102. true_url = formatted_url.concat("/");
  103. }else {
  104. true_url = formatted_url;
  105. }
  106. Log.i("formatted_url", formatted_url);
  107. Log.i("true_url", true_url);
  108. return LoginHelper.doLogin(username, password, "http://".concat(true_url));
  109. }
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement