1/*
2 * Copyright (C) 2014 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.launcher3.compat;
18
19import android.content.Intent;
20import android.os.Build;
21import android.os.UserHandle;
22
23import com.android.launcher3.Utilities;
24
25public class UserHandleCompat {
26    private UserHandle mUser;
27
28    private UserHandleCompat(UserHandle user) {
29        mUser = user;
30    }
31
32    private UserHandleCompat() {
33    }
34
35    public static UserHandleCompat myUserHandle() {
36        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
37            return new UserHandleCompat(android.os.Process.myUserHandle());
38        } else {
39            return new UserHandleCompat();
40        }
41    }
42
43    static UserHandleCompat fromUser(UserHandle user) {
44        if (user == null) {
45            return null;
46        } else {
47            return new UserHandleCompat(user);
48        }
49    }
50
51    UserHandle getUser() {
52        return mUser;
53    }
54
55    @Override
56    public String toString() {
57        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
58            return mUser.toString();
59        } else {
60            return "";
61        }
62    }
63
64    @Override
65    public boolean equals(Object other) {
66        if (!(other instanceof UserHandleCompat)) {
67            return false;
68        }
69        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
70            return mUser.equals(((UserHandleCompat) other).mUser);
71        } else {
72            return true;
73        }
74    }
75
76    @Override
77    public int hashCode() {
78        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
79            return mUser.hashCode();
80        } else {
81            return 0;
82        }
83    }
84
85    /**
86     * Adds {@link UserHandle} to the intent in for L or above.
87     * Pre-L the launcher doesn't support showing apps for multiple
88     * profiles so this is a no-op.
89     */
90    public void addToIntent(Intent intent, String name) {
91        if (Utilities.isLmpOrAbove() && mUser != null) {
92            intent.putExtra(name, mUser);
93        }
94    }
95}
96