Parcourir la source

trying to detect account additions and change the list view content (not working yet)

Andy Scherzinger il y a 9 ans
Parent
commit
7d30667b50

+ 2 - 1
src/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -27,6 +27,7 @@ import android.accounts.OperationCanceledException;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Bundle;
+import android.os.Handler;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
@@ -509,7 +510,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
                 null,
                 this,
                 new AccountCreationCallback(),
-                null);
+                new Handler());
     }
 
     /**

+ 61 - 2
src/com/owncloud/android/ui/activity/ManageAccountsActivity.java

@@ -23,6 +23,7 @@ import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerCallback;
 import android.accounts.AccountManagerFuture;
+import android.accounts.OperationCanceledException;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -154,9 +155,37 @@ public class ManageAccountsActivity extends ToolbarActivity
 
     @Override
     public void createAccount() {
-        // TODO Show create account screen if there isn't any account, probably via the drawer implementation
         AccountManager am = AccountManager.get(getApplicationContext());
-        am.addAccount(MainApp.getAccountType(), null, null, null, ManageAccountsActivity.this, null, null);
+        am.addAccount(MainApp.getAccountType(),
+                null,
+                null,
+                null,
+                ManageAccountsActivity.this,
+                new AccountCreationCallback() {
+                    @Override
+                    public void run(AccountManagerFuture<Bundle> future) {
+                        if (future != null) {
+                            try {
+                                Bundle result = future.getResult();
+                                String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
+                                AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
+                                ArrayList<AccountListItem> accounts = getAccountListItems();
+                                mAccountListAdapter.clear();
+                                mAccountListAdapter.addAll(accounts);
+                                mAccountListAdapter.notifyDataSetChanged();
+                                mListView.invalidate();
+                            } catch (OperationCanceledException e) {
+                                Log_OC.d(TAG, "Account creation canceled");
+                            } catch (Exception e) {
+                                Log_OC.e(TAG, "Account creation finished in exception: ", e);
+                            }
+
+                        } else {
+                            Log_OC.e(TAG, "Account creation callback with null bundle");
+                        }
+                    }
+                },
+                mHandler);
     }
 
     @Override
@@ -189,6 +218,36 @@ public class ManageAccountsActivity extends ToolbarActivity
         }
     }
 
+    /**
+     * Helper class handling a callback from the {@link AccountManager} after the creation of
+     * a new ownCloud {@link Account} finished, successfully or not.
+     * <p/>
+     * At this moment, only called after the creation of the first account.
+     */
+    public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
+        @Override
+        public void run(AccountManagerFuture<Bundle> future) {
+            if (future != null) {
+                try {
+                    Bundle result = future.getResult();
+                    String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
+                    AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
+                    ArrayList<AccountListItem> accounts = getAccountListItems();
+                    ManageAccountsActivity.this.mAccountListAdapter = new AccountListAdapter(ManageAccountsActivity
+                            .this, accounts);
+                    ManageAccountsActivity.this.mAccountListAdapter.notifyDataSetChanged();
+                } catch (OperationCanceledException e) {
+                    Log_OC.d(TAG, "Account creation canceled");
+                } catch (Exception e) {
+                    Log_OC.e(TAG, "Account creation finished in exception: ", e);
+                }
+
+            } else {
+                Log_OC.e(TAG, "Account creation callback with null bundle");
+            }
+        }
+    }
+
     @Override
     protected void onDestroy() {
         if (mDownloadServiceConnection != null) {

+ 5 - 1
src/com/owncloud/android/ui/adapter/AccountListAdapter.java

@@ -46,7 +46,7 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> {
     private static final String TAG = AccountListAdapter.class.getSimpleName();
 
     private final Context mContext;
-    private final List<AccountListItem> mValues;
+    private List<AccountListItem> mValues;
     private AccountListAdapterListener mListener;
 
     public AccountListAdapter(Context context, List<AccountListItem> values) {
@@ -56,6 +56,10 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> {
         this.mListener = (AccountListAdapterListener) context;
     }
 
+    public void setAccountList(List<AccountListItem> values) {
+        this.mValues = values;
+    }
+
     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
         AccountViewHolderItem viewHolder;