Robin Lee | baefdcf | 2015-08-26 10:57:44 +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; |
| 18 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 19 | import android.annotation.LayoutRes; |
| 20 | import android.annotation.Nullable; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 21 | import android.app.AlertDialog; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 22 | import android.app.Dialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.app.Fragment; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.os.AsyncTask; |
| 28 | import android.os.Bundle; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 31 | import android.os.Process; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 32 | import android.os.RemoteException; |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 33 | import android.os.UserHandle; |
| 34 | import android.os.UserManager; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 35 | import android.security.Credentials; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 36 | import android.security.IKeyChainService; |
| 37 | import android.security.KeyChain; |
| 38 | import android.security.KeyChain.KeyChainConnection; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 39 | import android.security.KeyStore; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 40 | import android.util.Log; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 41 | import android.util.SparseArray; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 45 | import android.widget.AdapterView; |
| 46 | import android.widget.AdapterView.OnItemClickListener; |
| 47 | import android.widget.ArrayAdapter; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 48 | import android.widget.ListView; |
| 49 | import android.widget.TextView; |
| 50 | |
Robin Lee | b70b3d8 | 2016-02-01 12:52:16 +0000 | [diff] [blame] | 51 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Ricky Wai | 9579274 | 2016-05-24 19:28:53 +0100 | [diff] [blame] | 52 | import com.android.internal.widget.LockPatternUtils; |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 53 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 54 | import com.android.settingslib.RestrictedLockUtils; |
| 55 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Robin Lee | b70b3d8 | 2016-02-01 12:52:16 +0000 | [diff] [blame] | 56 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 57 | import java.util.ArrayList; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 58 | import java.util.EnumSet; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 59 | import java.util.List; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 60 | import java.util.SortedMap; |
| 61 | import java.util.TreeMap; |
| 62 | |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 63 | import static android.view.View.GONE; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 64 | import static android.view.View.VISIBLE; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 65 | |
Udam Saini | 0708d9e | 2016-03-28 16:35:13 -0700 | [diff] [blame] | 66 | public class UserCredentialsSettings extends OptionsMenuFragment implements OnItemClickListener { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 67 | private static final String TAG = "UserCredentialsSettings"; |
| 68 | |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 69 | private ListView mListView; |
| 70 | |
| 71 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 72 | public int getMetricsCategory() { |
Robin Lee | b70b3d8 | 2016-02-01 12:52:16 +0000 | [diff] [blame] | 73 | return MetricsEvent.USER_CREDENTIALS; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onResume() { |
| 78 | super.onResume(); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 79 | refreshItems(); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public View onCreateView( |
| 84 | LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 85 | final View rootView = inflater.inflate(R.layout.user_credentials, parent, false); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 86 | |
| 87 | // Set up an OnItemClickListener for the credential list. |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 88 | mListView = (ListView) rootView.findViewById(R.id.credential_list); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 89 | mListView.setOnItemClickListener(this); |
| 90 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 91 | return rootView; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 96 | final Credential item = (Credential) parent.getItemAtPosition(position); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 97 | CredentialDialogFragment.show(this, item); |
| 98 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 99 | |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 100 | protected void announceRemoval(String alias) { |
| 101 | if (isAdded()) { |
| 102 | mListView.announceForAccessibility(getString(R.string.user_credential_removed, alias)); |
| 103 | } |
| 104 | } |
| 105 | |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 106 | protected void refreshItems() { |
| 107 | if (isAdded()) { |
| 108 | new AliasLoader().execute(); |
| 109 | } |
| 110 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 111 | |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 112 | public static class CredentialDialogFragment extends InstrumentedDialogFragment { |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 113 | private static final String TAG = "CredentialDialogFragment"; |
| 114 | private static final String ARG_CREDENTIAL = "credential"; |
| 115 | |
| 116 | public static void show(Fragment target, Credential item) { |
| 117 | final Bundle args = new Bundle(); |
| 118 | args.putParcelable(ARG_CREDENTIAL, item); |
| 119 | |
Robin Lee | f8e2dbf | 2016-04-07 13:17:24 +0100 | [diff] [blame] | 120 | if (target.getFragmentManager().findFragmentByTag(TAG) == null) { |
| 121 | final DialogFragment frag = new CredentialDialogFragment(); |
| 122 | frag.setTargetFragment(target, /* requestCode */ -1); |
| 123 | frag.setArguments(args); |
| 124 | frag.show(target.getFragmentManager(), TAG); |
| 125 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 130 | final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 131 | |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 132 | View root = getActivity().getLayoutInflater() |
| 133 | .inflate(R.layout.user_credential_dialog, null); |
| 134 | ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 135 | View contentView = getCredentialView(item, R.layout.user_credential, null, |
| 136 | infoContainer, /* expanded */ true); |
| 137 | infoContainer.addView(contentView); |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 138 | |
| 139 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 140 | .setView(root) |
| 141 | .setTitle(R.string.user_credential_title) |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 142 | .setPositiveButton(R.string.done, null); |
| 143 | |
| 144 | final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS; |
| 145 | final int myUserId = UserHandle.myUserId(); |
| 146 | if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) { |
| 147 | DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
| 148 | @Override public void onClick(DialogInterface dialog, int id) { |
| 149 | final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced( |
| 150 | getContext(), restriction, myUserId); |
| 151 | if (admin != null) { |
| 152 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), |
| 153 | admin); |
| 154 | } else { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 155 | new RemoveCredentialsTask(getContext(), getTargetFragment()) |
| 156 | .execute(item); |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 157 | } |
| 158 | dialog.dismiss(); |
| 159 | } |
| 160 | }; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 161 | if (item.isSystem()) { |
| 162 | // TODO: a safe means of clearing wifi certificates. Configs refer to aliases |
| 163 | // directly so deleting certs will break dependent access points. |
| 164 | builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener); |
| 165 | } |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 166 | } |
| 167 | return builder.create(); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public int getMetricsCategory() { |
| 172 | return MetricsEvent.DIALOG_USER_CREDENTIAL; |
| 173 | } |
| 174 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 175 | /** |
| 176 | * Deletes all certificates and keys under a given alias. |
| 177 | * |
| 178 | * If the {@link Credential} is for a system alias, all active grants to the alias will be |
| 179 | * removed using {@link KeyChain}. |
| 180 | */ |
| 181 | private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> { |
| 182 | private Context context; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 183 | private Fragment targetFragment; |
| 184 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 185 | public RemoveCredentialsTask(Context context, Fragment targetFragment) { |
| 186 | this.context = context; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 187 | this.targetFragment = targetFragment; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 188 | } |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 189 | |
| 190 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 191 | protected Credential[] doInBackground(Credential... credentials) { |
| 192 | for (final Credential credential : credentials) { |
| 193 | if (credential.isSystem()) { |
| 194 | removeGrantsAndDelete(credential); |
| 195 | continue; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 196 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 197 | throw new UnsupportedOperationException( |
| 198 | "Not implemented for wifi certificates. This should not be reachable."); |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 199 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 200 | return credentials; |
| 201 | } |
| 202 | |
| 203 | private void removeGrantsAndDelete(final Credential credential) { |
| 204 | final KeyChainConnection conn; |
| 205 | try { |
| 206 | conn = KeyChain.bind(getContext()); |
| 207 | } catch (InterruptedException e) { |
| 208 | Log.w(TAG, "Connecting to KeyChain", e); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | try { |
| 213 | IKeyChainService keyChain = conn.getService(); |
| 214 | keyChain.removeKeyPair(credential.alias); |
| 215 | } catch (RemoteException e) { |
| 216 | Log.w(TAG, "Removing credentials", e); |
| 217 | } finally { |
| 218 | conn.close(); |
| 219 | } |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 223 | protected void onPostExecute(Credential... credentials) { |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 224 | if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) { |
| 225 | final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 226 | for (final Credential credential : credentials) { |
| 227 | target.announceRemoval(credential.alias); |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 228 | } |
| 229 | target.refreshItems(); |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 232 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Opens a background connection to KeyStore to list user credentials. |
| 237 | * The credentials are stored in a {@link CredentialAdapter} attached to the main |
| 238 | * {@link ListView} in the fragment. |
| 239 | */ |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 240 | private class AliasLoader extends AsyncTask<Void, Void, List<Credential>> { |
| 241 | /** |
| 242 | * @return a list of credentials ordered: |
| 243 | * <ol> |
| 244 | * <li>first by purpose;</li> |
| 245 | * <li>then by alias.</li> |
| 246 | * </ol> |
| 247 | */ |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 248 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 249 | protected List<Credential> doInBackground(Void... params) { |
| 250 | final KeyStore keyStore = KeyStore.getInstance(); |
| 251 | |
| 252 | // Certificates can be installed into SYSTEM_UID or WIFI_UID through CertInstaller. |
| 253 | final int myUserId = UserHandle.myUserId(); |
| 254 | final int systemUid = UserHandle.getUid(myUserId, Process.SYSTEM_UID); |
| 255 | final int wifiUid = UserHandle.getUid(myUserId, Process.WIFI_UID); |
| 256 | |
| 257 | List<Credential> credentials = new ArrayList<>(); |
| 258 | credentials.addAll(getCredentialsForUid(keyStore, systemUid).values()); |
| 259 | credentials.addAll(getCredentialsForUid(keyStore, wifiUid).values()); |
| 260 | return credentials; |
| 261 | } |
| 262 | |
| 263 | private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) { |
| 264 | final SortedMap<String, Credential> aliasMap = new TreeMap<>(); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 265 | for (final Credential.Type type : Credential.Type.values()) { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 266 | for (final String alias : keyStore.list(type.prefix, uid)) { |
Ricky Wai | 9579274 | 2016-05-24 19:28:53 +0100 | [diff] [blame] | 267 | // Do not show work profile keys in user credentials |
| 268 | if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) || |
| 269 | alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) { |
| 270 | continue; |
| 271 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 272 | Credential c = aliasMap.get(alias); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 273 | if (c == null) { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 274 | c = new Credential(alias, uid); |
| 275 | aliasMap.put(alias, c); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 276 | } |
| 277 | c.storedTypes.add(type); |
| 278 | } |
| 279 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 280 | return aliasMap; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 284 | protected void onPostExecute(List<Credential> credentials) { |
| 285 | final Credential[] credentialArray = credentials.toArray(new Credential[0]); |
| 286 | mListView.setAdapter(new CredentialAdapter(getContext(), credentialArray)); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Helper class to display {@link Credential}s in a list. |
| 292 | */ |
| 293 | private static class CredentialAdapter extends ArrayAdapter<Credential> { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 294 | private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference; |
| 295 | |
| 296 | public CredentialAdapter(Context context, final Credential[] objects) { |
| 297 | super(context, LAYOUT_RESOURCE, objects); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 301 | public View getView(int position, @Nullable View view, ViewGroup parent) { |
| 302 | return getCredentialView(getItem(position), LAYOUT_RESOURCE, view, parent, |
| 303 | /* expanded */ false); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 307 | /** |
| 308 | * Mapping from View IDs in {@link R} to the types of credentials they describe. |
| 309 | */ |
| 310 | private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>(); |
| 311 | static { |
| 312 | credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_PRIVATE_KEY); |
| 313 | credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE); |
| 314 | credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE); |
| 315 | } |
| 316 | |
| 317 | protected static View getCredentialView(Credential item, @LayoutRes int layoutResource, |
| 318 | @Nullable View view, ViewGroup parent, boolean expanded) { |
| 319 | if (view == null) { |
| 320 | view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false); |
| 321 | } |
| 322 | |
| 323 | ((TextView) view.findViewById(R.id.alias)).setText(item.alias); |
| 324 | ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem() |
| 325 | ? R.string.credential_for_vpn_and_apps |
| 326 | : R.string.credential_for_wifi); |
| 327 | |
| 328 | view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE); |
| 329 | if (expanded) { |
| 330 | for (int i = 0; i < credentialViewTypes.size(); i++) { |
| 331 | final View detail = view.findViewById(credentialViewTypes.keyAt(i)); |
| 332 | detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i)) |
| 333 | ? View.VISIBLE : View.GONE); |
| 334 | } |
| 335 | } |
| 336 | return view; |
| 337 | } |
| 338 | |
| 339 | static class AliasEntry { |
| 340 | public String alias; |
| 341 | public int uid; |
| 342 | } |
| 343 | |
Robin Lee | e268042 | 2016-01-25 12:24:27 +0000 | [diff] [blame] | 344 | static class Credential implements Parcelable { |
| 345 | static enum Type { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 346 | CA_CERTIFICATE (Credentials.CA_CERTIFICATE), |
| 347 | USER_CERTIFICATE (Credentials.USER_CERTIFICATE), |
| 348 | USER_PRIVATE_KEY (Credentials.USER_PRIVATE_KEY), |
| 349 | USER_SECRET_KEY (Credentials.USER_SECRET_KEY); |
| 350 | |
| 351 | final String prefix; |
| 352 | |
| 353 | Type(String prefix) { |
| 354 | this.prefix = prefix; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the |
| 360 | * prefixes from {@link CredentialItem.storedTypes}. |
| 361 | */ |
| 362 | final String alias; |
| 363 | |
| 364 | /** |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 365 | * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can |
| 366 | * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates. |
| 367 | */ |
| 368 | final int uid; |
| 369 | |
| 370 | /** |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 371 | * Should contain some non-empty subset of: |
| 372 | * <ul> |
| 373 | * <li>{@link Credentials.CA_CERTIFICATE}</li> |
| 374 | * <li>{@link Credentials.USER_CERTIFICATE}</li> |
| 375 | * <li>{@link Credentials.USER_PRIVATE_KEY}</li> |
| 376 | * <li>{@link Credentials.USER_SECRET_KEY}</li> |
| 377 | * </ul> |
| 378 | */ |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 379 | final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 380 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 381 | Credential(final String alias, final int uid) { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 382 | this.alias = alias; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 383 | this.uid = uid; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 384 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 385 | |
| 386 | Credential(Parcel in) { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 387 | this(in.readString(), in.readInt()); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 388 | |
| 389 | long typeBits = in.readLong(); |
| 390 | for (Type i : Type.values()) { |
| 391 | if ((typeBits & (1L << i.ordinal())) != 0L) { |
| 392 | storedTypes.add(i); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | public void writeToParcel(Parcel out, int flags) { |
| 398 | out.writeString(alias); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 399 | out.writeInt(uid); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 400 | |
| 401 | long typeBits = 0; |
| 402 | for (Type i : storedTypes) { |
| 403 | typeBits |= 1L << i.ordinal(); |
| 404 | } |
| 405 | out.writeLong(typeBits); |
| 406 | } |
| 407 | |
| 408 | public int describeContents() { |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | public static final Parcelable.Creator<Credential> CREATOR |
| 413 | = new Parcelable.Creator<Credential>() { |
| 414 | public Credential createFromParcel(Parcel in) { |
| 415 | return new Credential(in); |
| 416 | } |
| 417 | |
| 418 | public Credential[] newArray(int size) { |
| 419 | return new Credential[size]; |
| 420 | } |
| 421 | }; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 422 | |
| 423 | public boolean isSystem() { |
| 424 | return UserHandle.getAppId(uid) == Process.SYSTEM_UID; |
| 425 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 426 | } |
| 427 | } |