Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. package com.project.test;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.net.ConnectivityManager;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.preference.PreferenceManager;
  13. import android.telephony.TelephonyManager;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import edu.bellevue.android.blackboard.BlackboardHelper;
  21.  
  22. public class MainActivity extends Activity implements Runnable{
  23. /** Called when the activity is first created. */
  24.  
  25. private ProgressDialog pd;
  26. private Handler handler = new Handler(){
  27. public void handleMessage(Message m)
  28. {
  29. pd.setMessage("Success!");
  30. if(BlackboardHelper.isLoggedIn()){
  31. Intent i = new Intent(MainActivity.this,CourseViewer.class);
  32. startActivity(i);
  33. }
  34. pd.dismiss();
  35. }
  36. };
  37.  
  38. public boolean onCreateOptionsMenu(Menu m)
  39. {
  40. m.add("Settings");
  41. return super.onCreateOptionsMenu(m);
  42. }
  43.  
  44. public boolean onOptionsItemSelected(MenuItem mi)
  45. {
  46. if (mi.getTitle().equals("Settings"))
  47. {
  48. Intent i = new Intent(this,PrefActivity.class);
  49. startActivity(i);
  50. }
  51. return true;
  52. }
  53. @Override
  54. public void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.main);
  57. Button loginButton = (Button)findViewById(R.id.btnLogIn);
  58. loginButton.setOnClickListener(new View.OnClickListener() {
  59.  
  60. public void onClick(View v) {
  61. Thread t = new Thread(MainActivity.this);
  62. pd = ProgressDialog.show(MainActivity.this, "Please Wait", "Logging In...");
  63. t.start();
  64. }
  65. });
  66.  
  67. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  68. if (prefs.getBoolean("autologin", false)){
  69. ((TextView)findViewById(R.id.txtUserName)).setText(prefs.getString("username","n/a"));
  70. ((TextView)findViewById(R.id.txtPassword)).setText(prefs.getString("password","n/a"));
  71. Thread t = new Thread(MainActivity.this);
  72. pd = ProgressDialog.show(MainActivity.this, "Please Wait", "Logging In...");
  73. t.start();
  74. return;
  75. }
  76.  
  77.  
  78.  
  79. }
  80.  
  81. public void run()
  82. {
  83. // TODO Auto-generated method stub
  84. String userName = (((TextView)findViewById(R.id.txtUserName)).getText()).toString();
  85. String password = (((TextView)findViewById(R.id.txtPassword)).getText()).toString();
  86.  
  87. BlackboardHelper.logIn(userName, password);
  88. handler.sendEmptyMessage(0);
  89. }
  90.  
  91. public static boolean shouldConnect(SharedPreferences prefs, Context ctx)
  92. {
  93.  
  94. ConnectivityManager connMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
  95. int netType = connMan.getActiveNetworkInfo().getType();
  96. int netSubType = connMan.getActiveNetworkInfo().getSubtype();
  97.  
  98. if (netType == ConnectivityManager.TYPE_WIFI)
  99. {
  100. return prefs.getBoolean("wifi", false);
  101. } else if (netType == ConnectivityManager.TYPE_MOBILE)
  102. {
  103. if (prefs.getBoolean("mobile",false))
  104. {
  105. TelephonyManager tm = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);
  106. if (tm.isNetworkRoaming())
  107. {
  108. return (prefs.getBoolean("roaming",false));
  109. }else
  110. {
  111. return true;
  112. }
  113.  
  114. }else
  115. {
  116. return false;
  117. }
  118. }else
  119. {return false;}
  120.  
  121. }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement