mirror of
https://github.com/etesync/android
synced 2025-07-01 20:32:39 +00:00
Import - Use ResultFragment in all import framgnets
This commit is contained in:
parent
56aba7c669
commit
1e9e055924
@ -81,9 +81,7 @@ public class ImportFragment extends DialogFragment {
|
|||||||
} else {
|
} else {
|
||||||
ImportResult data = new ImportResult();
|
ImportResult data = new ImportResult();
|
||||||
data.e = new Exception(getString(R.string.import_permission_required));
|
data.e = new Exception(getString(R.string.import_permission_required));
|
||||||
getFragmentManager().beginTransaction()
|
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
|
||||||
.add(ResultFragment.newInstance(data), null)
|
|
||||||
.commitAllowingStateLoss();
|
|
||||||
|
|
||||||
dismissAllowingStateLoss();
|
dismissAllowingStateLoss();
|
||||||
}
|
}
|
||||||
@ -162,9 +160,8 @@ public class ImportFragment extends DialogFragment {
|
|||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
ImportResult data = new ImportResult();
|
ImportResult data = new ImportResult();
|
||||||
data.e = new Exception("Failed to open file chooser.\nPlease install one.");
|
data.e = new Exception("Failed to open file chooser.\nPlease install one.");
|
||||||
getFragmentManager().beginTransaction()
|
|
||||||
.add(ResultFragment.newInstance(data), null)
|
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
|
||||||
.commitAllowingStateLoss();
|
|
||||||
|
|
||||||
dismissAllowingStateLoss();
|
dismissAllowingStateLoss();
|
||||||
}
|
}
|
||||||
@ -196,9 +193,7 @@ public class ImportFragment extends DialogFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadFinished(ImportResult data) {
|
public void loadFinished(ImportResult data) {
|
||||||
getFragmentManager().beginTransaction()
|
((ResultFragment.OnImportCallback) getActivity()).onImportResult(data);
|
||||||
.add(ResultFragment.newInstance(data), null)
|
|
||||||
.commitAllowingStateLoss();
|
|
||||||
|
|
||||||
dismissAllowingStateLoss();
|
dismissAllowingStateLoss();
|
||||||
|
|
||||||
|
@ -34,7 +34,6 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
|
|
||||||
private Account account;
|
private Account account;
|
||||||
private CollectionInfo info;
|
private CollectionInfo info;
|
||||||
private ResultFragment.OnImportCallback importCallback;
|
|
||||||
|
|
||||||
public static LocalCalendarImportFragment newInstance(Account account, CollectionInfo info) {
|
public static LocalCalendarImportFragment newInstance(Account account, CollectionInfo info) {
|
||||||
LocalCalendarImportFragment frag = new LocalCalendarImportFragment();
|
LocalCalendarImportFragment frag = new LocalCalendarImportFragment();
|
||||||
@ -66,33 +65,6 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
importAccount();
|
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() {
|
protected void importAccount() {
|
||||||
final List<CalendarAccount> calendarAccountList = CalendarAccount.loadAll(getContext().getContentResolver());
|
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;
|
ProgressDialog progressDialog;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -221,7 +193,7 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(LocalCalendar... calendars) {
|
protected ResultFragment.ImportResult doInBackground(LocalCalendar... calendars) {
|
||||||
return importEvents(calendars[0]);
|
return importEvents(calendars[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,34 +204,47 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Boolean result) {
|
protected void onPostExecute(ResultFragment.ImportResult result) {
|
||||||
progressDialog.dismiss();
|
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 {
|
try {
|
||||||
LocalCalendar localCalendar = LocalCalendar.findByName(account,
|
LocalCalendar localCalendar = LocalCalendar.findByName(account,
|
||||||
getContext().getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI),
|
getContext().getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI),
|
||||||
LocalCalendar.Factory.INSTANCE, info.url);
|
LocalCalendar.Factory.INSTANCE, info.url);
|
||||||
LocalEvent[] localEvents = fromCalendar.getAll();
|
LocalEvent[] localEvents = fromCalendar.getAll();
|
||||||
progressDialog.setMax(localEvents.length);
|
int total = localEvents.length;
|
||||||
|
progressDialog.setMax(total);
|
||||||
|
result.total = total;
|
||||||
int progress = 0;
|
int progress = 0;
|
||||||
for (LocalEvent currentLocalEvent : localEvents) {
|
for (LocalEvent currentLocalEvent : localEvents) {
|
||||||
Event event = currentLocalEvent.getEvent();
|
Event event = currentLocalEvent.getEvent();
|
||||||
try {
|
try {
|
||||||
LocalEvent localEvent = new LocalEvent(localCalendar, event, event.uid, null);
|
LocalEvent localEvent = event.uid == null ? null :
|
||||||
localEvent.addAsDirty();
|
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) {
|
} catch (CalendarStorageException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
}
|
}
|
||||||
publishProgress(++progress);
|
publishProgress(++progress);
|
||||||
}
|
}
|
||||||
return true;
|
} catch (Exception e) {
|
||||||
} catch (Exception aE) {
|
e.printStackTrace();
|
||||||
aE.printStackTrace();
|
result.e = e;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package com.etesync.syncadapter.ui.importlocal;
|
package com.etesync.syncadapter.ui.importlocal;
|
||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.ContentProviderClient;
|
import android.content.ContentProviderClient;
|
||||||
import android.content.Context;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -27,6 +25,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import at.bitfire.vcard4android.Contact;
|
import at.bitfire.vcard4android.Contact;
|
||||||
|
import at.bitfire.vcard4android.ContactsStorageException;
|
||||||
|
|
||||||
import static android.content.ContentValues.TAG;
|
import static android.content.ContentValues.TAG;
|
||||||
import static com.etesync.syncadapter.Constants.KEY_ACCOUNT;
|
import static com.etesync.syncadapter.Constants.KEY_ACCOUNT;
|
||||||
@ -36,7 +35,6 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
|
|
||||||
private Account account;
|
private Account account;
|
||||||
private CollectionInfo info;
|
private CollectionInfo info;
|
||||||
private ResultFragment.OnImportCallback importCallback;
|
|
||||||
|
|
||||||
public static LocalContactImportFragment newInstance(Account account, CollectionInfo info) {
|
public static LocalContactImportFragment newInstance(Account account, CollectionInfo info) {
|
||||||
LocalContactImportFragment frag = new LocalContactImportFragment();
|
LocalContactImportFragment frag = new LocalContactImportFragment();
|
||||||
@ -76,33 +74,6 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
importAccount();
|
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() {
|
protected void importAccount() {
|
||||||
ContentProviderClient provider = getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI);
|
ContentProviderClient provider = getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI);
|
||||||
Cursor cursor;
|
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;
|
ProgressDialog progressDialog;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -155,7 +126,7 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(LocalAddressBook... addressBooks) {
|
protected ResultFragment.ImportResult doInBackground(LocalAddressBook... addressBooks) {
|
||||||
return importContacts(addressBooks[0]);
|
return importContacts(addressBooks[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,29 +137,46 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Boolean result) {
|
protected void onPostExecute(ResultFragment.ImportResult result) {
|
||||||
progressDialog.dismiss();
|
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 {
|
try {
|
||||||
LocalAddressBook addressBook = new LocalAddressBook(account,
|
LocalAddressBook addressBook = new LocalAddressBook(account,
|
||||||
getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI));
|
getContext().getContentResolver().acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI));
|
||||||
LocalContact[] localContacts = localAddressBook.getAll();
|
LocalContact[] localContacts = localAddressBook.getAll();
|
||||||
int total = localContacts.length;
|
int total = localContacts.length;
|
||||||
progressDialog.setMax(total);
|
progressDialog.setMax(total);
|
||||||
|
result.total = total;
|
||||||
int progress = 0;
|
int progress = 0;
|
||||||
for (LocalContact currentLocalContact : localContacts) {
|
for (LocalContact currentLocalContact : localContacts) {
|
||||||
Contact contact = currentLocalContact.getContact();
|
Contact contact = currentLocalContact.getContact();
|
||||||
(new LocalContact(addressBook, contact, contact.uid, null)).createAsDirty();
|
(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);
|
publishProgress(++progress);
|
||||||
}
|
}
|
||||||
return true;
|
} catch (Exception e) {
|
||||||
} catch (Exception aE) {
|
result.e = e;
|
||||||
aE.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user