1 /*
<lambda>null2  * Copyright (C) 2024 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.multiuser.widget.repository
18 
19 import android.content.Context
20 import android.content.ContextWrapper
21 import android.content.pm.UserInfo
22 import android.graphics.Bitmap
23 import android.os.UserManager
24 import com.android.internal.util.UserIcons
25 import com.android.multiuser.widget.data.model.User
26 import java.io.File
27 import java.io.FileOutputStream
28 import java.io.IOException
29 import kotlin.collections.List
30 
31 class UsersRepository(private val userManager: UserManager?) {
32     fun getUsers(context: Context): List<User> {
33         if(userManager == null) {
34             // This ensures the error layout is shown in the UI.
35             return listOf()
36         }
37         var userInfoList = userManager.getAliveUsers().filter { it.isFull() }
38         return userInfoList.map { userInfo: UserInfo -> mapUserInfoToUser(userInfo, context) }
39     }
40 
41     private fun mapUserInfoToUser(userInfo: UserInfo, context: Context): User {
42         // Get the user icon. If the user manager doesn't provide an icon,
43         // get the default icon and convert it to the appropriate size.
44         val userIcon = userManager?.getUserIcon(userInfo.id) ?: UserIcons.getDefaultUserIcon(
45             context.resources,
46             userInfo.id,
47             false
48         ).let { UserIcons.convertToBitmapAtUserIconSize(context.resources, it) }
49 
50 
51         return User(
52             id = userInfo.id,
53             name = userInfo.name ?: "",
54             creationTime = userInfo.creationTime,
55             iconPath = saveToInternalStorage(userIcon, userInfo.id, context),
56             isCurrentUser = (context.user.identifier == userInfo.id),
57             isAdmin = userManager?.isUserAdmin(userInfo.id) ?: false,
58         )
59     }
60 
61     private fun saveToInternalStorage(bitmapImage: Bitmap, id: Int, context: Context): String {
62         val cw = ContextWrapper(context)
63         // path to /data/user/{id}/com.android.multiuser/app_imageDir
64         val directory: File = cw.getDir("imageDir", Context.MODE_PRIVATE)
65         val filePath = File(directory, "$id.png")
66         var fos: FileOutputStream? = null
67         try {
68             fos = FileOutputStream(filePath)
69             bitmapImage.compress(Bitmap.CompressFormat.PNG, /*quality= */100, fos)
70         } catch (e: Exception) {
71             e.printStackTrace()
72         } finally {
73             try {
74                 fos?.close()
75             } catch (e: IOException) {
76                 e.printStackTrace()
77             }
78         }
79         return filePath.absolutePath
80     }
81 }
82