Merge pull request #186 from owncloud/fixed_contradicted_messages_in_login_view
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / AccountSelectActivity.java
index 78ec8c0..be369dc 100644 (file)
@@ -1,3 +1,21 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012 Bartek Przybylski
+ *   Copyright (C) 2012-2013 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.ui.activity;
 
 import java.util.HashMap;
@@ -14,14 +32,11 @@ import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Handler;
-import android.util.Log;
 import android.view.ContextMenu;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ContextMenu.ContextMenuInfo;
-import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
-import android.widget.AdapterView.OnItemLongClickListener;
 import android.widget.CheckedTextView;
 import android.widget.ListView;
 import android.widget.SimpleAdapter;
@@ -33,19 +48,31 @@ import com.actionbarsherlock.view.Menu;
 import com.actionbarsherlock.view.MenuInflater;
 import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.AccountUtils;
-import com.owncloud.android.authenticator.AccountAuthenticator;
+import com.owncloud.android.authentication.AccountAuthenticator;
+import com.owncloud.android.Log_OC;
 
 import com.owncloud.android.R;
 
 public class AccountSelectActivity extends SherlockListActivity implements
         AccountManagerCallback<Boolean> {
 
+    private static final String  TAG = "AccountSelectActivity";
+    
+    private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
+    
     private final Handler mHandler = new Handler();
+    private Account mPreviousAccount = null;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        if (savedInstanceState != null) {
+            mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
+        } else {
+            mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
+        }
+        
         ActionBar action_bar = getSupportActionBar();
         action_bar.setDisplayShowTitleEnabled(true);
         action_bar.setDisplayHomeAsUpEnabled(false);
@@ -56,6 +83,29 @@ public class AccountSelectActivity extends SherlockListActivity implements
         super.onResume();
         populateAccountList();
     }
+    
+    @Override
+    protected void onPause() {
+        super.onPause();
+        if (this.isFinishing()) {
+            Account current = AccountUtils.getCurrentOwnCloudAccount(this);
+            if ((mPreviousAccount == null && current != null) || 
+                (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
+                /// the account set as default changed since this activity was created 
+            
+                // trigger synchronization
+                ContentResolver.cancelSync(null, AccountAuthenticator.AUTHORITY);
+                Bundle bundle = new Bundle();
+                bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
+                ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), AccountAuthenticator.AUTHORITY, bundle);
+                
+                // restart the main activity
+                Intent i = new Intent(this, FileDisplayActivity.class);
+                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                startActivity(i);
+            }
+        }
+    }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
@@ -76,17 +126,7 @@ public class AccountSelectActivity extends SherlockListActivity implements
         String accountName = ((TextView) v.findViewById(android.R.id.text1))
                 .getText().toString();
         AccountUtils.setCurrentOwnCloudAccount(this, accountName);
-
-        // trigger synchronization when current account is changed
-        ContentResolver.cancelSync(null, "org.owncloud");
-        Bundle bundle = new Bundle();
-        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
-        ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), "org.owncloud", bundle);
-        
-        Intent i = new Intent(this, FileDisplayActivity.class);
-        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-        startActivity(i);
-        finish();
+        finish();   // immediate exit
     }
 
     @Override
@@ -95,20 +135,27 @@ public class AccountSelectActivity extends SherlockListActivity implements
             Intent intent = new Intent(
                     android.provider.Settings.ACTION_ADD_ACCOUNT);
             intent.putExtra("authorities",
-                    new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
+                    new String[] { AccountAuthenticator.AUTHORITY });
             startActivity(intent);
             return true;
         }
         return false;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public boolean onContextItemSelected(android.view.MenuItem item) {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                 .getMenuInfo();
         int index = info.position;
-        HashMap<String, String> map = (HashMap<String, String>) getListAdapter()
-                .getItem(index);
+        HashMap<String, String> map = null;
+        try {
+            map = (HashMap<String, String>) getListAdapter().getItem(index);
+        } catch (ClassCastException e) {
+            Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
+            return false;
+        }
+        
         String accountName = map.get("NAME");
         AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
         Account accounts[] = am
@@ -119,7 +166,7 @@ public class AccountSelectActivity extends SherlockListActivity implements
             }
         }
 
-        return false;
+        return true;
     }
 
     private void populateAccountList() {