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 refresh()72 private void refresh() { 73 final FeatureInfo[] features = mPackageManager.getSystemAvailableFeatures(); 74 if (features != null) { 75 final String[] descriptions = Arrays.stream(features) 76 .filter(fi -> fi != null && fi.name != null) 77 .sorted(Comparator.<FeatureInfo, String>comparing(fi -> fi.name) 78 .thenComparing(fi -> fi.version)) 79 .map(fi -> String.format("%s (v=%d)", fi.name, fi.version)) 80 .toArray(String[]::new); 81 mSysFeaturesList.setAdapter(new ArrayAdapter<>(getContext(), 82 android.R.layout.simple_list_item_1, descriptions)); 83 } else { 84 Log.e(TAG, "no features available on this device!"); 85 } 86 } 87 } 88