1 /*
2  * Copyright (C) 2020 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.google.android.car.kitchensink.systemfeatures;
17 
18 import android.annotation.Nullable;
19 import android.content.pm.FeatureInfo;
20 import android.content.pm.PackageManager;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ArrayAdapter;
27 import android.widget.ListView;
28 
29 import androidx.annotation.NonNull;
30 import androidx.fragment.app.Fragment;
31 
32 import com.google.android.car.kitchensink.R;
33 
34 import java.util.Arrays;
35 import java.util.Comparator;
36 import java.util.Objects;
37 
38 /**
39  * Shows system features as available by PackageManager
40  */
41 public class SystemFeaturesFragment extends Fragment {
42     private static final String TAG = "CAR.SYSFEATURES.KS";
43 
44     private ListView mSysFeaturesList;
45     private PackageManager mPackageManager;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         mPackageManager = Objects.requireNonNull(
50                 getContext().getPackageManager());
51 
52         super.onCreate(savedInstanceState);
53     }
54 
55     @Nullable
56     @Override
onCreateView( @onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)57     public View onCreateView(
58             @NonNull LayoutInflater inflater,
59             @Nullable ViewGroup container,
60             @Nullable Bundle savedInstanceState) {
61         mSysFeaturesList = (ListView) inflater.inflate(R.layout.system_feature_fragment,
62                 container, false);
63         return mSysFeaturesList;
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         super.onResume();
69         refresh();
70     }
71 
72     @Override
onPause()73     public void onPause() {
74         super.onPause();
75     }
76 
refresh()77     private void refresh() {
78         final FeatureInfo[] features = mPackageManager.getSystemAvailableFeatures();
79         if (features != null) {
80             final String[] descriptions = Arrays.stream(features)
81                 .filter(fi -> fi != null && fi.name != null)
82                 .sorted(Comparator.<FeatureInfo, String>comparing(fi -> fi.name)
83                                   .thenComparing(fi -> fi.version))
84                 .map(fi -> String.format("%s (v=%d)", fi.name, fi.version))
85                 .toArray(String[]::new);
86             mSysFeaturesList.setAdapter(new ArrayAdapter<>(getContext(),
87                     android.R.layout.simple_list_item_1, descriptions));
88         } else {
89             Log.e(TAG, "no features available on this device!");
90         }
91     }
92 }
93