Application settings: added connection and credentials testing thread
authordrizea <danielrizea27@gmail.com>
Thu, 14 Jun 2012 16:24:28 +0000 (19:24 +0300)
committerdrizea <danielrizea27@gmail.com>
Thu, 14 Jun 2012 16:24:28 +0000 (19:24 +0300)
Open-ILS/src/Android/AndroidManifest.xml
Open-ILS/src/Android/bin/EvergreenApp.apk
Open-ILS/src/Android/bin/classes.dex
Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/GlobalConfigs.class
Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/Utils.class
Open-ILS/src/Android/bin/resources.ap_
Open-ILS/src/Android/src/org/evergreen/android/globals/GlobalConfigs.java
Open-ILS/src/Android/src/org/evergreen/android/globals/NoAccessToHttpAddress.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/globals/NoNetworkAccessException.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/globals/Utils.java
Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java

index 59a5bc7..73aed05 100644 (file)
@@ -5,6 +5,7 @@
     android:versionName="1.0" >
 
     <uses-sdk android:minSdkVersion="8" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.INTERNET" /> 
     <application
         android:icon="@drawable/ic_launcher"
index dd27bde..dbb7510 100644 (file)
Binary files a/Open-ILS/src/Android/bin/EvergreenApp.apk and b/Open-ILS/src/Android/bin/EvergreenApp.apk differ
index 0f3e178..ee1dc7c 100644 (file)
Binary files a/Open-ILS/src/Android/bin/classes.dex and b/Open-ILS/src/Android/bin/classes.dex differ
index 4e4ddd1..6ec8940 100644 (file)
Binary files a/Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/GlobalConfigs.class and b/Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/GlobalConfigs.class differ
index c7d3d1e..5738c7c 100644 (file)
Binary files a/Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/Utils.class and b/Open-ILS/src/Android/bin/classes/org/evergreen/android/globals/Utils.class differ
index 890eb4c..4e5716c 100644 (file)
Binary files a/Open-ILS/src/Android/bin/resources.ap_ and b/Open-ILS/src/Android/bin/resources.ap_ differ
index 4d1a654..caf1eac 100644 (file)
@@ -95,7 +95,10 @@ public class GlobalConfigs {
                }catch(Exception e){};
                
                
