root / src / com / rackspace / cloud / android / PithosGroupsActivity.java @ ad4d3f12
History | View | Annotate | Download (10.4 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 |
Log.i("GROUPS", "****************************"); |
138 |
Log.i("GROUPS", "group:" + groupPos + " child:" + childPos); |
139 |
Log.i("GROUPS", "****************************"); |
140 |
|
141 |
AlertDialog alertDialog = null;
|
142 |
switch (item.getItemId()) {
|
143 |
// case R.id.new_folder:
|
144 |
// new
|
145 |
// NewFolderDialog(GssClient.this,GssClient.this,(FolderResource)currentResource).show();
|
146 |
// return true;
|
147 |
case 2: |
148 |
final boolean isUser = user != null; |
149 |
alertDialog = new AlertDialog.Builder(this).create(); |
150 |
if (isUser) {
|
151 |
alertDialog.setTitle("Delete User");
|
152 |
alertDialog.setMessage("R u sure you want to delete this user?"
|
153 |
+ user); |
154 |
} else {
|
155 |
alertDialog.setTitle("Delete Group");
|
156 |
alertDialog |
157 |
.setMessage("R u sure you want to delete this group and all its users?"
|
158 |
+ group.getName()); |
159 |
} |
160 |
alertDialog.setButton("OK", new DialogInterface.OnClickListener() { |
161 |
public void onClick(DialogInterface dialog, int which) { |
162 |
Map<String, String> headers = new HashMap<String, String>(); |
163 |
if (isUser) {
|
164 |
group.getUsers().remove(user); |
165 |
new GroupOrUserTask().execute("X-Account-Group-" + group.getName(), |
166 |
group.getUserString()); |
167 |
} else {
|
168 |
new GroupOrUserTask().execute("X-Account-Group-" + group.getName(), |
169 |
"~");
|
170 |
} |
171 |
} |
172 |
}); |
173 |
alertDialog.setButton2("CANCEL",
|
174 |
new DialogInterface.OnClickListener() {
|
175 |
public void onClick(DialogInterface dialog, int which) { |
176 |
|
177 |
} |
178 |
}); |
179 |
alertDialog.setCancelable(true);
|
180 |
alertDialog.show(); |
181 |
return true; |
182 |
case 1: |
183 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
184 |
PithosGroupsActivity.this); |
185 |
alert.setTitle("Add User");
|
186 |
final AutoCompleteTextView input = new AutoCompleteTextView( |
187 |
PithosGroupsActivity.this); |
188 |
input.setTextColor(Color.BLACK);
|
189 |
// input.setAdapter(new
|
190 |
// UserAdapter(GssGroups.this,getDroidApplication().getUserDetails(),
|
191 |
// new ArrayList<String>(),true));
|
192 |
alert.setView(input); |
193 |
alert.setPositiveButton("Add",
|
194 |
new DialogInterface.OnClickListener() {
|
195 |
public void onClick(DialogInterface dialog, |
196 |
int whichButton) {
|
197 |
String value = input.getText().toString().trim();
|
198 |
new GroupOrUserTask().execute("X-Account-Group-" + group.getName(), |
199 |
group.getUserString() + value); |
200 |
} |
201 |
}); |
202 |
|
203 |
alert.setNegativeButton("Cancel",
|
204 |
new DialogInterface.OnClickListener() {
|
205 |
public void onClick(DialogInterface dialog, |
206 |
int whichButton) {
|
207 |
dialog.cancel(); |
208 |
} |
209 |
}); |
210 |
alert.show(); |
211 |
return true; |
212 |
default:
|
213 |
return super.onContextItemSelected(item); |
214 |
} |
215 |
} |
216 |
|
217 |
private HttpBundle updateObject( String header, String value) throws CloudServersException{ |
218 |
Map<String, String> headers = new HashMap<String, String>(); |
219 |
|
220 |
headers.put(header,value); |
221 |
HttpBundle bundel = new ContainerObjectManager(
|
222 |
getApplicationContext()).updateObject( |
223 |
null, null, null, "application/json", |
224 |
headers, null);
|
225 |
return bundel;
|
226 |
} |
227 |
|
228 |
private class GroupOrUserTask extends |
229 |
AsyncTask<String, Void, HttpBundle> { |
230 |
|
231 |
private CloudServersException exception;
|
232 |
|
233 |
@Override
|
234 |
protected void onPreExecute() { |
235 |
showDialog(); |
236 |
app.setDownloadingObject(true);
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
protected HttpBundle doInBackground(String... arg0) { |
241 |
HttpBundle bundle = null;
|
242 |
try {
|
243 |
bundle = updateObject(arg0[0], arg0[1]); |
244 |
} catch (CloudServersException e) {
|
245 |
exception = e; |
246 |
} |
247 |
return bundle;
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
protected void onPostExecute(HttpBundle bundle) { |
252 |
hideDialog(); |
253 |
HttpResponse response = bundle.getResponse(); |
254 |
if (response != null) { |
255 |
int statusCode = response.getStatusLine().getStatusCode();
|
256 |
Log.i("Groups","status:"+statusCode); |
257 |
if (statusCode == 202) { |
258 |
setResult(Activity.RESULT_OK); |
259 |
refresh(); |
260 |
((GroupsListView) findViewById(R.id.groupsListView)) |
261 |
.getGroupsAdapter().notifyDataSetChanged(); |
262 |
|
263 |
} else {
|
264 |
CloudServersException cse = parseCloudServersException(response); |
265 |
cse.printStackTrace(); |
266 |
if ("".equals(cse.getMessage())) { |
267 |
showError("There was a problem updating your Groups.",
|
268 |
bundle); |
269 |
} else {
|
270 |
showError("There was a problem updating your Groups. "
|
271 |
+ cse.getMessage(), bundle); |
272 |
} |
273 |
} |
274 |
} else if (exception != null) { |
275 |
showError("There was a problem updating your Groups. "
|
276 |
+ exception.getMessage(), bundle); |
277 |
} |
278 |
|
279 |
} |
280 |
} |
281 |
|
282 |
private void showNew() { |
283 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
284 |
PithosGroupsActivity.this); |
285 |
alert.setTitle("New Group");
|
286 |
final EditText input = new EditText(PithosGroupsActivity.this); |
287 |
alert.setView(input); |
288 |
alert.setPositiveButton("Create",
|
289 |
new DialogInterface.OnClickListener() {
|
290 |
public void onClick(DialogInterface dialog, int whichButton) { |
291 |
String value = input.getText().toString().trim();
|
292 |
new GroupOrUserTask().execute("X-Account-Group-" + value, "placeholder,"); |
293 |
|
294 |
} |
295 |
}); |
296 |
|
297 |
alert.setNegativeButton("Cancel",
|
298 |
new DialogInterface.OnClickListener() {
|
299 |
public void onClick(DialogInterface dialog, int whichButton) { |
300 |
dialog.cancel(); |
301 |
} |
302 |
}); |
303 |
alert.show(); |
304 |
|
305 |
} |
306 |
|
307 |
protected void refresh() { |
308 |
try {
|
309 |
new ContainerManager(getApplicationContext()).createList(true); |
310 |
} catch (CloudServersException e) {
|
311 |
// TODO Auto-generated catch block
|
312 |
e.printStackTrace(); |
313 |
} |
314 |
((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter() |
315 |
.notifyDataSetChanged(); |
316 |
} |
317 |
|
318 |
protected Container getContainer() { |
319 |
return (Container) this.getIntent().getExtras().get("container"); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
protected void onSaveInstanceState(Bundle outState) { |
324 |
super.onSaveInstanceState(outState);
|
325 |
|
326 |
} |
327 |
|
328 |
public void startTracker() { |
329 |
if (!"google_sdk".equals(Build.PRODUCT) && !"sdk".equals(Build.PRODUCT)) { |
330 |
tracker = GoogleAnalyticsTracker.getInstance(); |
331 |
tracker.start(Config.WEB_PROPERTY_ID, 20, this); |
332 |
} |
333 |
} |
334 |
|
335 |
public void trackPageView(String page) { |
336 |
if (tracker != null) { |
337 |
tracker.trackPageView(page); |
338 |
} |
339 |
} |
340 |
|
341 |
@Override
|
342 |
protected void onDestroy() { |
343 |
super.onDestroy();
|
344 |
if (tracker != null) { |
345 |
tracker.stop(); |
346 |
} |
347 |
} |
348 |
|
349 |
public void trackEvent(String category, String action, String label, |
350 |
int value) {
|
351 |
if (tracker != null) { |
352 |
tracker.trackEvent(category, action, label, value); |
353 |
} |
354 |
} |
355 |
|
356 |
} |