|
@@ -1,107 +1,140 @@
|
|
|
/*
|
|
|
- * ownCloud Android client application
|
|
|
+ * Nextcloud Android client application
|
|
|
*
|
|
|
- * @author Andy Scherzinger
|
|
|
- * @author Chris Narkiewicz
|
|
|
- * 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;
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
+import android.content.Intent;
|
|
|
import android.graphics.Paint;
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
-import android.widget.ArrayAdapter;
|
|
|
import android.widget.ImageView;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import com.nextcloud.client.account.UserAccountManager;
|
|
|
+import com.owncloud.android.MainApp;
|
|
|
import com.owncloud.android.R;
|
|
|
import com.owncloud.android.lib.common.OwnCloudAccount;
|
|
|
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;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
/**
|
|
|
- * This Adapter populates a ListView with all accounts within the app.
|
|
|
+ * This Adapter populates a RecyclerView with all accounts within the app.
|
|
|
*/
|
|
|
-public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements DisplayUtils.AvatarGenerationListener {
|
|
|
+public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
|
|
|
+ implements DisplayUtils.AvatarGenerationListener {
|
|
|
private static final String TAG = AccountListAdapter.class.getSimpleName();
|
|
|
- private float mAccountAvatarRadiusDimension;
|
|
|
- private final BaseActivity mContext;
|
|
|
- private List<AccountListItem> mValues;
|
|
|
- private AccountListAdapterListener mListener;
|
|
|
- private Drawable mTintedCheck;
|
|
|
+
|
|
|
+ private float accountAvatarRadiusDimension;
|
|
|
+ private final BaseActivity context;
|
|
|
+ private List<AccountListItem> values;
|
|
|
+ private AccountListAdapterListener accountListAdapterListener;
|
|
|
+ private Drawable tintedCheck;
|
|
|
private UserAccountManager accountManager;
|
|
|
|
|
|
+ private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
|
|
|
+ private static final int KEY_USER_INFO_REQUEST_CODE = 13;
|
|
|
+
|
|
|
public AccountListAdapter(BaseActivity context, UserAccountManager accountManager, List<AccountListItem> values, Drawable tintedCheck) {
|
|
|
- super(context, -1, values);
|
|
|
- this.mContext = context;
|
|
|
+ this.context = context;
|
|
|
this.accountManager = accountManager;
|
|
|
- this.mValues = values;
|
|
|
+ this.values = values;
|
|
|
if (context instanceof AccountListAdapterListener) {
|
|
|
- this.mListener = (AccountListAdapterListener) context;
|
|
|
+ this.accountListAdapterListener = (AccountListAdapterListener) context;
|
|
|
+ }
|
|
|
+ this.accountAvatarRadiusDimension = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
|
|
|
+ this.tintedCheck = tintedCheck;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemViewType(int position) {
|
|
|
+ if (position == values.size() - 1) {
|
|
|
+ return AccountListItem.TYPE_ACTION_ADD;
|
|
|
}
|
|
|
- this.mAccountAvatarRadiusDimension = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
|
|
|
- this.mTintedCheck = tintedCheck;
|
|
|
+ return AccountListItem.TYPE_ACCOUNT;
|
|
|
}
|
|
|
|
|
|
- @NonNull
|
|
|
@Override
|
|
|
- public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
|
|
|
- AccountViewHolderItem viewHolder;
|
|
|
- View view = convertView;
|
|
|
-
|
|
|
- if (view == null) {
|
|
|
- LayoutInflater inflater = mContext.getLayoutInflater();
|
|
|
- view = inflater.inflate(R.layout.account_item, parent, false);
|
|
|
-
|
|
|
- viewHolder = new AccountViewHolderItem();
|
|
|
- viewHolder.imageViewItem = view.findViewById(R.id.user_icon);
|
|
|
- viewHolder.checkViewItem = view.findViewById(R.id.ticker);
|
|
|
- viewHolder.checkViewItem.setImageDrawable(mTintedCheck);
|
|
|
- viewHolder.usernameViewItem = view.findViewById(R.id.user_name);
|
|
|
- viewHolder.accountViewItem = view.findViewById(R.id.account);
|
|
|
-
|
|
|
- view.setTag(viewHolder);
|
|
|
+ public @NonNull
|
|
|
+ RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
+ View view;
|
|
|
+ if (AccountListItem.TYPE_ACCOUNT == viewType) {
|
|
|
+ view = LayoutInflater.from(context).inflate(R.layout.account_item, parent, false);
|
|
|
+ AccountViewHolderItem viewHolder = new AccountViewHolderItem(view);
|
|
|
+ viewHolder.checkViewItem.setImageDrawable(tintedCheck);
|
|
|
+ return viewHolder;
|
|
|
} else {
|
|
|
- viewHolder = (AccountViewHolderItem) view.getTag();
|
|
|
+ view = LayoutInflater.from(context).inflate(R.layout.account_action, parent, false);
|
|
|
+ return new AddAccountViewHolderItem(view);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- AccountListItem accountListItem = mValues.get(position);
|
|
|
+ @Override
|
|
|
+ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
|
+ AccountListItem accountListItem = values.get(position);
|
|
|
|
|
|
if (accountListItem != null) {
|
|
|
// create account item
|
|
|
if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
|
|
|
Account account = accountListItem.getAccount();
|
|
|
- setAccount(viewHolder, account);
|
|
|
- setUsername(viewHolder, account);
|
|
|
- setAvatar(viewHolder, account);
|
|
|
- setCurrentlyActiveState(viewHolder, account);
|
|
|
-
|
|
|
- TextView usernameView = viewHolder.usernameViewItem;
|
|
|
- TextView accountView = viewHolder.accountViewItem;
|
|
|
+ AccountViewHolderItem item = (AccountViewHolderItem)holder;
|
|
|
+ setAccount(item, account);
|
|
|
+ setUsername(item, account);
|
|
|
+ setAvatar(item, account);
|
|
|
+ setCurrentlyActiveState(item, account);
|
|
|
+
|
|
|
+ TextView usernameView = item.usernameViewItem;
|
|
|
+ TextView accountView = item.accountViewItem;
|
|
|
+
|
|
|
+ // OnClickListener for when the user selects an account
|
|
|
+ holder.itemView.setOnClickListener(view -> {
|
|
|
+ final Intent intent = new Intent(context, UserInfoActivity.class);
|
|
|
+ if (accountListItem.isEnabled()) {
|
|
|
+ intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
|
|
|
+ try {
|
|
|
+ OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext());
|
|
|
+ intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName());
|
|
|
+ } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
|
|
|
+ Log_OC.d(TAG, "Failed to find NC account");
|
|
|
+ }
|
|
|
+ context.startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
if (!accountListItem.isEnabled()) {
|
|
|
usernameView.setPaintFlags(usernameView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
|
|
@@ -112,43 +145,51 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
|
|
|
}
|
|
|
|
|
|
} // create add account action item
|
|
|
- else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && mListener != null) {
|
|
|
- return setupAddAccountListItem(parent);
|
|
|
+ else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && accountListAdapterListener != null) {
|
|
|
+ setupAddAccountListItem((AddAccountViewHolderItem)holder);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return view;
|
|
|
}
|
|
|
|
|
|
- @NonNull
|
|
|
- private View setupAddAccountListItem(ViewGroup parent) {
|
|
|
- LayoutInflater inflater = mContext.getLayoutInflater();
|
|
|
- View actionView = inflater.inflate(R.layout.account_action, parent, false);
|
|
|
-
|
|
|
- TextView userName = actionView.findViewById(R.id.user_name);
|
|
|
- userName.setText(R.string.prefs_add_account);
|
|
|
- userName.setTextColor(ThemeUtils.primaryColor(getContext(), true));
|
|
|
+ /**
|
|
|
+ * Sets up a View to be used for adding a new account
|
|
|
+ *
|
|
|
+ * @param holder the add account view holder
|
|
|
+ */
|
|
|
+ private void setupAddAccountListItem(AddAccountViewHolderItem holder) {
|
|
|
+ View actionView = holder.itemView;
|
|
|
|
|
|
- ((ImageView) actionView.findViewById(R.id.user_icon)).setImageResource(R.drawable.ic_account_plus);
|
|
|
+ holder.usernameViewItem.setTextColor(ThemeUtils.primaryColor(context, true));
|
|
|
|
|
|
// bind action listener
|
|
|
- boolean isProviderOrOwnInstallationVisible = mContext.getResources()
|
|
|
+ boolean isProviderOrOwnInstallationVisible = context.getResources()
|
|
|
.getBoolean(R.bool.show_provider_or_own_installation);
|
|
|
|
|
|
if (isProviderOrOwnInstallationVisible) {
|
|
|
- actionView.setOnClickListener(v -> mListener.showFirstRunActivity());
|
|
|
+ actionView.setOnClickListener(v -> accountListAdapterListener.showFirstRunActivity());
|
|
|
} else {
|
|
|
- actionView.setOnClickListener(v -> mListener.createAccount());
|
|
|
+ actionView.setOnClickListener(v -> accountListAdapterListener.createAccount());
|
|
|
}
|
|
|
-
|
|
|
- return actionView;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the name of the account, in the view holder
|
|
|
+ *
|
|
|
+ * @param viewHolder the view holder that contains the account
|
|
|
+ * @param account the account
|
|
|
+ */
|
|
|
private void setAccount(AccountViewHolderItem viewHolder, Account account) {
|
|
|
viewHolder.accountViewItem.setText(DisplayUtils.convertIdn(account.name, false));
|
|
|
viewHolder.accountViewItem.setTag(account.name);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the current active state of the account to true if it is the account being used currently,
|
|
|
+ * false otherwise
|
|
|
+ *
|
|
|
+ * @param viewHolder the view holder that contains the account
|
|
|
+ * @param account the account
|
|
|
+ */
|
|
|
private void setCurrentlyActiveState(AccountViewHolderItem viewHolder, Account account) {
|
|
|
Account currentAccount = accountManager.getCurrentAccount();
|
|
|
if (currentAccount != null && currentAccount.name.equals(account.name)) {
|
|
@@ -158,12 +199,18 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the avatar of the account
|
|
|
+ *
|
|
|
+ * @param viewHolder the view holder that contains the account
|
|
|
+ * @param account the account
|
|
|
+ */
|
|
|
private void setAvatar(AccountViewHolderItem viewHolder, Account account) {
|
|
|
try {
|
|
|
View viewItem = viewHolder.imageViewItem;
|
|
|
viewItem.setTag(account.name);
|
|
|
- DisplayUtils.setAvatar(account, this, mAccountAvatarRadiusDimension, mContext.getResources(), viewItem,
|
|
|
- mContext);
|
|
|
+ DisplayUtils.setAvatar(account, this, accountAvatarRadiusDimension, context.getResources(), viewItem,
|
|
|
+ context);
|
|
|
} catch (Exception e) {
|
|
|
Log_OC.e(TAG, "Error calculating RGB value for account list item.", e);
|
|
|
// use user icon as a fallback
|
|
@@ -171,9 +218,15 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the username of the account
|
|
|
+ *
|
|
|
+ * @param viewHolder the view holder that contains the account
|
|
|
+ * @param account the account
|
|
|
+ */
|
|
|
private void setUsername(AccountViewHolderItem viewHolder, Account account) {
|
|
|
try {
|
|
|
- OwnCloudAccount oca = new OwnCloudAccount(account, mContext);
|
|
|
+ OwnCloudAccount oca = new OwnCloudAccount(account, context);
|
|
|
viewHolder.usernameViewItem.setText(oca.getDisplayName());
|
|
|
} catch (Exception e) {
|
|
|
Log_OC.w(TAG, "Account not found right after being read; using account name instead");
|
|
@@ -192,6 +245,48 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
|
|
|
return String.valueOf(((ImageView)callContext).getTag()).equals(tag);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the total number of items in the data set held by the adapter
|
|
|
+ *
|
|
|
+ * @return The total number of items in this adapter.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int getItemCount() {
|
|
|
+ return this.values.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns an AccountListItem from the specified position in the values list
|
|
|
+ *
|
|
|
+ * @param position of the object to be returned
|
|
|
+ * @return An AccountListItem of the specified position
|
|
|
+ */
|
|
|
+ public AccountListItem getItem(int position) {
|
|
|
+ return values.get(position);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Deletes the elements in the values list and notifies the Adapter
|
|
|
+ */
|
|
|
+ public void clear() {
|
|
|
+ final int size = values.size();
|
|
|
+ values.clear();
|
|
|
+ notifyItemRangeRemoved(0, size);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Adds all of the items to the data set
|
|
|
+ *
|
|
|
+ * @param items The item list to be added
|
|
|
+ */
|
|
|
+ public void addAll(List<AccountListItem> items){
|
|
|
+ if(values == null){
|
|
|
+ values = new ArrayList<>();
|
|
|
+ }
|
|
|
+ values.addAll(items);
|
|
|
+ notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Listener interface for Activities using the {@link AccountListAdapter}
|
|
|
*/
|
|
@@ -205,11 +300,30 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
|
|
|
/**
|
|
|
* Account ViewHolderItem to get smooth scrolling.
|
|
|
*/
|
|
|
- private static class AccountViewHolderItem {
|
|
|
+ static class AccountViewHolderItem extends RecyclerView.ViewHolder {
|
|
|
private ImageView imageViewItem;
|
|
|
private ImageView checkViewItem;
|
|
|
|
|
|
private TextView usernameViewItem;
|
|
|
private TextView accountViewItem;
|
|
|
+
|
|
|
+ AccountViewHolderItem(@NonNull View view) {
|
|
|
+ super(view);
|
|
|
+ this.imageViewItem = view.findViewById(R.id.user_icon);
|
|
|
+ this.checkViewItem = view.findViewById(R.id.ticker);
|
|
|
+ this.usernameViewItem = view.findViewById(R.id.user_name);
|
|
|
+ this.accountViewItem = view.findViewById(R.id.account);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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);
|
|
|
+ }
|
|
|
}
|
|
|
}
|