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

Commit cda94482 authored by Steve Block's avatar Steve Block
Browse files

Fix FsUtils.readDataFromUrl() to do the network request on a background thread

This is required now that doing network requests on the main thread
throws NetworkOnMainThreadException.

Also updates TestsListPreloaderThread to create the FileFilter lazily
and removes a superfluous FileFilter param from the Summarizer
constructor.

Change-Id: If15fddac934cd8be611f6693b25837657abbddc3
parent 01b04e10
Loading
Loading
Loading
Loading
+60 −33
Original line number Diff line number Diff line
@@ -131,13 +131,28 @@ public class FsUtils {
        return bytes;
    }

    public static byte[] readDataFromUrl(URL url) {
        if (url == null) {
            Log.w(LOG_TAG, "readDataFromUrl(): url is null!");
            return null;
    static class UrlDataGetter extends Thread {
        private URL mUrl;
        private byte[] mBytes;
        private boolean mGetComplete;
        public UrlDataGetter(URL url) {
            mUrl = url;
        }
        public byte[] get() {
            start();
            synchronized(this) {
                while (!mGetComplete) {
                    try{
                        wait();
                    } catch(InterruptedException e) {
                    }

        HttpGet httpRequest = new HttpGet(url.toString());
                }
            }
            return mBytes;
        }
        public synchronized void run() {
            mGetComplete = false;
            HttpGet httpRequest = new HttpGet(mUrl.toString());
            ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
                @Override
                public byte[] handleResponse(HttpResponse response) throws IOException {
@@ -149,7 +164,7 @@ public class FsUtils {
                }
            };

        byte[] bytes = null;
            mBytes = null;
            try {
                /**
                 * TODO: Not exactly sure why some requests hang indefinitely, but adding this
@@ -159,17 +174,29 @@ public class FsUtils {
                do {
                    timedOut = false;
                    try {
                    bytes = getHttpClient().execute(httpRequest, handler);
                        mBytes = getHttpClient().execute(httpRequest, handler);
                    } catch (SocketTimeoutException e) {
                        timedOut = true;
                    Log.w(LOG_TAG, "Expected SocketTimeoutException: " + url, e);
                        Log.w(LOG_TAG, "Expected SocketTimeoutException: " + mUrl, e);
                    }
                } while (timedOut);
            } catch (IOException e) {
            Log.e(LOG_TAG, "url=" + url, e);
                Log.e(LOG_TAG, "url=" + mUrl, e);
            }

        return bytes;
            mGetComplete = true;
            notify();
        }
    }

    public static byte[] readDataFromUrl(URL url) {
        if (url == null) {
            Log.w(LOG_TAG, "readDataFromUrl(): url is null!");
            return null;
        }

        UrlDataGetter getter = new UrlDataGetter(url);
        return getter.get();
    }

    public static List<String> getLayoutTestsDirContents(String dirRelativePath, boolean recurse,
+1 −3
Original line number Diff line number Diff line
@@ -127,7 +127,6 @@ public class ManagerService extends Service {
        }
    };

    private FileFilter mFileFilter;
    private Summarizer mSummarizer;

    private String mCurrentlyRunningTest;
@@ -146,8 +145,7 @@ public class ManagerService extends Service {
    public void onCreate() {
        super.onCreate();

        mFileFilter = new FileFilter();
        mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH, getApplicationContext());
        mSummarizer = new Summarizer(RESULTS_ROOT_DIR_PATH, getApplicationContext());
    }

    @Override
+2 −2
Original line number Diff line number Diff line
@@ -213,8 +213,8 @@ public class Summarizer {

    private SummarizerDBHelper mDbHelper;

    public Summarizer(FileFilter fileFilter, String resultsRootDirPath, Context context) {
        mFileFilter = fileFilter;
    public Summarizer(String resultsRootDirPath, Context context) {
        mFileFilter = new FileFilter();
        mResultsRootDirPath = resultsRootDirPath;

        /**
+1 −1
Original line number Diff line number Diff line
@@ -50,13 +50,13 @@ public class TestsListPreloaderThread extends Thread {
     * @param doneMsg
     */
    public TestsListPreloaderThread(String path, Message doneMsg) {
        mFileFilter = new FileFilter();
        mRelativePath = path;
        mDoneMsg = doneMsg;
    }

    @Override
    public void run() {
        mFileFilter = new FileFilter();
        if (FileFilter.isTestFile(mRelativePath)) {
            mTestsList.add(mRelativePath);
        } else {