1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.deviceinfo.storage; 18 19 import android.content.Context; 20 import android.content.pm.UserInfo; 21 import android.graphics.drawable.Drawable; 22 import android.os.UserManager; 23 import android.util.SparseArray; 24 25 import com.android.internal.util.Preconditions; 26 import com.android.settings.Utils; 27 import com.android.settingslib.utils.AsyncLoaderCompat; 28 29 /** 30 * Fetches a user icon as a loader using a given icon loading lambda. 31 */ 32 public class UserIconLoader extends AsyncLoaderCompat<SparseArray<Drawable>> { 33 private FetchUserIconTask mTask; 34 35 /** 36 * Task to load all user icons. 37 */ 38 public interface FetchUserIconTask { getUserIcons()39 SparseArray<Drawable> getUserIcons(); 40 } 41 42 /** 43 * Handle the output of this task. 44 */ 45 public interface UserIconHandler { handleUserIcons(SparseArray<Drawable> fetchedIcons)46 void handleUserIcons(SparseArray<Drawable> fetchedIcons); 47 } 48 UserIconLoader(Context context, FetchUserIconTask task)49 public UserIconLoader(Context context, FetchUserIconTask task) { 50 super(context); 51 mTask = Preconditions.checkNotNull(task); 52 } 53 54 @Override loadInBackground()55 public SparseArray<Drawable> loadInBackground() { 56 return mTask.getUserIcons(); 57 } 58 59 @Override onDiscardResult(SparseArray<Drawable> result)60 protected void onDiscardResult(SparseArray<Drawable> result) {} 61 62 /** 63 * Loads the user icons using a given context. This returns a {@link SparseArray} which maps 64 * user ids to their user icons. 65 */ loadUserIconsWithContext(Context context)66 public static SparseArray<Drawable> loadUserIconsWithContext(Context context) { 67 SparseArray<Drawable> value = new SparseArray<>(); 68 UserManager um = context.getSystemService(UserManager.class); 69 for (UserInfo userInfo : um.getUsers()) { 70 value.put(userInfo.id, Utils.getUserIcon(context, um, userInfo)); 71 } 72 return value; 73 } 74 } 75