UserHandleCompat.java revision 7066003b2032a49ae5e59dab9b706259bdeb7e6e
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.annotation.TargetApi; 20import android.content.Intent; 21import android.os.Build; 22import android.os.UserHandle; 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 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 36 public static UserHandleCompat myUserHandle() { 37 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 38 return new UserHandleCompat(android.os.Process.myUserHandle()); 39 } else { 40 return new UserHandleCompat(); 41 } 42 } 43 44 static UserHandleCompat fromUser(UserHandle user) { 45 if (user == null) { 46 return null; 47 } else { 48 return new UserHandleCompat(user); 49 } 50 } 51 52 UserHandle getUser() { 53 return mUser; 54 } 55 56 @Override 57 public String toString() { 58 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 59 return mUser.toString(); 60 } else { 61 return ""; 62 } 63 } 64 65 @Override 66 public boolean equals(Object other) { 67 if (!(other instanceof UserHandleCompat)) { 68 return false; 69 } 70 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 71 return mUser.equals(((UserHandleCompat) other).mUser); 72 } else { 73 return true; 74 } 75 } 76 77 @Override 78 public int hashCode() { 79 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_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 */ 91 public void addToIntent(Intent intent, String name) { 92 if (Utilities.isLmpOrAbove() && mUser != null) { 93 intent.putExtra(name, mUser); 94 } 95 } 96} 97