Browse Source

Fixing duplicated code

alejandro 8 years ago
parent
commit
887645921f

+ 2 - 16
src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -115,9 +115,6 @@ import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
 import com.owncloud.android.utils.DisplayUtils;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.security.cert.X509Certificate;
 import java.util.Map;
 
@@ -341,19 +338,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             }
 
             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
-                BufferedReader buffreader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.custom_error)));
-                String line;
-                StringBuilder text = new StringBuilder();
-                try {
-                    while (( line = buffreader.readLine()) != null) {
-                        text.append(line);
-                        text.append('\n');
-                    }
-                } catch (IOException e) {
-                    Log_OC.e(TAG,e.getMessage());
-                    return;
-                }
-                mLoginWebView.loadData(text.toString(),"text/html; charset=UTF-8", null);
+
+                mLoginWebView.loadData(DisplayUtils.getData(getResources().openRawResource(R.raw.custom_error)),"text/html; charset=UTF-8", null);
             }
         });
     }

+ 2 - 20
src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java

@@ -36,10 +36,7 @@ import android.widget.ProgressBar;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.lib.common.utils.Log_OC;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
+import com.owncloud.android.utils.DisplayUtils;
 
 /**
  * This activity shows an URL as a web view
@@ -126,22 +123,7 @@ public class ExternalSiteWebView extends FileActivity {
 
         webview.setWebViewClient(new WebViewClient() {
             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
-
-                BufferedReader buffreader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.custom_error)));
-                String line;
-                StringBuilder text = new StringBuilder();
-                try {
-                    while (( line = buffreader.readLine()) != null) {
-                        text.append(line);
-                        text.append('\n');
-                    }
-                } catch (IOException e) {
-                    Log_OC.e(TAG,e.getMessage());
-                    return;
-                }
-
-                webview.loadData(text.toString(),"text/html; charset=UTF-8", null);
-
+                webview.loadData(DisplayUtils.getData(getResources().openRawResource(R.raw.custom_error)),"text/html; charset=UTF-8", null);
             }
         });
 

+ 26 - 0
src/main/java/com/owncloud/android/utils/DisplayUtils.java

@@ -81,7 +81,10 @@ import com.owncloud.android.utils.svg.SvgDrawableTranscoder;
 import org.greenrobot.eventbus.EventBus;
 import org.parceler.Parcels;
 
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.math.BigDecimal;
 import java.net.IDN;
 import java.text.DateFormat;
@@ -646,4 +649,27 @@ public class DisplayUtils {
             activity.startActivity(recentlyAddedIntent);
         }
     }
+
+
+    /**
+     * Get String data from a InputStream
+     *
+     * @param inputStream        The File InputStream
+     */
+    public static String getData(InputStream inputStream){
+
+        BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputStream));
+        String line;
+        StringBuilder text = new StringBuilder();
+        try {
+            while (( line = buffreader.readLine()) != null) {
+                text.append(line);
+                text.append('\n');
+            }
+        } catch (IOException e) {
+            Log_OC.e(TAG,e.getMessage());
+        }
+        return text.toString();
+    }
+
 }