1 /* 2 * Copyright (C) 2020 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.security; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.os.Looper; 22 import android.os.RemoteException; 23 import android.security.AppUriAuthenticationPolicy; 24 import android.security.IKeyChainService; 25 import android.security.KeyChain; 26 import android.util.Log; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceViewHolder; 30 import androidx.recyclerview.widget.LinearLayoutManager; 31 import androidx.recyclerview.widget.RecyclerView; 32 33 import com.android.settings.R; 34 35 import java.util.concurrent.ExecutorService; 36 import java.util.concurrent.Executors; 37 38 /** 39 * Preference that shows the credential management app's authentication policy. 40 */ 41 public class CredentialManagementAppPolicyPreference extends Preference { 42 43 private static final String TAG = "CredentialManagementApp"; 44 45 private final ExecutorService mExecutor = Executors.newSingleThreadExecutor(); 46 private final Handler mHandler = new Handler(Looper.getMainLooper()); 47 48 private final Context mContext; 49 50 private boolean mHasCredentialManagerPackage; 51 private String mCredentialManagerPackageName; 52 private AppUriAuthenticationPolicy mCredentialManagerPolicy; 53 CredentialManagementAppPolicyPreference(Context context)54 public CredentialManagementAppPolicyPreference(Context context) { 55 super(context); 56 setLayoutResource(R.layout.credential_management_app_policy); 57 mContext = context; 58 } 59 60 @Override onBindViewHolder(PreferenceViewHolder view)61 public void onBindViewHolder(PreferenceViewHolder view) { 62 super.onBindViewHolder(view); 63 64 mExecutor.execute(() -> { 65 try { 66 IKeyChainService service = KeyChain.bind(mContext).getService(); 67 mHasCredentialManagerPackage = service.hasCredentialManagementApp(); 68 mCredentialManagerPackageName = service.getCredentialManagementAppPackageName(); 69 mCredentialManagerPolicy = service.getCredentialManagementAppPolicy(); 70 } catch (InterruptedException | RemoteException e) { 71 Log.e(TAG, "Unable to display credential management app policy"); 72 } 73 mHandler.post(() -> displayPolicy(view)); 74 }); 75 } 76 displayPolicy(PreferenceViewHolder view)77 private void displayPolicy(PreferenceViewHolder view) { 78 if (mHasCredentialManagerPackage) { 79 RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 80 recyclerView.setLayoutManager(new LinearLayoutManager(mContext)); 81 82 CredentialManagementAppAdapter recyclerViewAdapter = new CredentialManagementAppAdapter( 83 mContext, mCredentialManagerPackageName, 84 mCredentialManagerPolicy.getAppAndUriMappings(), 85 /* include header= */ false, /* include expander= */ true); 86 recyclerView.setAdapter(recyclerViewAdapter); 87 } 88 } 89 } 90