Commit ebc3d2d4 authored by Grigory Fedorov's avatar Grigory Fedorov

AccountInfoEditor: avatar can be taken from camera. avatar can be removed.

parent 2023e181
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<application <application
android:name=".data.Application" android:name=".data.Application"
......
...@@ -22,6 +22,7 @@ import android.graphics.drawable.BitmapDrawable; ...@@ -22,6 +22,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.LayerDrawable;
import android.support.annotation.NonNull;
import com.xabber.android.R; import com.xabber.android.R;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
...@@ -286,13 +287,18 @@ public class AvatarManager implements OnLoadListener, OnLowMemoryListener, OnPac ...@@ -286,13 +287,18 @@ public class AvatarManager implements OnLoadListener, OnLowMemoryListener, OnPac
if (value != null) { if (value != null) {
return new BitmapDrawable(application.getResources(), value); return new BitmapDrawable(application.getResources(), value);
} else { } else {
Drawable[] layers = new Drawable[2]; return getDefaultAccountAvatar(account);
layers[0] = new ColorDrawable(accountColors[AccountManager.getInstance().getColorLevel(account)]);
layers[1] = application.getResources().getDrawable(R.drawable.ic_avatar_1);
return new LayerDrawable(layers);
} }
} }
@NonNull
public Drawable getDefaultAccountAvatar(String account) {
Drawable[] layers = new Drawable[2];
layers[0] = new ColorDrawable(accountColors[AccountManager.getInstance().getColorLevel(account)]);
layers[1] = application.getResources().getDrawable(R.drawable.ic_avatar_1);
return new LayerDrawable(layers);
}
/** /**
* Gets avatar for regular user. * Gets avatar for regular user.
* *
......
...@@ -5,13 +5,16 @@ import android.app.Fragment; ...@@ -5,13 +5,16 @@ import android.app.Fragment;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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.PopupMenu;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
...@@ -44,6 +47,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -44,6 +47,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
public static final int MAX_AVATAR_SIZE_PIXELS = 192; public static final int MAX_AVATAR_SIZE_PIXELS = 192;
public static final String TEMP_FILE_NAME = "cropped"; public static final String TEMP_FILE_NAME = "cropped";
public static final int KB_SIZE_IN_BYTES = 1024; public static final int KB_SIZE_IN_BYTES = 1024;
public static final int TAKE_PHOTO_REQUEST_CODE = 3;
private VCard vCard; private VCard vCard;
private EditText prefixName; private EditText prefixName;
...@@ -54,7 +58,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -54,7 +58,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 croppedImageUri; private Uri newAvatarImageUri;
private EditText organization; private EditText organization;
private EditText organizationUnit; private EditText organizationUnit;
private EditText birthDate; private EditText birthDate;
...@@ -73,6 +77,9 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -73,6 +77,9 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private TextView avatarSize; private TextView avatarSize;
private Lister lister; private Lister lister;
private View changeAvatarButton;
private Uri photoFileUri;
interface Lister { interface Lister {
void onVCardSavingStarted(); void onVCardSavingStarted();
...@@ -134,12 +141,13 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -134,12 +141,13 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
nickName = (EditText) view.findViewById(R.id.vcard_nickname); nickName = (EditText) view.findViewById(R.id.vcard_nickname);
avatar = (ImageView) view.findViewById(R.id.vcard_avatar); avatar = (ImageView) view.findViewById(R.id.vcard_avatar);
view.findViewById(R.id.vcard_change_avatar).setOnClickListener(new View.OnClickListener() { changeAvatarButton = view.findViewById(R.id.vcard_change_avatar);
@Override changeAvatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { @Override
changeAvatar(); public void onClick(View v) {
} changeAvatar();
} }
}
); );
birthDate = (EditText) view.findViewById(R.id.vcard_birth_date); birthDate = (EditText) view.findViewById(R.id.vcard_birth_date);
...@@ -228,13 +236,75 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -228,13 +236,75 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
} }
private void changeAvatar() { private void changeAvatar() {
PopupMenu menu = new PopupMenu(getActivity(), changeAvatarButton);
menu.inflate(R.menu.change_avatar);
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_choose_from_gallery:
chooseFromGallery();
return true;
case R.id.action_take_photo:
takePhoto();
return true;
case R.id.action_remove_avatar:
removeAvatar();
return true;
default:
return false;
}
}
});
menu.show();
}
private void chooseFromGallery() {
Crop.pickImage(getActivity()); Crop.pickImage(getActivity());
} }
private void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File imageFile = null;
try {
imageFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
photoFileUri = Uri.fromFile(imageFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
photoFileUri);
startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST_CODE);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
return File.createTempFile(
TEMP_FILE_NAME, /* prefix */
".jpg", /* suffix */
getActivity().getExternalFilesDir(null) /* directory */
);
}
private void removeAvatar() {
newAvatarImageUri = null;
avatar.setImageDrawable(AvatarManager.getInstance().getDefaultAccountAvatar(account));
}
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent result) { public void onActivityResult(int requestCode, int resultCode, Intent result) {
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 == TAKE_PHOTO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
beginCrop(photoFileUri);
} else if (requestCode == Crop.REQUEST_CROP) { } else if (requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, result); handleCrop(resultCode, result);
} }
...@@ -242,17 +312,17 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -242,17 +312,17 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
} }
private void beginCrop(Uri source) { private void beginCrop(Uri source) {
croppedImageUri = Uri.fromFile(new File(getActivity().getCacheDir(), TEMP_FILE_NAME)); newAvatarImageUri = Uri.fromFile(new File(getActivity().getCacheDir(), TEMP_FILE_NAME));
Crop.of(source, croppedImageUri).withMaxSize(MAX_AVATAR_SIZE_PIXELS, MAX_AVATAR_SIZE_PIXELS).start(getActivity()); Crop.of(source, newAvatarImageUri).withMaxSize(MAX_AVATAR_SIZE_PIXELS, MAX_AVATAR_SIZE_PIXELS).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) {
// null prompts image view to reload file. // null prompts image view to reload file.
avatar.setImageURI(null); avatar.setImageURI(null);
avatar.setImageURI(croppedImageUri); avatar.setImageURI(newAvatarImageUri);
File file = new File(croppedImageUri.getPath()); File file = new File(newAvatarImageUri.getPath());
avatarSize.setText(file.length() / KB_SIZE_IN_BYTES + "KB"); avatarSize.setText(file.length() / KB_SIZE_IN_BYTES + "KB");
avatarSize.setVisibility(View.VISIBLE); avatarSize.setVisibility(View.VISIBLE);
} else if (resultCode == Crop.RESULT_ERROR) { } else if (resultCode == Crop.RESULT_ERROR) {
...@@ -289,12 +359,14 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -289,12 +359,14 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
vCard.setField(VCardProperty.FN.name(), formattedNameText); vCard.setField(VCardProperty.FN.name(), formattedNameText);
} }
if (croppedImageUri != null) { if (newAvatarImageUri != null) {
try { try {
vCard.setAvatar(new URL(croppedImageUri.toString())); vCard.setAvatar(new URL(newAvatarImageUri.toString()));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else {
vCard.removeAvatar();
} }
vCard.setField(VCardProperty.BDAY.name(), getValueFromEditText(birthDate)); vCard.setField(VCardProperty.BDAY.name(), getValueFromEditText(birthDate));
......
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_choose_from_gallery"
android:title="@string/choose_from_gallery"
/>
<item android:id="@+id/action_take_photo"
android:title="@string/take_photo"
/>
<item android:id="@+id/action_remove_avatar"
android:title="@string/remove_avatar"
/>
</menu>
\ No newline at end of file
...@@ -54,4 +54,7 @@ ...@@ -54,4 +54,7 @@
<string name="account_user_info_save_fail">Could not save account user info</string> <string name="account_user_info_save_fail">Could not save account user info</string>
<string name="account_user_info_save_started">Saving account user info…</string> <string name="account_user_info_save_started">Saving account user info…</string>
<string name="saving">Saving…</string> <string name="saving">Saving…</string>
<string name="choose_from_gallery">Choose from gallery</string>
<string name="take_photo">Take photo</string>
<string name="remove_avatar">Remove avatar</string>
</resources> </resources>
\ No newline at end of file
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