Browse Source

remove/re-add for proper win support...

AndyScherzinger 8 years ago
parent
commit
8d46e8b9d2

+ 537 - 0
src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactListFragment.java

@@ -0,0 +1,537 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2017 Tobias Kaminsky
+ * Copyright (C) 2017 Nextcloud GmbH.
+ * <p>
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ * <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 Affero General Public License for more details.
+ * <p>
+ * 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.fragment.contactsbackup;
+
+import android.Manifest;
+import android.accounts.Account;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.database.Cursor;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Bundle;
+import android.os.Handler;
+import android.provider.ContactsContract;
+import android.support.annotation.NonNull;
+import android.support.design.widget.Snackbar;
+import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.CheckedTextView;
+import android.widget.ImageView;
+
+import com.evernote.android.job.JobRequest;
+import com.evernote.android.job.util.support.PersistableBundleCompat;
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.services.FileDownloader;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.services.ContactsImportJob;
+import com.owncloud.android.ui.TextDrawable;
+import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
+import com.owncloud.android.ui.events.VCardToggleEvent;
+import com.owncloud.android.ui.fragment.FileFragment;
+import com.owncloud.android.utils.BitmapUtils;
+import com.owncloud.android.utils.PermissionUtil;
+
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+import org.greenrobot.eventbus.ThreadMode;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import ezvcard.Ezvcard;
+import ezvcard.VCard;
+import ezvcard.property.StructuredName;
+
+/**
+ * This fragment shows all contacts from a file and allows to import them.
+ */
+
+public class ContactListFragment extends FileFragment {
+    public static final String TAG = ContactListFragment.class.getSimpleName();
+
+    public static final String FILE_NAME = "FILE_NAME";
+    public static final String ACCOUNT = "ACCOUNT";
+
+    public static final String CHECKED_ITEMS_ARRAY_KEY = "CHECKED_ITEMS";
+
+
+    @BindView(R.id.contactlist_recyclerview)
+    public RecyclerView recyclerView;
+
+    @BindView(R.id.contactlist_restore_selected)
+    public Button restoreContacts;
+
+    private ContactListAdapter contactListAdapter;
+
+    public static ContactListFragment newInstance(OCFile file, Account account) {
+        ContactListFragment frag = new ContactListFragment();
+        Bundle arguments = new Bundle();
+        arguments.putParcelable(FILE_NAME, file);
+        arguments.putParcelable(ACCOUNT, account);
+        frag.setArguments(arguments);
+
+        return frag;
+    }
+
+    @Override
+    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+
+        View view = inflater.inflate(R.layout.contactlist_fragment, null);
+        ButterKnife.bind(this, view);
+
+        setHasOptionsMenu(true);
+
+        ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+        contactsPreferenceActivity.getSupportActionBar().setTitle(R.string.actionbar_contacts_restore);
+        contactsPreferenceActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+        contactsPreferenceActivity.setDrawerIndicatorEnabled(false);
+
+        ArrayList<VCard> vCards = new ArrayList<>();
+
+        try {
+            OCFile ocFile = getArguments().getParcelable(FILE_NAME);
+            setFile(ocFile);
+            Account 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);
+            } else {
+                File file = new File(ocFile.getStoragePath());
+                vCards.addAll(Ezvcard.parse(file).all());
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        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) {
+            contactListAdapter = new ContactListAdapter(getContext(), vCards);
+        } else {
+            Set<Integer> checkedItems = new HashSet<>();
+            int[] itemsArray = savedInstanceState.getIntArray(CHECKED_ITEMS_ARRAY_KEY);
+            for (int i = 0; i < itemsArray.length; i++) {
+                checkedItems.add(itemsArray[i]);
+            }
+            if (checkedItems.size() > 0) {
+                onMessageEvent(new VCardToggleEvent(true));
+            }
+            contactListAdapter = new ContactListAdapter(getContext(), vCards, checkedItems);
+        }
+        recyclerView.setAdapter(contactListAdapter);
+        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
+
+        return view;
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putIntArray(CHECKED_ITEMS_ARRAY_KEY, contactListAdapter.getCheckedIntArray());
+    }
+
+
+    @Subscribe(threadMode = ThreadMode.MAIN)
+    public void onMessageEvent(VCardToggleEvent event) {
+        if (event.showRestoreButton) {
+            restoreContacts.setVisibility(View.VISIBLE);
+            restoreContacts.setBackgroundColor(getResources().getColor(R.color.primary_button_background_color));
+        } else {
+            restoreContacts.setVisibility(View.GONE);
+            restoreContacts.setBackgroundColor(getResources().getColor(R.color.standard_grey));
+        }
+
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+        contactsPreferenceActivity.setDrawerIndicatorEnabled(true);
+    }
+
+    public void onResume() {
+        super.onResume();
+        ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+        contactsPreferenceActivity.setDrawerIndicatorEnabled(false);
+    }
+
+
+    @Override
+    public void onStart() {
+        super.onStart();
+        EventBus.getDefault().register(this);
+    }
+
+    @Override
+    public void onStop() {
+        EventBus.getDefault().unregister(this);
+        super.onStop();
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        boolean retval;
+        ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        switch (item.getItemId()) {
+            case android.R.id.home:
+                contactsPreferenceActivity.onBackPressed();
+                retval = true;
+                break;
+            default:
+                retval = super.onOptionsItemSelected(item);
+                break;
+        }
+        return retval;
+    }
+
+
+    static class ContactItemViewHolder extends RecyclerView.ViewHolder {
+        private ImageView badge;
+        private CheckedTextView name;
+
+        ContactItemViewHolder(View itemView) {
+            super(itemView);
+
+            badge = (ImageView) itemView.findViewById(R.id.contactlist_item_icon);
+            name = (CheckedTextView) itemView.findViewById(R.id.contactlist_item_name);
+
+            itemView.setTag(this);
+        }
+
+        public void setVCardListener(View.OnClickListener onClickListener) {
+            itemView.setOnClickListener(onClickListener);
+        }
+
+        public ImageView getBadge() {
+            return badge;
+        }
+
+        public void setBadge(ImageView badge) {
+            this.badge = badge;
+        }
+
+        public CheckedTextView getName() {
+            return name;
+        }
+
+        public void setName(CheckedTextView name) {
+            this.name = name;
+        }
+    }
+
+    private void importContacts(ContactAccount account) {
+
+
+        PersistableBundleCompat bundle = new PersistableBundleCompat();
+        bundle.putString(ContactsImportJob.ACCOUNT_NAME, account.name);
+        bundle.putString(ContactsImportJob.ACCOUNT_TYPE, account.type);
+        bundle.putString(ContactsImportJob.VCARD_FILE_PATH, getFile().getStoragePath());
+        bundle.putIntArray(ContactsImportJob.CHECKED_ITEMS_ARRAY, contactListAdapter.getCheckedIntArray());
+
+        new JobRequest.Builder(ContactsImportJob.TAG)
+                .setExtras(bundle)
+                .setExecutionWindow(3_000L, 10_000L)
+                .setRequiresCharging(false)
+                .setPersisted(false)
+                .setUpdateCurrent(false)
+                .build()
+                .schedule();
+
+
+        Snackbar.make(recyclerView, R.string.contacts_preferences_import_scheduled, Snackbar.LENGTH_LONG).show();
+
+        Handler handler = new Handler();
+        handler.postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                getFragmentManager().popBackStack();
+            }
+        }, 2500);
+    }
+
+    private void getAccountForImport() {
+        final ArrayList<ContactAccount> accounts = new ArrayList<>();
+
+        // add local one
+        accounts.add(new ContactAccount("Local contacts", null, null));
+
+        Cursor cursor = null;
+        try {
+            cursor = getContext().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
+                    new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
+                    null,
+                    null,
+                    null);
+
+            if (cursor != null && cursor.getCount() > 0) {
+                while (cursor.moveToNext()) {
+                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
+                    String type = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
+
+                    ContactAccount account = new ContactAccount(name, name, type);
+
+                    if (!accounts.contains(account)) {
+                        accounts.add(account);
+                    }
+                }
+
+                cursor.close();
+            }
+        } catch (Exception e) {
+            Log_OC.d(TAG, e.getMessage());
+        } finally {
+            cursor.close();
+        }
+
+        if (accounts.size() == 1) {
+            importContacts(accounts.get(0));
+        } else {
+
+            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, accounts);
+            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
+            builder.setTitle(R.string.contactlist_account_chooser_title)
+                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            importContacts(accounts.get(which));
+                        }
+                    }).show();
+        }
+    }
+
+    private boolean checkAndAskForContactsWritePermission() {
+        // check permissions
+        if (!PermissionUtil.checkSelfPermission(getContext(), Manifest.permission.WRITE_CONTACTS)) {
+            PermissionUtil.requestWriteContactPermission(this);
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    @Override
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+
+        if (requestCode == PermissionUtil.PERMISSIONS_WRITE_CONTACTS) {
+            for (int index = 0; index < permissions.length; index++) {
+                if (Manifest.permission.WRITE_CONTACTS.equalsIgnoreCase(permissions[index])) {
+                    if (grantResults[index] >= 0) {
+                        getAccountForImport();
+                    } else {
+                        Snackbar.make(getView(), R.string.contactlist_no_permission, Snackbar.LENGTH_LONG).show();
+                    }
+                    break;
+                }
+            }
+        }
+    }
+
+    private class ContactAccount {
+        private String displayName;
+        private String name;
+        private String type;
+
+        ContactAccount(String displayName, String name, String type) {
+            this.displayName = displayName;
+            this.name = name;
+            this.type = type;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj instanceof ContactAccount) {
+                ContactAccount other = (ContactAccount) obj;
+                return this.name.equalsIgnoreCase(other.name) && this.type.equalsIgnoreCase(other.type);
+            } else {
+                return false;
+            }
+        }
+
+        @Override
+        public String toString() {
+            return displayName;
+        }
+    }
+}
+
+class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.ContactItemViewHolder> {
+    private List<VCard> vCards;
+    private Set<Integer> checkedVCards;
+
+    private Context context;
+
+    ContactListAdapter(Context context, List<VCard> vCards) {
+        this.vCards = vCards;
+        this.context = context;
+        this.checkedVCards = new HashSet<>();
+    }
+
+    ContactListAdapter(Context context, List<VCard> vCards,
+                       Set<Integer> checkedVCards) {
+        this.vCards = vCards;
+        this.context = context;
+        this.checkedVCards = checkedVCards;
+    }
+
+
+    public int getCheckedCount() {
+        if (checkedVCards != null) {
+            return checkedVCards.size();
+        } else {
+            return 0;
+        }
+    }
+
+    public int[] getCheckedIntArray() {
+        int[] intArray;
+        if (checkedVCards != null && checkedVCards.size() > 0) {
+            intArray = new int[checkedVCards.size()];
+            int i = 0;
+            for (int position: checkedVCards) {
+                intArray[i] = position;
+                i++;
+            }
+            return intArray;
+        } else {
+            intArray = new int[0];
+            return intArray;
+        }
+    }
+
+    @Override
+    public ContactListFragment.ContactItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        View view = LayoutInflater.from(context).inflate(R.layout.contactlist_list_item, parent, false);
+
+        return new ContactListFragment.ContactItemViewHolder(view);
+    }
+
+    @Override
+    public void onBindViewHolder(final ContactListFragment.ContactItemViewHolder holder, final int position) {
+        final VCard vcard = vCards.get(position);
+
+        if (vcard != null) {
+
+            if (checkedVCards.contains(position)) {
+                holder.getName().setChecked(true);
+            } 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("");
+            }
+
+            // photo
+            if (vcard.getPhotos().size() > 0) {
+                byte[] data = vcard.getPhotos().get(0).getData();
+
+                Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
+                RoundedBitmapDrawable drawable = BitmapUtils.bitmapToCircularBitmapDrawable(context.getResources(),
+                        thumbnail);
+
+                holder.getBadge().setImageDrawable(drawable);
+            } else {
+                try {
+                    holder.getBadge().setImageDrawable(
+                            TextDrawable.createNamedAvatar(
+                                    holder.getName().getText().toString(),
+                                    context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius)
+                            )
+                    );
+                } catch (Exception e) {
+                    holder.getBadge().setImageResource(R.drawable.ic_user);
+                }
+            }
+
+            // Checkbox
+            holder.setVCardListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    holder.getName().setChecked(!holder.getName().isChecked());
+
+                    if (holder.getName().isChecked()) {
+                        if (!checkedVCards.contains(position)) {
+                            checkedVCards.add(position);
+                        }
+                        if (checkedVCards.size() == 1) {
+                            EventBus.getDefault().post(new VCardToggleEvent(true));
+                        }
+
+
+                    } else {
+                        if (checkedVCards.contains(position)) {
+                            checkedVCards.remove(position);
+                        }
+
+                        if (checkedVCards.size() == 0) {
+                            EventBus.getDefault().post(new VCardToggleEvent(false));
+                        }
+                    }
+                }
+            });
+        }
+    }
+
+    @Override
+    public int getItemCount() {
+        return vCards.size();
+    }
+
+}

