• 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.settings.applications.managedomainurls;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.hardware.usb.IUsbManager;
23 import android.os.IBinder;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.Logger;
32 import com.android.settingslib.applications.AppUtils;
33 
34 /**
35  * Business logic to reset a user preference for auto launching an app.
36  *
37  * <p>i.e. if a user has both NavigationAppA and NavigationAppB installed and NavigationAppA is set
38  * as the default navigation app, the user can reset that preference to pick a different default
39  * navigation app.
40  */
41 public class ClearDefaultsPreferenceController extends
42         AppLaunchSettingsBasePreferenceController<Preference> {
43 
44     private static final Logger LOG = new Logger(ClearDefaultsPreferenceController.class);
45 
46     private final IUsbManager mUsbManager;
47     private final PackageManager mPm;
48 
ClearDefaultsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49     public ClearDefaultsPreferenceController(Context context, String preferenceKey,
50             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52         IBinder b = ServiceManager.getService(Context.USB_SERVICE);
53         mUsbManager = IUsbManager.Stub.asInterface(b);
54         mPm = context.getPackageManager();
55     }
56 
57     @Override
getPreferenceType()58     protected Class<Preference> getPreferenceType() {
59         return Preference.class;
60     }
61 
62     @Override
updateState(Preference preference)63     protected void updateState(Preference preference) {
64         boolean autoLaunchEnabled = AppUtils.hasPreferredActivities(mPm, getPackageName())
65                 || isDefaultBrowser(getPackageName())
66                 || hasUsbDefaults(mUsbManager, getPackageName());
67 
68         preference.setEnabled(autoLaunchEnabled);
69         if (autoLaunchEnabled) {
70             preference.setTitle(R.string.auto_launch_reset_text);
71             preference.setSummary(R.string.auto_launch_enable_text);
72         } else {
73             preference.setTitle(R.string.auto_launch_disable_text);
74             preference.setSummary(null);
75         }
76     }
77 
78     @Override
handlePreferenceClicked(Preference preference)79     protected boolean handlePreferenceClicked(Preference preference) {
80         if (mUsbManager != null) {
81             int userId = getCurrentUserId();
82             mPm.clearPackagePreferredActivities(getPackageName());
83             if (isDefaultBrowser(getPackageName())) {
84                 mPm.setDefaultBrowserPackageNameAsUser(/* packageName= */ null, userId);
85             }
86             try {
87                 mUsbManager.clearDefaults(getPackageName(), userId);
88             } catch (RemoteException e) {
89                 LOG.e("mUsbManager.clearDefaults", e);
90             }
91             refreshUi();
92         }
93         return true;
94     }
95 
isDefaultBrowser(String packageName)96     private boolean isDefaultBrowser(String packageName) {
97         String defaultBrowser = mPm.getDefaultBrowserPackageNameAsUser(getCurrentUserId());
98         return packageName.equals(defaultBrowser);
99     }
100 
hasUsbDefaults(IUsbManager usbManager, String packageName)101     private boolean hasUsbDefaults(IUsbManager usbManager, String packageName) {
102         try {
103             if (usbManager != null) {
104                 return usbManager.hasDefaults(packageName, getCurrentUserId());
105             }
106         } catch (RemoteException e) {
107             LOG.e("mUsbManager.hasDefaults", e);
108         }
109         return false;
110     }
111 }
112