package com.moac.android.opensecretsanta.activity;

import android.accounts.*;
import android.os.Bundle;
import android.os.Handler;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import com.moac.android.opensecretsanta.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AllPreferencesActivity extends PreferenceActivity {

    private final static String TAG = "AllPreferencesActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        AccountManager.get(this).getAccountsByTypeAndFeatures(Constants.ACCOUNT_TYPE_GOOGLE, Constants.FEATURES_MAIL,
          new AccountManagerCallback <Account[]>() {
              @Override
              public void run(AccountManagerFuture<Account[]> future) {
                  ListPreference gmailLp = ((ListPreference)getPreferenceManager().findPreference(getString(R.string.gmail_account_preference)));

                  try {
                      Account[] accounts = future.getResult();
                      if(accounts != null && accounts.length > 0) {
                          String[] accountEntries = new String[accounts.length];

                          for (int i=0; i < accounts.length; i++) {
                              accountEntries[i] = accounts[i].name;
                          }
                          gmailLp.setEntries(accountEntries);
                          gmailLp.setEntryValues(accountEntries);
                    }

                  } catch(Exception e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
              }
          }, new Handler());
    }
}

@@ -213,16 +213,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import c
@@ -815,17 +815,16 @@
 Callback
- 
 %3CAccount
@@ -945,16 +945,20 @@
         
+    
 ListPref
@@ -991,16 +991,17 @@
 ference)
+ 
 getPrefe
@@ -1078,16 +1078,92 @@
 nce)));%0A
+                      List%3CString%3E accountEntries = new ArrayList%3CString%3E();
 %0A       
@@ -1314,90 +1314,8 @@
 ) %7B%0A
-                          String%5B%5D accountEntries = new String%5Baccounts.length%5D;%0A%0A
     
@@ -1343,16 +1343,17 @@
  for
- 
 (int i
-=
+ = 
 0; i
@@ -1427,14 +1427,13 @@
 ries
-%5Bi%5D = 
+.add(
 acco
@@ -1444,16 +1444,17 @@
 %5Bi%5D.name
+)
 ;%0A      
@@ -1500,112 +1500,149 @@
     
+ %7D%0A%0A
      
-gmailLp.setEntries(accountEntries);%0A                          gmailLp.setEntryValues(accountEntries
+             %7D catch(Exception e) %7B%0A                      Log.e(TAG, %22onCreate() - An error occurred populating the account list%22, e
 );%0A 
@@ -1650,36 +1650,33 @@
                 
- 
- %7D%0A
+%7D
 %0A               
@@ -1682,168 +1682,183 @@
     
-%7D catch(Exception e) %7B%0A                      e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A                  %7D
+String%5B%5D entries = accountEntries.toArray(new String%5BaccountEntries.size()%5D);%0A                  gmailLp.setEntries(entries);%0A                  gmailLp.setEntryValues(entries);
 %0A   

Fix crash when no email addresses in settings