Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 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.settings.vpn2; |
| 18 | |
Pavel Grafov | 16d588c | 2021-03-18 22:46:08 +0000 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 20 | import android.content.Context; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 21 | import android.content.pm.PackageInfo; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.graphics.drawable.Drawable; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 24 | import android.os.UserHandle; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 25 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 26 | import androidx.preference.Preference; |
| 27 | |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 28 | import com.android.internal.net.LegacyVpnInfo; |
| 29 | import com.android.internal.net.VpnConfig; |
Pavel Grafov | 16d588c | 2021-03-18 22:46:08 +0000 | [diff] [blame] | 30 | import com.android.settingslib.RestrictedLockUtils; |
| 31 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Bonian Chen | 61c4506 | 2023-01-09 08:10:11 +0000 | [diff] [blame] | 32 | import com.android.settings.R; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
Aurimas Liutikas | e0069d3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 35 | * {@link androidx.preference.Preference} containing information about a VPN |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 36 | * application. Tracks the package name and connection state. |
| 37 | */ |
| 38 | public class AppPreference extends ManageablePreference { |
| 39 | public static final int STATE_CONNECTED = LegacyVpnInfo.STATE_CONNECTED; |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 40 | public static final int STATE_DISCONNECTED = STATE_NONE; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 41 | |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 42 | private final String mPackageName; |
| 43 | private final String mName; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 44 | |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 45 | public AppPreference(Context context, int userId, String packageName) { |
Bonian Chen | 61c4506 | 2023-01-09 08:10:11 +0000 | [diff] [blame] | 46 | super(context, null /* attrs */, R.style.VpnAppManagementPreferenceStyle, 0); |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 47 | super.setUserId(userId); |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 48 | |
| 49 | mPackageName = packageName; |
Pavel Grafov | 16d588c | 2021-03-18 22:46:08 +0000 | [diff] [blame] | 50 | disableIfConfiguredByAdmin(); |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 51 | |
| 52 | // Fetch icon and VPN label |
| 53 | String label = packageName; |
| 54 | Drawable icon = null; |
| 55 | try { |
| 56 | // Make all calls to the package manager as the appropriate user. |
| 57 | Context userContext = getUserContext(); |
| 58 | PackageManager pm = userContext.getPackageManager(); |
| 59 | // The nested catch block is for the case that the app doesn't exist, so we can fall |
| 60 | // back to the default activity icon. |
| 61 | try { |
| 62 | PackageInfo pkgInfo = pm.getPackageInfo(mPackageName, 0 /* flags */); |
| 63 | if (pkgInfo != null) { |
| 64 | icon = pkgInfo.applicationInfo.loadIcon(pm); |
| 65 | label = VpnConfig.getVpnLabel(userContext, mPackageName).toString(); |
| 66 | } |
| 67 | } catch (PackageManager.NameNotFoundException pkgNotFound) { |
| 68 | // Use default app label and icon as fallback |
| 69 | } |
| 70 | if (icon == null) { |
| 71 | icon = pm.getDefaultActivityIcon(); |
| 72 | } |
| 73 | } catch (PackageManager.NameNotFoundException userNotFound) { |
| 74 | // No user, no useful information to obtain. Quietly fail. |
| 75 | } |
| 76 | mName = label; |
| 77 | |
| 78 | setTitle(mName); |
| 79 | setIcon(icon); |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Pavel Grafov | 16d588c | 2021-03-18 22:46:08 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Disable this preference if VPN is set as always on by a profile or device owner. |
| 84 | * NB: it should be called after super.setUserId() otherwise admin information can be lost. |
| 85 | */ |
| 86 | private void disableIfConfiguredByAdmin() { |
| 87 | if (isDisabledByAdmin()) { |
| 88 | // Already disabled due to user restriction. |
| 89 | return; |
| 90 | } |
| 91 | final DevicePolicyManager dpm = getContext() |
| 92 | .createContextAsUser(UserHandle.of(getUserId()), /* flags= */ 0) |
| 93 | .getSystemService(DevicePolicyManager.class); |
| 94 | if (mPackageName.equals(dpm.getAlwaysOnVpnPackage())) { |
| 95 | final EnforcedAdmin admin = RestrictedLockUtils.getProfileOrDeviceOwner( |
| 96 | getContext(), UserHandle.of(mUserId)); |
| 97 | setDisabledByAdmin(admin); |
| 98 | } |
| 99 | } |
| 100 | |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 101 | public PackageInfo getPackageInfo() { |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 102 | try { |
Robin Lee | ab6a65c | 2015-05-01 17:42:30 +0100 | [diff] [blame] | 103 | PackageManager pm = getUserContext().getPackageManager(); |
| 104 | return pm.getPackageInfo(mPackageName, 0 /* flags */); |
| 105 | } catch (PackageManager.NameNotFoundException nnfe) { |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | |
Robin Lee | ab6a65c | 2015-05-01 17:42:30 +0100 | [diff] [blame] | 110 | public String getLabel() { |
| 111 | return mName; |
| 112 | } |
| 113 | |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 114 | public String getPackageName() { |
| 115 | return mPackageName; |
| 116 | } |
| 117 | |
Robin Lee | ab6a65c | 2015-05-01 17:42:30 +0100 | [diff] [blame] | 118 | private Context getUserContext() throws PackageManager.NameNotFoundException { |
Robin Lee | 7bf8654 | 2015-11-16 15:24:36 +0000 | [diff] [blame] | 119 | UserHandle user = UserHandle.of(mUserId); |
Robin Lee | ab6a65c | 2015-05-01 17:42:30 +0100 | [diff] [blame] | 120 | return getContext().createPackageContextAsUser( |
| 121 | getContext().getPackageName(), 0 /* flags */, user); |
| 122 | } |
| 123 | |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 124 | public int compareTo(Preference preference) { |
| 125 | if (preference instanceof AppPreference) { |
| 126 | AppPreference another = (AppPreference) preference; |
| 127 | int result; |
| 128 | if ((result = another.mState - mState) == 0 && |
| 129 | (result = mName.compareToIgnoreCase(another.mName)) == 0 && |
| 130 | (result = mPackageName.compareTo(another.mPackageName)) == 0) { |
Robin Lee | 7bf8654 | 2015-11-16 15:24:36 +0000 | [diff] [blame] | 131 | result = mUserId - another.mUserId; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 132 | } |
| 133 | return result; |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 134 | } else if (preference instanceof LegacyVpnPreference) { |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 135 | // Use comparator from ConfigPreference |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 136 | LegacyVpnPreference another = (LegacyVpnPreference) preference; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 137 | return -another.compareTo(this); |
| 138 | } else { |
| 139 | return super.compareTo(preference); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |