From 9486719cbbb9d03ef6a78d0b1cb7f3dde52e4d01 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 3 Apr 2017 22:36:52 +0100 Subject: [PATCH] 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. --- .../ui/importlocal/AccountResolver.java | 54 +++++++++++++++++++ .../LocalCalendarImportFragment.java | 9 +++- .../LocalContactImportFragment.java | 10 ++-- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/etesync/syncadapter/ui/importlocal/AccountResolver.java diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/AccountResolver.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/AccountResolver.java new file mode 100644 index 00000000..43e662cb --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/AccountResolver.java @@ -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 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; + } + } +} diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java index 0630eb90..2c008a12 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java @@ -12,6 +12,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; +import android.widget.ImageView; import android.widget.TextView; import com.etesync.syncadapter.R; @@ -85,10 +86,12 @@ public class LocalCalendarImportFragment extends ListFragment { private Context context; private List calendarAccounts; + private AccountResolver accountResolver; public ExpandableListAdapter(Context context, List calendarAccounts) { this.context = context; this.calendarAccounts = calendarAccounts; + this.accountResolver = new AccountResolver(context); } private class ChildViewHolder { @@ -98,6 +101,7 @@ public class LocalCalendarImportFragment extends ListFragment { private class GroupViewHolder { TextView titleTextView; TextView descriptionTextView; + ImageView iconImageView; } @Override @@ -174,11 +178,14 @@ public class LocalCalendarImportFragment extends ListFragment { .findViewById(R.id.title); viewHolder.descriptionTextView = (TextView) convertView .findViewById(R.id.description); + viewHolder.iconImageView = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(viewHolder); } 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; } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalContactImportFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalContactImportFragment.java index 08b41d27..dfa42309 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalContactImportFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalContactImportFragment.java @@ -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 public void accountSelected(int index) { new ImportContacts().execute(localAddressBooks.get(index)); @@ -192,6 +192,7 @@ public class LocalContactImportFragment extends Fragment { private List mAddressBooks; private OnAccountSelected mOnAccountSelected; + private AccountResolver accountResolver; /** * 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. */ - public ImportContactAdapter(List addressBooks, OnAccountSelected onAccountSelected) { + public ImportContactAdapter(Context context, List addressBooks, OnAccountSelected onAccountSelected) { mAddressBooks = addressBooks; mOnAccountSelected = onAccountSelected; + accountResolver = new AccountResolver(context); } // Create new views (invoked by the layout manager) @@ -239,7 +241,9 @@ public class LocalContactImportFragment extends Fragment { @Override public void onBindViewHolder(ViewHolder viewHolder, final int position) { 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