diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountSettingsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountSettingsHelper.java index 783fe1a..2743760 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountSettingsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountSettingsHelper.java @@ -89,6 +89,24 @@ public class AccountSettingsHelper extends BaseHelper { } } + /** + * Update account image visibility + * + * @param visibility New account image visibility + * @return The result of the operation + */ + public boolean setAccountImageVisibility(AccountImageVisibility visibility){ + APIRequest request = new APIRequest(getContext(), "settings/set_account_image_visibility"); + request.addString("visibility", AccountImageVisibilityToString(visibility)); + + try { + return new APIRequestHelper().exec(request).getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + /** * Parse an API entry into an AccountImageSettings entry * @@ -127,4 +145,26 @@ public class AccountSettingsHelper extends BaseHelper { throw new AssertionError(); } } + + /** + * Turn an account image visibility level into a string ready for the API + * + * @param visibility Visibility level to convert + * @return Matching string + */ + private static String AccountImageVisibilityToString(AccountImageVisibility visibility){ + switch (visibility){ + case OPEN: + return "open"; + + case PUBLIC: + return "public"; + + case FRIENDS: + return "friends"; + + default: + throw new AssertionError(); + } + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateAccountImageVisibilityTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateAccountImageVisibilityTask.java new file mode 100644 index 0000000..f638e99 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateAccountImageVisibilityTask.java @@ -0,0 +1,22 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility; +import org.communiquons.android.comunic.client.data.helpers.AccountSettingsHelper; + +/** + * Update account image visibility task + * + * @author Pierre HUBERT + */ +public class UpdateAccountImageVisibilityTask extends SafeAsyncTask { + public UpdateAccountImageVisibilityTask(Context context) { + super(context); + } + + @Override + protected Boolean doInBackground(AccountImageVisibility... accountImageVisibilities) { + return new AccountSettingsHelper(getContext()).setAccountImageVisibility(accountImageVisibilities[0]); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/accountsettings/AccountImageSettingsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/accountsettings/AccountImageSettingsFragment.java index fbc0def..911921a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/accountsettings/AccountImageSettingsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/accountsettings/AccountImageSettingsFragment.java @@ -12,12 +12,15 @@ import android.support.v7.preference.Preference; import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility; import org.communiquons.android.comunic.client.data.models.AccountImageSettings; import org.communiquons.android.comunic.client.ui.asynctasks.DeleteUserAccountImageTask; import org.communiquons.android.comunic.client.ui.asynctasks.GetAccountImageSettingsTask; import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.ui.asynctasks.UpdateAccountImageVisibilityTask; import org.communiquons.android.comunic.client.ui.asynctasks.UploadNewAccountImageTask; import org.communiquons.android.comunic.client.ui.preference.AccountImagePreference; +import org.communiquons.android.comunic.client.ui.preference.AccountImageVisibilityPreference; import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; import java.io.FileNotFoundException; @@ -27,19 +30,22 @@ import static org.communiquons.android.comunic.client.ui.Constants.IntentRequest /** * Account image settings fragment */ -public class AccountImageSettingsFragment extends BaseAccountSettingsFragment implements Preference.OnPreferenceClickListener { +public class AccountImageSettingsFragment extends BaseAccountSettingsFragment + implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { private static final String TAG = AccountImageSettingsFragment.class.getSimpleName(); //Preferences private static final String PREFERENCE_UPDATE_ACCOUNT_IMAGE = "update_account_image"; private static final String PREFERENCE_DELETE_ACCOUNT_IMAGE = "delete_account_image"; + private static final String PREFERENCE_ACCOUNT_IMAGE_VISIBILITY = "account_image_visibility_level"; private AccountImageSettings mAccountImageSettings; private GetAccountImageSettingsTask mGetAccountImageSettingsTask; private UploadNewAccountImageTask mUploadNewAccountImageTask; private DeleteUserAccountImageTask mDeleteUserAccountImageTask; + private UpdateAccountImageVisibilityTask mUpdateAccountImageVisibilityTask; @Override public void onCreatePreferences(Bundle bundle, String s) { @@ -52,6 +58,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE).setOnPreferenceClickListener(this); findPreference(PREFERENCE_DELETE_ACCOUNT_IMAGE).setOnPreferenceClickListener(this); + findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY).setOnPreferenceChangeListener(this); } @Override @@ -59,7 +66,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im super.onResume(); getActivity().setTitle(R.string.preferences_account_image_title); - if(mAccountImageSettings == null) + if (mAccountImageSettings == null) load_settings(); else onGotAccountImageSettings(mAccountImageSettings); @@ -72,24 +79,26 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im removeLoadingDialog(); } - private void unset_loading_tasks(){ - if(mGetAccountImageSettingsTask != null) + private void unset_loading_tasks() { + if (mGetAccountImageSettingsTask != null) mGetAccountImageSettingsTask.setOnPostExecuteListener(null); - if(mUploadNewAccountImageTask != null) + if (mUploadNewAccountImageTask != null) mUploadNewAccountImageTask.setOnPostExecuteListener(null); - if(mDeleteUserAccountImageTask != null) + if (mDeleteUserAccountImageTask != null) mDeleteUserAccountImageTask.setOnPostExecuteListener(null); - } + if (mUpdateAccountImageVisibilityTask != null) + mUpdateAccountImageVisibilityTask.setOnPostExecuteListener(null); + } /** * Load account image settings. As soon as this method is called, cached information about * account image are cleared, so on pause and resume of the fragment it can be loaded again */ - private void load_settings(){ + private void load_settings() { unset_loading_tasks(); showLoadingDialog(); @@ -105,12 +114,12 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im mGetAccountImageSettingsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - private void onGotAccountImageSettings(@Nullable AccountImageSettings accountImageSettings){ + private void onGotAccountImageSettings(@Nullable AccountImageSettings accountImageSettings) { removeLoadingDialog(); //Check for errors - if(accountImageSettings == null){ + if (accountImageSettings == null) { Toast.makeText(getActivity(), R.string.err_get_account_image_settings, Toast.LENGTH_SHORT).show(); return; @@ -119,17 +128,21 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im mAccountImageSettings = accountImageSettings; //Apply settings - ((AccountImagePreference)findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE)) + ((AccountImagePreference) findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE)) .setImage_url(mAccountImageSettings.getImageURL()); findPreference(PREFERENCE_DELETE_ACCOUNT_IMAGE).setEnabled( accountImageSettings.isHas_image()); + findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY) + .setEnabled(accountImageSettings.isHas_image()); + ((AccountImageVisibilityPreference) findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY)) + .setValue(mAccountImageSettings.getVisibility()); } @Override public boolean onPreferenceClick(Preference preference) { - switch (preference.getKey()){ + switch (preference.getKey()) { //Upload new account image case PREFERENCE_UPDATE_ACCOUNT_IMAGE: @@ -138,27 +151,40 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im //Delete account image case PREFERENCE_DELETE_ACCOUNT_IMAGE: - confirmDeleteAccountImage(); - break; + confirmDeleteAccountImage(); + break; } return false; } + @Override + public boolean onPreferenceChange(Preference preference, Object o) { + + //Update account image visibility + if (preference.getKey().equals(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY)) { + updateAccountImageVisibility( + AccountImageVisibilityPreference.StringToAccountImageVisibility((String)o)); + return true; + } + + return false; + } + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //Check if the request was to choose a new account image - if(requestCode == ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT) + if (requestCode == ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT) pickNewAccountImageCallback(resultCode, data); } /** * Prompt the user to choose a new account image */ - private void pickNewAccountImage(){ + private void pickNewAccountImage() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT); @@ -168,10 +194,10 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im * Pick new account image callback * * @param resultCode Result code of the operation - * @param data Associated data + * @param data Associated data */ - private void pickNewAccountImageCallback(int resultCode, Intent data){ - if(resultCode != Activity.RESULT_OK) + private void pickNewAccountImageCallback(int resultCode, Intent data) { + if (resultCode != Activity.RESULT_OK) return; try { @@ -187,7 +213,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im * * @param bitmap The new account image, as bitmap */ - private void uploadNewAccountImage(Bitmap bitmap){ + private void uploadNewAccountImage(Bitmap bitmap) { showLoadingDialog(); unset_loading_tasks(); @@ -195,7 +221,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im mUploadNewAccountImageTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { @Override public void OnPostExecute(Boolean result) { - if(!result) + if (!result) Toast.makeText(getActivity(), R.string.err_upload_account_image, Toast.LENGTH_SHORT).show(); @@ -208,7 +234,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im /** * Prompt user confirmation to delete account image */ - private void confirmDeleteAccountImage(){ + private void confirmDeleteAccountImage() { new AlertDialog.Builder(getActivity()) .setTitle(R.string.dialog_delete_accountimage_title) .setMessage(R.string.dialog_delete_accountimage_message) @@ -227,7 +253,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im /** * Perform account image deletion */ - private void deleteAccountImage(){ + private void deleteAccountImage() { showLoadingDialog(); unset_loading_tasks(); @@ -236,7 +262,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im @Override public void OnPostExecute(Boolean result) { - if(!result) + if (!result) Toast.makeText(getActivity(), R.string.err_delete_account_image, Toast.LENGTH_SHORT).show(); @@ -245,4 +271,29 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im }); mDeleteUserAccountImageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } + + /** + * Update account image visibility level + * + * @param visibility New Visibility level + */ + private void updateAccountImageVisibility(AccountImageVisibility visibility) { + + unset_loading_tasks(); + showLoadingDialog(); + + mUpdateAccountImageVisibilityTask = new UpdateAccountImageVisibilityTask(getActivity()); + mUpdateAccountImageVisibilityTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(Boolean result) { + if (!result) + Toast.makeText(getActivity(), R.string.err_update_account_image_visibility, + Toast.LENGTH_SHORT).show(); + + load_settings(); + } + }); + mUpdateAccountImageVisibilityTask.executeOnExecutor( + AsyncTask.THREAD_POOL_EXECUTOR, visibility); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/preference/AccountImageVisibilityPreference.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/preference/AccountImageVisibilityPreference.java new file mode 100644 index 0000000..a893ce3 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/preference/AccountImageVisibilityPreference.java @@ -0,0 +1,91 @@ +package org.communiquons.android.comunic.client.ui.preference; + +import android.content.Context; +import android.support.v7.preference.ListPreference; +import android.util.AttributeSet; + +import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility; + +/** + * Account image visibility preference + */ +public class AccountImageVisibilityPreference extends ListPreference { + + public AccountImageVisibilityPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public AccountImageVisibilityPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public AccountImageVisibilityPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public AccountImageVisibilityPreference(Context context) { + super(context); + } + + /** + * Set new selected value + * + * @param visibility Visibility level + */ + public void setValue(AccountImageVisibility visibility) { + setValue(AccountImageVisibilityToString(visibility)); + } + + /** + * Get the currently selected visibility + * + * @return Currently selected visibility level + */ + public AccountImageVisibility getVisibility() { + return StringToAccountImageVisibility(getValue()); + } + + /** + * Turn an AccountImageVisibility entry into a string + * + * @param visibility The visibility to convert + * @return Generated string + */ + private static String AccountImageVisibilityToString(AccountImageVisibility visibility) { + switch (visibility) { + case OPEN: + return "open"; + + case PUBLIC: + return "public"; + + case FRIENDS: + return "friends"; + + default: + throw new AssertionError(); + } + } + + /** + * Turn a string into an account image visibility level + * + * @param string The string to convert + * @return Matching account visibility level + */ + public static AccountImageVisibility StringToAccountImageVisibility(String string) { + switch (string) { + case "open": + return AccountImageVisibility.OPEN; + + case "public": + return AccountImageVisibility.PUBLIC; + + case "friends": + return AccountImageVisibility.FRIENDS; + + default: + throw new AssertionError(); + } + } +} diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index bcca5d5..1ce52e9 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -284,4 +284,9 @@ Supprimer Une erreur a survenu lors de la suppression de votre image de compte ! Une erreur a survenu lors de la mise en ligne de votre nouvelle image de compte ! + Niveau de visibilité de l\'image de compte + Tout le monde + Les utilisateurs connectés à Comunic + Uniquement mes amis + Une erreur a survenu lors de la mise à jour du niveau de visibilité de l\'image de compte ! \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..da1eb4d --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,18 @@ + + + + + + @string/array_everyone + @string/array_authenticated_users + @string/array_friends_only + + + + open + public + friends + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e03335d..ee4bff2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -283,4 +283,9 @@ Delete Could not delete your account image! Could not upload new account image! + Account image visibility level + Everyone + Authenticated users + Friends only + Could not update account image visibility level! diff --git a/app/src/main/res/xml/account_preference_image.xml b/app/src/main/res/xml/account_preference_image.xml index eac7b0a..307f5d0 100644 --- a/app/src/main/res/xml/account_preference_image.xml +++ b/app/src/main/res/xml/account_preference_image.xml @@ -8,5 +8,11 @@ android:key="delete_account_image" android:title="@string/action_delete_account_image" /> + \ No newline at end of file