1 /*
2  * Copyright (C) 2018 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.car.settings.applications;
18 
19 import android.content.Context;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.XmlRes;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.applications.appinfo.AppAllServicesPreferenceController;
29 import com.android.car.settings.applications.appinfo.HibernationSwitchPreferenceController;
30 import com.android.car.settings.common.Logger;
31 import com.android.car.settings.common.SettingsFragment;
32 import com.android.settingslib.applications.ApplicationsState;
33 
34 /**
35  * Shows details about an application.
36  */
37 public class ApplicationDetailsFragment extends SettingsFragment {
38     private static final Logger LOG = new Logger(ApplicationDetailsFragment.class);
39     public static final String EXTRA_PACKAGE_NAME = "extra_package_name";
40 
41     private PackageManager mPm;
42 
43     private String mPackageName;
44     private PackageInfo mPackageInfo;
45     private ApplicationsState mAppState;
46     private ApplicationsState.AppEntry mAppEntry;
47 
48     /** Creates an instance of this fragment, passing packageName as an argument. */
getInstance(String packageName)49     public static ApplicationDetailsFragment getInstance(String packageName) {
50         ApplicationDetailsFragment applicationDetailFragment = new ApplicationDetailsFragment();
51         Bundle bundle = new Bundle();
52         bundle.putString(EXTRA_PACKAGE_NAME, packageName);
53         applicationDetailFragment.setArguments(bundle);
54         return applicationDetailFragment;
55     }
56 
57     @Override
58     @XmlRes
getPreferenceScreenResId()59     protected int getPreferenceScreenResId() {
60         return R.xml.application_details_fragment;
61     }
62 
63     @Override
onAttach(Context context)64     public void onAttach(Context context) {
65         super.onAttach(context);
66         mPm = context.getPackageManager();
67 
68         // These should be loaded before onCreate() so that the controller operates as expected.
69         mPackageName = getArguments().getString(EXTRA_PACKAGE_NAME);
70 
71         mAppState = ApplicationsState.getInstance(requireActivity().getApplication());
72 
73         retrieveAppEntry();
74 
75         use(ApplicationPreferenceController.class,
76                 R.string.pk_application_details_app)
77                 .setAppEntry(mAppEntry).setAppState(mAppState);
78         use(ApplicationActionButtonsPreferenceController.class,
79                 R.string.pk_application_details_action_buttons)
80                 .setAppEntry(mAppEntry).setAppState(mAppState).setPackageName(mPackageName);
81         use(AppAllServicesPreferenceController.class,
82                 R.string.pk_all_services_settings).setPackageName(mPackageName);
83         use(NotificationsPreferenceController.class,
84                 R.string.pk_application_details_notifications).setPackageInfo(mPackageInfo);
85         use(PermissionsPreferenceController.class,
86                 R.string.pk_application_details_permissions).setPackageName(mPackageName);
87         use(StoragePreferenceController.class,
88                 R.string.pk_application_details_storage)
89                 .setAppEntry(mAppEntry).setAppState(mAppState).setPackageName(mPackageName);
90         use(PrioritizeAppPerformancePreferenceController.class,
91                 R.string.pk_application_details_prioritize_app_performance)
92                 .setPackageInfo(mPackageInfo);
93         use(HibernationSwitchPreferenceController.class,
94                 R.string.pk_hibernation_switch)
95                 .setPackageName(mPackageName);
96         use(VersionPreferenceController.class,
97                 R.string.pk_application_details_version).setPackageInfo(mPackageInfo);
98     }
99 
retrieveAppEntry()100     private void retrieveAppEntry() {
101         mAppEntry = mAppState.getEntry(mPackageName, UserHandle.myUserId());
102         if (mAppEntry != null) {
103             try {
104                 mPackageInfo = mPm.getPackageInfo(mPackageName,
105                         PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.MATCH_ANY_USER
106                                 | PackageManager.GET_SIGNATURES | PackageManager.GET_PERMISSIONS);
107             } catch (PackageManager.NameNotFoundException e) {
108                 LOG.e("Exception when retrieving package:" + mPackageName, e);
109                 mPackageInfo = null;
110             }
111         } else {
112             mPackageInfo = null;
113         }
114     }
115 }
116