Revision ad4d3f12 src/com/rackspace/cloud/android/PithosGroupsActivity.java

b/src/com/rackspace/cloud/android/PithosGroupsActivity.java
4 4
import java.util.HashMap;
5 5
import java.util.Map;
6 6

  
7
import org.apache.http.HttpEntity;
8
import org.apache.http.HttpResponse;
9

  
7 10
import android.app.Activity;
8 11
import android.app.AlertDialog;
9 12
import android.app.ExpandableListActivity;
10 13
import android.content.Context;
11 14
import android.content.DialogInterface;
12 15
import android.graphics.Color;
16
import android.os.AsyncTask;
13 17
import android.os.Build;
14 18
import android.os.Bundle;
15 19
import android.util.Log;
......
25 29
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
26 30

  
27 31
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
32

  
28 33
import com.rackspace.cloud.android.widget.GroupsListView;
29 34
import com.rackspace.cloud.files.api.client.Container;
30 35
import com.rackspace.cloud.files.api.client.ContainerManager;
......
34 39
import com.rackspace.cloud.servers.api.client.CloudServersException;
35 40
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
36 41

  
37
public class PithosGroupsActivity extends Activity {
42
public class PithosGroupsActivity extends CloudActivity {
38 43
	private Context context;
39 44
	private boolean loading;
40 45
	private GoogleAnalyticsTracker tracker;
46

  
41 47
	@Override
42 48
	public void onCreate(Bundle savedInstanceState) {
43 49
		super.onCreate(savedInstanceState);
......
57 63
		 * } });
58 64
		 */
59 65
	}
60
	
66

  
61 67
	@Override
62 68
	public boolean onCreateOptionsMenu(Menu menu) {
63 69
		super.onCreateOptionsMenu(menu);
......
68 74

  
69 75
	@Override
70 76
	/*
71
	 * option performed for delete depends on if you
72
	 * are at the top of a container or in a folder
77
	 * option performed for delete depends on if you are at the top of a
78
	 * container or in a folder
73 79
	 */
74 80
	public boolean onOptionsItemSelected(MenuItem item) {
75 81
		switch (item.getItemId()) {
......
78 84
			return true;
79 85
		default:
80 86
			break;
81
		
87

  
82 88
		}
83 89
		return false;
84 90
	}
85
	
86
	
87
	
91

  
88 92
	@Override
89 93
	public void onCreateContextMenu(ContextMenu menu, View v,
90 94
			ContextMenuInfo menuInfo) {
91 95
		super.onCreateContextMenu(menu, v, menuInfo);
92
		ExpandableListView.ExpandableListContextMenuInfo info =
93
				(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
94
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
95
		if(type==1)
96
		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
97
		int type = ExpandableListView
98
				.getPackedPositionType(info.packedPosition);
99
		if (type == 1)
96 100
			menu.add(1, 2, 3, "Delete");
97
		else{
101
		else {
98 102
			menu.add(1, 1, 2, "Add User");
99 103
			menu.add(1, 2, 3, "Delete");
100 104
		}
101 105
	}
102
	
106

  
103 107
	@Override
104 108
	public boolean onContextItemSelected(MenuItem item) {
105 109
		final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
106 110
				.getMenuInfo();
107 111
		int groupPos = 0, childPos = 0;
108
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
112
		int type = ExpandableListView
113
				.getPackedPositionType(info.packedPosition);
109 114
		final GroupResource group;
110 115
		final String user;
111 116
		final GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView));
112 117
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
113
			groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
114
			childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
115
			group = (GroupResource) groupsList.getGroupsAdapter().getGroup(groupPos);
116
			user = (String) groupsList.getGroupsAdapter().getChild(groupPos, childPos);
117
			
118
		}
119
		else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
120
			groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
121
			group = (GroupResource) groupsList.getGroupsAdapter().getGroup(groupPos);
122
			user=null;
123
		}
