Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 16e9247d authored by Daniel Nishi's avatar Daniel Nishi Committed by Android (Google) Code Review
Browse files

Merge "Badge the icons for profile storage screen."

parents c0189802 3414a690
Loading
Loading
Loading
Loading
+15 −10
Original line number Diff line number Diff line
@@ -20,22 +20,27 @@
    android:title="@string/storage_settings">
    <com.android.settings.deviceinfo.StorageItemPreference
        android:key="pref_photos_videos"
        android:title="@string/storage_photos_videos">
    </com.android.settings.deviceinfo.StorageItemPreference>
        android:title="@string/storage_photos_videos"
        android:icon="@drawable/ic_photo_library_vd_theme_24"
        android:order="2" />
    <com.android.settings.deviceinfo.StorageItemPreference
        android:key="pref_music_audio"
        android:title="@string/storage_music_audio">
    </com.android.settings.deviceinfo.StorageItemPreference>
        android:title="@string/storage_music_audio"
        android:icon="@drawable/ic_music_note_vd_theme_24"
        android:order="3" />
    <com.android.settings.deviceinfo.StorageItemPreference
        android:key="pref_games"
        android:title="@string/storage_games">
    </com.android.settings.deviceinfo.StorageItemPreference>
        android:title="@string/storage_games"
        android:icon="@drawable/ic_videogame_vd_theme_24"
        android:order="4" />
    <com.android.settings.deviceinfo.StorageItemPreference
        android:key="pref_other_apps"
        android:title="@string/storage_other_apps">
    </com.android.settings.deviceinfo.StorageItemPreference>
        android:title="@string/storage_other_apps"
        android:icon="@drawable/ic_apps_vd_theme_24"
        android:order="5" />
    <com.android.settings.deviceinfo.StorageItemPreference
        android:key="pref_files"
        android:title="@string/storage_files">
    </com.android.settings.deviceinfo.StorageItemPreference>
        android:title="@string/storage_files"
        android:icon="@drawable/ic_folder_vd_theme_24"
        android:order="6" />
</PreferenceScreen>
+1 −2
Original line number Diff line number Diff line
@@ -73,8 +73,7 @@ public class StorageProfileFragment extends DashboardFragment

        mPreferenceController.setVolume(mVolume);
        mUserId = args.getInt(USER_ID_EXTRA, UserHandle.myUserId());
        // TODO(b/36224168): Use the user id to appropriately badge the preferences.
        mPreferenceController.setUserId(mUserId);
        mPreferenceController.setUserId(UserHandle.of(mUserId));
    }

    @Override
+32 −2
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.storage.VolumeInfo;
@@ -165,8 +168,35 @@ public class StorageItemPreferenceController extends PreferenceController {
    /**
     * Sets the user id for which this preference controller is handling.
     */
    public void setUserId(int userId) {
        mUserId = userId;
    public void setUserId(UserHandle userHandle) {
        mUserId = userHandle.getIdentifier();

        PackageManager pm = mContext.getPackageManager();
        badgePreference(pm, userHandle, mPhotoPreference);
        badgePreference(pm, userHandle, mAudioPreference);
        badgePreference(pm, userHandle, mGamePreference);
        badgePreference(pm, userHandle, mAppPreference);
        badgePreference(pm, userHandle, mSystemPreference);
        badgePreference(pm, userHandle, mFilePreference);
    }

    private void badgePreference(PackageManager pm, UserHandle userHandle, Preference preference) {
        if (preference != null) {
            Drawable currentIcon = preference.getIcon();
            // Sigh... Applying the badge to the icon clobbers the tint on the base drawable.
            // For some reason, re-applying it here means the tint remains.
            currentIcon = applyTint(mContext, currentIcon);
            preference.setIcon(pm.getUserBadgedIcon(currentIcon, userHandle));
        }
    }

    private static Drawable applyTint(Context context, Drawable icon) {
        TypedArray array =
                context.obtainStyledAttributes(new int[]{android.R.attr.colorControlNormal});
        icon = icon.mutate();
        icon.setTint(array.getColor(0, 0));
        array.recycle();
        return icon;
    }

    @Override
+40 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.os.storage.VolumeInfo;
import android.support.v7.preference.PreferenceScreen;
@@ -243,4 +244,43 @@ public class StorageItemPreferenceControllerTest {
        assertThat(system.getSummary().toString()).isEqualTo("16.00KB");
        assertThat(files.getSummary().toString()).isEqualTo("5.00KB");
    }

    @Test
    public void settingUserIdAppliesNewIcons() {
        StorageItemPreference audio = spy(new StorageItemPreference(mContext));
        audio.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        StorageItemPreference image = spy(new StorageItemPreference(mContext));
        image.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        StorageItemPreference games = spy(new StorageItemPreference(mContext));
        games.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        StorageItemPreference apps = spy(new StorageItemPreference(mContext));
        apps.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        StorageItemPreference system = spy(new StorageItemPreference(mContext));
        system.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        StorageItemPreference files = spy(new StorageItemPreference(mContext));
        files.setIcon(R.drawable.ic_photo_library_vd_theme_24);
        PreferenceScreen screen = mock(PreferenceScreen.class);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.PHOTO_KEY))).thenReturn(image);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.OTHER_APPS_KEY))).thenReturn(apps);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.SYSTEM_KEY))).thenReturn(system);
        when(screen.findPreference(
                eq(StorageItemPreferenceController.FILES_KEY))).thenReturn(files);
        mController.displayPreference(screen);

        mController.setUserId(new UserHandle(10));

        verify(audio, times(2)).setIcon(any(Drawable.class));
        verify(image, times(2)).setIcon(any(Drawable.class));
        verify(games, times(2)).setIcon(any(Drawable.class));
        verify(apps, times(2)).setIcon(any(Drawable.class));
        verify(system, times(2)).setIcon(any(Drawable.class));
        verify(files, times(2)).setIcon(any(Drawable.class));
    }
}
 No newline at end of file