+ 462 - 0
src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactsBackupFragment.java

@@ -0,0 +1,462 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ * Copyright (C) 2017 Nextcloud GmbH.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) 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.fragment.contactsbackup;
+
+import android.Manifest;
+import android.app.DatePickerDialog;
+import android.content.DialogInterface;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.design.widget.Snackbar;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v7.widget.AppCompatButton;
+import android.support.v7.widget.SwitchCompat;
+import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.CompoundButton;
+import android.widget.DatePicker;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.evernote.android.job.JobRequest;
+import com.evernote.android.job.util.support.PersistableBundleCompat;
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.db.PreferenceManager;
+import com.owncloud.android.services.ContactsBackupJob;
+import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
+import com.owncloud.android.ui.fragment.FileFragment;
+import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.utils.PermissionUtil;
+
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Vector;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import butterknife.OnClick;
+
+import static com.owncloud.android.ui.activity.ContactsPreferenceActivity.PREFERENCE_CONTACTS_AUTOMATIC_BACKUP;
+import static com.owncloud.android.ui.activity.ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP;
+
+public class ContactsBackupFragment extends FileFragment {
+    public static final String TAG = ContactsBackupFragment.class.getSimpleName();
+
+    private SharedPreferences sharedPreferences;
+
+    @BindView(R.id.contacts_automatic_backup)
+    public SwitchCompat backupSwitch;
+
+    @BindView(R.id.contacts_header_restore)
+    public TextView contactsRestoreHeader;
+
+    @BindView(R.id.contacts_datepicker)
+    public AppCompatButton contactsDatePickerBtn;
+
+    @BindView(R.id.contacts_last_backup_timestamp)
+    public TextView lastBackup;
+
+    private Date selectedDate = null;
+    private boolean calendarPickerOpen;
+
+    private DatePickerDialog datePickerDialog;
+
+
+    private static final String KEY_CALENDAR_PICKER_OPEN = "IS_CALENDAR_PICKER_OPEN";
+    private static final String KEY_CALENDAR_DAY = "CALENDAR_DAY";
+    private static final String KEY_CALENDAR_MONTH = "CALENDAR_MONTH";
+    private static final String KEY_CALENDAR_YEAR = "CALENDAR_YEAR";
+
+
+    @Override
+    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+
+        View view = inflater.inflate(R.layout.contacts_backup_fragment, null);
+        ButterKnife.bind(this, view);
+
+        setHasOptionsMenu(true);
+
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        contactsPreferenceActivity.getSupportActionBar().setTitle(R.string.actionbar_contacts);
+        contactsPreferenceActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+
+        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(contactsPreferenceActivity);
+
+        backupSwitch.setChecked(sharedPreferences.getBoolean(PREFERENCE_CONTACTS_AUTOMATIC_BACKUP, false));
+
+        backupSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+                if (isChecked &&
+                        checkAndAskForContactsReadPermission(PermissionUtil.PERMISSIONS_READ_CONTACTS_AUTOMATIC)) {
+                    // store value
+                    setAutomaticBackup(backupSwitch, true);
+
+                    // enable daily job
+                    ContactsPreferenceActivity.startContactBackupJob(contactsPreferenceActivity.getAccount());
+                } else {
+                    setAutomaticBackup(backupSwitch, false);
+
+                    // cancel pending jobs
+                    ContactsPreferenceActivity.cancelContactBackupJob(contactsPreferenceActivity);
+                }
+            }
+        });
+
+        // display last backup
+        Long lastBackupTimestamp = sharedPreferences.getLong(PREFERENCE_CONTACTS_LAST_BACKUP, -1);
+
+        if (lastBackupTimestamp == -1) {
+            lastBackup.setText(R.string.contacts_preference_backup_never);
+        } else {
+            lastBackup.setText(DisplayUtils.getRelativeTimestamp(contactsPreferenceActivity, lastBackupTimestamp));
+        }
+
+        if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_CALENDAR_PICKER_OPEN, false)) {
+            if (savedInstanceState.getInt(KEY_CALENDAR_YEAR, -1) != -1 &&
+                    savedInstanceState.getInt(KEY_CALENDAR_MONTH, -1) != -1 &&
+                    savedInstanceState.getInt(KEY_CALENDAR_DAY, -1) != -1) {
+                selectedDate = new Date(savedInstanceState.getInt(KEY_CALENDAR_YEAR),
+                        savedInstanceState.getInt(KEY_CALENDAR_MONTH), savedInstanceState.getInt(KEY_CALENDAR_DAY));
+            }
+            calendarPickerOpen = true;
+        }
+
+
+        return view;
+    }
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+
+
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+
+        if (calendarPickerOpen) {
+            if (selectedDate != null) {
+                openDate(selectedDate);
+            } else {
+                openDate(null);
+            }
+        }
+
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        String backupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR;
+        OCFile backupFolder = contactsPreferenceActivity.getStorageManager().getFileByPath(backupFolderString);
+
+        Vector<OCFile> backupFiles = contactsPreferenceActivity.getStorageManager().getFolderContent(backupFolder,
+                false);
+
+        if (backupFiles == null || backupFiles.size() == 0 ||
+                sharedPreferences.getLong(PREFERENCE_CONTACTS_LAST_BACKUP, -1) == -1) {
+            contactsRestoreHeader.setVisibility(View.GONE);
+            contactsDatePickerBtn.setVisibility(View.GONE);
+        } else {
+            contactsRestoreHeader.setVisibility(View.VISIBLE);
+            contactsDatePickerBtn.setVisibility(View.VISIBLE);
+        }
+    }
+
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        boolean retval;
+        switch (item.getItemId()) {
+            case android.R.id.home:
+                if (contactsPreferenceActivity.isDrawerOpen()) {
+                    contactsPreferenceActivity.closeDrawer();
+                } else {
+                    contactsPreferenceActivity.openDrawer();
+                }
+                retval = true;
+                break;
+
+            default:
+                retval = super.onOptionsItemSelected(item);
+                break;
+        }
+        return retval;
+    }
+
+    @Override
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+
+        if (requestCode == PermissionUtil.PERMISSIONS_READ_CONTACTS_AUTOMATIC) {
+            for (int index = 0; index < permissions.length; index++) {
+                if (Manifest.permission.READ_CONTACTS.equalsIgnoreCase(permissions[index])) {
+                    if (grantResults[index] >= 0) {
+                        setAutomaticBackup(backupSwitch, true);
+                    } else {
+                        setAutomaticBackup(backupSwitch, false);
+                    }
+
+                    break;
+                }
+            }
+        }
+
+        if (requestCode == PermissionUtil.PERMISSIONS_READ_CONTACTS_MANUALLY) {
+            for (int index = 0; index < permissions.length; index++) {
+                if (Manifest.permission.READ_CONTACTS.equalsIgnoreCase(permissions[index])) {
+                    if (grantResults[index] >= 0) {
+                        startContactsBackupJob();
+                    }
+
+                    break;
+                }
+            }
+        }
+    }
+
+    @OnClick(R.id.contacts_backup_now)
+    public void backupContacts() {
+        if (checkAndAskForContactsReadPermission(PermissionUtil.PERMISSIONS_READ_CONTACTS_MANUALLY)) {
+            startContactsBackupJob();
+        }
+    }
+
+    private void startContactsBackupJob() {
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        PersistableBundleCompat bundle = new PersistableBundleCompat();
+        bundle.putString(ContactsBackupJob.ACCOUNT, contactsPreferenceActivity.getAccount().name);
+        bundle.putBoolean(ContactsBackupJob.FORCE, true);
+
+        new JobRequest.Builder(ContactsBackupJob.TAG)
+                .setExtras(bundle)
+                .setExecutionWindow(3_000L, 10_000L)
+                .setRequiresCharging(false)
+                .setPersisted(false)
+                .setUpdateCurrent(false)
+                .build()
+                .schedule();
+
+        Snackbar.make(getView().findViewById(R.id.contacts_linear_layout), R.string.contacts_preferences_backup_scheduled,
+                Snackbar.LENGTH_LONG).show();
+    }
+
+    private void setAutomaticBackup(SwitchCompat backupSwitch, boolean bool) {
+        backupSwitch.setChecked(bool);
+        SharedPreferences.Editor editor = sharedPreferences.edit();
+        editor.putBoolean(PREFERENCE_CONTACTS_AUTOMATIC_BACKUP, bool);
+        editor.apply();
+    }
+
+    private boolean checkAndAskForContactsReadPermission(final int permission) {
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+        // check permissions
+        if ((PermissionUtil.checkSelfPermission(contactsPreferenceActivity, Manifest.permission.READ_CONTACTS))) {
+            return true;
+        } else {
+            // Check if we should show an explanation
+            if (PermissionUtil.shouldShowRequestPermissionRationale(contactsPreferenceActivity,
+                    android.Manifest.permission.READ_CONTACTS)) {
+                // Show explanation to the user and then request permission
+                Snackbar snackbar = Snackbar.make(getView().findViewById(R.id.contacts_linear_layout),
+                        R.string.contacts_read_permission,
+                        Snackbar.LENGTH_INDEFINITE)
+                        .setAction(R.string.common_ok, new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                PermissionUtil.requestReadContactPermission(contactsPreferenceActivity, permission);
+                            }
+                        });
+
+                DisplayUtils.colorSnackbar(contactsPreferenceActivity, snackbar);
+
+                snackbar.show();
+
+                return false;
+            } else {
+                // No explanation needed, request the permission.
+                PermissionUtil.requestReadContactPermission(contactsPreferenceActivity, permission);
+
+                return false;
+            }
+        }
+    }
+
+    @OnClick(R.id.contacts_datepicker)
+    public void openCleanDate() {
+        openDate(null);
+    }
+
+    public void openDate(@Nullable Date savedDate) {
+        final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
+
+
+        String backupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR;
+        OCFile backupFolder = contactsPreferenceActivity.getStorageManager().getFileByPath(backupFolderString);
+
+        Vector<OCFile> backupFiles = contactsPreferenceActivity.getStorageManager().getFolderContent(backupFolder,
+                false);
+
+        Collections.sort(backupFiles, new Comparator<OCFile>() {
+            @Override
+            public int compare(OCFile o1, OCFile o2) {
+                if (o1.getModificationTimestamp() == o2.getModificationTimestamp()) {
+                    return 0;
+                }
+
+                if (o1.getModificationTimestamp() > o2.getModificationTimestamp()) {
+                    return 1;
+                } else {
+                    return -1;
+                }
+            }
+        });
+
+        Calendar cal = Calendar.getInstance();
+        int year;
+        int month;
+        int day;
+
+        if (savedDate == null) {
+            year = cal.get(Calendar.YEAR);
+            month = cal.get(Calendar.MONTH) + 1;
+            day = cal.get(Calendar.DAY_OF_MONTH);
+        } else {
+            year = savedDate.getYear();
+            month = savedDate.getMonth();
+            day = savedDate.getDay();
+        }
+
+
+        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
+            @Override
+            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
+                selectedDate = new Date(year, month, dayOfMonth);
+
+                String backupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR;
+                OCFile backupFolder = contactsPreferenceActivity.getStorageManager().getFileByPath(backupFolderString);
+                Vector<OCFile> backupFiles = contactsPreferenceActivity.getStorageManager().getFolderContent(
+                        backupFolder, false);
+
+                // find file with modification with date and time between 00:00 and 23:59
+                // if more than one file exists, take oldest
+                Calendar date = Calendar.getInstance();
+                date.set(year, month, dayOfMonth);
+
+                // start
+                date.set(Calendar.HOUR, 0);
+                date.set(Calendar.MINUTE, 0);
+                date.set(Calendar.SECOND, 1);
+                date.set(Calendar.MILLISECOND, 0);
+                date.set(Calendar.AM_PM, Calendar.AM);
+                Long start = date.getTimeInMillis();
+
+                // end
+                date.set(Calendar.HOUR, 23);
+                date.set(Calendar.MINUTE, 59);
+                date.set(Calendar.SECOND, 59);
+                Long end = date.getTimeInMillis();
+
+                OCFile backupToRestore = null;
+
+                for (OCFile file : backupFiles) {
+                    if (start < file.getModificationTimestamp() && end > file.getModificationTimestamp()) {
+                        if (backupToRestore == null) {
+                            backupToRestore = file;
+                        } else if (backupToRestore.getModificationTimestamp() < file.getModificationTimestamp()) {
+                            backupToRestore = file;
+                        }
+                    }
+                }
+
+                if (backupToRestore != null) {
+                    Fragment contactListFragment = ContactListFragment.newInstance(backupToRestore,
+                            contactsPreferenceActivity.getAccount());
+
+                    FragmentTransaction transaction = contactsPreferenceActivity.getSupportFragmentManager().
+                            beginTransaction();
+                    transaction.replace(R.id.frame_container, contactListFragment);
+                    transaction.addToBackStack(null);
+                    transaction.commit();
+                } else {
+                    Toast.makeText(contactsPreferenceActivity, R.string.contacts_preferences_no_file_found,
+                            Toast.LENGTH_SHORT).show();
+                }
+            }
+        };
+
+        if (backupFiles.size() > 0 && backupFiles.lastElement() != null) {
+            datePickerDialog = new DatePickerDialog(contactsPreferenceActivity,
+                    dateSetListener, year, month, day);
+            datePickerDialog.getDatePicker().setMaxDate(backupFiles.lastElement().getModificationTimestamp());
+            datePickerDialog.getDatePicker().setMinDate(backupFiles.firstElement().getModificationTimestamp());
+
+            datePickerDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+                @Override
+                public void onDismiss(DialogInterface dialog) {
+                    selectedDate = null;
+                }
+            });
+
+            datePickerDialog.show();
+        } else {
+            Toast.makeText(contactsPreferenceActivity, R.string.contacts_preferences_something_strange_happened,
+                    Toast.LENGTH_SHORT).show();
+        }
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        if (datePickerDialog != null) {
+            datePickerDialog.dismiss();
+        }
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        if (datePickerDialog != null) {
+            outState.putBoolean(KEY_CALENDAR_PICKER_OPEN, datePickerDialog.isShowing());
+
+            if (datePickerDialog.isShowing()) {
+                outState.putInt(KEY_CALENDAR_DAY, datePickerDialog.getDatePicker().getDayOfMonth());
+                outState.putInt(KEY_CALENDAR_MONTH, datePickerDialog.getDatePicker().getMonth());
+                outState.putInt(KEY_CALENDAR_YEAR, datePickerDialog.getDatePicker().getYear());
+            }
+
+        }
+    }
+
+}