Backend for setting identity and contact pictures
Pictures are not saved correctly, something is corrupting them.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
android:paddingTop="10dp" >
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/contact_picture"
|
||||
android:id="@+id/picture"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/ic_contact_picture" />
|
||||
@@ -16,17 +16,17 @@
|
||||
android:id="@+id/contact_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@+id/contact_picture"
|
||||
android:layout_toRightOf="@+id/contact_picture"
|
||||
android:layout_alignBottom="@+id/picture"
|
||||
android:layout_toRightOf="@+id/picture"
|
||||
android:ems="10"
|
||||
android:hint="@string/public_name"
|
||||
android:hint="@string/contact_name"
|
||||
android:inputType="text" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/import_destination_from_file"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_picture"
|
||||
android:layout_below="@+id/picture"
|
||||
android:layout_alignParentRight="true"
|
||||
android:text="@string/label_browse" />
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
android:paddingTop="10dp" >
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/identity_picture"
|
||||
android:id="@+id/picture"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/ic_contact_picture" />
|
||||
@@ -16,8 +16,8 @@
|
||||
android:id="@+id/public_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@+id/identity_picture"
|
||||
android:layout_toRightOf="@+id/identity_picture"
|
||||
android:layout_alignBottom="@+id/picture"
|
||||
android:layout_toRightOf="@+id/picture"
|
||||
android:ems="10"
|
||||
android:hint="@string/public_name"
|
||||
android:inputType="text" />
|
||||
@@ -26,7 +26,7 @@
|
||||
android:id="@+id/description"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/identity_picture"
|
||||
android:layout_below="@+id/picture"
|
||||
android:ems="10"
|
||||
android:hint="@string/description"
|
||||
android:inputType="text" />
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
<string name="address_book_empty">Address book is empty</string>
|
||||
<string name="action_new_contact">New contact</string>
|
||||
|
||||
<string name="contact_name">Contact name</string>
|
||||
<string name="email_destination">Email destination</string>
|
||||
<string name="label_browse">Browse</string>
|
||||
<string name="save_contact">Save contact</string>
|
||||
|
||||
@@ -7,16 +7,15 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import i2p.bote.android.R;
|
||||
import i2p.bote.android.util.BoteHelper;
|
||||
import i2p.bote.android.util.EditPictureFragment;
|
||||
import i2p.bote.fileencryption.PasswordException;
|
||||
import i2p.bote.packet.dht.Contact;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
@@ -28,10 +27,10 @@ import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class EditContactFragment extends Fragment {
|
||||
public class EditContactFragment extends EditPictureFragment {
|
||||
public static final String CONTACT_DESTINATION = "contact_destination";
|
||||
|
||||
static final int REQUEST_DESTINATION_FILE = 1;
|
||||
static final int REQUEST_DESTINATION_FILE = 3;
|
||||
|
||||
private String mDestination;
|
||||
EditText mNameField;
|
||||
@@ -73,6 +72,12 @@ public class EditContactFragment extends Fragment {
|
||||
if (mDestination != null) {
|
||||
try {
|
||||
Contact contact = BoteHelper.getContact(mDestination);
|
||||
|
||||
String pic = contact.getPictureBase64();
|
||||
if (pic != null && !pic.isEmpty()) {
|
||||
setPictureB64(pic);
|
||||
}
|
||||
|
||||
mNameField.setText(contact.getName());
|
||||
mDestinationField.setText(mDestination);
|
||||
mTextField.setText(contact.getText());
|
||||
@@ -109,6 +114,7 @@ public class EditContactFragment extends Fragment {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_save_contact:
|
||||
String picture = getPictureB64();
|
||||
String name = mNameField.getText().toString();
|
||||
String destination = mDestinationField.getText().toString();
|
||||
String text = mTextField.getText().toString();
|
||||
@@ -116,7 +122,7 @@ public class EditContactFragment extends Fragment {
|
||||
mError.setText("");
|
||||
|
||||
try {
|
||||
String err = BoteHelper.saveContact(destination, name, null, text);
|
||||
String err = BoteHelper.saveContact(destination, name, picture, text);
|
||||
if (err == null)
|
||||
getActivity().finish();
|
||||
else
|
||||
@@ -160,6 +166,8 @@ public class EditContactFragment extends Fragment {
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.List;
|
||||
import i2p.bote.I2PBote;
|
||||
import i2p.bote.android.R;
|
||||
import i2p.bote.android.util.BoteHelper;
|
||||
import i2p.bote.android.util.EditPictureFragment;
|
||||
import i2p.bote.android.util.RobustAsyncTask;
|
||||
import i2p.bote.android.util.TaskFragment;
|
||||
import i2p.bote.StatusListener;
|
||||
@@ -18,7 +19,6 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
@@ -33,7 +33,7 @@ import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class EditIdentityFragment extends Fragment {
|
||||
public class EditIdentityFragment extends EditPictureFragment {
|
||||
private Callbacks mCallbacks = sDummyCallbacks;
|
||||
|
||||
public interface Callbacks {
|
||||
@@ -124,6 +124,12 @@ public class EditIdentityFragment extends Fragment {
|
||||
// Load the identity to edit
|
||||
try {
|
||||
EmailIdentity identity = BoteHelper.getIdentity(mKey);
|
||||
|
||||
String pic = identity.getPictureBase64();
|
||||
if (pic != null && !pic.isEmpty()) {
|
||||
setPictureB64(pic);
|
||||
}
|
||||
|
||||
mNameField.setText(identity.getPublicName());
|
||||
mDescField.setText(identity.getDescription());
|
||||
mDefaultField.setChecked(identity.isDefault());
|
||||
@@ -154,6 +160,7 @@ public class EditIdentityFragment extends Fragment {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_save_identity:
|
||||
String picture = getPictureB64();
|
||||
String publicName = mNameField.getText().toString();
|
||||
String description = mDescField.getText().toString();
|
||||
boolean setDefault = mDefaultField.isChecked();
|
||||
|
||||
@@ -116,8 +116,11 @@ public class BoteHelper extends GeneralHelper {
|
||||
}
|
||||
|
||||
public static String encodePicture(Bitmap picture) {
|
||||
if (picture == null)
|
||||
return null;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
boolean success = picture.compress(CompressFormat.PNG, 100, baos);
|
||||
// TODO something is corrupting here
|
||||
picture.compress(CompressFormat.PNG, 0, baos);
|
||||
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
106
src/i2p/bote/android/util/EditPictureFragment.java
Normal file
106
src/i2p/bote/android/util/EditPictureFragment.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package i2p.bote.android.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import i2p.bote.android.R;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class EditPictureFragment extends Fragment {
|
||||
static final int REQUEST_PICTURE_FILE = 1;
|
||||
static final int CROP_PICTURE = 2;
|
||||
|
||||
Uri mPictureCaptureUri;
|
||||
Bitmap mPicture;
|
||||
ImageView mPictureView;
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
mPictureView = (ImageView) view.findViewById(R.id.picture);
|
||||
|
||||
// Set up listener for picture changing
|
||||
mPictureView.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View view) {
|
||||
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
i.setType("image/*");
|
||||
startActivityForResult(
|
||||
Intent.createChooser(i, "Select a picture"),
|
||||
REQUEST_PICTURE_FILE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void setPictureB64(String pic) {
|
||||
mPicture = BoteHelper.decodePicture(pic);
|
||||
System.out.println("mPicture == null? " + (mPicture == null));
|
||||
mPictureView.setImageBitmap(mPicture);
|
||||
}
|
||||
|
||||
protected String getPictureB64() {
|
||||
return BoteHelper.encodePicture(mPicture);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (resultCode != Activity.RESULT_OK) {
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
System.out.println("Cancelled");
|
||||
if (mPictureCaptureUri != null ) {
|
||||
getActivity().getContentResolver().delete(mPictureCaptureUri, null, null);
|
||||
mPictureCaptureUri = null;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch (requestCode) {
|
||||
case REQUEST_PICTURE_FILE:
|
||||
mPictureCaptureUri = data.getData();
|
||||
cropPicture();
|
||||
break;
|
||||
|
||||
case CROP_PICTURE:
|
||||
Bundle extras = data.getExtras();
|
||||
if (extras != null) {
|
||||
mPicture = extras.getParcelable("data");
|
||||
mPictureView.setImageBitmap(mPicture);
|
||||
}
|
||||
File f = new File(mPictureCaptureUri.getPath());
|
||||
if (f.exists())
|
||||
f.delete();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void cropPicture() {
|
||||
Intent intent = new Intent("com.android.camera.action.CROP");
|
||||
intent.setType("image/*");
|
||||
|
||||
List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities(intent, 0);
|
||||
if (list.size() == 0) {
|
||||
Toast.makeText(getActivity(), "No image cropping app found", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
intent.setData(mPictureCaptureUri);
|
||||
intent.putExtra("outputX", 72);
|
||||
intent.putExtra("outputY", 72);
|
||||
intent.putExtra("aspectX", 1);
|
||||
intent.putExtra("aspectY", 1);
|
||||
intent.putExtra("scale", true);
|
||||
intent.putExtra("return-data", true);
|
||||
|
||||
startActivityForResult(
|
||||
Intent.createChooser(intent, "Select a cropping app"),
|
||||
CROP_PICTURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user