Fix NPE on null cursor close

Only query once for photo dimensions. Don't crash if cursor is null.
Use default of 720 if unable to query provider.

Bug:12552331
Change-Id: I2bd5ad3db86316b3e61d719bc44b524de8758387
(cherry picked from commit 409737c0585f374e2cd0ad1fea9128905a276b85)
diff --git a/src/com/android/contacts/activities/AttachPhotoActivity.java b/src/com/android/contacts/activities/AttachPhotoActivity.java
index 2d9e9f5..c09af3c 100644
--- a/src/com/android/contacts/activities/AttachPhotoActivity.java
+++ b/src/com/android/contacts/activities/AttachPhotoActivity.java
@@ -69,6 +69,8 @@
 
     // Height and width (in pixels) to request for the photo - queried from the provider.
     private static int mPhotoDim;
+    // Default photo dimension to use if unable to query the provider.
+    private static final int mDefaultPhotoDim = 720;
 
     private Uri mContactUri;
 
@@ -91,14 +93,20 @@
 
         mContentResolver = getContentResolver();
 
-        // Load the photo dimension to request.
-        Cursor c = mContentResolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
-                new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
-        try {
-            c.moveToFirst();
-            mPhotoDim = c.getInt(0);
-        } finally {
-            c.close();
+        // Load the photo dimension to request. mPhotoDim is a static class
+        // member varible so only need to load this if this is the first time
+        // through.
+        if (mPhotoDim == 0) {
+            Cursor c = mContentResolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
+                    new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
+            if (c != null) {
+                try {
+                    c.moveToFirst();
+                    mPhotoDim = c.getInt(0);
+                } finally {
+                    c.close();
+                }
+            }
         }
     }
 
@@ -128,28 +136,20 @@
             final Intent myIntent = getIntent();
             final Uri inputUri = myIntent.getData();
 
-            final int perm = checkUriPermission(inputUri, android.os.Process.myPid(),
-                    android.os.Process.myUid(), Intent.FLAG_GRANT_READ_URI_PERMISSION |
-                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
-
             final Uri toCrop;
-
-            if (perm == PackageManager.PERMISSION_DENIED) {
-                // Work around to save a read-only URI into a temporary file provider URI so that
-                // we can add the FLAG_GRANT_WRITE_URI_PERMISSION flag to the eventual
-                // crop intent b/10837468
-                ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false);
-                toCrop = mTempPhotoUri;
-            } else {
-                toCrop = inputUri;
-            }
+            // Save the URI into a temporary file provider URI so that
+            // we can add the FLAG_GRANT_WRITE_URI_PERMISSION flag to the eventual
+            // crop intent for read-only URI's.
+            // TODO: With b/10837468 fixed should be able to avoid this copy.
+            ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false);
+            toCrop = mTempPhotoUri;
 
             final Intent intent = new Intent("com.android.camera.action.CROP", toCrop);
             if (myIntent.getStringExtra("mimeType") != null) {
                 intent.setDataAndType(toCrop, myIntent.getStringExtra("mimeType"));
             }
             ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri);
-            ContactPhotoUtils.addCropExtras(intent, mPhotoDim);
+            ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim);
 
             startActivityForResult(intent, REQUEST_CROP_PHOTO);