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
17package com.android.settings.deviceinfo.storage;
18
19import android.content.Context;
20import android.content.pm.UserInfo;
21import android.graphics.drawable.Drawable;
22import android.os.UserManager;
23import android.util.Log;
24import android.util.SparseArray;
25
26import com.android.internal.util.Preconditions;
27import com.android.settings.Utils;
28import com.android.settings.utils.AsyncLoader;
29
30/**
31 * Fetches a user icon as a loader using a given icon loading lambda.
32 */
33public class UserIconLoader extends AsyncLoader<SparseArray<Drawable>> {
34    private FetchUserIconTask mTask;
35
36    /**
37     * Task to load all user icons.
38     */
39    public interface FetchUserIconTask {
40        SparseArray<Drawable> getUserIcons();
41    }
42
43    /**
44     * Handle the output of this task.
45     */
46    public interface UserIconHandler {
47        void handleUserIcons(SparseArray<Drawable> fetchedIcons);
48    }
49
50    public UserIconLoader(Context context, FetchUserIconTask task) {
51        super(context);
52        mTask = Preconditions.checkNotNull(task);
53    }
54
55    @Override
56    public SparseArray<Drawable> loadInBackground() {
57        return mTask.getUserIcons();
58    }
59
60    @Override
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     */
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