• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.settings.deviceinfo.legal;
18 
19 import android.content.Context;
20 import android.content.pm.ModuleInfo;
21 import android.content.pm.PackageManager;
22 
23 import androidx.preference.PreferenceGroup;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.internal.util.ArrayUtils;
27 import com.android.settings.core.BasePreferenceController;
28 
29 import java.io.IOException;
30 import java.util.Comparator;
31 import java.util.List;
32 
33 public class ModuleLicensesPreferenceController extends BasePreferenceController {
ModuleLicensesPreferenceController(Context context, String preferenceKey)34     public ModuleLicensesPreferenceController(Context context, String preferenceKey) {
35         super(context, preferenceKey);
36     }
37 
38     @Override
getAvailabilityStatus()39     public int getAvailabilityStatus() {
40         return AVAILABLE_UNSEARCHABLE;
41     }
42 
43     @Override
displayPreference(PreferenceScreen screen)44     public void displayPreference(PreferenceScreen screen) {
45         super.displayPreference(screen);
46 
47         PackageManager packageManager = mContext.getPackageManager();
48         List<ModuleInfo> modules = packageManager.getInstalledModules(0 /* flags */);
49         PreferenceGroup group = screen.findPreference(getPreferenceKey());
50         modules.stream()
51                 .sorted(Comparator.comparing(o -> o.getName().toString()))
52                 .filter(new Predicate(mContext))
53                 .forEach(module ->
54                         group.addPreference(
55                                 new ModuleLicensePreference(group.getContext(), module)));
56     }
57 
58     static class Predicate implements java.util.function.Predicate<ModuleInfo> {
59         private final Context mContext;
60 
Predicate(Context context)61         public Predicate(Context context) {
62             mContext = context;
63         }
64         @Override
test(ModuleInfo module)65         public boolean test(ModuleInfo module) {
66             try {
67                 return ArrayUtils.contains(
68                         ModuleLicenseProvider.getPackageAssetManager(
69                                 mContext.getPackageManager(),
70                                 module.getPackageName())
71                                         .list(""),
72                         ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME);
73             } catch (IOException | PackageManager.NameNotFoundException e) {
74                 return false;
75             }
76         }
77     }
78 }
79