Merge "Badge texts correctly for accessibility in Settings" into lmp-dev
diff --git a/src/com/android/settings/DataUsageSummary.java b/src/com/android/settings/DataUsageSummary.java
index c8691e1..ec52128 100644
--- a/src/com/android/settings/DataUsageSummary.java
+++ b/src/com/android/settings/DataUsageSummary.java
@@ -819,14 +819,21 @@
 
         View title = null;
         if (detail.detailLabels != null) {
-            for (CharSequence label : detail.detailLabels) {
+            final int n = detail.detailLabels.length;
+            for (int i = 0; i < n; ++i) {
+                CharSequence label = detail.detailLabels[i];
+                CharSequence contentDescription = detail.detailContentDescriptions[i];
                 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
-                ((TextView) title.findViewById(R.id.app_title)).setText(label);
+                TextView appTitle = (TextView) title.findViewById(R.id.app_title);
+                appTitle.setText(label);
+                appTitle.setContentDescription(contentDescription);
                 mAppTitles.addView(title);
             }
         } else {
             title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
-            ((TextView) title.findViewById(R.id.app_title)).setText(detail.label);
+            TextView appTitle = (TextView) title.findViewById(R.id.app_title);
+            appTitle.setText(detail.label);
+            appTitle.setContentDescription(detail.contentDescription);
             mAppTitles.addView(title);
         }
 
@@ -2222,6 +2229,7 @@
             if (detail != null) {
                 icon.setImageDrawable(detail.icon);
                 title.setText(detail.label);
+                title.setContentDescription(detail.contentDescription);
             } else {
                 icon.setImageDrawable(null);
                 title.setText(null);
diff --git a/src/com/android/settings/fuelgauge/PowerGaugePreference.java b/src/com/android/settings/fuelgauge/PowerGaugePreference.java
index 990b654..8157b5d 100644
--- a/src/com/android/settings/fuelgauge/PowerGaugePreference.java
+++ b/src/com/android/settings/fuelgauge/PowerGaugePreference.java
@@ -34,12 +34,15 @@
     private BatteryEntry mInfo;
     private int mProgress;
     private CharSequence mProgressText;
+    private final String mContentDescription;
 
-    public PowerGaugePreference(Context context, Drawable icon, BatteryEntry info) {
+    public PowerGaugePreference(Context context, Drawable icon, String contentDescription,
+            BatteryEntry info) {
         super(context);
         setLayoutResource(R.layout.preference_app_percentage);
         setIcon(icon != null ? icon : new ColorDrawable(0));
         mInfo = info;
+        mContentDescription = contentDescription;
     }
 
     public void setPercent(double percentOfMax, double percentOfTotal) {
@@ -62,5 +65,10 @@
 
         final TextView text1 = (TextView) view.findViewById(android.R.id.text1);
         text1.setText(mProgressText);
+
+        if (mContentDescription != null) {
+            final TextView titleView = (TextView) view.findViewById(android.R.id.title);
+            titleView.setContentDescription(mContentDescription);
+        }
     }
 }
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 3ce2ac0..e6534eb 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -21,6 +21,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.graphics.drawable.Drawable;
 import android.os.BatteryStats;
 import android.os.Bundle;
 import android.os.Handler;
@@ -294,8 +295,14 @@
                 }
                 final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
                 final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
+                final Drawable badgedIcon = mUm.getBadgedDrawableForUser(entry.getIcon(),
+                        userHandle);
+                // TODO: type of this will be replaced by CharSequence (see
+                // https://b.corp.google.com/issue?id=16401636 )
+                final String contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
+                        userHandle);
                 final PowerGaugePreference pref = new PowerGaugePreference(getActivity(),
-                        mUm.getBadgedDrawableForUser(entry.getIcon(), userHandle), entry);
+                        badgedIcon, contentDescription, entry);
 
                 final double percentOfMax = (sipper.value * 100) / mStatsHelper.getMaxPower();
                 sipper.percent = percentOfTotal;
diff --git a/src/com/android/settings/location/RecentLocationApps.java b/src/com/android/settings/location/RecentLocationApps.java
index 8e1ec05..5506181 100644
--- a/src/com/android/settings/location/RecentLocationApps.java
+++ b/src/com/android/settings/location/RecentLocationApps.java
@@ -22,6 +22,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.IPackageManager;
+import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Process;
@@ -30,6 +31,8 @@
 import android.os.UserManager;
 import android.preference.Preference;
 import android.util.Log;
+import android.view.View;
+import android.widget.TextView;
 
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
@@ -74,12 +77,35 @@
         }
     }
 
