Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / android / activity / setup / OAuthSetup.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 package org.mustard2.android.activity.setup;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25
26 import org.mustard2.android.MessagingController;
27 import org.mustard2.android.MessagingListener;
28 import org.mustard2.android.MustardApp;
29 import org.mustard2.android.R;
30 import org.mustard2.manager.DbManager;
31
32 import android.app.Activity;
33 import android.app.AlertDialog;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.os.Bundle;
37 import android.os.Handler;
38 import android.os.Message;
39 import android.util.Log;
40 import android.view.View;
41 import android.widget.Button;
42 import android.widget.EditText;
43 import android.widget.Toast;
44
45 public class OAuthSetup extends Activity {
46
47         private String TAG = getClass().getCanonicalName();
48         
49         private Button mRefreshButton;
50         private Button mNewButton;
51         private EditText mOauthUrlEdit;
52         private DbManager mDbHelper;
53
54         
55         private OAuthKeysHandler mHandler = new OAuthKeysHandler();
56
57     public static void actionOAuthSetup(Activity fromActivity) {
58         Intent i = new Intent(fromActivity, OAuthSetup.class);
59         fromActivity.startActivity(i);
60     }
61
62         
63         class OAuthKeysHandler extends Handler {
64                 
65                 
66                 private static final int MSG_PROGRESS = 2;
67                 private static final int MSG_RESULT_OK = 3;
68                 private static final int MSG_RESULT_KO = 4;
69                 
70                 @Override
71                 public void handleMessage(Message msg) {
72                         
73                         switch (msg.what) {
74                         case MSG_PROGRESS:
75                                 setProgressBarIndeterminateVisibility(msg.arg1 != 0);
76                                 break;
77                         case MSG_RESULT_OK:
78                                 showSuccessMessage();
79                                 break;
80                                 
81                         case MSG_RESULT_KO:
82                                 showErrorMessage((String)msg.obj);
83                                 break;
84                                 
85                         default:
86                                 break;
87                         }
88                 }
89
90                 public void progress(boolean progress) {
91                         Message msg = new Message();
92                         msg.what = MSG_PROGRESS;
93                         msg.arg1 = progress ? 1 : 0;
94                         sendMessage(msg);
95                 }
96                 
97                 public void result(boolean ok, String reason) {
98                         if(ok) {
99                                 sendEmptyMessage(MSG_RESULT_OK);
100                         } else {
101                                 Message msg = new Message();
102                                 msg.what = MSG_RESULT_KO;
103                                 msg.obj = reason;
104                                 sendMessage(msg);
105                         }
106                 }
107                 
108         }
109         
110         private MessagingListener mListener = new MessagingListener() {
111                 
112             @Override
113                 public void loadOAuthKeysStarted(Context context) {
114                 mHandler.progress(true);
115             }
116             
117             @Override
118                 public void loadOAuthKeysFinished(Context context) {
119                 mHandler.progress(false);
120                 mHandler.result(true,null);
121             }
122
123             @Override
124                 public void loadOAuthKeysFailed(Context context, String reason) {
125                 mHandler.progress(false);
126                 mHandler.result(false,reason);
127             }
128             
129         };
130         
131         private void showSuccessMessage() {
132                 Toast.makeText(this, R.string.oauth_keys_fetched_ok, Toast.LENGTH_SHORT).show();
133         }
134         
135         private void showErrorMessage(String reason) {
136                 Toast.makeText(this, getString(R.string.oauth_keys_fetched_ko,reason), Toast.LENGTH_LONG).show();
137         }
138
139         
140         
141         @Override
142         public void onCreate(Bundle savedInstanceState) {
143                 super.onCreate(savedInstanceState);
144                 setContentView(R.layout.oauth_setup);
145                 mDbHelper = new DbManager(this);
146                 mDbHelper.open();
147                 doPrepareButtons();
148         }
149         
150         private void doPrepareButtons() {
151                 
152                 mRefreshButton = (Button) findViewById(R.id.btn_oauth_refresh_keys);
153                 mRefreshButton.setOnClickListener(new View.OnClickListener() {
154                         public void onClick(View v) {
155                                 doFetchNewKeys(false);
156                         }
157                 });
158                 
159                 mNewButton = (Button) findViewById(R.id.btn_oauth_new_keys);
160                 mNewButton.setOnClickListener(new View.OnClickListener() {
161                         public void onClick(View v) {
162                                 doFetchNewKeys(true);
163                         }
164                 });
165                 
166                 mOauthUrlEdit = (EditText) findViewById(R.id.oauth_new_keys);
167                 
168         }
169         
170         private void doFetchNewKeys(boolean external) {
171                 
172                 URL url = null;
173                 if(external) {
174                         String surl = mOauthUrlEdit.getText().toString();
175                         if (surl.equals("")) 
176                                 return;
177                         try {
178                                 url = new URL(surl);
179                         } catch(MalformedURLException e) {
180                                 new AlertDialog.Builder(OAuthSetup.this)
181                                 .setTitle(getString(R.string.error))
182                                 .setMessage(getString(R.string.malformed_URL))
183                                 .setNeutralButton(getString(R.string.close), null).show();
184                                 return;
185                         }
186                 }
187                 MessagingController.getInstance(this).fetchOKeys(this, url, mListener);
188         }
189         
190         @Override
191         public void onDestroy() {
192                 super.onDestroy();
193                 if(MustardApp.DEBUG) Log.i(TAG, "onDestroy");
194                 if(mDbHelper != null) {
195                         try {
196                                 mDbHelper.close();
197                         } catch (Exception e) {
198                                 if (MustardApp.DEBUG) e.printStackTrace();
199                         }
200                 }
201         }
202 }