Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7f0314d authored by Matt Garnes's avatar Matt Garnes
Browse files

Detect text encoding with juniversalchardet.

- When opening files in the built in editor for display, detect the
  encoding with juniversalchardet, so that the correct encoding will be
  used.
- Only supported in the JavaConsole (non-privileged).

Change-Id: If35229c4aee97526eab10be59a40d1dac3e43036
(cherry picked from commit b0d83a69)
parent 58810e2c
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ LOCAL_AAPT_FLAGS := --auto-add-overlay


LOCAL_STATIC_JAVA_LIBRARIES += libtruezip
LOCAL_STATIC_JAVA_LIBRARIES += libtruezip
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4
LOCAL_STATIC_JAVA_LIBRARIES += juniversalchardet


LOCAL_RESOURCE_DIR += $(addprefix $(LOCAL_PATH)/, themes/res res)
LOCAL_RESOURCE_DIR += $(addprefix $(LOCAL_PATH)/, themes/res res)


@@ -36,5 +37,10 @@ LOCAL_CERTIFICATE := platform
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_PROGUARD_FLAG_FILES := proguard.flags


include $(BUILD_PACKAGE)
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := \
    juniversalchardet:libs/juniversalchardet/juniversalchardet-1.0.3.jar


include $(BUILD_MULTI_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))
include $(call all-makefiles-under,$(LOCAL_PATH))
+470 −0

File added.

Preview size limit exceeded, changes collapsed.

+202 KiB

File added.

No diff preview for this file type.

+29 −3
Original line number Original line Diff line number Diff line
@@ -69,7 +69,12 @@ import com.cyanogenmod.filemanager.ash.SyntaxHighlightFactory;
import com.cyanogenmod.filemanager.ash.SyntaxHighlightProcessor;
import com.cyanogenmod.filemanager.ash.SyntaxHighlightProcessor;
import com.cyanogenmod.filemanager.commands.AsyncResultListener;
import com.cyanogenmod.filemanager.commands.AsyncResultListener;
import com.cyanogenmod.filemanager.commands.WriteExecutable;
import com.cyanogenmod.filemanager.commands.WriteExecutable;
import com.cyanogenmod.filemanager.commands.shell.InvalidCommandDefinitionException;
import com.cyanogenmod.filemanager.console.Console;
import com.cyanogenmod.filemanager.console.ConsoleAllocException;
import com.cyanogenmod.filemanager.console.ConsoleBuilder;
import com.cyanogenmod.filemanager.console.ConsoleBuilder;
import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
import com.cyanogenmod.filemanager.console.java.JavaConsole;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.Preferences;
import com.cyanogenmod.filemanager.preferences.Preferences;
@@ -86,11 +91,13 @@ import com.cyanogenmod.filemanager.util.FileHelper;
import com.cyanogenmod.filemanager.util.MediaHelper;
import com.cyanogenmod.filemanager.util.MediaHelper;
import com.cyanogenmod.filemanager.util.ResourcesHelper;
import com.cyanogenmod.filemanager.util.ResourcesHelper;
import com.cyanogenmod.filemanager.util.StringHelper;
import com.cyanogenmod.filemanager.util.StringHelper;
import org.mozilla.universalchardet.UniversalDetector;


import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -275,12 +282,19 @@ public class EditorActivity extends Activity implements TextWatcher {
        long mSize;
        long mSize;
        FileSystemObject mReadFso;
        FileSystemObject mReadFso;
        OnProgressListener mListener;
        OnProgressListener mListener;
        boolean mDetectEncoding = false;
        UniversalDetector mDetector;
        String mDetectedEncoding;


        /**
        /**
         * Constructor of <code>AsyncReader</code>. For enclosing access.
         * Constructor of <code>AsyncReader</code>. For enclosing access.
         */
         */
        public AsyncReader() {
        public AsyncReader(boolean detectEncoding) {
            super();
            super();
            mDetectEncoding = detectEncoding;
            if (mDetectEncoding) {
                mDetector = new UniversalDetector(null);
            }
        }
        }


        /**
        /**
@@ -300,6 +314,9 @@ public class EditorActivity extends Activity implements TextWatcher {
            if (!cancelled && StringHelper.isBinaryData(mByteBuffer.toByteArray())) {
            if (!cancelled && StringHelper.isBinaryData(mByteBuffer.toByteArray())) {
                EditorActivity.this.mBinary = true;
                EditorActivity.this.mBinary = true;
                EditorActivity.this.mReadOnly = true;
                EditorActivity.this.mReadOnly = true;
            } else if (mDetector != null) {
                mDetector.dataEnd();
                mDetectedEncoding = mDetector.getDetectedCharset();
            }
            }
        }
        }


@@ -321,6 +338,9 @@ public class EditorActivity extends Activity implements TextWatcher {
            try {
            try {
                if (result == null) return;
                if (result == null) return;
                byte[] partial = (byte[]) result;
                byte[] partial = (byte[]) result;
                if (mDetectEncoding) {
                    mDetector.handleData(partial, 0, partial.length);
                }
                this.mByteBuffer.write(partial, 0, partial.length);
                this.mByteBuffer.write(partial, 0, partial.length);
                this.mSize += partial.length;
                this.mSize += partial.length;
                if (this.mListener != null && this.mReadFso != null) {
                if (this.mListener != null && this.mReadFso != null) {
@@ -1108,7 +1128,7 @@ public class EditorActivity extends Activity implements TextWatcher {
                try {
                try {
                    while (true) {
                    while (true) {
                        // Configure the reader
                        // Configure the reader
                        this.mReader = new AsyncReader();
                        this.mReader = new AsyncReader(true);
                        this.mReader.mReadFso = fso;
                        this.mReader.mReadFso = fso;
                        this.mReader.mListener = new OnProgressListener() {
                        this.mReader.mListener = new OnProgressListener() {
                            @Override
                            @Override
@@ -1153,7 +1173,13 @@ public class EditorActivity extends Activity implements TextWatcher {
                        }
                        }
                        Log.i(TAG, "Bytes read: " + data.length()); //$NON-NLS-1$
                        Log.i(TAG, "Bytes read: " + data.length()); //$NON-NLS-1$
                    } else {
                    } else {
                        final String data = new String(this.mReader.mByteBuffer.toByteArray());
                        String data;
                        if (this.mReader.mDetectedEncoding != null) {
                            data = new String(this.mReader.mByteBuffer.toByteArray(),
                                              this.mReader.mDetectedEncoding);
                        } else {
                            data = new String(this.mReader.mByteBuffer.toByteArray());
                        }
                        this.mReader.mBuffer = new SpannableStringBuilder(data);
                        this.mReader.mBuffer = new SpannableStringBuilder(data);
                        Log.i(TAG, "Bytes read: " + data.getBytes().length); //$NON-NLS-1$
                        Log.i(TAG, "Bytes read: " + data.getBytes().length); //$NON-NLS-1$
                    }
                    }