root / src / com / rackspace / cloud / android / PithosGroupsActivity.java @ 378fe36a
History | View | Annotate | Download (10.7 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.HashMap; |
5 |
import java.util.Map; |
6 |
|
7 |
import org.apache.http.HttpEntity; |
8 |
import org.apache.http.HttpResponse; |
9 |
|
10 |
import android.app.Activity; |
11 |
import android.app.AlertDialog; |
12 |
import android.app.ExpandableListActivity; |
13 |
import android.content.Context; |
14 |
import android.content.DialogInterface; |
15 |
import android.graphics.Color; |
16 |
import android.os.AsyncTask; |
17 |
import android.os.Build; |
18 |
import android.os.Bundle; |
19 |
import android.util.Log; |
20 |
import android.view.ContextMenu; |
21 |
import android.view.ContextMenu.ContextMenuInfo; |
22 |
import android.view.Menu; |
23 |
import android.view.MenuInflater; |
24 |
import android.view.MenuItem; |
25 |
import android.view.View; |
26 |
import android.widget.AutoCompleteTextView; |
27 |
import android.widget.EditText; |
28 |
import android.widget.ExpandableListView; |
29 |
import android.widget.ExpandableListView.ExpandableListContextMenuInfo; |
30 |
|
31 |
import com.google.android.apps.analytics.GoogleAnalyticsTracker; |
32 |
|
33 |
import com.rackspace.cloud.android.widget.GroupsListView; |
34 |
import com.rackspace.cloud.files.api.client.Container; |
35 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
36 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
37 |
import com.rackspace.cloud.files.api.client.GroupResource; |
38 |
import com.rackspace.cloud.servers.api.client.Account; |
39 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
40 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
41 |
|
42 |
public class PithosGroupsActivity extends CloudActivity { |
43 |
private Context context; |
44 |
private boolean loading; |
45 |
private GoogleAnalyticsTracker tracker;
|
46 |
|
47 |
@Override
|
48 |
public void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState);
|
50 |
startTracker(); |
51 |
trackPageView(GoogleAnalytics.PAGE_FOLDER); |
52 |
setContentView(R.layout.groups); |
53 |
GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView)); |
54 |
registerForContextMenu(groupsList); |
55 |
/*
|
56 |
* groupsList.setOnGroupExpandListener(new OnGroupExpandListener() {
|
57 |
*
|
58 |
* @Override public void onGroupExpand(int groupPosition) {
|
59 |
* GroupResource g = (GroupResource)
|
60 |
* groupsList.getListdapter().getGroup(groupPosition); if(g!=null){
|
61 |
* getGroupUserTask(groupPosition).execute(g.getUri()); }
|
62 |
*
|
63 |
* } });
|
64 |
*/
|
65 |
} |
66 |
|
67 |
@Override
|
68 |
public boolean onCreateOptionsMenu(Menu menu) { |
69 |
super.onCreateOptionsMenu(menu);
|
70 |
MenuInflater inflater = getMenuInflater(); |
71 |
inflater.inflate(R.menu.groups_menu, menu); |
72 |
return true; |
73 |
} |
74 |
|
75 |
@Override
|
76 |
/*
|
77 |
* option performed for delete depends on if you are at the top of a
|
78 |
* container or in a folder
|
79 |
*/
|
80 |
public boolean onOptionsItemSelected(MenuItem item) { |
81 |
switch (item.getItemId()) {
|
82 |
case R.id.add_group:
|
83 |
showNew(); |
84 |
return true; |
85 |
default:
|
86 |
break;
|
87 |
|
88 |
} |
89 |
return false; |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public void onCreateContextMenu(ContextMenu menu, View v, |
94 |
ContextMenuInfo menuInfo) { |
95 |
super.onCreateContextMenu(menu, v, menuInfo);
|
96 |
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; |
97 |
int type = ExpandableListView
|
98 |
.getPackedPositionType(info.packedPosition); |
99 |
if (type == 1) |
100 |
menu.add(1, 2, 3, "Delete"); |
101 |
else {
|
102 |
menu.add(1, 1, 2, "Add User"); |
103 |
menu.add(1, 2, 3, "Delete"); |
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public boolean onContextItemSelected(MenuItem item) { |
109 |
final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
|
110 |
.getMenuInfo(); |
111 |
int groupPos = 0, childPos = 0; |
112 |
int type = ExpandableListView
|
113 |
.getPackedPositionType(info.packedPosition); |
114 |
final GroupResource group;
|
115 |
final String user; |
116 |
final GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView));
|
117 |
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
|
118 |
groupPos = ExpandableListView |
119 |
.getPackedPositionGroup(info.packedPosition); |
120 |
childPos = ExpandableListView |
121 |
.getPackedPositionChild(info.packedPosition); |
122 |
group = (GroupResource) groupsList.getGroupsAdapter().getGroup( |
123 |
groupPos); |
124 |
user = (String) groupsList.getGroupsAdapter().getChild(groupPos,
|
125 |
childPos); |
126 |
|
127 |
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { |
128 |
groupPos = ExpandableListView |
129 |
.getPackedPositionGroup(info.packedPosition); |
130 |
group = (GroupResource) groupsList.getGroupsAdapter().getGroup( |
131 |
groupPos); |
132 |
user = null;
|
133 |
} else {
|
134 |
group = null;
|
135 |
user = null;
|
136 |
} |
137 |
|
138 |
AlertDialog alertDialog = null;
|
139 |
switch (item.getItemId()) {
|
140 |
// case R.id.new_folder:
|
141 |
// new
|
142 |
// NewFolderDialog(GssClient.this,GssClient.this,(FolderResource)currentResource).show();
|
143 |
// return true;
|
144 |
case 2: |
145 |
final boolean isUser = user != null; |
146 |
alertDialog = new AlertDialog.Builder(this).create(); |
147 |
if (isUser) {
|
148 |
alertDialog.setTitle("Delete User");
|
149 |
alertDialog.setMessage("R u sure you want to delete this user?"
|
150 |
+ user); |
151 |
} else {
|
152 |
alertDialog.setTitle("Delete Group");
|
153 |
alertDialog |
154 |
.setMessage("R u sure you want to delete this group and all its users?"
|
155 |
+ group.getName()); |
156 |
} |
157 |
alertDialog.setButton("OK", new DialogInterface.OnClickListener() { |
158 |
public void onClick(DialogInterface dialog, int which) { |
159 |
Map<String, String> headers = new HashMap<String, String>(); |
160 |
if (isUser) {
|
161 |
group.getUsers().remove(user); |
162 |
new GroupOrUserTask().execute("X-Account-Group-" |
163 |
+ group.getName(), group.getUserString()); |
164 |
} else {
|
165 |
new GroupOrUserTask().execute("X-Account-Group-" |
166 |
+ group.getName(), "~");
|
167 |
} |
168 |
} |
169 |
}); |
170 |
alertDialog.setButton2("CANCEL",
|
171 |
new DialogInterface.OnClickListener() {
|
172 |
public void onClick(DialogInterface dialog, int which) { |
173 |
|
174 |
} |
175 |
}); |
176 |
alertDialog.setCancelable(true);
|
177 |
alertDialog.show(); |
178 |
return true; |
179 |
case 1: |
180 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
181 |
PithosGroupsActivity.this); |
182 |
alert.setTitle("Add User");
|
183 |
final AutoCompleteTextView input = new AutoCompleteTextView( |
184 |
PithosGroupsActivity.this); |
185 |
input.setTextColor(Color.BLACK);
|
186 |
// input.setAdapter(new
|
187 |
// UserAdapter(GssGroups.this,getDroidApplication().getUserDetails(),
|
188 |
// new ArrayList<String>(),true));
|
189 |
alert.setView(input); |
190 |
alert.setPositiveButton("Add",
|
191 |
new DialogInterface.OnClickListener() {
|
192 |
public void onClick(DialogInterface dialog, |
193 |
int whichButton) {
|
194 |
String value = input.getText().toString().trim();
|
195 |
String uuid = Account.getAccount().getUUIDForDisplayName(value, PithosGroupsActivity.this);
|
196 |
if(uuid == null){ |
197 |
showAlert("User Does Not Exist", "The username you supplied is not valid"); |
198 |
} |
199 |
else{
|
200 |
new GroupOrUserTask().execute("X-Account-Group-" |
201 |
+ group.getName(), group.getUserString() |
202 |
+ value); |
203 |
} |
204 |
} |
205 |
}); |
206 |
|
207 |
alert.setNegativeButton("Cancel",
|
208 |
new DialogInterface.OnClickListener() {
|
209 |
public void onClick(DialogInterface dialog, |
210 |
int whichButton) {
|
211 |
dialog.cancel(); |
212 |
} |
213 |
}); |
214 |
alert.show(); |
215 |
return true; |
216 |
default:
|
217 |
return super.onContextItemSelected(item); |
218 |
} |
219 |
} |
220 |
|
221 |
private HttpBundle updateObject(String header, String value) |
222 |
throws CloudServersException {
|
223 |
Map<String, String> headers = new HashMap<String, String>(); |
224 |
|
225 |
headers.put(header, value); |
226 |
HttpBundle bundel = new ContainerObjectManager(getApplicationContext())
|
227 |
.updateObject(null, null, null, "application/json", headers, |
228 |
null);
|
229 |
return bundel;
|
230 |
} |
231 |
|
232 |
private class GroupOrUserTask extends AsyncTask<String, Void, HttpBundle> { |
233 |
|
234 |
private CloudServersException exception;
|
235 |
|
236 |
@Override
|
237 |
protected void onPreExecute() { |
238 |
showDialog(); |
239 |
app.setDownloadingObject(true);
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
protected HttpBundle doInBackground(String... arg0) { |
244 |
HttpBundle bundle = null;
|
245 |
try {
|
246 |
bundle = updateObject(arg0[0], arg0[1]); |
247 |
bundle.parseResponse(); |
248 |
} catch (CloudServersException e) {
|
249 |
exception = e; |
250 |
} |
251 |
return bundle;
|
252 |
} |
253 |
|
254 |
@Override
|
255 |
protected void onPostExecute(HttpBundle bundle) { |
256 |
hideDialog(); |
257 |
HttpResponse response = bundle.getResponse(); |
258 |
if (response != null) { |
259 |
int statusCode = response.getStatusLine().getStatusCode();
|
260 |
Log.i("Groups", "status:" + statusCode); |
261 |
if (statusCode == 202) { |
262 |
setResult(Activity.RESULT_OK); |
263 |
refresh(); |
264 |
((GroupsListView) findViewById(R.id.groupsListView)) |
265 |
.getGroupsAdapter().notifyDataSetChanged(); |
266 |
|
267 |
} else {
|
268 |
CloudServersException cse = parseCloudServersException(response); |
269 |
cse.printStackTrace(); |
270 |
if ("".equals(cse.getMessage())) { |
271 |
showError("There was a problem updating your Groups.",
|
272 |
bundle); |
273 |
} else {
|
274 |
showError("There was a problem updating your Groups. "
|
275 |
+ cse.getMessage(), bundle); |
276 |
} |
277 |
} |
278 |
} else if (exception != null) { |
279 |
showError("There was a problem updating your Groups. "
|
280 |
+ exception.getMessage(), bundle); |
281 |
} |
282 |
|
283 |
} |
284 |
} |
285 |
|
286 |
private void showNew() { |
287 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
288 |
PithosGroupsActivity.this); |
289 |
alert.setTitle("New Group");
|
290 |
final EditText input = new EditText(PithosGroupsActivity.this); |
291 |
alert.setView(input); |
292 |
alert.setPositiveButton("Create",
|
293 |
new DialogInterface.OnClickListener() {
|
294 |
public void onClick(DialogInterface dialog, int whichButton) { |
295 |
String value = input.getText().toString().trim();
|
296 |
new GroupOrUserTask().execute("X-Account-Group-" |
297 |
+ value, "placeholder,");
|
298 |
|
299 |
} |
300 |
}); |
301 |
|
302 |
alert.setNegativeButton("Cancel",
|
303 |
new DialogInterface.OnClickListener() {
|
304 |
public void onClick(DialogInterface dialog, int whichButton) { |
305 |
dialog.cancel(); |
306 |
} |
307 |
}); |
308 |
alert.show(); |
309 |
|
310 |
} |
311 |
|
312 |
protected void refresh() { |
313 |
new AsyncTask<Void, Void, Void>() { |
314 |
|
315 |
@Override
|
316 |
protected Void doInBackground(Void... params) { |
317 |
try {
|
318 |
new ContainerManager(getApplicationContext())
|
319 |
.createList(true);
|
320 |
} catch (CloudServersException e) {
|
321 |
// TODO Auto-generated catch block
|
322 |
e.printStackTrace(); |
323 |
} |
324 |
return null; |
325 |
} |
326 |
|
327 |
protected void onPostExecute(Void result) { |
328 |
((GroupsListView) findViewById(R.id.groupsListView)) |
329 |
.getGroupsAdapter().notifyDataSetChanged(); |
330 |
}; |
331 |
|
332 |
}.execute(); |
333 |
|
334 |
} |
335 |
|
336 |
protected Container getContainer() { |
337 |
return (Container) this.getIntent().getExtras().get("container"); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
protected void onSaveInstanceState(Bundle outState) { |
342 |
super.onSaveInstanceState(outState);
|
343 |
|
344 |
} |
345 |
|
346 |
public void startTracker() { |
347 |
if (!"google_sdk".equals(Build.PRODUCT) && !"sdk".equals(Build.PRODUCT)) { |
348 |
tracker = GoogleAnalyticsTracker.getInstance(); |
349 |
tracker.start(Config.WEB_PROPERTY_ID, 20, this); |
350 |
} |
351 |
} |
352 |
|
353 |
public void trackPageView(String page) { |
354 |
if (tracker != null) { |
355 |
tracker.trackPageView(page); |
356 |
} |
357 |
} |
358 |
|
359 |
@Override
|
360 |
protected void onDestroy() { |
361 |
super.onDestroy();
|
362 |
if (tracker != null) { |
363 |
tracker.stop(); |
364 |
} |
365 |
} |
366 |
|
367 |
public void trackEvent(String category, String action, String label, |
368 |
int value) {
|
369 |
if (tracker != null) { |
370 |
tracker.trackEvent(category, action, label, value); |
371 |
} |
372 |
} |
373 |
|
374 |
} |