124
		else{
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 {
125 134
			group = null;
126 135
			user = null;
127 136
		}
128
		Log.i("GROUPS","****************************");
129
		Log.i("GROUPS","group:"+groupPos+" child:"+childPos);
130
		Log.i("GROUPS","****************************");
131
		
137
		Log.i("GROUPS", "****************************");
138
		Log.i("GROUPS", "group:" + groupPos + " child:" + childPos);
139
		Log.i("GROUPS", "****************************");
140

  
132 141
		AlertDialog alertDialog = null;
133 142
		switch (item.getItemId()) {
134 143
		// case R.id.new_folder:
......
136 145
		// NewFolderDialog(GssClient.this,GssClient.this,(FolderResource)currentResource).show();
137 146
		// return true;
138 147
		case 2:
139
			final boolean isUser = user !=null;
148
			final boolean isUser = user != null;
140 149
			alertDialog = new AlertDialog.Builder(this).create();
141
			if(isUser){
150
			if (isUser) {
142 151
				alertDialog.setTitle("Delete User");
143
				alertDialog
144
						.setMessage("R u sure you want to delete this user?"+user);
145
			}
146
			else{
152
				alertDialog.setMessage("R u sure you want to delete this user?"
153
						+ user);
154
			} else {
147 155
				alertDialog.setTitle("Delete Group");
148 156
				alertDialog
149
						.setMessage("R u sure you want to delete this group and all its users?"+group.getName());
157
						.setMessage("R u sure you want to delete this group and all its users?"
158
								+ group.getName());
150 159
			}
151 160
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
152 161
				public void onClick(DialogInterface dialog, int which) {
153
					HttpBundle bundel;
154
					try {
155
						Map<String,String> headers = new HashMap<String,String>();
156
						if(isUser){
157
							//groupsList.get
158
							//int gp = (int) groupsList.getGroupsAdapter().getCombinedGroupId(combinedpos);
159
							//Log.i("Groups","Position "+gp+" "+groupsList.getAdapter().getItem(gp));
160
							group.getUsers().remove(user);
161
							headers.put("X-Account-Group-"+group.getName(), group.getUserString());
162
						}
163
						else{
164
							headers.put("X-Account-Group-"+group.getName(), "~");
165
						}
166
						bundel = new ContainerObjectManager(getApplicationContext()).updateObject(null, null, null, "application/json", headers,null);
167
						//Log.d("papala",bundel.getResponse().getStatusLine().getStatusCode()+" status");
168
						if(false)
169
							throw new CloudServersException();
170
						refresh();
171
					} catch (CloudServersException e) {
172
						// TODO Auto-generated catch block
173
						e.printStackTrace();
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
								"~");
174 170
					}
175 171
				}
176 172
			});
......
184 180
			alertDialog.show();
185 181
			return true;
186 182
		case 1:
187
			final AlertDialog.Builder alert = new AlertDialog.Builder(PithosGroupsActivity.this);
188
        	alert.setTitle("Add User");
189
        	final AutoCompleteTextView input = new AutoCompleteTextView(PithosGroupsActivity.this);
190
        	input.setTextColor(Color.BLACK);
191
    		//input.setAdapter(new UserAdapter(GssGroups.this,getDroidApplication().getUserDetails(), new ArrayList<String>(),true));
192
    		alert.setView(input);
193
    		alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
194
    			public void onClick(DialogInterface dialog, int whichButton) {
195
    				String value = input.getText().toString().trim();
196
    				Map<String,String> headers = new HashMap<String,String>();
197
    				
198
    				headers.put("X-Account-Group-"+group.getName(), group.getUserString()+value);
199
    				HttpBundle bundel;
200
					try {
201
						bundel = new ContainerObjectManager(getApplicationContext()).updateObject(null, null, null, "application/json", headers,null);
202
						refresh();
203
					} catch (CloudServersException e) {
204
						// TODO Auto-generated catch block
205
						e.printStackTrace();
206
					}
207
    				((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged();
208
    			}
209
    		});
210

  
211
    		alert.setNegativeButton("Cancel",
212
    				new DialogInterface.OnClickListener() {
213
    					public void onClick(DialogInterface dialog, int whichButton) {
214
    						dialog.cancel();
215
    					}
216
    				});
217
    		alert.show();
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();
218 211
			return true;
219 212
		default:
220 213
			return super.onContextItemSelected(item);
221 214
		}
222 215
	}
223

  
224 216
	
225
	private void showNew(){
226
		final AlertDialog.Builder alert = new AlertDialog.Builder(PithosGroupsActivity.this);
227
		alert.setTitle("New Group");
228
		final EditText input = new EditText(PithosGroupsActivity.this);
229
		alert.setView(input);
230
		alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
231
			public void onClick(DialogInterface dialog, int whichButton) {
232
				String value = input.getText().toString().trim();
233
				Map<String,String> headers = new HashMap<String,String>();
234
				headers.put("X-Account-Group-"+value, "placeholder,");
235
				HttpBundle bundel;
236
				try {
237
					bundel = new ContainerObjectManager(getApplicationContext()).updateObject(null, null, null, "application/json", headers,null);
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);
238 259
					refresh();
239
				} catch (CloudServersException e) {
240
					// TODO Auto-generated catch block
241
					e.printStackTrace();
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
					}
242 273
				}
243
				((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged();
244
				
274
			} else if (exception != null) {
275
				showError("There was a problem updating your Groups. "
276
						+ exception.getMessage(), bundle);
245 277
			}
246
		});
247 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

  
248 297
		alert.setNegativeButton("Cancel",
249 298
				new DialogInterface.OnClickListener() {
250
			public void onClick(DialogInterface dialog, int whichButton) {
251
				dialog.cancel();
252
			}
253
		});
299
					public void onClick(DialogInterface dialog, int whichButton) {
300
						dialog.cancel();
301
					}
302
				});
254 303
		alert.show();
255
		        	
304

  
256 305
	}
257
	
258
	protected void refresh(){
306

  
307
	protected void refresh() {
259 308
		try {
260 309
			new ContainerManager(getApplicationContext()).createList(true);
261 310
		} catch (CloudServersException e) {
262 311
			// TODO Auto-generated catch block
263 312
			e.printStackTrace();
264 313
		}
265
		((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged();
314
		((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter()
315
				.notifyDataSetChanged();
266 316
	}
267 317

  
268 318
	protected Container getContainer() {

Also available in: Unified diff