Browse Source

Add Load Contact in Background and ProgressBar

Fixing Comparator

Format code
alejandro 8 years ago
parent
commit
f555d75460

+ 7 - 3
src/main/java/com/owncloud/android/services/ContactsImportJob.java

@@ -89,10 +89,14 @@ public class ContactsImportJob extends Job {
 
             for (int i = 0; i < intArray.length; i++) {
                 VCard vCard = vCards.get(intArray[i]);
-                if (!ownContactList.containsKey(vCard)) {
-                    operations.insertContact(vCard);
+                if (ContactListFragment.getDisplayName(vCard).length() != 0) {
+                    if (!ownContactList.containsKey(vCard)) {
+                        operations.insertContact(vCard);
+                    } else {
+                        operations.updateContact(vCard, ownContactList.get(vCard));
+                    }
                 } else {
-                    operations.updateContact(vCard, ownContactList.get(vCard));
+                    operations.insertContact(vCard); //Insert All the contacts without name
                 }
             }
         } catch (Exception e) {

+ 115 - 76
src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactListFragment.java

@@ -52,6 +52,9 @@ import android.widget.Button;
 import android.widget.CheckedTextView;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.ProgressBar;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
 
 import com.evernote.android.job.JobRequest;
 import com.evernote.android.job.util.support.PersistableBundleCompat;
@@ -85,7 +88,8 @@ import butterknife.BindView;
 import butterknife.ButterKnife;
 import ezvcard.Ezvcard;
 import ezvcard.VCard;
-import ezvcard.property.StructuredName;
+
+import static com.owncloud.android.ui.fragment.contactsbackup.ContactListFragment.getDisplayName;
 
 /**
  * This fragment shows all contacts from a file and allows to import them.
@@ -107,8 +111,26 @@ public class ContactListFragment extends FileFragment {
     @BindView(R.id.contactlist_restore_selected)
     public Button restoreContacts;
 
+    @BindView(R.id.empty_list_view_text)
+    public TextView emptyContentMessage;
+
+    @BindView(R.id.empty_list_view_headline)
+    public TextView emptyContentHeadline;
+
+    @BindView(R.id.empty_list_icon)
+    public ImageView emptyContentIcon;
+
+    @BindView(R.id.empty_list_progress)
+    public ProgressBar emptyContentProgressBar;
+
+    @BindView(R.id.empty_list_container)
+    public RelativeLayout emptyListContainer;
+
+
     private ContactListAdapter contactListAdapter;
     private Account account;
+    ArrayList<VCard> vCards = new ArrayList<>();
+    private OCFile ocFile;
 
     public static ContactListFragment newInstance(OCFile file, Account account) {
         ContactListFragment frag = new ContactListFragment();
@@ -142,56 +164,6 @@ public class ContactListFragment extends FileFragment {
         contactsPreferenceActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         contactsPreferenceActivity.setDrawerIndicatorEnabled(false);
 
-        ArrayList<VCard> vCards = new ArrayList<>();
-
-        try {
-            OCFile ocFile = getArguments().getParcelable(FILE_NAME);
-            setFile(ocFile);
-            account = getArguments().getParcelable(ACCOUNT);
-
-            if (!ocFile.isDown()) {
-                Intent i = new Intent(getContext(), FileDownloader.class);
-                i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
-                i.putExtra(FileDownloader.EXTRA_FILE, ocFile);
-                getContext().startService(i);
-
-                // Listen for download messages
-                IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.getDownloadAddedMessage());
-                downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
-                DownloadFinishReceiver mDownloadFinishReceiver = new DownloadFinishReceiver();
-                getContext().registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
-            } else {
-                File file = new File(ocFile.getStoragePath());
-                vCards.addAll(Ezvcard.parse(file).all());
-                Collections.sort(vCards, new Comparator<VCard>() {
-                    @Override
-                    public int compare(VCard o1, VCard o2) {
-                        if (o1.getFormattedName() != null && o2.getFormattedName() != null) {
-                            return o1.getFormattedName().getValue().compareTo(o2.getFormattedName().getValue());
-                        } else {
-                            if (o1.getFormattedName() == null) {
-                                return 1; // Send the contact to the end of the list
-                            } else {
-                                return -1;
-                            }
-                        }
-                    }
-                });
-            }
-        } catch (IOException e) {
-            Log_OC.e(TAG, "Error processing contacts file!", e);
-        }
-
-        restoreContacts.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-
-                if (checkAndAskForContactsWritePermission()) {
-                    getAccountForImport();
-                }
-            }
-        });
-
         recyclerView = (RecyclerView) view.findViewById(R.id.contactlist_recyclerview);
 
         if (savedInstanceState == null) {
@@ -210,6 +182,35 @@ public class ContactListFragment extends FileFragment {
         recyclerView.setAdapter(contactListAdapter);
         recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 
+        ocFile = getArguments().getParcelable(FILE_NAME);
+        setFile(ocFile);
+        account = getArguments().getParcelable(ACCOUNT);
+
+        if (!ocFile.isDown()) {
+            Intent i = new Intent(getContext(), FileDownloader.class);
+            i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
+            i.putExtra(FileDownloader.EXTRA_FILE, ocFile);
+            getContext().startService(i);
+
+            // Listen for download messages
+            IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.getDownloadAddedMessage());
+            downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
+            DownloadFinishReceiver mDownloadFinishReceiver = new DownloadFinishReceiver();
+            getContext().registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
+        } else {
+            loadContactsTask.execute();
+        }
+
+        restoreContacts.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+
+                if (checkAndAskForContactsWritePermission()) {
+                    getAccountForImport();
+                }
+            }
+        });
+
         return view;
     }
 
@@ -250,6 +251,9 @@ public class ContactListFragment extends FileFragment {
     @Override
     public void onStop() {
         EventBus.getDefault().unregister(this);
+        if (loadContactsTask != null) {
+            loadContactsTask.cancel(true);
+        }
         super.onStop();
     }
 
@@ -276,6 +280,14 @@ public class ContactListFragment extends FileFragment {
         return retval;
     }
 
+    private void setLoadingMessage() {
+        emptyContentHeadline.setText(R.string.file_list_loading);
+        emptyContentMessage.setText("");
+
+        emptyContentIcon.setVisibility(View.GONE);
+        emptyContentProgressBar.setVisibility(View.VISIBLE);
+    }
+
     private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
         selectAll.setChecked(checked);
         if (checked) {
@@ -461,39 +473,72 @@ public class ContactListFragment extends FileFragment {
 
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (intent.getAction().equalsIgnoreCase(FileDownloader.getDownloadFinishMessage())){
+            if (intent.getAction().equalsIgnoreCase(FileDownloader.getDownloadFinishMessage())) {
                 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
 
                 FileDataStorageManager storageManager = new FileDataStorageManager(account,
                         getContext().getContentResolver());
-                OCFile ocFile = storageManager.getFileByPath(downloadedRemotePath);
-                File file = new File(ocFile.getStoragePath());
+                ocFile = storageManager.getFileByPath(downloadedRemotePath);
+                loadContactsTask.execute();
+            }
+        }
+    }
+
+    public static class VCardComparator implements Comparator<VCard> {
+        @Override
+        public int compare(VCard o1, VCard o2) {
+            String contac1 = getDisplayName(o1);
+            String contac2 = getDisplayName(o2);
+
+            return contac1.compareToIgnoreCase(contac2);
+        }
+
+
+    }
+
+    AsyncTask loadContactsTask = new AsyncTask() {
 
+        @Override
+        protected void onPreExecute() {
+            setLoadingMessage();
+        }
+
+        @Override
+        protected Object doInBackground(Object[] params) {
+            if (!isCancelled()) {
+                File file = new File(ocFile.getStoragePath());
                 try {
-                    contactListAdapter.replaceVCards(Ezvcard.parse(file).all());
+                    vCards.addAll(Ezvcard.parse(file).all());
+                    Collections.sort(vCards, new VCardComparator());
                 } catch (IOException e) {
                     Log_OC.e(TAG, "IO Exception: " + file.getAbsolutePath());
+                    return false;
                 }
+                return true;
             }
+            return false;
         }
-    }
 
-    public static class VCardComparator implements Comparator<VCard> {
         @Override
-        public int compare(VCard o1, VCard o2) {
-            if (o1.getFormattedName() != null && o2.getFormattedName() != null) {
-                return o1.getFormattedName().getValue().compareTo(o2.getFormattedName().getValue());
-            } else {
-                if (o1.getFormattedName() == null) {
-                    return 1; // Send the contact to the end of the list
-                } else {
-                    return -1;
-                }
+        protected void onPostExecute(Object o) {
+            if (!isCancelled()) {
+                emptyListContainer.setVisibility(View.GONE);
+                contactListAdapter.replaceVCards(vCards);
             }
         }
+    };
+
+    public static String getDisplayName(VCard vCard) {
+        if (vCard.getFormattedName() != null) {
+            return vCard.getFormattedName().getValue();
+        } else if (vCard.getTelephoneNumbers() != null && vCard.getTelephoneNumbers().size() > 0) {
+            return vCard.getTelephoneNumbers().get(0).getText();
+        } else if (vCard.getEmails() != null && vCard.getEmails().size() > 0) {
+            return vCard.getEmails().get(0).getValue();
+        }
 
+        return "";
     }
-
 }
 
 class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.ContactItemViewHolder> {
@@ -562,15 +607,8 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
             } else {
                 holder.getName().setChecked(false);
             }
-            // name
-            StructuredName name = vcard.getStructuredName();
-            if (name != null) {
-                String first = (name.getGiven() == null) ? "" : name.getGiven() + " ";
-                String last = (name.getFamily() == null) ? "" : name.getFamily();
-                holder.getName().setText(first + last);
-            } else {
-                holder.getName().setText("");
-            }
+
+            holder.getName().setText(getDisplayName(vcard));
 
             // photo
             if (vcard.getPhotos().size() > 0) {
@@ -642,4 +680,5 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
 
         notifyDataSetChanged();
     }
+
 }

+ 40 - 27
src/main/res/layout/contactlist_fragment.xml

@@ -1,5 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
+<?xml version="1.0" encoding="utf-8"?><!--
   Nextcloud Android client application
 
   Copyright (C) 2017 Tobias Kaminsky
@@ -18,39 +17,53 @@
   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/>.
 -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:layout_width="match_parent"
-              android:layout_height="match_parent"
-              android:animateLayoutChanges="true"
-              android:orientation="vertical">
-
-    <android.support.v7.widget.RecyclerView
-        android:id="@+id/contactlist_recyclerview"
-        android:layout_width="match_parent"
-        android:layout_height="0dp"
-        android:layout_weight="1"
-        android:choiceMode="multipleChoice"/>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:animateLayoutChanges="true"
+    android:orientation="vertical">
 
     <LinearLayout
-        android:id="@+id/contactlist_restore_selected_container"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:background="@color/white"
-        android:orientation="vertical"
-        android:visibility="gone">
+        android:layout_height="match_parent"
+        android:orientation="vertical">
 
-        <ImageView
+        <android.support.v7.widget.RecyclerView
+            android:id="@+id/contactlist_recyclerview"
             android:layout_width="match_parent"
-            android:layout_height="1dp"
-            android:src="@drawable/uploader_list_separator"/>
+            android:layout_height="0dp"
+            android:layout_weight="1"
+            android:choiceMode="multipleChoice" />
 
-        <android.support.v7.widget.AppCompatButton
-            android:id="@+id/contactlist_restore_selected"
-            style="@style/Button.Borderless"
+        <LinearLayout
+            android:id="@+id/contactlist_restore_selected_container"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:text="@string/contaclist_restore_selected"/>
+            android:background="@color/white"
+            android:orientation="vertical"
+            android:visibility="gone">
+
+            <ImageView
+                android:layout_width="match_parent"
+                android:layout_height="1dp"
+                android:src="@drawable/uploader_list_separator" />
 
+            <android.support.v7.widget.AppCompatButton
+                android:id="@+id/contactlist_restore_selected"
+                style="@style/Button.Borderless"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/contaclist_restore_selected" />
+
+        </LinearLayout>
     </LinearLayout>
 
-</LinearLayout>
+    <RelativeLayout
+        android:id="@+id/empty_list_container"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerInParent="true">
+
+        <include layout="@layout/empty_list" />
+    </RelativeLayout>
+</RelativeLayout>