-    private Preference createRecentLocationEntry(
+    /**
+     * Subclass of {@link Preference} to intercept views and allow content description to be set on
+     * them for accessibility purposes.
+     */
+    private static class AccessiblePreference extends Preference {
+        public String mContentDescription;
+
+        public AccessiblePreference(Context context, String contentDescription) {
+            super(context);
+            mContentDescription = contentDescription;
+        }
+
+        @Override
+        protected void onBindView(View view) {
+            super.onBindView(view);
+            if (mContentDescription != null) {
+                final TextView titleView = (TextView) view.findViewById(android.R.id.title);
+                titleView.setContentDescription(mContentDescription);
+            }
+        }
+    }
+
+    private AccessiblePreference createRecentLocationEntry(
             Drawable icon,
             CharSequence label,
             boolean isHighBattery,
+            String contentDescription,
             Preference.OnPreferenceClickListener listener) {
-        Preference pref = new Preference(mActivity);
+        AccessiblePreference pref = new AccessiblePreference(mActivity, contentDescription);
         pref.setIcon(icon);
         pref.setTitle(label);
         if (isHighBattery) {
@@ -172,16 +198,20 @@
         int uid = ops.getUid();
         int userId = UserHandle.getUserId(uid);
 
-        Preference preference = null;
+        AccessiblePreference preference = null;
         try {
             IPackageManager ipm = AppGlobals.getPackageManager();
             ApplicationInfo appInfo =
                     ipm.getApplicationInfo(packageName, PackageManager.GET_META_DATA, userId);
+            Resources res = mActivity.getResources();
 
-            Drawable icon = um.getBadgedDrawableForUser(
-                    mPackageManager.getApplicationIcon(appInfo), new UserHandle(userId));
+            final UserHandle userHandle = new UserHandle(userId);
+            Drawable appIcon = mPackageManager.getApplicationIcon(appInfo);
+            Drawable icon = um.getBadgedDrawableForUser(appIcon, userHandle);
+            CharSequence appLabel = mPackageManager.getApplicationLabel(appInfo);
+            String badgedAppLabel = um.getBadgedLabelForUser(appLabel.toString(), userHandle);
             preference = createRecentLocationEntry(icon,
-                    mPackageManager.getApplicationLabel(appInfo), highBattery,
+                    appLabel, highBattery, badgedAppLabel,
                     new PackageEntryClickedListener(packageName));
         } catch (RemoteException e) {
             Log.w(TAG, "Error while retrieving application info", e);
diff --git a/src/com/android/settings/net/UidDetail.java b/src/com/android/settings/net/UidDetail.java
index fd44d47..0b14254 100644
--- a/src/com/android/settings/net/UidDetail.java
+++ b/src/com/android/settings/net/UidDetail.java
@@ -20,6 +20,8 @@
 
 public class UidDetail {
     public CharSequence label;
+    public CharSequence contentDescription;
     public CharSequence[] detailLabels;
+    public CharSequence[] detailContentDescriptions;
     public Drawable icon;
 }
diff --git a/src/com/android/settings/net/UidDetailProvider.java b/src/com/android/settings/net/UidDetailProvider.java
index cd101c9..b933025 100644
--- a/src/com/android/settings/net/UidDetailProvider.java
+++ b/src/com/android/settings/net/UidDetailProvider.java
@@ -148,27 +148,30 @@
         final String[] packageNames = pm.getPackagesForUid(uid);
         final int length = packageNames != null ? packageNames.length : 0;
         try {
+            final UserHandle userHandle = new UserHandle(UserHandle.getUserId(uid));
             if (length == 1) {
                 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
                 detail.label = info.loadLabel(pm).toString();
-                detail.icon = um.getBadgedDrawableForUser(info.loadIcon(pm),
-                        new UserHandle(UserHandle.getUserId(uid)));
+                detail.icon = um.getBadgedDrawableForUser(info.loadIcon(pm), userHandle);
             } else if (length > 1) {
                 detail.detailLabels = new CharSequence[length];
+                detail.detailContentDescriptions = new CharSequence[length];
                 for (int i = 0; i < length; i++) {
                     final String packageName = packageNames[i];
                     final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
                     final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
 
                     detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
+                    detail.detailContentDescriptions[i] = um.getBadgedLabelForUser(
+                            detail.detailLabels[i], userHandle);
                     if (packageInfo.sharedUserLabel != 0) {
                         detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
                                 packageInfo.applicationInfo).toString();
-                        detail.icon = um.getBadgedDrawableForUser(appInfo.loadIcon(pm),
-                                new UserHandle(UserHandle.getUserId(uid)));
+                        detail.icon = um.getBadgedDrawableForUser(appInfo.loadIcon(pm), userHandle);
                     }
                 }
             }
+            detail.contentDescription = um.getBadgedLabelForUser(detail.label, userHandle);
         } catch (NameNotFoundException e) {
         }