1 /*
2  * Copyright (C) 2015 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.settingslib.applications;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.IntentFilter;
22 import android.content.pm.PackageManager;
23 import android.hardware.usb.IUsbManager;
24 import android.os.RemoteException;
25 import android.os.UserHandle;
26 import android.util.Log;
27 
28 import com.android.settingslib.R;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 public class AppUtils {
34     private static final String TAG = "AppUtils";
35 
getLaunchByDefaultSummary(ApplicationsState.AppEntry appEntry, IUsbManager usbManager, PackageManager pm, Context context)36     public static CharSequence getLaunchByDefaultSummary(ApplicationsState.AppEntry appEntry,
37             IUsbManager usbManager, PackageManager pm, Context context) {
38         String packageName = appEntry.info.packageName;
39         boolean hasPreferred = hasPreferredActivities(pm, packageName)
40                 || hasUsbDefaults(usbManager, packageName);
41         int status = pm.getIntentVerificationStatusAsUser(packageName, UserHandle.myUserId());
42         // consider a visible current link-handling state to be any explicitly designated behavior
43         boolean hasDomainURLsPreference =
44                 status != PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
45         return context.getString(hasPreferred || hasDomainURLsPreference
46                 ? R.string.launch_defaults_some
47                 : R.string.launch_defaults_none);
48     }
49 
hasUsbDefaults(IUsbManager usbManager, String packageName)50     public static boolean hasUsbDefaults(IUsbManager usbManager, String packageName) {
51         try {
52             if (usbManager != null) {
53                 return usbManager.hasDefaults(packageName, UserHandle.myUserId());
54             }
55         } catch (RemoteException e) {
56             Log.e(TAG, "mUsbManager.hasDefaults", e);
57         }
58         return false;
59     }
60 
hasPreferredActivities(PackageManager pm, String packageName)61     public static boolean hasPreferredActivities(PackageManager pm, String packageName) {
62         // Get list of preferred activities
63         List<ComponentName> prefActList = new ArrayList<>();
64         // Intent list cannot be null. so pass empty list
65         List<IntentFilter> intentList = new ArrayList<>();
66         pm.getPreferredActivities(intentList, prefActList, packageName);
67         Log.d(TAG, "Have " + prefActList.size() + " number of activities in preferred list");
68         return prefActList.size() > 0;
69     }
70 
71 }
72