diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java index 5b4752f..4160e11 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java @@ -24,6 +24,11 @@ import java.util.ArrayList; */ public class GroupsHelper extends BaseHelper { + /** + * Debug tag + */ + private static final String TAG = GroupsHelper.class.getSimpleName(); + /** * Groups information cache */ @@ -64,6 +69,28 @@ public class GroupsHelper extends BaseHelper { } + /** + * Get information about a single group + * + * @param groupID Target group ID + * @param force Specify whether the request should be forced or not + * @return null in case of failure / Group information else + */ + @Nullable + public GroupInfo getInfoSingle(int groupID, boolean force){ + + //Prepare request + ArrayList ids = new ArrayList<>(); + ids.add(groupID); + + //Execute request + ArrayMap list = getInfoMultiple(ids, force); + if(list == null || !list.containsKey(groupID)) + return null; + + return list.get(groupID); + } + /** * Get information about multiple groups * @@ -172,11 +199,27 @@ public class GroupsHelper extends BaseHelper { public AdvancedGroupInfo getAdvancedInformation(int groupID){ APIRequest request = new APIRequest(getContext(), "groups/get_advanced_info"); request.addInt("id", groupID); + request.setTryContinueOnError(true); try { //Execute request and get result - JSONObject object = new APIRequestHelper().exec(request).getJSONObject(); + APIResponse response = new APIRequestHelper().exec(request); + + if (response.getResponse_code() != 200) { + if(response.getResponse_code() == 401){ + //Access was explicitly denied to the group + AdvancedGroupInfo groupInfo = new AdvancedGroupInfo(); + groupInfo.setAccess_forbidden(true); + return groupInfo; + } + + //Could not get group information + return null; + } + + //Parse and return result + JSONObject object = response.getJSONObject(); return parse_advanced_group_info(object); } catch (Exception e) { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java index a1566ee..abec865 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java @@ -10,12 +10,21 @@ package org.communiquons.android.comunic.client.data.models; public class AdvancedGroupInfo extends GroupInfo { //Private fields + private boolean access_forbidden = false; private int time_create; private String url; private String description; private int number_likes; private boolean is_liking; + public boolean isAccess_forbidden() { + return access_forbidden; + } + + public void setAccess_forbidden(boolean access_forbidden) { + this.access_forbidden = access_forbidden; + } + public int getTime_create() { return time_create; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index 2dbd36f..443a702 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -11,6 +11,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -43,10 +44,12 @@ import org.communiquons.android.comunic.client.ui.fragments.LatestPostsFragment; import org.communiquons.android.comunic.client.ui.fragments.NotificationsFragment; import org.communiquons.android.comunic.client.ui.fragments.SinglePostFragment; import org.communiquons.android.comunic.client.ui.fragments.UpdateConversationFragment; +import org.communiquons.android.comunic.client.ui.fragments.groups.GroupAccessDeniedFragment; import org.communiquons.android.comunic.client.ui.fragments.groups.GroupPageMainFragment; import org.communiquons.android.comunic.client.ui.fragments.groups.UserGroupsFragment; import org.communiquons.android.comunic.client.ui.fragments.userpage.UserAccessDeniedFragment; import org.communiquons.android.comunic.client.ui.fragments.userpage.UserPageFragment; +import org.communiquons.android.comunic.client.ui.listeners.OnOpenGroupListener; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; import org.communiquons.android.comunic.client.ui.listeners.onPostOpenListener; import org.communiquons.android.comunic.client.ui.listeners.openConversationListener; @@ -64,7 +67,7 @@ import static org.communiquons.android.comunic.client.ui.Constants.IntentRequest */ public class MainActivity extends BaseActivity implements openConversationListener, updateConversationListener, onOpenUsersPageListener, - onPostOpenListener, NavigationBar.OnNavigationItemSelectedListener { + onPostOpenListener, NavigationBar.OnNavigationItemSelectedListener, OnOpenGroupListener { /** * Debug tag @@ -757,28 +760,13 @@ public class MainActivity extends BaseActivity implements } - /** - * Request the page of a group to be opened - * - * @param activity Current activity (MUST BE THIS ACTIVITY) - * @param groupID Group ID to open - */ - public static void OpenGroup(@NonNull Activity activity, int groupID){ - - if(!(activity instanceof MainActivity)) - throw new RuntimeException("Specified activity is not an instance of MainActivity!"); - - ((MainActivity)activity).openGroupPage(groupID); - - } - /** * Open group page * * @param groupID The ID of the group to open */ - private void openGroupPage(int groupID){ - + @Override + public void onOpenGroup(int groupID) { //Specify fragment arguments Bundle args = new Bundle(); args.putInt(GroupPageMainFragment.ARGUMENT_GROUP_ID, groupID); @@ -792,6 +780,26 @@ public class MainActivity extends BaseActivity implements transaction.addToBackStack(null); transaction.replace(R.id.main_fragment, fragment); transaction.commit(); + } + @Override + public void onOpenGroupAccessDenied(int groupID) { + + //Add arguments + Bundle args = new Bundle(); + args.putInt(GroupAccessDeniedFragment.ARGUMENT_GROUP_ID, groupID); + + //Create fragment + Fragment fragment = new GroupAccessDeniedFragment(); + fragment.setArguments(args); + + //Remove last transaction from list + getSupportFragmentManager().popBackStackImmediate(); + + //Perform fragment transaction + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.addToBackStack(null); + transaction.replace(R.id.main_fragment, fragment); + transaction.commit(); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupInfoTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupInfoTask.java new file mode 100644 index 0000000..a2b8ea5 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupInfoTask.java @@ -0,0 +1,21 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.helpers.GroupsHelper; +import org.communiquons.android.comunic.client.data.models.GroupInfo; + +public class GetGroupInfoTask extends SafeAsyncTask { + + public GetGroupInfoTask(Context context) { + super(context); + } + + @Override + protected GroupInfo doInBackground(Integer... integers) { + + //By default, we force the request + return new GroupsHelper(getContext()).getInfoSingle(integers[0], true); + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupAccessDeniedFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupAccessDeniedFragment.java new file mode 100644 index 0000000..a3723ce --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupAccessDeniedFragment.java @@ -0,0 +1,137 @@ +package org.communiquons.android.comunic.client.ui.fragments.groups; + +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.enums.GroupsMembershipLevels; +import org.communiquons.android.comunic.client.data.models.GroupInfo; +import org.communiquons.android.comunic.client.ui.asynctasks.GetGroupInfoTask; +import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.ui.listeners.OnOpenGroupListener; +import org.communiquons.android.comunic.client.ui.views.GroupImageView; +import org.communiquons.android.comunic.client.ui.views.GroupMembershipStatusView; + +import java.util.Objects; + +/** + * Group access denied fragment + * + * This fragment appears when the user was explicitly denied + * access to a group advanced information + * + * @author Pierre HUBERT + */ +public class GroupAccessDeniedFragment extends AbstractGroupFragment { + + /** + * Debug tag + */ + private static final String TAG = GroupAccessDeniedFragment.class.getSimpleName(); + + /** + * Mandatory argument to use this fragment : target group id + */ + public static final String ARGUMENT_GROUP_ID = "group_id"; + + /** + * Current group ID + */ + private int mGroupID; + + /** + * Views + */ + private GroupImageView mGroupImageView; + private TextView mGroupNameView; + private GroupMembershipStatusView mGroupMembershipView; + + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.layout_group_access_denied, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + mGroupImageView = view.findViewById(R.id.groupImageView); + mGroupNameView = view.findViewById(R.id.groupName); + mGroupMembershipView = view.findViewById(R.id.groupMembershipStatusView); + } + + @Override + public void onStart() { + super.onStart(); + + assert getArguments() != null; + mGroupID = getArguments().getInt(ARGUMENT_GROUP_ID); + + getGroupInfo(); + } + + @Override + public void onGroupMembershipUpdated(boolean success, int groupID) { + super.onGroupMembershipUpdated(success, groupID); + getGroupInfo(); + } + + /** + * Get and return information about the group + */ + private void getGroupInfo(){ + + getTasksManager().unsetSpecificTasks(GetGroupInfoTask.class); + + GetGroupInfoTask task = new GetGroupInfoTask(getActivity()); + task.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(GroupInfo groupInfo) { + onGotGroupInfo(groupInfo); + } + }); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mGroupID); + getTasksManager().addTask(task); + + } + + /** + * Method called once we have got group information + * + * @param info Information about the group + */ + private void onGotGroupInfo(@Nullable GroupInfo info){ + + //Check for errors + if(info == null){ + Toast.makeText(getActivity(), R.string.err_get_group_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Check if the user is now at least a member of the group + if(info.getMembershipLevel() == GroupsMembershipLevels.MEMBER + || info.getMembershipLevel() == GroupsMembershipLevels.MODERATOR + || info.getMembershipLevel() == GroupsMembershipLevels.ADMINISTRATOR){ + + //Go back to the group + Objects.requireNonNull(getActivity()) + .getSupportFragmentManager().popBackStack(); + ((OnOpenGroupListener)getActivity()).onOpenGroup(info.getId()); + } + + //Apply group information + mGroupImageView.setGroup(info); + mGroupNameView.setText(info.getDisplayName()); + mGroupMembershipView.setGroup(info); + mGroupMembershipView.setOnGroupMembershipUpdateListener(this); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java index 6a3a430..e370448 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java @@ -15,9 +15,12 @@ import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.models.AdvancedGroupInfo; import org.communiquons.android.comunic.client.ui.asynctasks.GetGroupAdvancedInfoTask; import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.ui.listeners.OnOpenGroupListener; import org.communiquons.android.comunic.client.ui.utils.UiUtils; import org.communiquons.android.comunic.client.ui.views.GroupImageView; +import java.util.Objects; + /** * Group main page * @@ -121,6 +124,13 @@ public class GroupPageMainFragment extends AbstractGroupFragment { return; } + //Check if access to the group was denied + if(info.isAccess_forbidden()){ + ((OnOpenGroupListener) Objects.requireNonNull(getActivity())) + .onOpenGroupAccessDenied(mGroupID); + return; + } + mAdvancedGroupInfo = info; applyGroupInfo(); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/UserGroupsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/UserGroupsFragment.java index f4b5bad..18e955a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/UserGroupsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/UserGroupsFragment.java @@ -23,6 +23,7 @@ import org.communiquons.android.comunic.client.ui.adapters.GroupsListAdapter; import org.communiquons.android.comunic.client.ui.asynctasks.GetUserGroupsTask; import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.ui.listeners.OnGroupActionListener; +import org.communiquons.android.comunic.client.ui.listeners.OnOpenGroupListener; import java.util.ArrayList; import java.util.Objects; @@ -134,7 +135,7 @@ public class UserGroupsFragment extends AbstractGroupFragment implements OnGroup mGroupsView.setAdapter(mGroupsAdapter); mGroupsView.setLayoutManager(new LinearLayoutManager(getActivity())); - mGroupsView.addItemDecoration(new DividerItemDecoration(getActivity(), + mGroupsView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getActivity()), DividerItemDecoration.VERTICAL)); @@ -173,6 +174,12 @@ public class UserGroupsFragment extends AbstractGroupFragment implements OnGroup @Override public void onOpenGroup(int groupID) { Log.v(TAG, "Open group " + groupID); - MainActivity.OpenGroup(Objects.requireNonNull(getActivity()), groupID); + ((OnOpenGroupListener)Objects.requireNonNull(getActivity())).onOpenGroup(groupID); + } + + @Override + public void onOpenGroupAccessDenied(int groupID) { + ((OnOpenGroupListener)Objects.requireNonNull(getActivity())) + .onOpenGroupAccessDenied(groupID); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnGroupActionListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnGroupActionListener.java index e5036d9..4905638 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnGroupActionListener.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnGroupActionListener.java @@ -5,13 +5,7 @@ package org.communiquons.android.comunic.client.ui.listeners; * * @author Pierre HUBERT */ -public interface OnGroupActionListener extends OnGroupMembershipUpdateListener { - - /** - * Open a group page - * - * @param groupID The ID of the group to open - */ - void onOpenGroup(int groupID); +public interface OnGroupActionListener + extends OnGroupMembershipUpdateListener, OnOpenGroupListener { } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnOpenGroupListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnOpenGroupListener.java new file mode 100644 index 0000000..2f75594 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnOpenGroupListener.java @@ -0,0 +1,24 @@ +package org.communiquons.android.comunic.client.ui.listeners; + +/** + * This interface is implemented by all classes that have to relay + * the opening of a group + * + * @author Pierre HUBERT + */ +public interface OnOpenGroupListener { + + /** + * Open a group page + * + * @param groupID The ID of the group to open + */ + void onOpenGroup(int groupID); + + /** + * Open a group access denied fragment page + * + * @param groupID The ID of the group to open + */ + void onOpenGroupAccessDenied(int groupID); +} diff --git a/app/src/main/res/layout/layout_group_access_denied.xml b/app/src/main/res/layout/layout_group_access_denied.xml new file mode 100644 index 0000000..f8cbea4 --- /dev/null +++ b/app/src/main/res/layout/layout_group_access_denied.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + \ 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 9984480..dc7a9aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -312,4 +312,5 @@ Leave Could not update group membership! Could not get group information! + Access to the group denied.