OSDN Git Service

Do not use NfcAdapter API in old platforms.
authorYuli Huang <yuli@google.com>
Fri, 29 Jun 2012 10:59:17 +0000 (18:59 +0800)
committerYuli Huang <yuli@google.com>
Fri, 29 Jun 2012 11:23:17 +0000 (19:23 +0800)
bug:6698904
Change-Id: I8cab312a212e4cba43187a73bc76d3f2164527a9

src/com/android/gallery3d/app/PhotoPage.java
src/com/android/gallery3d/ui/ActionModeHandler.java

index 17f57c2..22b0163 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.gallery3d.app;
 
+import android.annotation.TargetApi;
 import android.app.ActionBar.OnMenuVisibilityListener;
 import android.app.Activity;
 import android.content.ActivityNotFoundException;
@@ -25,6 +26,7 @@ import android.content.Intent;
 import android.graphics.Rect;
 import android.net.Uri;
 import android.nfc.NfcAdapter;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
@@ -37,6 +39,7 @@ import android.widget.ShareActionProvider;
 import android.widget.Toast;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.common.ApiHelper;
 import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.data.DataManager;
 import com.android.gallery3d.data.FilterDeleteSet;
@@ -318,18 +321,24 @@ public class PhotoPage extends ActivityState implements
         }
     }
 
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
+    private void setNfcBeamPushUris(Uri[] uris) {
+        if (mNfcAdapter != null &&
+                Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.JELLY_BEAN) {
+            mNfcAdapter.setBeamPushUris(uris, (Activity)mActivity);
+        }
+    }
+
     private void updateShareURI(Path path) {
         if (mShareActionProvider != null) {
             DataManager manager = mActivity.getDataManager();
             int type = manager.getMediaType(path);
             Intent intent = new Intent(Intent.ACTION_SEND);
             intent.setType(MenuExecutor.getMimeType(type));
-            intent.putExtra(Intent.EXTRA_STREAM, manager.getContentUri(path));
+            Uri uri = manager.getContentUri(path);
+            intent.putExtra(Intent.EXTRA_STREAM, uri);
             mShareActionProvider.setShareIntent(intent);
-            if (mNfcAdapter != null) {
-                mNfcAdapter.setBeamPushUris(new Uri[]{manager.getContentUri(path)},
-                        (Activity)mActivity);
-            }
+            setNfcBeamPushUris(new Uri[]{uri});
             mPendingSharePath = null;
         } else {
             // This happens when ActionBar is not created yet.
index 746d41d..2a0157a 100644 (file)
 
 package com.android.gallery3d.ui;
 
+import android.annotation.TargetApi;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.nfc.NfcAdapter;
+import android.os.Build;
 import android.os.Handler;
 import android.view.ActionMode;
 import android.view.LayoutInflater;
@@ -36,6 +38,7 @@ import android.widget.ShareActionProvider.OnShareTargetSelectedListener;
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.GalleryActionBar;
 import com.android.gallery3d.app.GalleryActivity;
+import com.android.gallery3d.common.ApiHelper;
 import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.data.DataManager;
 import com.android.gallery3d.data.MediaObject;
@@ -224,14 +227,20 @@ public class ActionModeHandler implements ActionMode.Callback {
         return operation;
     }
 
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
+    private void setNfcBeamPushUris(Uri[] uris) {
+        if (mNfcAdapter != null &&
+                Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.JELLY_BEAN) {
+            mNfcAdapter.setBeamPushUris(uris, (Activity)mActivity);
+        }
+    }
+
     // Share intent needs to expand the selection set so we can get URI of
     // each media item
     private Intent computeSharingIntent(JobContext jc) {
         ArrayList<Path> expandedPaths = mSelectionManager.getSelected(true);
         if (expandedPaths.size() == 0) {
-            if (mNfcAdapter != null) {
-                mNfcAdapter.setBeamPushUris(null, (Activity)mActivity);
-            }
+            setNfcBeamPushUris(null);
             return null;
         }
         final ArrayList<Uri> uris = new ArrayList<Uri>();
@@ -259,14 +268,9 @@ public class ActionModeHandler implements ActionMode.Callback {
                 intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
             }
             intent.setType(mimeType);
-            if (mNfcAdapter != null) {
-                mNfcAdapter.setBeamPushUris(uris.toArray(new Uri[uris.size()]),
-                        (Activity)mActivity);
-            }
+            setNfcBeamPushUris(uris.toArray(new Uri[uris.size()]));
         } else {
-            if (mNfcAdapter != null) {
-                mNfcAdapter.setBeamPushUris(null, (Activity)mActivity);
-            }
+            setNfcBeamPushUris(null);
         }
 
         return intent;