1 /*
2  * Copyright (C) 2024 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.intentresolver.chooser;
18 
19 
20 import android.content.Context;
21 
22 import java.text.Collator;
23 import java.util.Comparator;
24 
25 /**
26  * Sort intents alphabetically based on display label.
27  */
28 public class DisplayResolveInfoAzInfoComparator implements Comparator<DisplayResolveInfo> {
29     Comparator<DisplayResolveInfo> mComparator;
DisplayResolveInfoAzInfoComparator(Context context)30     public DisplayResolveInfoAzInfoComparator(Context context) {
31         Collator collator = Collator
32                 .getInstance(context.getResources().getConfiguration().locale);
33         // Adding two stage comparator, first stage compares using displayLabel, next stage
34         //  compares using resolveInfo.userHandle
35         mComparator = Comparator.comparing(DisplayResolveInfo::getDisplayLabel, collator)
36                 .thenComparingInt(target -> target.getResolveInfo().userHandle.getIdentifier());
37     }
38 
39     @Override
compare( DisplayResolveInfo lhsp, DisplayResolveInfo rhsp)40     public int compare(
41             DisplayResolveInfo lhsp, DisplayResolveInfo rhsp) {
42         return mComparator.compare(lhsp, rhsp);
43     }
44 }
45