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