Rework ChangeStatus to deal better with non standard CROP intent
authorDa Risk <da_risk@beem-project.com>
Tue, 27 Oct 2015 01:12:06 +0100
changeset 1066 744a173ea554
parent 1065 a2ec39a59c06
child 1067 029597e0ca0c
Rework ChangeStatus to deal better with non standard CROP intent
app/src/main/aidl/com/beem/project/beem/service/aidl/IXmppFacade.aidl
app/src/main/java/com/beem/project/beem/providers/AvatarProvider.java
app/src/main/java/com/beem/project/beem/service/BeemAvatarManager.java
app/src/main/java/com/beem/project/beem/service/XmppFacade.java
app/src/main/java/com/beem/project/beem/ui/ChangeStatus.java
--- a/app/src/main/aidl/com/beem/project/beem/service/aidl/IXmppFacade.aidl	Tue Oct 27 01:04:13 2015 +0100
+++ b/app/src/main/aidl/com/beem/project/beem/service/aidl/IXmppFacade.aidl	Tue Oct 27 01:12:06 2015 +0100
@@ -49,6 +49,7 @@
 import com.beem.project.beem.service.PresenceAdapter;
 import com.beem.project.beem.service.UserInfo;
 
+import android.graphics.Bitmap;
 import android.net.Uri;
 
 interface IXmppFacade {
@@ -96,9 +97,10 @@
      * make a jingle audio call
      * @param jid the receiver id
      */
-     void call(in String jid);
+    void call(in String jid);
 
     boolean publishAvatar(in Uri avatarUri);
+    boolean publishAvatarBitmap(in Bitmap avatarUri);
 
     void disableAvatarPublishing();
 
--- a/app/src/main/java/com/beem/project/beem/providers/AvatarProvider.java	Tue Oct 27 01:04:13 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/providers/AvatarProvider.java	Tue Oct 27 01:12:06 2015 +0100
@@ -57,7 +57,7 @@
 
     /** The content uri of this provider. */
     public static final Uri CONTENT_URI =
-	Uri.parse("content://com.beem.project.beem.providers.avatarprovider/avatars/");
+        Uri.parse("content://com.beem.project.beem.providers.avatarprovider/avatars/");
 
     /**
      * Create an AvatarProvider.
--- a/app/src/main/java/com/beem/project/beem/service/BeemAvatarManager.java	Tue Oct 27 01:04:13 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/service/BeemAvatarManager.java	Tue Oct 27 01:12:06 2015 +0100
@@ -108,7 +108,7 @@
      * @param bitmap the avatar to publish
      * @return true on success false otherwise
      */
-    private boolean publishAvatar(Bitmap bitmap) {
+    public boolean publishAvatar(Bitmap bitmap) {
 	//TODO use the metadata available in the mediastore
 	AvatarMetadataExtension meta = new AvatarMetadataExtension();
 	// Probably a bug on prosody but only the last data sent is kept
--- a/app/src/main/java/com/beem/project/beem/service/XmppFacade.java	Tue Oct 27 01:04:13 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/service/XmppFacade.java	Tue Oct 27 01:12:06 2015 +0100
@@ -43,6 +43,7 @@
 */
 package com.beem.project.beem.service;
 
+import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.RemoteException;
 
@@ -153,7 +154,7 @@
 
     @Override
     public boolean publishAvatar(Uri avatarUri) throws RemoteException {
-    	initConnection();
+	initConnection();
 	BeemAvatarManager mgr = mConnexion.getAvatarManager();
 	if (mgr == null)
 	    return false;
@@ -162,6 +163,16 @@
     }
 
     @Override
+    public boolean publishAvatarBitmap(Bitmap avatar) throws RemoteException {
+	initConnection();
+	BeemAvatarManager mgr = mConnexion.getAvatarManager();
+	if (mgr == null)
+	    return false;
+
+	return mgr.publishAvatar(avatar);
+    }
+
+    @Override
     public void disableAvatarPublishing() throws RemoteException {
     	initConnection();
 	BeemAvatarManager mgr = mConnexion.getAvatarManager();
--- a/app/src/main/java/com/beem/project/beem/ui/ChangeStatus.java	Tue Oct 27 01:04:13 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/ui/ChangeStatus.java	Tue Oct 27 01:12:06 2015 +0100
@@ -44,10 +44,6 @@
 
 package com.beem.project.beem.ui;
 
-import java.io.File;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
@@ -60,9 +56,9 @@
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
+import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.Environment;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
@@ -114,13 +110,7 @@
 
     private static final int CAMERA_WITH_DATA = 0;
     private static final int PHOTO_PICKED_WITH_DATA = 1;
-
-    private static final File PHOTO_DIR = new File(
-            Environment.getExternalStorageDirectory() + "/DCIM/Camera");
-
-    private static final String KEY_CURRENT_PHOTO_FILE = "currentphotofile";
-
-    private static final Uri MY_AVATAR_URI = Uri.parse(AvatarProvider.CONTENT_URI + "/my_avatar");
+    private static final int PHOTO_CROPPED = 2;
 
     private EditText mStatusMessageEditText;
     private Toast mToast;
@@ -130,6 +120,7 @@
     private Spinner mSpinner;
     private ImageButton mAvatar;
     private Uri mAvatarUri;
+    private Bitmap avatarBitmap;
 
     private SharedPreferences mSettings;
     private ArrayAdapter<CharSequence> mAdapter;
@@ -139,7 +130,6 @@
     private final BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
     private boolean mShowCurrentAvatar = true;
     private boolean mDisableAvatar;
-    private File mCurrentPhotoFile;
 
     /**
      * Constructor.
@@ -190,9 +180,6 @@
 	this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     protected void onResume() {
 	super.onResume();
@@ -204,46 +191,18 @@
 	bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     protected void onPause() {
 	super.onPause();
 	unbindService(mServConn);
     }
 
-    /* (non-Javadoc)
-     * @see android.app.Activity#onDestroy()
-     */
     @Override
     protected void onDestroy() {
 	super.onDestroy();
 	this.unregisterReceiver(mReceiver);
     }
 
-    /*
-     * The activity is often reclaimed by the system memory.
-     */
-    @Override
-    protected void onSaveInstanceState(Bundle outState) {
-        if (mCurrentPhotoFile != null) {
-            outState.putString(KEY_CURRENT_PHOTO_FILE, mCurrentPhotoFile.toString());
-        }
-        super.onSaveInstanceState(outState);
-    }
-
-    @Override
-    protected void onRestoreInstanceState(Bundle savedInstanceState) {
-        String fileName = savedInstanceState.getString(KEY_CURRENT_PHOTO_FILE);
-        if (fileName != null) {
-            mCurrentPhotoFile = new File(fileName);
-        }
-        super.onRestoreInstanceState(savedInstanceState);
-    }
-
-
-
     @Override
     protected Dialog onCreateDialog(int id) {
 	if (id == SELECT_PHOTO_DLG)
@@ -253,29 +212,40 @@
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-	  // Ignore failed requests
-        if (resultCode != RESULT_OK) return;
+	// Ignore failed requests
+	if (resultCode != RESULT_OK) return;
 
-        switch (requestCode) {
-            case PHOTO_PICKED_WITH_DATA:
-		// We tell the activity to put the result in MY_AVATAR_URI
-		mAvatarUri = MY_AVATAR_URI;
+	switch (requestCode) {
+	    case PHOTO_PICKED_WITH_DATA:
+		mAvatarUri = data.getData();
 		Log.d(TAG, "selected avatar uri " + mAvatarUri);
 		if (mAvatarUri != null) {
-		    // force reload of image even it is the same uri
-		    mAvatar.setImageURI(null);
 		    mAvatar.setImageURI(mAvatarUri);
 		    mDisableAvatar = false;
 		    mShowCurrentAvatar = false;
+		    avatarBitmap = null;
 		}
-                break;
-
-            case CAMERA_WITH_DATA:
-                doCropPhoto(mCurrentPhotoFile);
-                break;
+		break;
+	    case CAMERA_WITH_DATA:
+		if (!doCropPhoto(data.getData())) {
+		    mAvatarUri = data.getData();
+		    mAvatar.setImageURI(mAvatarUri);
+		    mDisableAvatar = false;
+		    mShowCurrentAvatar = false;
+		    avatarBitmap = null;
+		}
+		break;
+	    case PHOTO_CROPPED:
+		avatarBitmap = data.getParcelableExtra("data");
+		if (avatarBitmap != null) {
+		    mAvatar.setImageBitmap(avatarBitmap);
+		    mDisableAvatar = false;
+		    mShowCurrentAvatar = false;
+		    mAvatarUri = null;
+		}
+		break;
 	    default:
 		Log.w(TAG, "onActivityResult : invalid request code");
-
         }
     }
 
@@ -347,6 +317,8 @@
 		mXmppFacade.disableAvatarPublishing();
 	    else if (mAvatarUri != null)
 		mXmppFacade.publishAvatar(mAvatarUri);
+	    else if (avatarBitmap != null)
+		mXmppFacade.publishAvatarBitmap(avatarBitmap);
 	} catch (RemoteException e) {
 	    Log.e(TAG, "Error while publishing avatar", e);
 	}
@@ -414,15 +386,6 @@
         return builder.create();
     }
 
-    /**
-     * Create a file name for the icon photo using current time.
-     * @return the filename
-     */
-    private String getPhotoFileName() {
-        Date date = new Date(System.currentTimeMillis());
-        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
-        return dateFormat.format(date) + ".jpg";
-    }
 
     /**
      * Launches Camera to take a picture and store it in a file.
@@ -430,9 +393,7 @@
     protected void doTakePhoto() {
         try {
             // Launch camera to take photo for selected contact
-            PHOTO_DIR.mkdirs();
-            mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());
-            final Intent intent = getTakePickIntent(mCurrentPhotoFile);
+            final Intent intent = getTakePickIntent();
             startActivityForResult(intent, CAMERA_WITH_DATA);
         } catch (ActivityNotFoundException e) {
             Toast.makeText(this, R.string.photoPickerNotFoundText, Toast.LENGTH_LONG).show();
@@ -441,38 +402,28 @@
 
     /**
      * Constructs an intent for capturing a photo and storing it in a temporary file.
-     * @param f the temporary file to use to store the picture
      * @return the intent
      */
-    public static Intent getTakePickIntent(File f) {
+    public static Intent getTakePickIntent() {
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
-        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
         return intent;
     }
 
     /**
      * Sends a newly acquired photo to Gallery for cropping.
-     * @param f the image file to crop
+     * @param uri the image file to crop
+     * @return true if was able to launch a cropping activity
      */
-    protected void doCropPhoto(final File f) {
+    protected boolean doCropPhoto(final Uri uri) {
         try {
-
-            // Add the image to the media store
-	    // level 8
-	    /*
-            MediaScannerConnection.scanFile(
-                    this,
-                    new String[] { f.getAbsolutePath() },
-                    new String[] { null },
-                    null);
-	    */
-
             // Launch gallery to crop the photo
-            final Intent intent = getCropImageIntent(Uri.fromFile(f));
-            startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
+            final Intent intent = getCropImageIntent(uri);
+            startActivityForResult(intent, PHOTO_CROPPED);
+            return true;
         } catch (ActivityNotFoundException e) {
             Log.e(TAG, "Cannot crop image", e);
             Toast.makeText(this, R.string.photoPickerNotFoundText, Toast.LENGTH_LONG).show();
+            return false;
         }
     }
 
@@ -489,7 +440,7 @@
         intent.putExtra("aspectY", 1);
         intent.putExtra("outputX", ICON_SIZE);
         intent.putExtra("outputY", ICON_SIZE);
-	intent.putExtra(MediaStore.EXTRA_OUTPUT, MY_AVATAR_URI);
+	intent.putExtra("return-data", true);
         return intent;
     }
 
@@ -518,9 +469,8 @@
         intent.putExtra("aspectY", 1);
         intent.putExtra("outputX", ICON_SIZE);
         intent.putExtra("outputY", ICON_SIZE);
-	intent.putExtra(MediaStore.EXTRA_OUTPUT, MY_AVATAR_URI);
 	// use this to get the bitmap in the intent
-//         intent.putExtra("return-data", true);
+        intent.putExtra("return-data", true);
         return intent;
     }