View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.ui.social;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.exoplatform.R;
25  import org.exoplatform.model.SocialSpaceInfo;
26  import org.exoplatform.singleton.AccountSetting;
27  import org.exoplatform.singleton.SocialServiceHelper;
28  import org.exoplatform.social.client.api.model.RestSpace;
29  
30  import android.app.LoaderManager.LoaderCallbacks;
31  import android.content.AsyncTaskLoader;
32  import android.content.Intent;
33  import android.content.Loader;
34  import android.os.Bundle;
35  import android.support.v4.app.FragmentActivity;
36  import android.util.Log;
37  import android.view.MenuItem;
38  import android.view.View;
39  import android.widget.AdapterView;
40  import android.widget.AdapterView.OnItemClickListener;
41  import android.widget.ListView;
42  
43  /**
44   * Created by The eXo Platform SAS
45   * 
46   * @author Philippe Aristote paristote@exoplatform.com
47   * @since Apr 21, 2015
48   */
49  public class SpaceSelectorActivity extends FragmentActivity implements LoaderCallbacks<List<SocialSpaceInfo>>, OnItemClickListener {
50  
51    private static final String   LOG_TAG        = "____eXo_SpaceSelectorActivity____";
52  
53    public static final String    SELECTED_SPACE = "SelectedSpace";
54  
55    private ListView              listViewSpaces;
56  
57    private SpaceListAdapter      listAdapterSpaces;
58  
59    private List<SocialSpaceInfo> listInfoSpaces;
60  
61    @Override
62    protected void onCreate(Bundle savedInstanceState) {
63      super.onCreate(savedInstanceState);
64      setContentView(R.layout.compose_message_space_selector_activity);
65      setTitle(R.string.ShareWithWhom);
66  
67      listViewSpaces = (ListView) findViewById(R.id.list_spaces);
68      listViewSpaces.setOnItemClickListener(this);
69      listAdapterSpaces = new SpaceListAdapter(this);
70      listViewSpaces.setAdapter(listAdapterSpaces);
71      listViewSpaces.setEmptyView(findViewById(R.id.list_spaces_empty_view));
72      getLoaderManager().initLoader(0, null, this).forceLoad();
73  
74    }
75  
76    /*
77     * Tap listeners
78     */
79  
80    /**
81     * Called when the "Public" item is tapped.<br/>
82     * Results in a call to
83     * 
84     * <pre>
85     * sendResultToComposer(-1);
86     * </pre>
87     * 
88     * @param view
89     */
90    public void selectPublicDestination(View view) {
91      sendResultToComposer(-1);
92    }
93  
94    @Override
95    public boolean onOptionsItemSelected(MenuItem item) {
96      switch (item.getItemId()) {
97      case android.R.id.home:
98        finish();
99        break;
100     default:
101       break;
102     }
103 
104     return true;
105   }
106   
107   /**
108    * Called when an item of the spaces list is tapped.<br/>
109    * Results in a call to
110    * 
111    * <pre>
112    * sendResultToComposer(...);
113    * </pre>
114    * 
115    * with the position of the selected space in the list.
116    */
117   @Override
118   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
119     SocialSpaceInfo selectedSpace = listInfoSpaces.get(position);
120     if (selectedSpace != null) {
121       sendResultToComposer(position);
122     } else {
123       // TODO handle failure to get space
124     }
125   }
126 
127   /**
128    * Return the result to the calling activity (the message composer).
129    * 
130    * @param result <br/>
131    *          if a space was selected, the intent will contain the extras:<br/>
132    *          - SELECTED_DESTINATION: the space technical name<br/>
133    *          - SELECTED_SPACE_DISPLAY_NAME: the space display name<br/>
134    *          - SELECTED_SPACE_IMAGE: the avatar url of the space<br/>
135    *          if "Public" was selected, the intent contains no extra
136    */
137   private void sendResultToComposer(int result) {
138     Intent data = new Intent();
139     if (result >= 0 && result < listInfoSpaces.size()) {
140       SocialSpaceInfo space = listInfoSpaces.get(result);
141       data.putExtra(SELECTED_SPACE, space);
142     }
143     setResult(RESULT_OK, data);
144     finish();
145   }
146 
147   /*
148    * Loader manager
149    */
150 
151   @Override
152   public Loader<List<SocialSpaceInfo>> onCreateLoader(int id, Bundle args) {
153     return new AsyncTaskLoader<List<SocialSpaceInfo>>(this) {
154       @Override
155       public List<SocialSpaceInfo> loadInBackground() {
156         List<SocialSpaceInfo> spacesNames = new ArrayList<SocialSpaceInfo>();
157         if (SocialServiceHelper.getInstance().spaceService == null) {
158           Log.e(LOG_TAG, "Cannot retrieve spaces. Social Space service is null.");
159           return null;
160         }
161         List<RestSpace> spaces = SocialServiceHelper.getInstance().spaceService.getMySocialSpaces();
162         String currentServer = AccountSetting.getInstance().getDomainName();
163         for (RestSpace space : spaces) {
164           SocialSpaceInfo sp = new SocialSpaceInfo();
165           sp.displayName = space.getDisplayName();
166           sp.name = space.getName();
167           sp.avatarUrl = currentServer + space.getAvatarUrl();
168           sp.groupId = space.getGroupId();
169           spacesNames.add(sp);
170         }
171         return spacesNames;
172       }
173     };
174   }
175 
176   @Override
177   public void onLoadFinished(Loader<List<SocialSpaceInfo>> loader, List<SocialSpaceInfo> data) {
178     if (data != null) {
179       listInfoSpaces = data;
180       listAdapterSpaces.setSpaceList(data);
181       listAdapterSpaces.notifyDataSetChanged();
182     }
183   }
184 
185   @Override
186   public void onLoaderReset(Loader<List<SocialSpaceInfo>> loader) {
187   }
188 }