Revision 732d9c2c src/com/rackspace/cloud/android/PithosGroupsActivity.java
b/src/com/rackspace/cloud/android/PithosGroupsActivity.java | ||
---|---|---|
1 | 1 |
package com.rackspace.cloud.android; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.Map; |
|
6 |
|
|
3 | 7 |
import android.app.Activity; |
8 |
import android.app.AlertDialog; |
|
4 | 9 |
import android.content.Context; |
10 |
import android.content.DialogInterface; |
|
11 |
import android.graphics.Color; |
|
5 | 12 |
import android.os.Build; |
6 | 13 |
import android.os.Bundle; |
14 |
import android.util.Log; |
|
15 |
import android.view.ContextMenu; |
|
16 |
import android.view.ContextMenu.ContextMenuInfo; |
|
17 |
import android.view.Menu; |
|
18 |
import android.view.MenuInflater; |
|
19 |
import android.view.MenuItem; |
|
20 |
import android.view.View; |
|
21 |
import android.widget.AutoCompleteTextView; |
|
22 |
import android.widget.EditText; |
|
23 |
import android.widget.ExpandableListView.ExpandableListContextMenuInfo; |
|
7 | 24 |
|
8 | 25 |
import com.google.android.apps.analytics.GoogleAnalyticsTracker; |
9 | 26 |
import com.rackspace.cloud.android.widget.GroupsListView; |
10 | 27 |
import com.rackspace.cloud.files.api.client.Container; |
28 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
|
29 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
|
30 |
import com.rackspace.cloud.files.api.client.GroupResource; |
|
31 |
import com.rackspace.cloud.servers.api.client.Account; |
|
32 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
|
33 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
|
11 | 34 |
|
12 | 35 |
public class PithosGroupsActivity extends Activity { |
13 | 36 |
private Context context; |
... | ... | |
20 | 43 |
trackPageView(GoogleAnalytics.PAGE_FOLDER); |
21 | 44 |
setContentView(R.layout.groups); |
22 | 45 |
GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView)); |
46 |
registerForContextMenu(groupsList); |
|
23 | 47 |
/* |
24 | 48 |
* groupsList.setOnGroupExpandListener(new OnGroupExpandListener() { |
25 | 49 |
* |
... | ... | |
31 | 55 |
* } }); |
32 | 56 |
*/ |
33 | 57 |
} |
58 |
|
|
59 |
@Override |
|
60 |
public boolean onCreateOptionsMenu(Menu menu) { |
|
61 |
super.onCreateOptionsMenu(menu); |
|
62 |
MenuInflater inflater = getMenuInflater(); |
|
63 |
inflater.inflate(R.menu.groups_menu, menu); |
|
64 |
return true; |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
/* |
|
69 |
* option performed for delete depends on if you |
|
70 |
* are at the top of a container or in a folder |
|
71 |
*/ |
|
72 |
public boolean onOptionsItemSelected(MenuItem item) { |
|
73 |
switch (item.getItemId()) { |
|
74 |
case R.id.add_group: |
|
75 |
showNew(); |
|
76 |
return true; |
|
77 |
default: |
|
78 |
break; |
|
79 |
|
|
80 |
} |
|
81 |
return false; |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public void onCreateContextMenu(ContextMenu menu, View v, |
|
86 |
ContextMenuInfo menuInfo) { |
|
87 |
super.onCreateContextMenu(menu, v, menuInfo); |
|
88 |
GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView)); |
|
89 |
final Object entry = groupsList.getAdapter().getItem((int) ((ExpandableListContextMenuInfo)menuInfo).id); |
|
90 |
if(entry instanceof GroupResource){ |
|
91 |
menu.add(1, 1, 2, "Add User"); |
|
92 |
menu.add(1, 2, 3, "Delete"); |
|
93 |
} |
|
94 |
else if(entry instanceof String){ |
|
95 |
menu.add(1, 2, 3, "Delete"); |
|
96 |
} |
|
97 |
|
|
98 |
/* |
|
99 |
* AdapterView.AdapterContextMenuInfo info = |
|
100 |
* (AdapterView.AdapterContextMenuInfo) menuInfo; GssFileListAdapter |
|
101 |
* adapt = (GssFileListAdapter) ((GssFileListView) |
|
102 |
* findViewById(R.id.fileListView)) .getAdapter(); final |
|
103 |
* GssFileListAdapterEntry entry = adapt.getItem(info.position); // |
|
104 |
* if(entry.file instanceof FolderResource) |
|
105 |
*/ |
|
106 |
|
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public boolean onContextItemSelected(MenuItem item) { |
|
111 |
final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item |
|
112 |
.getMenuInfo(); |
|
113 |
AlertDialog alertDialog = null; |
|
114 |
GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView)); |
|
115 |
final Object entry = groupsList.getAdapter().getItem((int) ((ExpandableListContextMenuInfo)info).id); |
|
116 |
switch (item.getItemId()) { |
|
117 |
// case R.id.new_folder: |
|
118 |
// new |
|
119 |
// NewFolderDialog(GssClient.this,GssClient.this,(FolderResource)currentResource).show(); |
|
120 |
// return true; |
|
121 |
case 2: |
|
122 |
alertDialog = new AlertDialog.Builder(this).create(); |
|
123 |
alertDialog.setTitle("Delete Group"); |
|
124 |
alertDialog |
|
125 |
.setMessage("R u sure you want to delete this group and all its users?"); |
|
126 |
alertDialog.setButton("OK", new DialogInterface.OnClickListener() { |
|
127 |
public void onClick(DialogInterface dialog, int which) { |
|
128 |
HttpBundle bundel; |
|
129 |
try { |
|
130 |
Map<String,String> headers = new HashMap<String,String>(); |
|
131 |
headers.put("X-Account-Group-"+((GroupResource)entry).getName(), "~"); |
|
132 |
bundel = new ContainerObjectManager(getApplicationContext()).updateObject(null, null, null, "application/json", headers); |
|
133 |
Log.d("papala",bundel.getResponse().getStatusLine().getStatusCode()+" status"); |
|
134 |
refresh(); |
|
135 |
} catch (CloudServersException e) { |
|
136 |
// TODO Auto-generated catch block |
|
137 |
e.printStackTrace(); |
|
138 |
} |
|
139 |
} |
|
140 |
}); |
|
141 |
alertDialog.setButton2("CANCEL", |
|
142 |
new DialogInterface.OnClickListener() { |
|
143 |
public void onClick(DialogInterface dialog, int which) { |
|
144 |
|
|
145 |
} |
|
146 |
}); |
|
147 |
alertDialog.setCancelable(true); |
|
148 |
alertDialog.show(); |
|
149 |
return true; |
|
150 |
case 1: |
|
151 |
final AlertDialog.Builder alert = new AlertDialog.Builder(PithosGroupsActivity.this); |
|
152 |
alert.setTitle("Add User"); |
|
153 |
final AutoCompleteTextView input = new AutoCompleteTextView(PithosGroupsActivity.this); |
|
154 |
input.setTextColor(Color.BLACK); |
|
155 |
//input.setAdapter(new UserAdapter(GssGroups.this,getDroidApplication().getUserDetails(), new ArrayList<String>(),true)); |
|
156 |
alert.setView(input); |
|
157 |
alert.setPositiveButton("Add", new DialogInterface.OnClickListener() { |
|
158 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
159 |
String value = input.getText().toString().trim(); |
|
160 |
//getNewGroupTask().execute(entry.getUri(),value); |
|
161 |
} |
|
162 |
}); |
|
163 |
|
|
164 |
alert.setNegativeButton("Cancel", |
|
165 |
new DialogInterface.OnClickListener() { |
|
166 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
167 |
dialog.cancel(); |
|
168 |
} |
|
169 |
}); |
|
170 |
alert.show(); |
|
171 |
return true; |
|
172 |
default: |
|
173 |
return super.onContextItemSelected(item); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
|
|
178 |
private void showNew(){ |
|
179 |
final CharSequence[] items = {"New Group"}; |
|
180 |
AlertDialog.Builder builder = new AlertDialog.Builder(PithosGroupsActivity.this); |
|
181 |
builder.setTitle("Create ..."); |
|
182 |
builder.setItems(items, new DialogInterface.OnClickListener() { |
|
183 |
public void onClick(DialogInterface dialog, int item) { |
|
184 |
switch(item){ |
|
185 |
case 0: |
|
186 |
final AlertDialog.Builder alert = new AlertDialog.Builder(PithosGroupsActivity.this); |
|
187 |
alert.setTitle("New Group"); |
|
188 |
final EditText input = new EditText(PithosGroupsActivity.this); |
|
189 |
alert.setView(input); |
|
190 |
alert.setPositiveButton("Create", new DialogInterface.OnClickListener() { |
|
191 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
192 |
String value = input.getText().toString().trim(); |
|
193 |
Map<String,String> headers = new HashMap<String,String>(); |
|
194 |
headers.put("X-Account-Group-"+value, Account.getAccount().getUsername()+","); |
|
195 |
HttpBundle bundel; |
|
196 |
try { |
|
197 |
bundel = new ContainerObjectManager(getApplicationContext()).updateObject(null, null, null, "application/json", headers); |
|
198 |
refresh(); |
|
199 |
} catch (CloudServersException e) { |
|
200 |
// TODO Auto-generated catch block |
|
201 |
e.printStackTrace(); |
|
202 |
} |
|
203 |
((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged(); |
|
204 |
|
|
205 |
} |
|
206 |
}); |
|
207 |
|
|
208 |
alert.setNegativeButton("Cancel", |
|
209 |
new DialogInterface.OnClickListener() { |
|
210 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
211 |
dialog.cancel(); |
|
212 |
} |
|
213 |
}); |
|
214 |
alert.show(); |
|
215 |
break; |
|
216 |
default: |
|
217 |
break; |
|
218 |
} |
|
219 |
} |
|
220 |
}); |
|
221 |
AlertDialog alert = builder.create(); |
|
222 |
alert.show(); |
|
223 |
} |
|
224 |
|
|
225 |
protected void refresh(){ |
|
226 |
try { |
|
227 |
new ContainerManager(getApplicationContext()).createList(true); |
|
228 |
} catch (CloudServersException e) { |
|
229 |
// TODO Auto-generated catch block |
|
230 |
e.printStackTrace(); |
|
231 |
} |
|
232 |
((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged(); |
|
233 |
} |
|
34 | 234 |
|
35 | 235 |
protected Container getContainer() { |
36 | 236 |
return (Container) this.getIntent().getExtras().get("container"); |
Also available in: Unified diff