+               
                if(orgFile != null){
+                       organisations = new ArrayList<Organisation>();
+                       
                        System.out.println("Page content " + orgFile);
                        
                        String orgArray = orgFile.substring( orgFile.indexOf("=")+1, orgFile.indexOf(";"));  
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/globals/NoAccessToHttpAddress.java b/Open-ILS/src/Android/src/org/evergreen/android/globals/NoAccessToHttpAddress.java
new file mode 100644 (file)
index 0000000..89a9433
--- /dev/null
@@ -0,0 +1,10 @@
+package org.evergreen.android.globals;
+
+public class NoAccessToHttpAddress extends Exception{
+
+       /**
+        * 
+        */
+       private static final long serialVersionUID = 1L;
+
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/globals/NoNetworkAccessException.java b/Open-ILS/src/Android/src/org/evergreen/android/globals/NoNetworkAccessException.java
new file mode 100644 (file)
index 0000000..e13b34a
--- /dev/null
@@ -0,0 +1,8 @@
+package org.evergreen.android.globals;
+
+public class NoNetworkAccessException extends Exception{
+       /**
+        */
+       private static final long serialVersionUID = 1L;
+
+}
index 27dd591..0c0d058 100644 (file)
@@ -1,17 +1,30 @@
 package org.evergreen.android.globals;
 
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.InetAddress;
+import java.net.SocketTimeoutException;
 
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.test.IsolatedContext;
+import android.util.Log;
 
 public class Utils {
 
-       
+       private static String TAG = "Utils";
        /**
         * Gets the net page content.
         *
@@ -78,4 +91,87 @@ public class Utils {
                
                return in;
        }
+       
+       /**
+        *  Checks to see if network access is enabled
+        *  
+        *  @throws NoNetworkAccessException if neither data connection or wifi are enabled
+        *              NoAccessToHttpAddress if the library remote server can't be reached
+        *   
+        */
+       public static boolean checkNetworkStatus(ConnectivityManager cm, Context context )
+                       throws NoNetworkAccessException, NoAccessToHttpAddress{
+               
+               boolean HaveConnectedWifi = false;
+               boolean HaveConnectedMobile = false;
+               
+               
+               boolean networkAccessEnabled = false;
+               boolean httpAddressAccessReachable = false;
+               
+               NetworkInfo ni = cm.getActiveNetworkInfo();
+               if ( ni != null )
+               {
+                   if (ni.getType() == ConnectivityManager.TYPE_WIFI)
+                       if (ni.isConnected()){
+                           HaveConnectedWifi = true;
+                           networkAccessEnabled = true;
+                       }
+                   if (ni.getType() == ConnectivityManager.TYPE_MOBILE)
+                       if (ni.isConnected()){
+                           HaveConnectedMobile = true;
+                           networkAccessEnabled = true;
+                       }
+               }
+
+               
+               if(networkAccessEnabled){
+                       //check to see if httpAddress is avaialble using the network connection 
+                       // 2 seconds timeout
+                       httpAddressAccessReachable = checkIfNetAddressIsReachable(GlobalConfigs.httpAddress);
+               
+                       if(httpAddressAccessReachable == false)
+                               throw new NoAccessToHttpAddress();
+               }
+
+               if(!networkAccessEnabled)
+                       throw new NoNetworkAccessException();
+               
+               return networkAccessEnabled;
+               
+       }
+       
+       public static boolean checkIfNetAddressIsReachable(String url){
+               
+               boolean result = false;
+               try
+               {
+                   HttpGet request = new HttpGet(url);
+
+                   HttpParams httpParameters = new BasicHttpParams();  
+
+                   //timeout to 3 seconds
+                   HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
+                   HttpClient httpClient = new DefaultHttpClient(httpParameters);
+                   HttpResponse response = httpClient.execute(request);
+
+                   int status = response.getStatusLine().getStatusCode();
+
+                   if (status == HttpStatus.SC_OK) 
+                   {
+                       result = true;
+                   }
+
+               }
+               catch (SocketTimeoutException e)
+               {
+                   result = false; // this is somewhat expected
+               }
+               catch (Exception e) {
+                       Log.d(TAG, "Exception in is reachable " + e.getMessage());
+               }
+
+               return result;
+       }
+       
 }
index 53070e2..c745c5c 100644 (file)
@@ -3,20 +3,34 @@ package org.evergreen.android.views;
 import org.evergreen.android.R;
 import org.evergreen.android.accountAccess.AccountAccess;
 import org.evergreen.android.globals.GlobalConfigs;
+import org.evergreen.android.globals.NoAccessToHttpAddress;
+import org.evergreen.android.globals.NoNetworkAccessException;
+import org.evergreen.android.globals.Utils;
 
+import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.net.ConnectivityManager;
 import android.os.Bundle;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceManager;
+import android.provider.Settings;
+import android.util.Log;
+import android.widget.Toast;
 
 public class ApplicationPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener{
 
        
        private ProgressDialog progressDialog;
        
+       
+       private Context context;
+       
+       private String TAG = "ApplicationPreferences";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
 
@@ -24,7 +38,7 @@ public class ApplicationPreferences extends PreferenceActivity implements OnShar
                
                addPreferencesFromResource(R.xml.application_preference_screen);
                
-               Context context = getApplicationContext();
+               context = this;
                
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                //register preference listener
@@ -47,29 +61,103 @@ public class ApplicationPreferences extends PreferenceActivity implements OnShar
                
                //test connection
                
-               progressDialog = ProgressDialog.show(this, "Account login", "Please wait while we test the new user account information");
+               progressDialog = ProgressDialog.show(this, "Account login", "Please wait while we test the new user account information");      
                
                Thread thread = new Thread(new Runnable() {
                        
                        @Override
                        public void run() {
                                
+                               boolean routeToAddress = true;
                                AccountAccess account = new AccountAccess(GlobalConfigs.httpAddress);
-                               account.authenticate();
-                               runOnUiThread(new Runnable() {
+                               try{
+                                       Utils.checkNetworkStatus((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE), getApplicationContext());
+                               }catch(NoNetworkAccessException e){
+                                       routeToAddress = false;
                                        
-                                       @Override
-                                       public void run() {
-                                               progressDialog.dismiss();
+                                       Log.d(TAG," No network access");
+                                       
+                                       runOnUiThread(new Runnable() {
                                                
+                                               @Override
+                                               public void run() {
+                                                       progressDialog.dismiss();
+                                                       AlertDialog.Builder builder = new AlertDialog.Builder(context);
+                                                       builder.setMessage("There seams to be no network connectivity! Do you want to start network settings?").setPositiveButton("Yes", dialogClickListener)
+                                                   .setNegativeButton("No", dialogClickListener).show();
+                                               }
+                                       });
+
+                               }catch(NoAccessToHttpAddress e){
+                                       
+                                       Log.d(TAG, " no route to hoast");
+                                       routeToAddress = false;
+                                       runOnUiThread(new Runnable() {
+                                               
+                                               @Override
+                                               public void run() {
+                                                       Toast.makeText(getApplicationContext(), "There is no route to host :" + GlobalConfigs.httpAddress, Toast.LENGTH_LONG);
+                                               }
+       
+                                       });
+                               }
+
+                               
+                               if(routeToAddress){
+                                       if(account.authenticate()){
+                                                                       
+                                               runOnUiThread(new Runnable() {
+                                                       
+                                                       @Override
+                                                       public void run() {
+                                                               progressDialog.dismiss();
+                                                               Toast.makeText(context, "Autenthication successfully established :" + GlobalConfigs.httpAddress, Toast.LENGTH_LONG);
+                                                       }
+                                               });
+                                       }else{
+                                               runOnUiThread(new Runnable() {
+                                                       
+                                                       @Override
+                                                       public void run() {
+                                                               progressDialog.dismiss();
+                                                               Toast.makeText(context, "Please check username and password ", Toast.LENGTH_LONG);
+                                                       }
+                                               });
                                        }
-                               });
+                               }
+                               else
+                                       runOnUiThread(new Runnable() {
+                                               
+                                               @Override
+                                               public void run() {
+                                               progressDialog.dismiss();       
+                                               }
+                                       });
                        }
                });
                
                thread.start();
        }
+/*
+ *  Dialog interface for starting the network settings
+ */
+       DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
+           @Override
+           public void onClick(DialogInterface dialog, int which) {
+               switch (which){
+               case DialogInterface.BUTTON_POSITIVE:
+                   //Yes button clicked
+                       
+                       context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
 
-       
+                   break;
+
+               case DialogInterface.BUTTON_NEGATIVE:
+                   //No button clicked
+                       
+                   break;
+               }
+           }
+       };
        
 }