Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / android / activity / setup / AccountSetupCheckSettings.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.android.activity.setup;
23
24
25 import org.mustard2.android.Account;
26 import org.mustard2.android.AuthException;
27 import org.mustard2.android.MustardApp;
28 import org.mustard2.android.MustardException;
29 import org.mustard2.android.Preferences;
30 import org.mustard2.android.R;
31 import org.mustard2.manager.ProviderManager;
32 import org.mustard2.microblog.User;
33 import org.mustard2.provider.MicroBlogProvider;
34 import org.mustard2.provider.Provider;
35
36 import android.app.Activity;
37 import android.app.AlertDialog;
38 import android.content.DialogInterface;
39 import android.content.Intent;
40 import android.net.Uri;
41 import android.os.Bundle;
42 import android.os.Handler;
43 import android.os.Process;
44 import android.util.Log;
45 import android.view.View;
46 import android.view.View.OnClickListener;
47 import android.widget.Button;
48 import android.widget.ProgressBar;
49 import android.widget.TextView;
50
51 public class AccountSetupCheckSettings extends Activity implements OnClickListener {
52         
53         private static final String TAG = "mustard/AccountCheck";
54         
55     private static final String EXTRA_ACCOUNT = "account";
56     private static final String EXTRA_PROVIDER = "provider";
57     
58     private Handler mHandler = new Handler();
59
60     private ProgressBar mProgressBar;
61
62     private TextView mMessageView;
63
64     private Account mAccount;
65     
66     private Provider mProvider;
67
68     private boolean mCanceled;
69
70     private boolean mDestroyed;
71
72     public static void actionCheckSettings(Activity fromActivity, Account account, Provider provider) {
73         Log.v(TAG, "actionCheckSettings");
74         Intent i = new Intent(fromActivity, AccountSetupCheckSettings.class);
75         i.putExtra(EXTRA_ACCOUNT, account);
76         i.putExtra(EXTRA_PROVIDER, provider);
77         fromActivity.startActivityForResult(i, 1);
78     }
79
80     public static void actionCheckSettings(Activity fromActivity, Account account) {
81         Log.v(TAG, "actionCheckSettings");
82         Intent i = new Intent(fromActivity, AccountSetupCheckSettings.class);
83         i.putExtra(EXTRA_ACCOUNT, account);
84         fromActivity.startActivityForResult(i, 1);
85     }
86
87     @Override
88     public void onCreate(Bundle savedInstanceState) {
89         super.onCreate(savedInstanceState);
90         Log.v(TAG, " Start checking ");
91         setContentView(R.layout.account_setup_check_settings);
92         mMessageView = (TextView)findViewById(R.id.message);
93         mProgressBar = (ProgressBar)findViewById(R.id.progress);
94         ((Button)findViewById(R.id.cancel)).setOnClickListener(this);
95
96         setMessage(R.string.account_setup_check_settings_retr_info_msg);
97         mProgressBar.setIndeterminate(true);
98         
99         mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT);
100        
101 //        mProvider = (Provider)getIntent().getSerializableExtra(EXTRA_PROVIDER);
102         mProvider = mAccount.getProvider();
103         
104         new Thread() {
105             @Override
106                         public void run() {
107                 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
108                 try {
109                     if (mDestroyed) {
110                         return;
111                     }
112                     if (mCanceled) {
113                         finish();
114                         return;
115                     }
116                     
117                     setMessage(R.string.account_setup_check_settings_check_server_msg);
118 //                    Provider provider =  Providers.getProvider(getApplication(), mProvider.url );
119                     mProvider =  ProviderManager.getProvider(getApplication(), mProvider.url );
120                     
121                     mAccount.setProvider(mProvider);
122                     Log.v(TAG, " This is a " + mProvider.apiType + " URL ");
123                     setMessage(R.string.account_setup_check_settings_check_user_msg);
124 //                    mAccount.setApiType(mProvider.apiType);
125                     MicroBlogProvider mbprovider = 
126                         mAccount.getProvider(getApplication());
127                     
128
129
130                     if (mAccount.getNickname() == null) {
131                         // OAuth 
132                         Preferences preferences = ((MustardApp)getApplication()).getPreferences();
133                         Uri uri = mbprovider.checkUserOAuthSettings(getApplicationContext(), mProvider, mAccount, preferences);
134                         startActivity(new Intent(Intent.ACTION_VIEW, uri));
135
136                     } else {
137
138                         if (mbprovider.isUsernamePasswordSupported()) {
139                                 // Username/Password
140                                 
141                                 Log.d(TAG, ">> Checking");
142                                 mbprovider.checkUserPasswordSettings(getApplicationContext(), mProvider, mAccount);
143                                 Log.d(TAG, "Checking << ");
144                                 Log.d(TAG, ">> Version");
145                                 mAccount.setVersion(mbprovider.getVersion());
146                                 Log.d(TAG, "Version <<");
147                                 Log.d(TAG, ">> TextLimit");
148                                 mAccount.setTextLimit(mbprovider.getTextLimit());
149                                 Log.d(TAG, "TextLimit <<");
150                                 
151                                 Log.d(TAG, ">> User");
152                                 User user = mbprovider.getUser();
153                                 Log.d(TAG, "User << ");
154                                 mAccount.setAvatarUrl(user.getProfile_image_url());
155                                 long accountId = ((MustardApp)getApplication()).getAccountManager().createAccount(mAccount);
156                                 if (accountId > 0) {
157                                         mAccount.setId(accountId);
158                                 } else {
159                                         throw new MustardException("Can't create account");
160                                 }
161                         } else {
162                                 showErrorDialog(R.string.account_setup_failed_dlg_authtype_message, "");
163                                 mDestroyed=true;
164                         }
165                     }
166
167                 
168                     if (mDestroyed) {
169                         return;
170                     }
171                     if (mCanceled) {
172                         finish();
173                         return;
174                     }
175                     if (mDestroyed) {
176                         return;
177                     }
178                     if (mCanceled) {
179                         finish();
180                         return;
181                     }
182                     Intent data = new Intent();
183                     data.putExtra(EXTRA_ACCOUNT, mAccount);
184                     setResult(RESULT_OK, data);
185                     finish();
186                 } catch (final AuthException afe) {
187                     String message = afe.getMessage();
188                     int id = (message == null) 
189                             ? R.string.account_setup_failed_dlg_auth_message
190                             : R.string.account_setup_failed_dlg_auth_message_fmt;
191                     showErrorDialog(id, message);
192                 } catch (final MustardException me) {
193                     int id = R.string.account_setup_failed_dlg_generic_message_fmt;
194                     String message = me.getMessage();
195                     showErrorDialog(id, message);
196                 } catch (final Exception e) {
197                         int id = R.string.account_setup_failed_dlg_generic_message_fmt;
198                     String message = e.getMessage();
199                     showErrorDialog(id, message);
200                 }
201             }
202         }.start();
203     }
204
205     @Override
206     public void onDestroy() {
207         super.onDestroy();
208         mDestroyed = true;
209         mCanceled = true;
210     }
211
212     private void setMessage(final int resId) {
213         mHandler.post(new Runnable() {
214             public void run() {
215                 if (mDestroyed) {
216                     return;
217                 }
218                 mMessageView.setText(getString(resId));
219             }
220         });
221     }
222
223     private void showErrorDialog(final int msgResId, final Object... args) {
224         mHandler.post(new Runnable() {
225             public void run() {
226                 if (mDestroyed) {
227                     return;
228                 }
229                 mProgressBar.setIndeterminate(false);
230                 new AlertDialog.Builder(AccountSetupCheckSettings.this)
231                         .setIcon(android.R.drawable.ic_dialog_alert)
232                         .setTitle(getString(R.string.account_setup_failed_dlg_title))
233                         .setMessage(getString(msgResId, args))
234                         .setCancelable(true)
235                         .setPositiveButton(
236                                 getString(R.string.account_setup_failed_dlg_edit_details_action),
237                                 new DialogInterface.OnClickListener() {
238                                     public void onClick(DialogInterface dialog, int which) {
239                                         finish();
240                                     }
241                                 })
242                         .show();
243             }
244         });
245     }
246
247     private void onCancel() {
248         mCanceled = true;
249         setMessage(R.string.account_setup_check_settings_canceling_msg);
250     }
251
252     public void onClick(View v) {
253         switch (v.getId()) {
254             case R.id.cancel:
255                 onCancel();
256                 break;
257         }
258     }
259 }