Bug 1041632 - Part 4: eliminate enclosing instance member access in FilePickerResultHandler. r=ckitching
authorRichard Newman <rnewman@mozilla.com>
Mon, 21 Jul 2014 10:19:41 -0700
changeset 195367 f2cd4cc933c55f288e0d2fa186d7ad877bea535e
parent 195366 0ca1c7de5bd411f80998bf5d26352715c167d06a
child 195368 0f90d365211db06383e134042e8daa38fba5cce2
push id46575
push userkwierso@gmail.com
push dateTue, 22 Jul 2014 00:35:21 +0000
treeherdermozilla-inbound@fee5c4bdd713 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckitching
bugs1041632
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1041632 - Part 4: eliminate enclosing instance member access in FilePickerResultHandler. r=ckitching
mobile/android/base/FilePickerResultHandler.java
--- a/mobile/android/base/FilePickerResultHandler.java
+++ b/mobile/android/base/FilePickerResultHandler.java
@@ -1,63 +1,62 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
-import org.mozilla.gecko.mozglue.GeckoLoader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
 import org.mozilla.gecko.util.ActivityResultHandler;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Environment;
+import android.os.Process;
 import android.provider.MediaStore;
 import android.provider.OpenableColumns;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.CursorLoader;
 import android.support.v4.content.Loader;
 import android.text.TextUtils;
 import android.text.format.Time;
 import android.util.Log;
-import android.os.Process;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.IOException;
 
 class FilePickerResultHandler implements ActivityResultHandler {
     private static final String LOGTAG = "GeckoFilePickerResultHandler";
     private static final String UPLOADS_DIR = "uploads";
 
-    protected final FilePicker.ResultHandler handler;
+    private final FilePicker.ResultHandler handler;
     private final int tabId;
     private final File cacheDir;
 
     // this code is really hacky and doesn't belong anywhere so I'm putting it here for now
     // until I can come up with a better solution.
     private String mImageName = "";
 
     /* Use this constructor to asynchronously listen for results */
     public FilePickerResultHandler(final FilePicker.ResultHandler handler, final Context context, final int tabId) {
         this.tabId = tabId;
-        cacheDir = new File(context.getCacheDir(), UPLOADS_DIR);
+        this.cacheDir = new File(context.getCacheDir(), UPLOADS_DIR);
         this.handler = handler;
     }
 
-    private void sendResult(String res) {
+    void sendResult(String res) {
         if (handler != null) {
             handler.gotFile(res);
         }
     }
 
     @Override
     public void onActivityResult(int resultCode, Intent intent) {
         if (resultCode != Activity.RESULT_OK) {
@@ -106,18 +105,17 @@ class FilePickerResultHandler implements
         } catch(Exception ex) {
             // We'll try a different loader below
         } finally {
             if (cursor != null) {
                 cursor.close();
             }
         }
 
-        lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri));
-        return;
+        lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri, cacheDir, tabId));
     }
 
     public String generateImageName() {
         Time now = new Time();
         now.setToNow();
         mImageName = now.format("%Y-%m-%d %H.%M.%S") + ".jpg";
         return mImageName;
     }
@@ -155,30 +153,37 @@ class FilePickerResultHandler implements
             } else {
                 tryFileLoaderCallback();
             }
         }
 
         private void tryFileLoaderCallback() {
             final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
             final LoaderManager lm = fa.getSupportLoaderManager();
-            lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri));
+            lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri, cacheDir, tabId));
         }
 
         @Override
         public void onLoaderReset(Loader<Cursor> loader) { }
     }
 
+    /**
+     * This class's only dependency on FilePickerResultHandler is sendResult.
+     */
     private class FileLoaderCallbacks implements LoaderCallbacks<Cursor>,
                                                  Tabs.OnTabsChangedListener {
-        final private Uri uri;
-        private String tempFile;
+        private final Uri uri;
+        private final File cacheDir;
+        private final int tabId;
+        /* inner-access */ String tempFile;
 
-        public FileLoaderCallbacks(Uri uri) {
+        public FileLoaderCallbacks(Uri uri, File cacheDir, int tabId) {
             this.uri = uri;
+            this.cacheDir = cacheDir;
+            this.tabId = tabId;
         }
 
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
             return new CursorLoader(fa,
                                     uri,
                                     new String[] { OpenableColumns.DISPLAY_NAME },
@@ -188,17 +193,17 @@ class FilePickerResultHandler implements
         }
 
         @Override
         public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
             if (cursor.moveToFirst()) {
                 String name = cursor.getString(0);
                 // tmp filenames must be at least 3 characters long. Add a prefix to make sure that happens
                 String fileName = "tmp_" + Process.myPid() + "-";
-                String fileExt = null;
+                String fileExt;
                 int period;
 
                 final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
                 final ContentResolver cr = fa.getContentResolver();
 
                 // Generate an extension if we don't already have one
                 if (name == null || (period = name.lastIndexOf('.')) == -1) {
                     String mimeType = cr.getType(uri);