Commit 0cdd5530 authored by Grigory Fedorov's avatar Grigory Fedorov

AccountInfoEditorFragment: avatar selection improved. Image size is shown.

parent 97fa7b28
...@@ -12,6 +12,7 @@ import android.view.ViewGroup; ...@@ -12,6 +12,7 @@ import android.view.ViewGroup;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.soundcloud.android.crop.Crop; import com.soundcloud.android.crop.Crop;
...@@ -30,10 +31,10 @@ import org.jivesoftware.smack.SmackException; ...@@ -30,10 +31,10 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smackx.vcardtemp.packet.VCard; import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.net.MalformedURLException;
import java.net.URL;
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener, OnVCardListener { public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener, OnVCardListener {
...@@ -50,7 +51,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -50,7 +51,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private EditText nickName; private EditText nickName;
private String account; private String account;
private ImageView avatar; private ImageView avatar;
private Uri imageUri; private Uri croppedImageUri;
private EditText organization; private EditText organization;
private EditText organizationUnit; private EditText organizationUnit;
private EditText birthDate; private EditText birthDate;
...@@ -66,6 +67,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -66,6 +67,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private View progressBar; private View progressBar;
private boolean isSaveSuccess; private boolean isSaveSuccess;
private LinearLayout fields; private LinearLayout fields;
private TextView avatarSize;
public static AccountInfoEditorFragment newInstance(String account, String vCard) { public static AccountInfoEditorFragment newInstance(String account, String vCard) {
AccountInfoEditorFragment fragment = new AccountInfoEditorFragment(); AccountInfoEditorFragment fragment = new AccountInfoEditorFragment();
...@@ -105,6 +107,8 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -105,6 +107,8 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
progressBar = view.findViewById(R.id.vcard_save_progress_bar); progressBar = view.findViewById(R.id.vcard_save_progress_bar);
avatarSize = (TextView) view.findViewById(R.id.vcard_avatar_size_text_view);
prefixName = (EditText) view.findViewById(R.id.vcard_prefix_name); prefixName = (EditText) view.findViewById(R.id.vcard_prefix_name);
formattedName = (EditText) view.findViewById(R.id.vcard_formatted_name); formattedName = (EditText) view.findViewById(R.id.vcard_formatted_name);
givenName = (EditText) view.findViewById(R.id.vcard_given_name); givenName = (EditText) view.findViewById(R.id.vcard_given_name);
...@@ -202,24 +206,11 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -202,24 +206,11 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
} }
private void changeAvatar() { private void changeAvatar() {
// Intent pickAvatar = new Intent(Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(pickAvatar, 1);
Crop.pickImage(getActivity()); Crop.pickImage(getActivity());
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent result) { public void onActivityResult(int requestCode, int resultCode, Intent result) {
LogManager.i(this, "onActivityResult");
// super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
// imageUri = data.getData();
// avatar.setImageURI(imageUri);
// }
if (requestCode == Crop.REQUEST_PICK && resultCode == Activity.RESULT_OK) { if (requestCode == Crop.REQUEST_PICK && resultCode == Activity.RESULT_OK) {
beginCrop(result.getData()); beginCrop(result.getData());
} else if (requestCode == Crop.REQUEST_CROP) { } else if (requestCode == Crop.REQUEST_CROP) {
...@@ -229,28 +220,26 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -229,28 +220,26 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
} }
private void beginCrop(Uri source) { private void beginCrop(Uri source) {
imageUri = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped")); croppedImageUri = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped"));
Crop.of(source, imageUri).asSquare().start(getActivity()); Crop.of(source, croppedImageUri).start(getActivity());
} }
private void handleCrop(int resultCode, Intent result) { private void handleCrop(int resultCode, Intent result) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
avatar.setImageURI(Crop.getOutput(result));
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(getActivity(), Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
public static byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len; avatar.setImageURI(null);
while ((len = inputStream.read(buffer)) != -1) { avatar.setImageURI(croppedImageUri);
byteBuffer.write(buffer, 0, len);
File f = new File(croppedImageUri.getPath());
long size = f.length();
avatarSize.setVisibility(View.VISIBLE);
avatarSize.setText(size / 1024 + "KB");
} else if (resultCode == Crop.RESULT_ERROR) {
avatarSize.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity(), Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
} }
return byteBuffer.toByteArray();
} }
String getValueFromEditText(EditText editText) { String getValueFromEditText(EditText editText) {
...@@ -281,14 +270,12 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -281,14 +270,12 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
vCard.setField(VCardProperty.FN.name(), formattedNameText); vCard.setField(VCardProperty.FN.name(), formattedNameText);
} }
if (imageUri != null) { if (croppedImageUri != null) {
try { try {
InputStream inputStream = getActivity().getContentResolver().openInputStream(imageUri); vCard.setAvatar(new URL(croppedImageUri.toString()));
vCard.setAvatar(getBytes(inputStream)); } catch (MalformedURLException e) {
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
vCard.setField(VCardProperty.BDAY.name(), getValueFromEditText(birthDate)); vCard.setField(VCardProperty.BDAY.name(), getValueFromEditText(birthDate));
......
...@@ -144,13 +144,33 @@ ...@@ -144,13 +144,33 @@
android:layout_height="@dimen/avatar_large_size" android:layout_height="@dimen/avatar_large_size"
android:src="@drawable/ic_avatar_1" /> android:src="@drawable/ic_avatar_1" />
<Button <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Change" android:orientation="vertical"
android:id="@+id/vcard_change_avatar"
android:layout_marginLeft="16dp" >
android:layout_gravity="top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:id="@+id/vcard_change_avatar"
android:layout_marginLeft="16dp"
android:layout_gravity="top" />
<TextView
android:layout_marginLeft="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vcard_avatar_size_text_view"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="173KB"
android:visibility="invisible"
/>
</LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment