1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Merge: Improve presentation of accounts in import screen

This commit is contained in:
Tom Hacohen 2017-04-03 22:09:36 +01:00 committed by GitHub
commit d5ba48e59c
3 changed files with 44 additions and 35 deletions

View File

@ -8,6 +8,7 @@ import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.ContactsContract;
import android.util.Log;
import com.etesync.syncadapter.App;
@ -21,19 +22,15 @@ import java.util.List;
*/
public class CalendarAccount {
public String accountName;
public List<LocalCalendar> calendars = new ArrayList<>();
private Account account;
private List<LocalCalendar> calendars = new ArrayList<>();
private static final String[] CAL_COLS = new String[] {
Calendars._ID, Calendars.DELETED, Calendars.NAME, Calendars.CALENDAR_DISPLAY_NAME,
Calendars.ACCOUNT_NAME, Calendars.ACCOUNT_TYPE, Calendars.OWNER_ACCOUNT,
Calendars.VISIBLE, Calendars.CALENDAR_TIME_ZONE };
private static final String[] CAL_COLS = new String[]{
Calendars.ACCOUNT_NAME, Calendars.ACCOUNT_TYPE,
Calendars.DELETED, Calendars.NAME};
private static final String[] CAL_ID_COLS = new String[] { Events._ID };
private static final String CAL_ID_WHERE = Events.CALENDAR_ID + "=?";
protected CalendarAccount(String accountName) {
this.accountName = accountName;
protected CalendarAccount(Account account) {
this.account = account;
}
// Load all available calendars.
@ -47,7 +44,8 @@ public class CalendarAccount {
Cursor cur;
try {
cur = resolver.query(Calendars.CONTENT_URI,
CAL_COLS, null, null, Calendars.ACCOUNT_NAME + " ASC");
CAL_COLS, null, null,
ContactsContract.RawContacts.ACCOUNT_NAME + " ASC, " + ContactsContract.RawContacts.ACCOUNT_TYPE);
} catch (Exception except) {
App.log.warning("Calendar provider is missing columns, continuing anyway");
cur = resolver.query(Calendars.CONTENT_URI, null, null, null, null);
@ -56,35 +54,29 @@ public class CalendarAccount {
List<CalendarAccount> calendarAccounts = new ArrayList<>(cur.getCount());
CalendarAccount calendarAccount = null;
ContentProviderClient contentProviderClient = resolver.acquireContentProviderClient(CalendarContract.CONTENT_URI);
while (cur.moveToNext()) {
if (getLong(cur, Calendars.DELETED) != 0)
continue;
String accountName = getString(cur, Calendars.ACCOUNT_NAME);
if (calendarAccount == null || !calendarAccount.accountName.equals(accountName)) {
calendarAccount = new CalendarAccount(accountName);
String accountType = getString(cur, Calendars.ACCOUNT_TYPE);
if (calendarAccount == null ||
!calendarAccount.getAccountName().equals(accountName) ||
!calendarAccount.getAccountType().equals(accountType)) {
calendarAccount = new CalendarAccount(new Account(accountName, accountType));
calendarAccounts.add(calendarAccount);
}
long id = getLong(cur, Calendars._ID);
if (id == -1) {
continue;
}
final String[] args = new String[] { String.valueOf(id) };
Cursor eventsCur = resolver.query(Events.CONTENT_URI, CAL_ID_COLS, CAL_ID_WHERE, args, null);
Account account = new Account(accountName, getString(cur, Calendars.ACCOUNT_TYPE));
try {
LocalCalendar localCalendar = LocalCalendar.findByName(account, contentProviderClient,
LocalCalendar localCalendar = LocalCalendar.findByName(calendarAccount.getAccount(),
contentProviderClient,
LocalCalendar.Factory.INSTANCE, getString(cur, Calendars.NAME));
if (localCalendar != null) calendarAccount.calendars.add(localCalendar);
} catch (Exception ex) {
ex.printStackTrace();
}
eventsCur.close();
}
contentProviderClient.release();
cur.close();
@ -113,8 +105,24 @@ public class CalendarAccount {
return provider == null;
}
public String getAccountName() {
return account.name;
}
public String getAccountType() {
return account.type;
}
public List<LocalCalendar> getCalendars() {
return calendars;
}
public Account getAccount() {
return account;
}
@Override
public String toString() {
return accountName;
return account.toString();
}
}

View File

@ -74,7 +74,7 @@ public class LocalCalendarImportFragment extends ListFragment {
listCalendar.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView aExpandableListView, View aView, int groupPosition, int childPosition, long aL) {
new ImportEvents().execute(calendarAccountList.get(groupPosition).calendars.get(childPosition));
new ImportEvents().execute(calendarAccountList.get(groupPosition).getCalendars().get(childPosition));
return false;
}
});
@ -102,7 +102,7 @@ public class LocalCalendarImportFragment extends ListFragment {
@Override
public Object getChild(int groupPosition, int childPosititon) {
return calendarAccounts.get(groupPosition).calendars
return calendarAccounts.get(groupPosition).getCalendars()
.get(childPosititon).getDisplayName();
}
@ -137,13 +137,13 @@ public class LocalCalendarImportFragment extends ListFragment {
@Override
public int getChildrenCount(int groupPosition) {
return calendarAccounts.get(groupPosition).calendars
return calendarAccounts.get(groupPosition).getCalendars()
.size();
}
@Override
public Object getGroup(int groupPosition) {
return calendarAccounts.get(groupPosition).toString();
return calendarAccounts.get(groupPosition);
}
@Override
@ -159,7 +159,7 @@ public class LocalCalendarImportFragment extends ListFragment {
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
CalendarAccount calendarAccount = (CalendarAccount) getGroup(groupPosition);
GroupViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
@ -176,9 +176,9 @@ public class LocalCalendarImportFragment extends ListFragment {
.findViewById(R.id.description);
convertView.setTag(viewHolder);
}
viewHolder.titleTextView.setText(headerTitle);
viewHolder.descriptionTextView.setText(headerTitle);
viewHolder.titleTextView.setText(calendarAccount.getAccountName());
viewHolder.descriptionTextView.setText(calendarAccount.getAccountType());
return convertView;
}

View File

@ -86,7 +86,8 @@ public class LocalContactImportFragment extends Fragment {
try {
cursor = provider.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE}
, null, null, ContactsContract.RawContacts.ACCOUNT_NAME + " ASC");
, null, null,
ContactsContract.RawContacts.ACCOUNT_NAME + " ASC, " + ContactsContract.RawContacts.ACCOUNT_TYPE);
} catch (Exception except) {
Log.w(TAG, "Calendar provider is missing columns, continuing anyway");