Home | History | Annotate | Download | only in model
      1 /*
      2  * Copyright (C) 2015 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 package com.android.launcher3.model;
     17 
     18 import android.content.Context;
     19 
     20 import com.android.launcher3.ItemInfo;
     21 import com.android.launcher3.compat.UserHandleCompat;
     22 import com.android.launcher3.compat.UserManagerCompat;
     23 
     24 import java.util.Comparator;
     25 
     26 /**
     27  * A comparator to arrange items based on user profiles.
     28  */
     29 public abstract class AbstractUserComparator<T extends ItemInfo> implements Comparator<T> {
     30 
     31     private final UserManagerCompat mUserManager;
     32     private final UserHandleCompat mMyUser;
     33 
     34     public AbstractUserComparator(Context context) {
     35         mUserManager = UserManagerCompat.getInstance(context);
     36         mMyUser = UserHandleCompat.myUserHandle();
     37     }
     38 
     39     @Override
     40     public int compare(T lhs, T rhs) {
     41         if (mMyUser.equals(lhs.user)) {
     42             return -1;
     43         } else {
     44             Long aUserSerial = mUserManager.getSerialNumberForUser(lhs.user);
     45             Long bUserSerial = mUserManager.getSerialNumberForUser(rhs.user);
     46             return aUserSerial.compareTo(bUserSerial);
     47         }
     48     }
     49 }
     50