Browse Source

Merge pull request #1333 from nextcloud/textEncoding

Auto detect encoding for text preview
Tobias Kaminsky 7 years ago
parent
commit
4e013b5bb6
2 changed files with 23 additions and 17 deletions
  1. 1 0
      build.gradle
  2. 22 17
      src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java

+ 1 - 0
build.gradle

@@ -202,6 +202,7 @@ dependencies {
     implementation "com.android.support:cardview-v7:${supportLibraryVersion}"
     implementation "com.android.support:exifinterface:${supportLibraryVersion}"
     implementation 'com.github.tobiasKaminsky:android-floating-action-button:1.10.2'
+    implementation 'com.github.albfernandez:juniversalchardet:v2.0.0'
     implementation 'com.google.code.findbugs:annotations:2.0.1'
     implementation 'commons-io:commons-io:2.5'
     implementation 'com.github.evernote:android-job:v1.1.11'

+ 22 - 17
src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java

@@ -1,4 +1,4 @@
-/**
+/*
  *   ownCloud Android client application
  *
  *   Copyright (C) 2016 ownCloud Inc.
@@ -45,10 +45,12 @@ import com.owncloud.android.ui.fragment.FileFragment;
 import com.owncloud.android.utils.AnalyticsUtils;
 import com.owncloud.android.utils.MimeTypeUtil;
 
+import org.mozilla.universalchardet.ReaderFactory;
+
 import java.io.BufferedWriter;
-import java.io.FileInputStream;
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.StringWriter;
 import java.lang.ref.WeakReference;
 import java.util.LinkedList;
@@ -78,7 +80,7 @@ public class PreviewTextFragment extends FileFragment {
     /**
      * Creates an empty fragment for previews.
      * <p/>
-     * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
+     * MUST BE KEPT: the system uses it when tries to re-instantiate a fragment automatically
      * (for instance, when the device is turned a aside).
      * <p/>
      * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
@@ -208,23 +210,26 @@ public class PreviewTextFragment extends FileFragment {
             if (params.length != 1) {
                 throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location");
             }
-            final String location = (String) params[0];
+            String location = (String) params[0];
 
-            InputStreamReader inputStream = null;
-            Scanner sc = null;
+            Scanner scanner = null;
             StringWriter source = new StringWriter();
             BufferedWriter bufferedWriter = new BufferedWriter(source);
+            Reader reader = null;
+
             try {
-                inputStream = new InputStreamReader(new FileInputStream(location), "UTF8");
-                sc = new Scanner(inputStream);
-                while (sc.hasNextLine()) {
-                    bufferedWriter.append(sc.nextLine());
-                    if (sc.hasNextLine()) {
+                File file = new File(location);
+                reader = ReaderFactory.createReaderFromFile(file);
+                scanner = new Scanner(reader);
+
+                while (scanner.hasNextLine()) {
+                    bufferedWriter.append(scanner.nextLine());
+                    if (scanner.hasNextLine()) {
                         bufferedWriter.append("\n");
                     }
                 }
                 bufferedWriter.close();
-                IOException exc = sc.ioException();
+                IOException exc = scanner.ioException();
                 if (exc != null) {
                     throw exc;
                 }
@@ -232,16 +237,16 @@ public class PreviewTextFragment extends FileFragment {
                 Log_OC.e(TAG, e.getMessage(), e);
                 finish();
             } finally {
-                if (inputStream != null) {
+                if (reader != null) {
                     try {
-                        inputStream.close();
+                        reader.close();
                     } catch (IOException e) {
                         Log_OC.e(TAG, e.getMessage(), e);
                         finish();
                     }
                 }
-                if (sc != null) {
-                    sc.close();
+                if (scanner != null) {
+                    scanner.close();
                 }
             }
             return source;