• 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.car.developeroptions.applications.appinfo;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.hardware.usb.IUsbManager;
24 import android.os.ServiceManager;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.car.developeroptions.SettingsPreferenceFragment;
30 import com.android.car.developeroptions.applications.AppLaunchSettings;
31 import com.android.settingslib.applications.AppUtils;
32 import com.android.settingslib.applications.ApplicationsState;
33 
34 public class AppOpenByDefaultPreferenceController extends AppInfoPreferenceControllerBase {
35 
36     private IUsbManager mUsbManager;
37     private PackageManager mPackageManager;
38 
AppOpenByDefaultPreferenceController(Context context, String key)39     public AppOpenByDefaultPreferenceController(Context context, String key) {
40         super(context, key);
41         mUsbManager = IUsbManager.Stub.asInterface(ServiceManager.getService(Context.USB_SERVICE));
42         mPackageManager = context.getPackageManager();
43     }
44 
45     @Override
displayPreference(PreferenceScreen screen)46     public void displayPreference(PreferenceScreen screen) {
47         super.displayPreference(screen);
48         final ApplicationsState.AppEntry appEntry = mParent.getAppEntry();
49         if (appEntry == null || appEntry.info == null) {
50             mPreference.setEnabled(false);
51         } else if ((appEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0
52                 || !appEntry.info.enabled) {
53             mPreference.setEnabled(false);
54         }
55     }
56 
57     @Override
updateState(Preference preference)58     public void updateState(Preference preference) {
59         final PackageInfo packageInfo = mParent.getPackageInfo();
60         if (packageInfo != null && !AppUtils.isInstant(packageInfo.applicationInfo)) {
61             preference.setVisible(true);
62             preference.setSummary(AppUtils.getLaunchByDefaultSummary(mParent.getAppEntry(),
63                     mUsbManager, mPackageManager, mContext));
64         } else {
65             preference.setVisible(false);
66         }
67     }
68 
69     @Override
getDetailFragmentClass()70     protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
71         return AppLaunchSettings.class;
72     }
73 
74 }
75