From abb8ef6ed7071913c9a21316e5b64b8d022e90fe Mon Sep 17 00:00:00 2001 From: Melvin Blokhuijzen Date: Mon, 5 Oct 2015 21:39:50 +0200 Subject: [PATCH 1/5] Change View of accounts into ListView --- res/layout/login.xml | 90 ++- res/layout/login_list_item.xml | 2 +- .../andlyticsproject/AccountListAdapter.java | 48 ++ .../andlyticsproject/CheckboxListener.java | 8 + .../andlyticsproject/LoginActivity.java | 541 +++++++++--------- 5 files changed, 361 insertions(+), 328 deletions(-) create mode 100644 src/com/github/andlyticsproject/AccountListAdapter.java create mode 100644 src/com/github/andlyticsproject/CheckboxListener.java diff --git a/res/layout/login.xml b/res/layout/login.xml index 11976e09..c20a0977 100644 --- a/res/layout/login.xml +++ b/res/layout/login.xml @@ -3,62 +3,52 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/andBackground" - android:orientation="vertical" > + android:orientation="vertical"> - + android:layout_height="match_parent" + android:orientation="vertical" + android:paddingBottom="80dp"> - - - - - + android:layout_marginLeft="5dp" + android:layout_marginRight="5dp" + android:layout_marginTop="10dp" + android:paddingLeft="5dp" + android:paddingRight="5dp" + android:text="@string/account_required" + android:textStyle="bold" /> - + - + - - - - + + + android:padding="5dp"> diff --git a/src/com/github/andlyticsproject/AccountListAdapter.java b/src/com/github/andlyticsproject/AccountListAdapter.java new file mode 100644 index 00000000..d6ded8aa --- /dev/null +++ b/src/com/github/andlyticsproject/AccountListAdapter.java @@ -0,0 +1,48 @@ +package com.github.andlyticsproject; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.TextView; + +import com.github.andlyticsproject.model.DeveloperAccount; + +import java.util.ArrayList; +import java.util.List; + +public class AccountListAdapter extends ArrayAdapter { + + private final Context context; + private final int resource; + private final CheckboxListener listener; + private List developerAccounts = new ArrayList<>(); + + public AccountListAdapter(Context context, int resource, List developerAccounts, CheckboxListener listener) { + super(context, resource, developerAccounts); + this.resource = resource; + this.context = context; + this.listener = listener; + this.developerAccounts.addAll(developerAccounts); + } + + @Override + public View getView(final int position, View convertView, ViewGroup parent) { + LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + View rowView = layoutInflater.inflate(resource, parent, false); + final TextView developerEmail = (TextView) rowView.findViewById(R.id.login_list_item_text); + final CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.login_list_item_enabled); + checkBox.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + listener.checkBoxSelected(checkBox.isChecked(), developerAccounts.get(position)); + } + }); + developerEmail.setText(developerAccounts.get(position).getName()); + checkBox.setChecked(!developerAccounts.get(position).isHidden()); + return rowView; + } +} diff --git a/src/com/github/andlyticsproject/CheckboxListener.java b/src/com/github/andlyticsproject/CheckboxListener.java new file mode 100644 index 00000000..79e84d58 --- /dev/null +++ b/src/com/github/andlyticsproject/CheckboxListener.java @@ -0,0 +1,8 @@ +package com.github.andlyticsproject; + +import com.github.andlyticsproject.model.DeveloperAccount; + + +public interface CheckboxListener { + void checkBoxSelected(boolean checked, DeveloperAccount account); +} diff --git a/src/com/github/andlyticsproject/LoginActivity.java b/src/com/github/andlyticsproject/LoginActivity.java index a3af68ba..109c7bd2 100644 --- a/src/com/github/andlyticsproject/LoginActivity.java +++ b/src/com/github/andlyticsproject/LoginActivity.java @@ -16,11 +16,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.view.Window; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.CompoundButton.OnCheckedChangeListener; -import android.widget.LinearLayout; -import android.widget.TextView; +import android.widget.ListView; import com.github.andlyticsproject.model.DeveloperAccount; import com.github.andlyticsproject.sync.AutosyncHandler; @@ -38,276 +34,267 @@ * or * Main -> LoginActivity -> Main */ -public class LoginActivity extends Activity { - - private static final String TAG = LoginActivity.class.getSimpleName(); - - public static final String EXTRA_MANAGE_ACCOUNTS_MODE = "com.github.andlyticsproject.manageAccounts"; - - public static final String AUTH_TOKEN_TYPE_ANDROID_DEVELOPER = "androiddeveloper"; - - protected static final int CREATE_ACCOUNT_REQUEST = 1; - - private List developerAccounts; - - private boolean manageAccountsMode = false; - private boolean blockGoingBack = false; - private DeveloperAccount selectedAccount = null; - private View okButton; - private LinearLayout accountList; - - private AccountManager accountManager; - private DeveloperAccountManager developerAccountManager; - private AutosyncHandler syncHandler; - - // TODO Clean this code and res/layout/login.xml up e.g. using a ListView - // instead of a LinearLayout - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - - accountManager = AccountManager.get(this); - developerAccountManager = DeveloperAccountManager.getInstance(getApplicationContext()); - syncHandler = new AutosyncHandler(); - - // When called from accounts action item in Main, this flag is passed to - // indicate - // that LoginActivity should not auto login as we are managing the - // accounts, - // rather than performing the initial login - Bundle extras = getIntent().getExtras(); - if (extras != null) { - manageAccountsMode = extras.getBoolean(LoginActivity.EXTRA_MANAGE_ACCOUNTS_MODE); - } - - if (manageAccountsMode) { - getActionBar().setTitle(R.string.manage_accounts); - } - - selectedAccount = developerAccountManager.getSelectedDeveloperAccount(); - - setContentView(R.layout.login); - setProgressBarIndeterminateVisibility(false); - accountList = (LinearLayout) findViewById(R.id.login_input); - - okButton = findViewById(R.id.login_ok_button); - okButton.setClickable(true); - okButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - new AsyncTask() { - - @Override - protected void onPreExecute() { - setProgressBarIndeterminateVisibility(true); - okButton.setEnabled(false); - } - - @Override - protected Void doInBackground(Void... args) { - saveDeveloperAccounts(); - - return null; - } - - @Override - protected void onPostExecute(Void arg) { - setProgressBarIndeterminateVisibility(false); - okButton.setEnabled(true); - - if (selectedAccount != null) { - redirectToMain(selectedAccount.getName(), - selectedAccount.getDeveloperId()); - } else { - // Go to the first non hidden account - for (DeveloperAccount account : developerAccounts) { - if (account.isVisible()) { - redirectToMain(account.getName(), account.getDeveloperId()); - break; - } - } - } - } - }.execute(); - } - }); - } - - @Override - protected void onResume() { - super.onResume(); - - boolean skipAutologin = Preferences.getSkipAutologin(this); - - if (!manageAccountsMode & !skipAutologin & selectedAccount != null) { - redirectToMain(selectedAccount.getName(), selectedAccount.getDeveloperId()); - } else { - showAccountList(); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - getMenuInflater().inflate(R.menu.login_menu, menu); - return true; - } - - /** - * Called if item in option menu is selected. - * - * @param item - * The chosen menu item - * @return boolean true/false - */ - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.itemLoginmenuAdd: - addNewGoogleAccount(); - break; - case android.R.id.home: - if (!blockGoingBack) { - setResult(RESULT_OK); - finish(); - } - break; - default: - return false; - } - return true; - } - - @Override - public void onBackPressed() { - setResult(blockGoingBack ? RESULT_CANCELED : RESULT_OK); - super.onBackPressed(); - } - - protected void showAccountList() { - Account[] googleAccounts = accountManager.getAccountsByType(AutosyncHandler.ACCOUNT_TYPE_GOOGLE); - List dbAccounts = developerAccountManager.getAllDeveloperAccounts(); - developerAccounts = new ArrayList(); - - accountList.removeAllViews(); - for (int i = 0; i < googleAccounts.length; i++) { - DeveloperAccount developerAccount = DeveloperAccount - .createHidden(googleAccounts[i].name); - int idx = dbAccounts.indexOf(developerAccount); - // use persistent object if exists - if (idx != -1) { - developerAccount = dbAccounts.get(idx); - } - developerAccounts.add(developerAccount); - - // Setup auto sync - // only do this when managing accounts, otherwise sync may start - // in the background before accounts are actually configured - if (manageAccountsMode) { - // Ensure it matches the sync period (excluding disabled state) - syncHandler.setAutosyncPeriod(googleAccounts[i].name, - Preferences.getLastNonZeroAutosyncPeriod(this)); - // Now make it match the master sync (including disabled state) - syncHandler.setAutosyncPeriod(googleAccounts[i].name, - Preferences.getAutosyncPeriod(this)); - } - - View accountItem = getLayoutInflater().inflate(R.layout.login_list_item, null); - TextView accountName = (TextView) accountItem.findViewById(R.id.login_list_item_text); - accountName.setText(googleAccounts[i].name); - accountItem.setTag(developerAccount); - CheckBox enabled = (CheckBox) accountItem.findViewById(R.id.login_list_item_enabled); - enabled.setChecked(!developerAccount.isHidden()); - enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - DeveloperAccount account = (DeveloperAccount) ((View) buttonView.getParent()) - .getTag(); - if (isChecked) { - account.activate(); - } else { - account.hide(); - } - - if (manageAccountsMode && account.equals(selectedAccount)) { - // If they remove the current account, then stop them - // going back - blockGoingBack = account.isHidden(); - } - - okButton.setEnabled(isAtLeastOneAccountEnabled()); - } - }); - accountList.addView(accountItem); - } - - // Update ok button - okButton.setEnabled(isAtLeastOneAccountEnabled()); - } - - private void saveDeveloperAccounts() { - for (DeveloperAccount account : developerAccounts) { - if (account.isHidden()) { - // They are removing the account from Andlytics, disable - // syncing - syncHandler.setAutosyncEnabled(account.getName(), false); - } else { - // Make it match the master sync period (including - // disabled state) - syncHandler.setAutosyncPeriod(account.getName(), - Preferences.getAutosyncPeriod(LoginActivity.this)); - } - developerAccountManager.addOrUpdateDeveloperAccount(account); - } - } - - private boolean isAtLeastOneAccountEnabled() { - for (DeveloperAccount acc : developerAccounts) { - if (acc.isVisible()) { - return true; - } - } - - return false; - } - - private void addNewGoogleAccount() { - AccountManagerCallback callback = new AccountManagerCallback() { - public void run(AccountManagerFuture future) { - try { - Bundle bundle = future.getResult(); - bundle.keySet(); - Log.d(TAG, "account added: " + bundle); - - showAccountList(); - - } catch (OperationCanceledException e) { - Log.d(TAG, "addAccount was canceled"); - } catch (IOException e) { - Log.d(TAG, "addAccount failed: " + e); - } catch (AuthenticatorException e) { - Log.d(TAG, "addAccount failed: " + e); - } - // gotAccount(false); - } - }; - - // TODO request a weblogin: token here, so we have it cached? - accountManager.addAccount(AutosyncHandler.ACCOUNT_TYPE_GOOGLE, - LoginActivity.AUTH_TOKEN_TYPE_ANDROID_DEVELOPER, null, null /* options */, - LoginActivity.this, callback, null /* handler */); - } - - private void redirectToMain(String selectedAccount, String developerId) { - Preferences.saveSkipAutoLogin(this, false); - Intent intent = new Intent(LoginActivity.this, Main.class); - intent.putExtra(BaseActivity.EXTRA_AUTH_ACCOUNT_NAME, selectedAccount); - intent.putExtra(BaseActivity.EXTRA_DEVELOPER_ID, developerId); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(intent); - overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out); - finish(); - } - +public class LoginActivity extends Activity implements CheckboxListener { + + private static final String TAG = LoginActivity.class.getSimpleName(); + + public static final String EXTRA_MANAGE_ACCOUNTS_MODE = "com.github.andlyticsproject.manageAccounts"; + + public static final String AUTH_TOKEN_TYPE_ANDROID_DEVELOPER = "androiddeveloper"; + + protected static final int CREATE_ACCOUNT_REQUEST = 1; + + private List developerAccounts; + + private boolean manageAccountsMode = false; + private boolean blockGoingBack = false; + private DeveloperAccount selectedAccount = null; + private View okButton; + private ListView accountList; + + private AccountManager accountManager; + private DeveloperAccountManager developerAccountManager; + private AutosyncHandler syncHandler; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + + accountManager = AccountManager.get(this); + developerAccountManager = DeveloperAccountManager.getInstance(getApplicationContext()); + syncHandler = new AutosyncHandler(); + + // When called from accounts action item in Main, this flag is passed to + // indicate + // that LoginActivity should not auto login as we are managing the + // accounts, + // rather than performing the initial login + Bundle extras = getIntent().getExtras(); + if (extras != null) { + manageAccountsMode = extras.getBoolean(LoginActivity.EXTRA_MANAGE_ACCOUNTS_MODE); + } + + if (manageAccountsMode) { + getActionBar().setTitle(R.string.manage_accounts); + } + + selectedAccount = developerAccountManager.getSelectedDeveloperAccount(); + + setContentView(R.layout.login); + setProgressBarIndeterminateVisibility(false); + accountList = (ListView) findViewById(R.id.login_input); + + okButton = findViewById(R.id.login_ok_button); + okButton.setClickable(true); + okButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + new AsyncTask() { + + @Override + protected void onPreExecute() { + setProgressBarIndeterminateVisibility(true); + okButton.setEnabled(false); + } + + @Override + protected Void doInBackground(Void... args) { + saveDeveloperAccounts(); + + return null; + } + + @Override + protected void onPostExecute(Void arg) { + setProgressBarIndeterminateVisibility(false); + okButton.setEnabled(true); + + if (selectedAccount != null) { + redirectToMain(selectedAccount.getName(), + selectedAccount.getDeveloperId()); + } else { + // Go to the first non hidden account + for (DeveloperAccount account : developerAccounts) { + if (account.isVisible()) { + redirectToMain(account.getName(), account.getDeveloperId()); + break; + } + } + } + } + }.execute(); + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + + boolean skipAutologin = Preferences.getSkipAutologin(this); + + if (!manageAccountsMode & !skipAutologin & selectedAccount != null) { + redirectToMain(selectedAccount.getName(), selectedAccount.getDeveloperId()); + } else { + showAccountList(); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + getMenuInflater().inflate(R.menu.login_menu, menu); + return true; + } + + /** + * Called if item in option menu is selected. + * + * @param item The chosen menu item + * @return boolean true/false + */ + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.itemLoginmenuAdd: + addNewGoogleAccount(); + break; + case android.R.id.home: + if (!blockGoingBack) { + setResult(RESULT_OK); + finish(); + } + break; + default: + return false; + } + return true; + } + + @Override + public void onBackPressed() { + setResult(blockGoingBack ? RESULT_CANCELED : RESULT_OK); + super.onBackPressed(); + } + + protected void showAccountList() { + this.developerAccounts = getDeveloperAccounts(); + AccountListAdapter adapter = new AccountListAdapter(this, R.layout.login_list_item, developerAccounts, this); + accountList.setAdapter(adapter); + + okButton.setEnabled(isAtLeastOneAccountEnabled()); + } + + @Override + public void checkBoxSelected(boolean checked, DeveloperAccount account) { + if (checked) { + account.activate(); + } else { + account.hide(); + } + + if (manageAccountsMode && account.equals(selectedAccount)) { + // If they remove the current account, then stop them + // going back + blockGoingBack = account.isHidden(); + } + + okButton.setEnabled(isAtLeastOneAccountEnabled()); + } + + private List getDeveloperAccounts() { + Account[] googleAccounts = accountManager.getAccountsByType(AutosyncHandler.ACCOUNT_TYPE_GOOGLE); + List dbAccounts = developerAccountManager.getAllDeveloperAccounts(); + List result = new ArrayList<>(); + + for (int i = 0; i < googleAccounts.length; i++) { + DeveloperAccount developerAccount = DeveloperAccount + .createHidden(googleAccounts[i].name); + int idx = dbAccounts.indexOf(developerAccount); + // use persistent object if exists + if (idx != -1) { + developerAccount = dbAccounts.get(idx); + } + result.add(developerAccount); + + // Setup auto sync + // only do this when managing accounts, otherwise sync may start + // in the background before accounts are actually configured + if (manageAccountsMode) { + // Ensure it matches the sync period (excluding disabled state) + syncHandler.setAutosyncPeriod(googleAccounts[i].name, + Preferences.getLastNonZeroAutosyncPeriod(this)); + // Now make it match the master sync (including disabled state) + syncHandler.setAutosyncPeriod(googleAccounts[i].name, + Preferences.getAutosyncPeriod(this)); + } + } + + return result; + } + + private void saveDeveloperAccounts() { + for (DeveloperAccount account : developerAccounts) { + if (account.isHidden()) { + // They are removing the account from Andlytics, disable + // syncing + syncHandler.setAutosyncEnabled(account.getName(), false); + } else { + // Make it match the master sync period (including + // disabled state) + syncHandler.setAutosyncPeriod(account.getName(), + Preferences.getAutosyncPeriod(LoginActivity.this)); + } + developerAccountManager.addOrUpdateDeveloperAccount(account); + } + } + + private boolean isAtLeastOneAccountEnabled() { + for (DeveloperAccount acc : developerAccounts) { + if (acc.isVisible()) { + return true; + } + } + + return false; + } + + private void addNewGoogleAccount() { + AccountManagerCallback callback = new AccountManagerCallback() { + public void run(AccountManagerFuture future) { + try { + Bundle bundle = future.getResult(); + bundle.keySet(); + Log.d(TAG, "account added: " + bundle); + + showAccountList(); + + } catch (OperationCanceledException e) { + Log.d(TAG, "addAccount was canceled"); + } catch (IOException e) { + Log.d(TAG, "addAccount failed: " + e); + } catch (AuthenticatorException e) { + Log.d(TAG, "addAccount failed: " + e); + } + // gotAccount(false); + } + }; + + // TODO request a weblogin: token here, so we have it cached? + accountManager.addAccount(AutosyncHandler.ACCOUNT_TYPE_GOOGLE, + LoginActivity.AUTH_TOKEN_TYPE_ANDROID_DEVELOPER, null, null /* options */, + LoginActivity.this, callback, null /* handler */); + } + + private void redirectToMain(String selectedAccount, String developerId) { + Preferences.saveSkipAutoLogin(this, false); + Intent intent = new Intent(LoginActivity.this, Main.class); + intent.putExtra(BaseActivity.EXTRA_AUTH_ACCOUNT_NAME, selectedAccount); + intent.putExtra(BaseActivity.EXTRA_DEVELOPER_ID, developerId); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out); + finish(); + } } From 48fb4977c8a8d681a05f772ab3c51cf7f71d3f07 Mon Sep 17 00:00:00 2001 From: Melvin Blokhuijzen Date: Wed, 7 Oct 2015 17:22:08 +0200 Subject: [PATCH 2/5] Rename Interface --- src/com/github/andlyticsproject/AccountListAdapter.java | 6 +++--- .../github/andlyticsproject/AccountSelectedListener.java | 8 ++++++++ src/com/github/andlyticsproject/CheckboxListener.java | 8 -------- src/com/github/andlyticsproject/LoginActivity.java | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 src/com/github/andlyticsproject/AccountSelectedListener.java delete mode 100644 src/com/github/andlyticsproject/CheckboxListener.java diff --git a/src/com/github/andlyticsproject/AccountListAdapter.java b/src/com/github/andlyticsproject/AccountListAdapter.java index d6ded8aa..5c017e28 100644 --- a/src/com/github/andlyticsproject/AccountListAdapter.java +++ b/src/com/github/andlyticsproject/AccountListAdapter.java @@ -17,10 +17,10 @@ public class AccountListAdapter extends ArrayAdapter { private final Context context; private final int resource; - private final CheckboxListener listener; + private final AccountSelectedListener listener; private List developerAccounts = new ArrayList<>(); - public AccountListAdapter(Context context, int resource, List developerAccounts, CheckboxListener listener) { + public AccountListAdapter(Context context, int resource, List developerAccounts, AccountSelectedListener listener) { super(context, resource, developerAccounts); this.resource = resource; this.context = context; @@ -38,7 +38,7 @@ public View getView(final int position, View convertView, ViewGroup parent) { checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - listener.checkBoxSelected(checkBox.isChecked(), developerAccounts.get(position)); + listener.accountSelected(checkBox.isChecked(), developerAccounts.get(position)); } }); developerEmail.setText(developerAccounts.get(position).getName()); diff --git a/src/com/github/andlyticsproject/AccountSelectedListener.java b/src/com/github/andlyticsproject/AccountSelectedListener.java new file mode 100644 index 00000000..b6868563 --- /dev/null +++ b/src/com/github/andlyticsproject/AccountSelectedListener.java @@ -0,0 +1,8 @@ +package com.github.andlyticsproject; + +import com.github.andlyticsproject.model.DeveloperAccount; + + +public interface AccountSelectedListener { + void accountSelected(boolean checked, DeveloperAccount account); +} diff --git a/src/com/github/andlyticsproject/CheckboxListener.java b/src/com/github/andlyticsproject/CheckboxListener.java deleted file mode 100644 index 79e84d58..00000000 --- a/src/com/github/andlyticsproject/CheckboxListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.andlyticsproject; - -import com.github.andlyticsproject.model.DeveloperAccount; - - -public interface CheckboxListener { - void checkBoxSelected(boolean checked, DeveloperAccount account); -} diff --git a/src/com/github/andlyticsproject/LoginActivity.java b/src/com/github/andlyticsproject/LoginActivity.java index 109c7bd2..a147e975 100644 --- a/src/com/github/andlyticsproject/LoginActivity.java +++ b/src/com/github/andlyticsproject/LoginActivity.java @@ -34,7 +34,7 @@ * or * Main -> LoginActivity -> Main */ -public class LoginActivity extends Activity implements CheckboxListener { +public class LoginActivity extends Activity implements AccountSelectedListener { private static final String TAG = LoginActivity.class.getSimpleName(); @@ -187,7 +187,7 @@ protected void showAccountList() { } @Override - public void checkBoxSelected(boolean checked, DeveloperAccount account) { + public void accountSelected(boolean checked, DeveloperAccount account) { if (checked) { account.activate(); } else { From 78614fcc2979e3eae769c6c10c99aada92c728c6 Mon Sep 17 00:00:00 2001 From: Melvin Blokhuijzen Date: Wed, 7 Oct 2015 17:26:58 +0200 Subject: [PATCH 3/5] Add RecyclerView dependency --- build.gradle | 1 + res/layout/login.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index f2315f84..f9bc4af7 100755 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,7 @@ dependencies { compile 'com.google.code.gson:gson:2.1' compile 'net.sf.opencsv:opencsv:2.3' compile 'com.google.code.findbugs:jsr305:1.3.9' + compile 'com.android.support:recyclerview-v7:23.0.1' } android { diff --git a/res/layout/login.xml b/res/layout/login.xml index c20a0977..7da4ee5a 100644 --- a/res/layout/login.xml +++ b/res/layout/login.xml @@ -44,7 +44,7 @@ android:paddingLeft="5dp" android:paddingRight="5dp" /> - From 06f88429793b43d9ff51a90f9a9e66e28fa2c502 Mon Sep 17 00:00:00 2001 From: Melvin Blokhuijzen Date: Wed, 7 Oct 2015 17:28:51 +0200 Subject: [PATCH 4/5] Fix RecyclerView XML component --- res/layout/login.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/layout/login.xml b/res/layout/login.xml index 7da4ee5a..8f8619ed 100644 --- a/res/layout/login.xml +++ b/res/layout/login.xml @@ -44,7 +44,7 @@ android:paddingLeft="5dp" android:paddingRight="5dp" /> - From 400c1db3c36893ca991ddb666c566f55afab88d3 Mon Sep 17 00:00:00 2001 From: Melvin Blokhuijzen Date: Wed, 7 Oct 2015 18:01:28 +0200 Subject: [PATCH 5/5] Change ListView to RecyclerView --- .../andlyticsproject/AccountListAdapter.java | 57 ++++++++++++------- .../andlyticsproject/LoginActivity.java | 10 ++-- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/com/github/andlyticsproject/AccountListAdapter.java b/src/com/github/andlyticsproject/AccountListAdapter.java index 5c017e28..fa81a136 100644 --- a/src/com/github/andlyticsproject/AccountListAdapter.java +++ b/src/com/github/andlyticsproject/AccountListAdapter.java @@ -1,10 +1,9 @@ package com.github.andlyticsproject; -import android.content.Context; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.TextView; @@ -13,36 +12,52 @@ import java.util.ArrayList; import java.util.List; -public class AccountListAdapter extends ArrayAdapter { - - private final Context context; - private final int resource; +public class AccountListAdapter extends RecyclerView.Adapter { private final AccountSelectedListener listener; private List developerAccounts = new ArrayList<>(); - public AccountListAdapter(Context context, int resource, List developerAccounts, AccountSelectedListener listener) { - super(context, resource, developerAccounts); - this.resource = resource; - this.context = context; + public class ViewHolder extends RecyclerView.ViewHolder { + public TextView emailDeveloper; + public CheckBox checkBox; + + public ViewHolder(View itemView) { + super(itemView); + + emailDeveloper = (TextView) itemView.findViewById(R.id.login_list_item_text); + checkBox = (CheckBox) itemView.findViewById(R.id.login_list_item_enabled); + } + } + + public AccountListAdapter(List developerAccounts, AccountSelectedListener listener) { this.listener = listener; this.developerAccounts.addAll(developerAccounts); } @Override - public View getView(final int position, View convertView, ViewGroup parent) { - LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - View rowView = layoutInflater.inflate(resource, parent, false); - final TextView developerEmail = (TextView) rowView.findViewById(R.id.login_list_item_text); - final CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.login_list_item_enabled); - checkBox.setOnClickListener(new View.OnClickListener() { + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.login_list_item, parent, false); + + ViewHolder viewHolder = new ViewHolder(v); + + return viewHolder; + } + + @Override + public void onBindViewHolder(final ViewHolder holder, final int position) { + DeveloperAccount developerAccount = developerAccounts.get(position); + holder.emailDeveloper.setText(developerAccount.getName()); + holder.checkBox.setChecked(!developerAccount.isHidden()); + holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - listener.accountSelected(checkBox.isChecked(), developerAccounts.get(position)); + listener.accountSelected(holder.checkBox.isChecked(), developerAccounts.get(position)); } }); - developerEmail.setText(developerAccounts.get(position).getName()); - checkBox.setChecked(!developerAccounts.get(position).isHidden()); - return rowView; + + } + + @Override + public int getItemCount() { + return developerAccounts.size(); } } diff --git a/src/com/github/andlyticsproject/LoginActivity.java b/src/com/github/andlyticsproject/LoginActivity.java index a147e975..4871e475 100644 --- a/src/com/github/andlyticsproject/LoginActivity.java +++ b/src/com/github/andlyticsproject/LoginActivity.java @@ -10,13 +10,14 @@ import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; -import android.widget.ListView; import com.github.andlyticsproject.model.DeveloperAccount; import com.github.andlyticsproject.sync.AutosyncHandler; @@ -50,7 +51,7 @@ public class LoginActivity extends Activity implements AccountSelectedListener { private boolean blockGoingBack = false; private DeveloperAccount selectedAccount = null; private View okButton; - private ListView accountList; + private RecyclerView accountList; private AccountManager accountManager; private DeveloperAccountManager developerAccountManager; @@ -83,7 +84,7 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.login); setProgressBarIndeterminateVisibility(false); - accountList = (ListView) findViewById(R.id.login_input); + accountList = (RecyclerView) findViewById(R.id.login_input); okButton = findViewById(R.id.login_ok_button); okButton.setClickable(true); @@ -179,8 +180,9 @@ public void onBackPressed() { } protected void showAccountList() { + accountList.setLayoutManager(new LinearLayoutManager(this)); this.developerAccounts = getDeveloperAccounts(); - AccountListAdapter adapter = new AccountListAdapter(this, R.layout.login_list_item, developerAccounts, this); + AccountListAdapter adapter = new AccountListAdapter(developerAccounts, this); accountList.setAdapter(adapter); okButton.setEnabled(isAtLeastOneAccountEnabled());