mirror of
https://github.com/etesync/android
synced 2025-06-18 14:08:51 +00:00
Import: Resolved account name and icon when possible.
We assume the name of the account == package name, which is the best guess we have. Android doesn't have a way to get account name/icon at the moment.
This commit is contained in:
parent
e4fc23eb70
commit
9486719cbb
@ -0,0 +1,54 @@
|
|||||||
|
package com.etesync.syncadapter.ui.importlocal;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
|
import com.etesync.syncadapter.R;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
|
class AccountResolver {
|
||||||
|
private Context context;
|
||||||
|
private HashMap<String, AccountInfo> cache;
|
||||||
|
|
||||||
|
public AccountResolver(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
this.cache = new LinkedHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccountInfo resolve(String accountName) {
|
||||||
|
// Hardcoded swaps for known accounts:
|
||||||
|
if (accountName.equals("com.google")) {
|
||||||
|
accountName = "com.google.android.googlequicksearchbox";
|
||||||
|
}
|
||||||
|
AccountInfo ret = cache.get(accountName);
|
||||||
|
if (ret == null) {
|
||||||
|
try {
|
||||||
|
PackageManager packageManager = context.getPackageManager();
|
||||||
|
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(accountName, 0);
|
||||||
|
String name = (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo).toString() : accountName);
|
||||||
|
Drawable icon = context.getPackageManager().getApplicationIcon(accountName);
|
||||||
|
ret = new AccountInfo(name, icon);
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
ret = new AccountInfo(accountName, ContextCompat.getDrawable(context, R.drawable.ic_account_dark));
|
||||||
|
}
|
||||||
|
cache.put(accountName, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AccountInfo {
|
||||||
|
final String name;
|
||||||
|
final Drawable icon;
|
||||||
|
|
||||||
|
AccountInfo(String name, Drawable icon) {
|
||||||
|
this.name = name;
|
||||||
|
this.icon = icon;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.BaseExpandableListAdapter;
|
import android.widget.BaseExpandableListAdapter;
|
||||||
import android.widget.ExpandableListView;
|
import android.widget.ExpandableListView;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.etesync.syncadapter.R;
|
import com.etesync.syncadapter.R;
|
||||||
@ -85,10 +86,12 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private List<CalendarAccount> calendarAccounts;
|
private List<CalendarAccount> calendarAccounts;
|
||||||
|
private AccountResolver accountResolver;
|
||||||
|
|
||||||
public ExpandableListAdapter(Context context, List<CalendarAccount> calendarAccounts) {
|
public ExpandableListAdapter(Context context, List<CalendarAccount> calendarAccounts) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.calendarAccounts = calendarAccounts;
|
this.calendarAccounts = calendarAccounts;
|
||||||
|
this.accountResolver = new AccountResolver(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ChildViewHolder {
|
private class ChildViewHolder {
|
||||||
@ -98,6 +101,7 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
private class GroupViewHolder {
|
private class GroupViewHolder {
|
||||||
TextView titleTextView;
|
TextView titleTextView;
|
||||||
TextView descriptionTextView;
|
TextView descriptionTextView;
|
||||||
|
ImageView iconImageView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -174,11 +178,14 @@ public class LocalCalendarImportFragment extends ListFragment {
|
|||||||
.findViewById(R.id.title);
|
.findViewById(R.id.title);
|
||||||
viewHolder.descriptionTextView = (TextView) convertView
|
viewHolder.descriptionTextView = (TextView) convertView
|
||||||
.findViewById(R.id.description);
|
.findViewById(R.id.description);
|
||||||
|
viewHolder.iconImageView = (ImageView) convertView.findViewById(R.id.icon);
|
||||||
convertView.setTag(viewHolder);
|
convertView.setTag(viewHolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
viewHolder.titleTextView.setText(calendarAccount.getAccountName());
|
viewHolder.titleTextView.setText(calendarAccount.getAccountName());
|
||||||
viewHolder.descriptionTextView.setText(calendarAccount.getAccountType());
|
AccountResolver.AccountInfo accountInfo = accountResolver.resolve(calendarAccount.getAccountType());
|
||||||
|
viewHolder.descriptionTextView.setText(accountInfo.name);
|
||||||
|
viewHolder.iconImageView.setImageDrawable(accountInfo.icon);
|
||||||
|
|
||||||
return convertView;
|
return convertView;
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
recyclerView.setAdapter(new ImportContactAdapter(localAddressBooks, new OnAccountSelected() {
|
recyclerView.setAdapter(new ImportContactAdapter(getContext(), localAddressBooks, new OnAccountSelected() {
|
||||||
@Override
|
@Override
|
||||||
public void accountSelected(int index) {
|
public void accountSelected(int index) {
|
||||||
new ImportContacts().execute(localAddressBooks.get(index));
|
new ImportContacts().execute(localAddressBooks.get(index));
|
||||||
@ -192,6 +192,7 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
|
|
||||||
private List<LocalAddressBook> mAddressBooks;
|
private List<LocalAddressBook> mAddressBooks;
|
||||||
private OnAccountSelected mOnAccountSelected;
|
private OnAccountSelected mOnAccountSelected;
|
||||||
|
private AccountResolver accountResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide a reference to the type of views that you are using (custom ViewHolder)
|
* Provide a reference to the type of views that you are using (custom ViewHolder)
|
||||||
@ -221,9 +222,10 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
*
|
*
|
||||||
* @param addressBooks containing the data to populate views to be used by RecyclerView.
|
* @param addressBooks containing the data to populate views to be used by RecyclerView.
|
||||||
*/
|
*/
|
||||||
public ImportContactAdapter(List<LocalAddressBook> addressBooks, OnAccountSelected onAccountSelected) {
|
public ImportContactAdapter(Context context, List<LocalAddressBook> addressBooks, OnAccountSelected onAccountSelected) {
|
||||||
mAddressBooks = addressBooks;
|
mAddressBooks = addressBooks;
|
||||||
mOnAccountSelected = onAccountSelected;
|
mOnAccountSelected = onAccountSelected;
|
||||||
|
accountResolver = new AccountResolver(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new views (invoked by the layout manager)
|
// Create new views (invoked by the layout manager)
|
||||||
@ -239,7 +241,9 @@ public class LocalContactImportFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
|
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
|
||||||
viewHolder.titleTextView.setText(mAddressBooks.get(position).account.name);
|
viewHolder.titleTextView.setText(mAddressBooks.get(position).account.name);
|
||||||
viewHolder.descTextView.setText(mAddressBooks.get(position).account.type);
|
AccountResolver.AccountInfo accountInfo = accountResolver.resolve(mAddressBooks.get(position).account.type);
|
||||||
|
viewHolder.descTextView.setText(accountInfo.name);
|
||||||
|
viewHolder.iconImageView.setImageDrawable(accountInfo.icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user