Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / manager / OAuthManager.java
1 /**
2  * MUSTARD: Android's Client for StatusNet
3  * 
4  * Copyright (C) 2009-2010 macno.org, Michele Azzolari
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  * 
20  */
21
22 package org.mustard2.manager;
23
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.ArrayList;
27
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.mustard2.android.MustardApp;
31 import org.mustard2.db.OAuthTable;
32 import org.mustard2.util.AndroidUtil;
33 import org.mustard2.util.HttpManager;
34
35 import android.content.Context;
36 import android.database.Cursor;
37 import android.util.Log;
38
39 public class OAuthManager {
40         
41         private static final String TAG = "mustard/OAuthManager";
42         
43         public static final int OK=0;
44         public static final int KO=2;
45         public static final int PARTIAL=3;
46         public static final int EMPTY=4;
47         
48         private DbManager mDbAdapter;
49         
50         public  OAuthManager (Context context) {
51                 mDbAdapter = new DbManager(context);
52                 mDbAdapter.open();
53         }
54
55         public void close() {
56                 mDbAdapter.close();
57         }
58         
59         /**
60          * Download keys and try to insert into DB
61          * @param olist
62          * @param replace
63          * @return
64          */
65         public int load(ArrayList<OAuthInstance> olist,boolean replace) {
66                 boolean ok=false;
67                 boolean ko=false;
68                 if (MustardApp.DEBUG) Log.d("OAuthManager","Got " + olist.size() + " keys");
69                 for (OAuthInstance o : olist) {
70                         boolean r = mDbAdapter.createOauth(o.instance, o.key, o.secret,replace,o.apiType);
71                         if (r)
72                                 ok=true;
73                         else
74                                 ko=true;
75                         if (MustardApp.DEBUG) Log.d("OAuthManager","Inserting " + o.instance + " (" +  o.key + "/" +  o.secret + ") = " +r);
76                 }
77                 if (ok && !ko)
78                         return OK;
79                 else if (ok && ko)
80                         return PARTIAL;
81                 else
82                         return KO;
83         }
84         
85         public OAuthInstance get(String instance) {
86                 Log.v(TAG, "Searching for " + instance);
87                 OAuthInstance o = null;
88                 
89                 Cursor c = mDbAdapter.fetchOauth(instance);
90                 if (c.moveToNext()) {
91                         o = new OAuthInstance();
92                         o.id=c.getLong(c.getColumnIndexOrThrow(OAuthTable.KEY_ROWID));
93                         o.instance=c.getString(c.getColumnIndexOrThrow(OAuthTable.KEY_INSTANCE));
94                         o.key=c.getString(c.getColumnIndexOrThrow(OAuthTable.KEY_KEY));
95                         o.secret=c.getString(c.getColumnIndexOrThrow(OAuthTable.KEY_SECRET));
96                         o.apiType=c.getString(c.getColumnIndexOrThrow(OAuthTable.KEY_APITYPE));
97                 } else {
98                         Log.v(TAG, "Not found");
99                 }
100                 try {
101                         if (c!=null) {
102                                 c.close();
103                         }
104                 } catch (Exception e) {
105                 }
106                 return o;
107         }
108         
109         public static class OAuthInstance {
110                 public long id;
111                 public String instance;
112                 public String key;
113                 public String secret;
114                 public String apiType;
115         }
116         
117         public int get(Context context, URL url) throws Exception {
118                 boolean reserved = false;
119                 String version = AndroidUtil.getVersionName(context);
120                 try {
121                         if (url == null) {
122                                 url = new URL("http://mustard.macno.org/keys2.json?"+version);
123                                 reserved = true;
124                         }
125                 } catch(MalformedURLException e) {
126                         throw e;
127                 }
128                 String host = url.getHost();
129                 HttpManager mHttpManager = new HttpManager(context, host);
130                 
131                 ArrayList<OAuthInstance> oauths = new ArrayList<OAuthInstance>();
132                 JSONObject o = mHttpManager.getJsonObject(url.toExternalForm());
133                 JSONArray keys = o.getJSONArray("keys");
134                 if (keys != null) {
135                         for (int i=0;i<keys.length();i++) {
136                                 JSONObject enclosure = keys.getJSONObject(i);
137                                 OAuthInstance oi= new OAuthInstance();
138                                 oi.instance = enclosure.getString("instance");
139                                 oi.key = enclosure.getString("key");
140                                 oi.secret = enclosure.getString("secret");
141                                 oi.apiType = enclosure.getString("apitype");
142                                 oauths.add(oi);
143                         }
144                 }
145         
146                 if (oauths.size()==0) {
147                         return EMPTY;
148                 }
149                 
150                 return load(oauths,reserved);
151         }
152 }