1
0
mirror of https://github.com/etesync/android synced 2024-11-16 04:49:06 +00:00

Import - Use ResultFragment in all import framgnets

This commit is contained in:
Tal Hacohen 2017-03-30 16:00:21 +03:00
parent 56aba7c669
commit 1e9e055924
3 changed files with 56 additions and 88 deletions

View File

@ -81,9 +81,7 @@ public class ImportFragment extends DialogFragment {
} else {
ImportResult data = new ImportResult();
data.e = new Exception(getString(R.string.import_permission_required));
getFragmentManager().beginTransaction()
.add(ResultFragment.newInstance(data), null)
.commitAllowingStateLoss();
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
dismissAllowingStateLoss();
}
@ -162,9 +160,8 @@ public class ImportFragment extends DialogFragment {
} catch (ActivityNotFoundException e) {
ImportResult data = new ImportResult();
data.e = new Exception("Failed to open file chooser.\nPlease install one.");
getFragmentManager().beginTransaction()
.add(ResultFragment.newInstance(data), null)
.commitAllowingStateLoss();
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
dismissAllowingStateLoss();
}
@ -196,9 +193,7 @@ public class ImportFragment extends DialogFragment {
}
public void loadFinished(ImportResult data) {
getFragmentManager().beginTransaction()
.add(ResultFragment.newInstance(data), null)
.commitAllowingStateLoss();
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
dismissAllowingStateLoss();

View File

@ -34,7 +34,6 @@ public class LocalCalendarImportFragment extends ListFragment {
private Account account;
private CollectionInfo info;
private ResultFragment.OnImportCallback importCallback;
public static LocalCalendarImportFragment newInstance(Account account, CollectionInfo info) {
LocalCalendarImportFragment frag = new LocalCalendarImportFragment();
@ -66,33 +65,6 @@ public class LocalCalendarImportFragment extends ListFragment {
importAccount();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (ResultFragment.OnImportCallback) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement MyInterface ");
}
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (ResultFragment.OnImportCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
}
}
protected void importAccount() {
final List<CalendarAccount> calendarAccountList = CalendarAccount.loadAll(getContext().getContentResolver());
@ -204,7 +176,7 @@ public class LocalCalendarImportFragment extends ListFragment {
}
}
protected class ImportEvents extends AsyncTask<LocalCalendar, Integer, Boolean> {
protected class ImportEvents extends AsyncTask<LocalCalendar, Integer, ResultFragment.ImportResult> {
ProgressDialog progressDialog;
@Override
@ -221,7 +193,7 @@ public class LocalCalendarImportFragment extends ListFragment {
}
@Override
protected Boolean doInBackground(LocalCalendar... calendars) {
protected ResultFragment.ImportResult doInBackground(LocalCalendar... calendars) {
return importEvents(calendars[0]);
}
@ -232,34 +204,47 @@ public class LocalCalendarImportFragment extends ListFragment {
}
@Override
protected void onPostExecute(Boolean result) {
protected void onPostExecute(ResultFragment.ImportResult result) {
progressDialog.dismiss();
importCallback.onImportResult(new ResultFragment.ImportResult());
((ResultFragment.OnImportCallback) getActivity()).onImportResult(result);
}
private boolean importEvents(LocalCalendar fromCalendar) {
private ResultFragment.ImportResult importEvents(LocalCalendar fromCalendar) {
ResultFragment.ImportResult result = new ResultFragment.ImportResult();
try {
LocalCalendar localCalendar = LocalCalendar.findByName(account,
getContext().getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI),
LocalCalendar.Factory.INSTANCE, info.url);
LocalEvent[] localEvents = fromCalendar.getAll();
progressDialog.setMax(localEvents.length);
int total = localEvents.length;
progressDialog.setMax(total);
result.total = total;
int progress = 0;
for (LocalEvent currentLocalEvent : localEvents) {
Event event = currentLocalEvent.getEvent();
try {
LocalEvent localEvent = new LocalEvent(localCalendar, event, event.uid, null);
localEvent.addAsDirty();
LocalEvent localEvent = event.uid == null ? null :
localCalendar.getByUid(event.uid);
if (localEvent != null) {
localEvent.updateAsDirty(event);
result.updated++;
} else {
localEvent = new LocalEvent(localCalendar, event, event.uid, null);
localEvent.addAsDirty();
result.added++;
}
} catch (CalendarStorageException e) {
e.printStackTrace();
}
publishProgress(++progress);
}
return true;
} catch (Exception aE) {
aE.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
result.e = e;
}
return result;
}
}
}

View File

@ -1,10 +1,8 @@
package com.etesync.syncadapter.ui.importlocal;
import android.accounts.Account;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentProviderClient;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
@ -27,6 +25,7 @@ import java.util.ArrayList;
import java.util.List;
import at.bitfire.vcard4android.Contact;
import at.bitfire.vcard4android.ContactsStorageException;
import static android.content.ContentValues.TAG;
import static com.etesync.syncadapter.Constants.KEY_ACCOUNT;
@ -36,7 +35,6 @@ public class LocalContactImportFragment extends Fragment {
private Account account;
private CollectionInfo info;
private ResultFragment.OnImportCallback importCallback;
public static LocalContactImportFragment newInstance(Account account, CollectionInfo info) {
LocalContactImportFragment frag = new LocalContactImportFragment();
@ -76,33 +74,6 @@ public class LocalContactImportFragment extends Fragment {
importAccount();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (ResultFragment.OnImportCallback) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement MyInterface ");
}
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (ResultFragment.OnImportCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
}
}
protected void importAccount() {
ContentProviderClient provider = getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI);
Cursor cursor;
@ -138,7 +109,7 @@ public class LocalContactImportFragment extends Fragment {
}));
}
protected class ImportContacts extends AsyncTask<LocalAddressBook, Integer, Boolean> {
protected class ImportContacts extends AsyncTask<LocalAddressBook, Integer, ResultFragment.ImportResult> {
ProgressDialog progressDialog;
@Override
@ -155,7 +126,7 @@ public class LocalContactImportFragment extends Fragment {
}
@Override
protected Boolean doInBackground(LocalAddressBook... addressBooks) {
protected ResultFragment.ImportResult doInBackground(LocalAddressBook... addressBooks) {
return importContacts(addressBooks[0]);
}
@ -166,29 +137,46 @@ public class LocalContactImportFragment extends Fragment {
}
@Override
protected void onPostExecute(Boolean result) {
protected void onPostExecute(ResultFragment.ImportResult result) {
progressDialog.dismiss();
importCallback.onImportResult(new ResultFragment.ImportResult());
((ResultFragment.OnImportCallback) getActivity()).onImportResult(result);
}
private boolean importContacts(LocalAddressBook localAddressBook) {
private ResultFragment.ImportResult importContacts(LocalAddressBook localAddressBook) {
ResultFragment.ImportResult result = new ResultFragment.ImportResult();
try {
LocalAddressBook addressBook = new LocalAddressBook(account,
getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI));
LocalContact[] localContacts = localAddressBook.getAll();
int total = localContacts.length;
progressDialog.setMax(total);
result.total = total;
int progress = 0;
for (LocalContact currentLocalContact : localContacts) {
Contact contact = currentLocalContact.getContact();
(new LocalContact(addressBook, contact, contact.uid, null)).createAsDirty();
try {
LocalContact localContact = contact.uid == null ?
null : (LocalContact) addressBook.getByUid(contact.uid);
if (localContact != null) {
localContact.updateAsDirty(contact);
result.updated++;
} else {
localContact = new LocalContact(addressBook, contact, contact.uid, null);
localContact.createAsDirty();
result.added++;
}
} catch (ContactsStorageException e) {
e.printStackTrace();
result.e = e;
}
publishProgress(++progress);
}
return true;
} catch (Exception aE) {
aE.printStackTrace();
return false;
} catch (Exception e) {
result.e = e;
}
return result;
}
}