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.content.Intent; 20 import android.os.Build; 21 import android.os.UserHandle; 22 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 myUserHandle()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 fromUser(UserHandle user)43 static UserHandleCompat fromUser(UserHandle user) { 44 if (user == null) { 45 return null; 46 } else { 47 return new UserHandleCompat(user); 48 } 49 } 50 getUser()51 UserHandle getUser() { 52 return mUser; 53 } 54 55 @Override toString()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 equals(Object other)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 hashCode()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 */ addToIntent(Intent intent, String name)90 public void addToIntent(Intent intent, String name) { 91 if (Utilities.isLmpOrAbove() && mUser != null) { 92 intent.putExtra(name, mUser); 93 } 94 } 95 } 96