Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / PithosGroupsActivity.java @ fc8b1f79

History | View | Annotate | Download (9.9 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 android.app.Activity;
8
import android.app.AlertDialog;
9
import android.app.ExpandableListActivity;
10
import android.content.Context;
11
import android.content.DialogInterface;
12
import android.graphics.Color;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.util.Log;
16
import android.view.ContextMenu;
17
import android.view.ContextMenu.ContextMenuInfo;
18
import android.view.Menu;
19
import android.view.MenuInflater;
20
import android.view.MenuItem;
21
import android.view.View;
22
import android.widget.AutoCompleteTextView;
23
import android.widget.EditText;
24
import android.widget.ExpandableListView;
25
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
26

    
27
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
28
import com.rackspace.cloud.android.widget.GroupsListView;
29
import com.rackspace.cloud.files.api.client.Container;
30
import com.rackspace.cloud.files.api.client.ContainerManager;
31
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
32
import com.rackspace.cloud.files.api.client.GroupResource;
33
import com.rackspace.cloud.servers.api.client.Account;
34
import com.rackspace.cloud.servers.api.client.CloudServersException;
35
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
36

    
37
public class PithosGroupsActivity extends Activity {
38
        private Context context;
39
        private boolean loading;
40
        private GoogleAnalyticsTracker tracker;
41
        @Override
42
        public void onCreate(Bundle savedInstanceState) {
43
                super.onCreate(savedInstanceState);
44
                startTracker();
45
                trackPageView(GoogleAnalytics.PAGE_FOLDER);
46
                setContentView(R.layout.groups);
47
                GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView));
48
                registerForContextMenu(groupsList);
49
                /*
50
                 * groupsList.setOnGroupExpandListener(new OnGroupExpandListener() {
51
                 * 
52
                 * @Override public void onGroupExpand(int groupPosition) {
53
                 * GroupResource g = (GroupResource)
54
                 * groupsList.getListdapter().getGroup(groupPosition); if(g!=null){
55
                 * getGroupUserTask(groupPosition).execute(g.getUri()); }
56
                 * 
57
                 * } });
58
                 */
59
        }
60
        
61
        @Override
62
        public boolean onCreateOptionsMenu(Menu menu) {
63
                super.onCreateOptionsMenu(menu);
64
                MenuInflater inflater = getMenuInflater();
65
                inflater.inflate(R.menu.groups_menu, menu);
66
                return true;
67
        }
68

    
69
        @Override
70
        /*
71
         * option performed for delete depends on if you
72
         * are at the top of a container or in a folder
73
         */
74
        public boolean onOptionsItemSelected(MenuItem item) {
75
                switch (item.getItemId()) {
76
                case R.id.add_group:
77
                        showNew();
78
                        return true;
79
                default:
80
                        break;
81
                
82
                }
83
                return false;
84
        }
85
        
86
        
87
        
88
        @Override
89
        public void onCreateContextMenu(ContextMenu menu, View v,
90
                        ContextMenuInfo menuInfo) {
91
                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
                        menu.add(1, 2, 3, "Delete");
97
                else{
98
                        menu.add(1, 1, 2, "Add User");
99
                        menu.add(1, 2, 3, "Delete");
100
                }
101
        }
102
        
103
        @Override
104
        public boolean onContextItemSelected(MenuItem item) {
105
                final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
106
                                .getMenuInfo();
107
                int groupPos = 0, childPos = 0;
108
                int type = ExpandableListView.getPackedPositionType(info.packedPosition);
109
                final GroupResource group;
110
                final String user;
111
                final GroupsListView groupsList = ((GroupsListView) findViewById(R.id.groupsListView));
112
                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{
125
                        group = null;
126
                        user = null;
127
                }
128
                Log.i("GROUPS","****************************");
129
                Log.i("GROUPS","group:"+groupPos+" child:"+childPos);
130
                Log.i("GROUPS","****************************");
131
                
132
                AlertDialog alertDialog = null;
133
                switch (item.getItemId()) {
134
                // case R.id.new_folder:
135
                // new
136
                // NewFolderDialog(GssClient.this,GssClient.this,(FolderResource)currentResource).show();
137
                // return true;
138
                case 2:
139
                        final boolean isUser = user !=null;
140
                        alertDialog = new AlertDialog.Builder(this).create();
141
                        if(isUser){
142
                                alertDialog.setTitle("Delete User");
143
                                alertDialog
144
                                                .setMessage("R u sure you want to delete this user?"+user);
145
                        }
146
                        else{
147
                                alertDialog.setTitle("Delete Group");
148
                                alertDialog
149
                                                .setMessage("R u sure you want to delete this group and all its users?"+group.getName());
150
                        }
151
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
152
                                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();
174
                                        }
175
                                }
176
                        });
177
                        alertDialog.setButton2("CANCEL",
178
                                        new DialogInterface.OnClickListener() {
179
                                                public void onClick(DialogInterface dialog, int which) {
180

    
181
                                                }
182
                                        });
183
                        alertDialog.setCancelable(true);
184
                        alertDialog.show();
185
                        return true;
186
                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();
218
                        return true;
219
                default:
220
                        return super.onContextItemSelected(item);
221
                }
222
        }
223

    
224
        
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);
238
                                        refresh();
239
                                } catch (CloudServersException e) {
240
                                        // TODO Auto-generated catch block
241
                                        e.printStackTrace();
242
                                }
243
                                ((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged();
244
                                
245
                        }
246
                });
247
                        
248
                alert.setNegativeButton("Cancel",
249
                                new DialogInterface.OnClickListener() {
250
                        public void onClick(DialogInterface dialog, int whichButton) {
251
                                dialog.cancel();
252
                        }
253
                });
254
                alert.show();
255
                                
256
        }
257
        
258
        protected void refresh(){
259
                try {
260
                        new ContainerManager(getApplicationContext()).createList(true);
261
                } catch (CloudServersException e) {
262
                        // TODO Auto-generated catch block
263
                        e.printStackTrace();
264
                }
265
                ((GroupsListView) findViewById(R.id.groupsListView)).getGroupsAdapter().notifyDataSetChanged();
266
        }
267

    
268
        protected Container getContainer() {
269
                return (Container) this.getIntent().getExtras().get("container");
270
        }
271

    
272
        @Override
273
        protected void onSaveInstanceState(Bundle outState) {
274
                super.onSaveInstanceState(outState);
275

    
276
        }
277

    
278
        public void startTracker() {
279
                if (!"google_sdk".equals(Build.PRODUCT) && !"sdk".equals(Build.PRODUCT)) {
280
                        tracker = GoogleAnalyticsTracker.getInstance();
281
                        tracker.start(Config.WEB_PROPERTY_ID, 20, this);
282
                }
283
        }
284

    
285
        public void trackPageView(String page) {
286
                if (tracker != null) {
287
                        tracker.trackPageView(page);
288
                }
289
        }
290

    
291
        @Override
292
        protected void onDestroy() {
293
                super.onDestroy();
294
                if (tracker != null) {
295
                        tracker.stop();
296
                }
297
        }
298

    
299
        public void trackEvent(String category, String action, String label,
300
                        int value) {
301
                if (tracker != null) {
302
                        tracker.trackEvent(category, action, label, value);
303
                }
304
        }
305

    
306
}