Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 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.util;
     18 
     19 import android.content.ComponentName;
     20 
     21 import com.android.launcher3.ItemInfo;
     22 import com.android.launcher3.LauncherSettings.Favorites;
     23 import com.android.launcher3.ShortcutInfo;
     24 import com.android.launcher3.compat.UserHandleCompat;
     25 import com.android.launcher3.shortcuts.ShortcutKey;
     26 
     27 import java.util.HashSet;
     28 
     29 /**
     30  * A utility class to check for {@link ItemInfo}
     31  */
     32 public abstract class ItemInfoMatcher {
     33 
     34     public abstract boolean matches(ItemInfo info, ComponentName cn);
     35 
     36     public static ItemInfoMatcher ofComponents(
     37             final HashSet<ComponentName> components, final UserHandleCompat user) {
     38         return new ItemInfoMatcher() {
     39             @Override
     40             public boolean matches(ItemInfo info, ComponentName cn) {
     41                 return components.contains(cn) && info.user.equals(user);
     42             }
     43         };
     44     }
     45 
     46     public static ItemInfoMatcher ofPackages(
     47             final HashSet<String> packageNames, final UserHandleCompat user) {
     48         return new ItemInfoMatcher() {
     49             @Override
     50             public boolean matches(ItemInfo info, ComponentName cn) {
     51                 return packageNames.contains(cn.getPackageName()) && info.user.equals(user);
     52             }
     53         };
     54     }
     55 
     56     public static ItemInfoMatcher ofShortcutKeys(final HashSet<ShortcutKey> keys) {
     57         return new ItemInfoMatcher() {
     58             @Override
     59             public boolean matches(ItemInfo info, ComponentName cn) {
     60                 return info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
     61                         keys.contains(ShortcutKey.fromShortcutInfo((ShortcutInfo) info));
     62             }
     63         };
     64     }
     65 }
     66