Browse Source

cleanup account manager UI code, theme add-action

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 5 years ago
parent
commit
ccd7fd85d7

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

@@ -87,15 +87,15 @@ public class ManageAccountsActivity extends FileActivity
     private static final int SINGLE_ACCOUNT = 1;
     private static final int MIN_MULTI_ACCOUNT_SIZE = 2;
 
-    private RecyclerView mRecyclerView;
-    private final Handler mHandler = new Handler();
-    private String mAccountName;
-    private AccountListAdapter mAccountListAdapter;
-    private ServiceConnection mDownloadServiceConnection;
-    private ServiceConnection mUploadServiceConnection;
-    private Set<String> mOriginalAccounts;
-    private String mOriginalCurrentAccount;
-    private Drawable mTintedCheck;
+    private RecyclerView recyclerView;
+    private final Handler handler = new Handler();
+    private String accountName;
+    private AccountListAdapter accountListAdapter;
+    private ServiceConnection downloadServiceConnection;
+    private ServiceConnection uploadServiceConnection;
+    private Set<String> originalAccounts;
+    private String originalCurrentAccount;
+    private Drawable tintedCheck;
 
     private ArbitraryDataProvider arbitraryDataProvider;
 
@@ -105,24 +105,24 @@ public class ManageAccountsActivity extends FileActivity
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mTintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.account_circle_white));
+        tintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.account_circle_white));
         int tint = ThemeUtils.elementColor(this);
-        DrawableCompat.setTint(mTintedCheck, tint);
+        DrawableCompat.setTint(tintedCheck, tint);
 
         setContentView(R.layout.accounts_layout);
 
-        mRecyclerView = findViewById(R.id.account_list);
+        recyclerView = findViewById(R.id.account_list);
 
         setupToolbar();
         updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
 
         Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
-        mOriginalAccounts = DisplayUtils.toAccountNameSet(Arrays.asList(accountList));
+        originalAccounts = DisplayUtils.toAccountNameSet(Arrays.asList(accountList));
 
         Account currentAccount = getUserAccountManager().getCurrentAccount();
 
         if (currentAccount != null) {
-            mOriginalCurrentAccount = currentAccount.name;
+            originalCurrentAccount = currentAccount.name;
         }
 
         setAccount(currentAccount);
@@ -130,29 +130,23 @@ public class ManageAccountsActivity extends FileActivity
 
         arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
 
-        mAccountListAdapter = new AccountListAdapter(this, accountManager, getAccountListItems(), mTintedCheck);
+        accountListAdapter = new AccountListAdapter(this, accountManager, getAccountListItems(), tintedCheck);
 
-        mRecyclerView.setAdapter(mAccountListAdapter);
-        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
+        recyclerView.setAdapter(accountListAdapter);
+        recyclerView.setLayoutManager(new LinearLayoutManager(this));
         initializeComponentGetters();
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
-        switch (resultCode) {
-            case KEY_DELETE_CODE:
-                if (data != null) {
-                    Bundle bundle = data.getExtras();
-                    if (bundle.containsKey(UserInfoActivity.KEY_ACCOUNT)) {
-                        Account account = Parcels.unwrap(bundle.getParcelable(UserInfoActivity.KEY_ACCOUNT));
-                        mAccountName = account.name;
-                        performAccountRemoval(account);
-                    }
-                }
-                break;
-            default:
-                break;
+        if (resultCode == KEY_DELETE_CODE && data != null) {
+            Bundle bundle = data.getExtras();
+            if (bundle != null && bundle.containsKey(UserInfoActivity.KEY_ACCOUNT)) {
+                Account account = Parcels.unwrap(bundle.getParcelable(UserInfoActivity.KEY_ACCOUNT));
+                accountName = account.name;
+                performAccountRemoval(account);
+            }
         }
     }
 
@@ -184,7 +178,7 @@ public class ManageAccountsActivity extends FileActivity
         }
 
         Set<String> actualAccounts = DisplayUtils.toAccountNameSet(newList);
-        return !mOriginalAccounts.equals(actualAccounts);
+        return !originalAccounts.equals(actualAccounts);
     }
 
     /**
@@ -197,7 +191,7 @@ public class ManageAccountsActivity extends FileActivity
         if (account == null) {
             return true;
         } else {
-            return !account.name.equals(mOriginalCurrentAccount);
+            return !account.name.equals(originalCurrentAccount);
         }
     }
 
@@ -205,15 +199,15 @@ public class ManageAccountsActivity extends FileActivity
      * Initialize ComponentsGetters.
      */
     private void initializeComponentGetters() {
-        mDownloadServiceConnection = newTransferenceServiceConnection();
-        if (mDownloadServiceConnection != null) {
-            bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
-                    Context.BIND_AUTO_CREATE);
+        downloadServiceConnection = newTransferenceServiceConnection();
+        if (downloadServiceConnection != null) {
+            bindService(new Intent(this, FileDownloader.class), downloadServiceConnection,
+                        Context.BIND_AUTO_CREATE);
         }
-        mUploadServiceConnection = newTransferenceServiceConnection();
-        if (mUploadServiceConnection != null) {
-            bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
-                    Context.BIND_AUTO_CREATE);
+        uploadServiceConnection = newTransferenceServiceConnection();
+        if (uploadServiceConnection != null) {
+            bindService(new Intent(this, FileUploader.class), uploadServiceConnection,
+                        Context.BIND_AUTO_CREATE);
         }
     }
 
