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.Log;
24 import android.util.SparseArray;
25 
26 import com.android.internal.util.Preconditions;
27 import com.android.settings.Utils;
28 import com.android.settings.utils.AsyncLoader;
29 
30 /**
31  * Fetches a user icon as a loader using a given icon loading lambda.
32  */
33 public class UserIconLoader extends AsyncLoader<SparseArray<Drawable>> {
34     private FetchUserIconTask mTask;
35 
36     /**
37      * Task to load all user icons.
38      */
39     public interface FetchUserIconTask {
getUserIcons()40         SparseArray<Drawable> getUserIcons();
41     }
42 
43     /**
44      * Handle the output of this task.
45      */
46     public interface UserIconHandler {
handleUserIcons(SparseArray<Drawable> fetchedIcons)47         void handleUserIcons(SparseArray<Drawable> fetchedIcons);
48     }
49 
UserIconLoader(Context context, FetchUserIconTask task)50     public UserIconLoader(Context context, FetchUserIconTask task) {
51         super(context);
52         mTask = Preconditions.checkNotNull(task);
53     }
54 
55     @Override
loadInBackground()56     public SparseArray<Drawable> loadInBackground() {
57         return mTask.getUserIcons();
58     }
59 
60     @Override
onDiscardResult(SparseArray<Drawable> result)61     protected void onDiscardResult(SparseArray<Drawable> result) {}
62 
63     /**
64      * Loads the user icons using a given context. This returns a {@link SparseArray} which maps
65      * user ids to their user icons.
66      */
loadUserIconsWithContext(Context context)67     public static SparseArray<Drawable> loadUserIconsWithContext(Context context) {
68         SparseArray<Drawable> value = new SparseArray<>();
69         UserManager um = context.getSystemService(UserManager.class);
70         for (UserInfo userInfo : um.getUsers()) {
71             value.put(userInfo.id, Utils.getUserIcon(context, um, userInfo));
72         }
73         return value;
74     }
75 }
76