@@ -273,36 +267,36 @@ public class ManageAccountsActivity extends FileActivity
                                   Bundle result = future.getResult();
                                   String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
                                   accountManager.setCurrentOwnCloudAccount(name);
-                                  mAccountListAdapter = new AccountListAdapter(
+                                  accountListAdapter = new AccountListAdapter(
                                           this,
                                           accountManager,
                                           getAccountListItems(),
-                                          mTintedCheck
+                                          tintedCheck
                                   );
-                                  mRecyclerView.setAdapter(mAccountListAdapter);
-                                  runOnUiThread(() -> mAccountListAdapter.notifyDataSetChanged());
+                                  recyclerView.setAdapter(accountListAdapter);
+                                  runOnUiThread(() -> accountListAdapter.notifyDataSetChanged());
                               } catch (OperationCanceledException e) {
                                   Log_OC.d(TAG, "Account creation canceled");
                               } catch (Exception e) {
                                   Log_OC.e(TAG, "Account creation finished in exception: ", e);
                               }
                           }
-                      }, mHandler);
+                      }, handler);
     }
 
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onAccountRemovedEvent(AccountRemovedEvent event) {
         List<AccountListItem> accountListItemArray = getAccountListItems();
-        mAccountListAdapter.clear();
-        mAccountListAdapter.addAll(accountListItemArray);
-        mAccountListAdapter.notifyDataSetChanged();
+        accountListAdapter.clear();
+        accountListAdapter.addAll(accountListItemArray);
+        accountListAdapter.notifyDataSetChanged();
     }
 
     @Override
     public void run(AccountManagerFuture<Boolean> future) {
         if (future.isDone()) {
             // after remove account
-            Account account = new Account(mAccountName, MainApp.getAccountType(this));
+            Account account = new Account(accountName, MainApp.getAccountType(this));
             if (!accountManager.exists(account)) {
                 // Cancel transfers of the removed account
                 if (mUploaderBinder != null) {
@@ -324,8 +318,8 @@ public class ManageAccountsActivity extends FileActivity
 
             List<AccountListItem> accountListItemArray = getAccountListItems();
             if (accountListItemArray.size() > SINGLE_ACCOUNT) {
-                mAccountListAdapter = new AccountListAdapter(this, accountManager, accountListItemArray, mTintedCheck);
-                mRecyclerView.setAdapter(mAccountListAdapter);
+                accountListAdapter = new AccountListAdapter(this, accountManager, accountListItemArray, tintedCheck);
+                recyclerView.setAdapter(accountListAdapter);
             } else {
                 onBackPressed();
             }
@@ -334,19 +328,19 @@ public class ManageAccountsActivity extends FileActivity
 
     @Override
     protected void onDestroy() {
-        if (mDownloadServiceConnection != null) {
-            unbindService(mDownloadServiceConnection);
-            mDownloadServiceConnection = null;
+        if (downloadServiceConnection != null) {
+            unbindService(downloadServiceConnection);
+            downloadServiceConnection = null;
         }
-        if (mUploadServiceConnection != null) {
-            unbindService(mUploadServiceConnection);
-            mUploadServiceConnection = null;
+        if (uploadServiceConnection != null) {
+            unbindService(uploadServiceConnection);
+            uploadServiceConnection = null;
         }
 
         super.onDestroy();
     }
 
-    public Handler getHandler() { return mHandler; }
+    public Handler getHandler() { return handler; }
 
     @Override
     public FileUploader.FileUploaderBinder getFileUploaderBinder() {
@@ -374,15 +368,15 @@ public class ManageAccountsActivity extends FileActivity
 
     private void performAccountRemoval(Account account) {
         // disable account in recycler view
-        for (int i = 0; i < mAccountListAdapter.getItemCount(); i++) {
-            AccountListItem item = mAccountListAdapter.getItem(i);
+        for (int i = 0; i < accountListAdapter.getItemCount(); i++) {
+            AccountListItem item = accountListAdapter.getItem(i);
 
             if (item != null && item.getAccount().equals(account)) {
                 item.setEnabled(false);
                 break;
             }
 
-            mAccountListAdapter.notifyDataSetChanged();
+            accountListAdapter.notifyDataSetChanged();
         }
 
         // store pending account removal

+ 33 - 29
src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java

@@ -1,23 +1,26 @@
 /*
- * ownCloud Android client application
+ *   Nextcloud Android client application
  *
- * @author Andy Scherzinger
- * @author Chris Narkiewicz
- * @author Nick Antoniou
- * Copyright (C) 2016 ownCloud Inc.
- * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
- * <p/>
- * 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.
- * <p/>
- * 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.
- * <p/>
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *   @author Andy Scherzinger
+ *   @author Chris Narkiewicz
+ *   @author Nick Antoniou
+ *   Copyright (C) 2016 Andy Scherzinger
+ *   Copyright (C) 2016 ownCloud Inc.
+ *   Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
+ *   Copyright (C) 2019 Nick Antoniou
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ *   License as published by the Free Software Foundation; either
+ *   version 3 of the License, or any later version.
+ *
+ *   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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public
+ *   License along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.owncloud.android.ui.adapter;
@@ -40,6 +43,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.BaseActivity;
 import com.owncloud.android.ui.activity.UserInfoActivity;
 import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.utils.ThemeUtils;
 
 import org.parceler.Parcels;
 
@@ -55,12 +59,12 @@ import androidx.recyclerview.widget.RecyclerView;
 public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                 implements DisplayUtils.AvatarGenerationListener {
     private static final String TAG = AccountListAdapter.class.getSimpleName();
+
     private float accountAvatarRadiusDimension;
     private final BaseActivity context;
     private List<AccountListItem> values;
     private AccountListAdapterListener accountListAdapterListener;
     private Drawable tintedCheck;
-    private RecyclerView mRecyclerView;
     private UserAccountManager accountManager;
 
     private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
@@ -77,15 +81,9 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
         this.tintedCheck = tintedCheck;
     }
 
-    @Override
-    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
-        super.onAttachedToRecyclerView(recyclerView);
-        mRecyclerView = recyclerView;
-    }
-
     @Override
     public int getItemViewType(int position) {
-        if (position == values.size()-1) {
+        if (position == values.size() - 1) {
             return AccountListItem.TYPE_ACTION_ADD;
         }
         return AccountListItem.TYPE_ACCOUNT;
@@ -148,7 +146,7 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
 
             } // create add account action item
             else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && accountListAdapterListener != null) {
-                setupAddAccountListItem(holder.itemView);
+                setupAddAccountListItem((AddAccountViewHolderItem)holder);
             }
         }
     }
@@ -156,9 +154,13 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
     /**
      * Sets up a View to be used for adding a new account
      *
-     * @param actionView the action view
+     * @param holder the add account view holder
      */
-    private void setupAddAccountListItem(View actionView) {
+    private void setupAddAccountListItem(AddAccountViewHolderItem holder) {
+        View actionView = holder.itemView;
+
+        holder.usernameViewItem.setTextColor(ThemeUtils.primaryColor(context, true));
+
         // bind action listener
         boolean isProviderOrOwnInstallationVisible = context.getResources()
                 .getBoolean(R.bool.show_provider_or_own_installation);
@@ -318,8 +320,10 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
      * Account ViewHolderItem to get smooth scrolling.
      */
     static class AddAccountViewHolderItem extends RecyclerView.ViewHolder {
+        private TextView usernameViewItem;
         AddAccountViewHolderItem(@NonNull View view) {
             super(view);
+            this.usernameViewItem = view.findViewById(R.id.user_name);
         }
     }
 }

+ 12 - 10
src/main/java/com/owncloud/android/ui/adapter/AccountListItem.java

@@ -1,20 +1,22 @@
-/**
- *   ownCloud Android client application
+/*
+ *   Nextcloud Android client application
  *
  *   @author Andy Scherzinger
+ *   Copyright (C) 2016 Andy Scherzinger
  *   Copyright (C) 2016 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 free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ *   License as published by the Free Software Foundation; either
+ *   version 3 of the License, or any later version.
  *
  *   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.
+ *   GNU AFFERO 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/>.
+ *   You should have received a copy of the GNU Affero General Public
+ *   License along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.owncloud.android.ui.adapter;
@@ -25,8 +27,8 @@ import android.accounts.Account;
  * Container implementation to add {@link Account}s and the add action to the list.
  */
 public class AccountListItem {
-    public static final int TYPE_ACCOUNT = 0;
-    public static final int TYPE_ACTION_ADD = 1;
+    static final int TYPE_ACCOUNT = 0;
+    static final int TYPE_ACTION_ADD = 1;
 
     private Account mAccount;
     private int mType;

+ 1 - 1
src/test/java/com/owncloud/android/ui/activities/data/activities/RemoteActivitiesRepositoryTest.java

@@ -1,4 +1,4 @@
-/**
+/*
  *   Nextcloud Android client application
  *
  *   Copyright (C) 2018 Edvard Holst

+ 21 - 1
src/test/java/com/owncloud/android/ui/adapter/AccountListAdapterTest.java

@@ -1,3 +1,23 @@
+/*
+ *   Nextcloud Android client application
+ *
+ *   @author Nick Antoniou
+ *   Copyright (C) 2019 Nick Antoniou
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ *   License as published by the Free Software Foundation; either
+ *   version 3 of the License, or any later version.
+ *
+ *   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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public
+ *   License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 package com.owncloud.android.ui.adapter;
 
 import com.owncloud.android.R;
@@ -10,7 +30,7 @@ import org.mockito.Mockito;
 import java.util.ArrayList;
 import java.util.List;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;