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.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.RemoteException; 25 import android.security.IKeyChainService; 26 import android.security.KeyChain; 27 import android.util.Log; 28 import android.view.View; 29 import android.widget.ImageView; 30 import android.widget.TextView; 31 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.R; 35 import com.android.settings.core.BasePreferenceController; 36 import com.android.settingslib.widget.LayoutPreference; 37 38 import java.util.concurrent.ExecutorService; 39 import java.util.concurrent.Executors; 40 41 /** 42 * Controller that shows the header of the credential management app, which includes credential 43 * management app's name, icon and a description. 44 */ 45 public class CredentialManagementAppHeaderController extends BasePreferenceController { 46 47 private static final String TAG = "CredentialManagementApp"; 48 49 private final ExecutorService mExecutor = Executors.newSingleThreadExecutor(); 50 private final Handler mHandler = new Handler(Looper.getMainLooper()); 51 CredentialManagementAppHeaderController(Context context, String preferenceKey)52 public CredentialManagementAppHeaderController(Context context, String preferenceKey) { 53 super(context, preferenceKey); 54 mPackageManager = context.getPackageManager(); 55 } 56 57 private final PackageManager mPackageManager; 58 private String mCredentialManagerPackageName; 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 return AVAILABLE_UNSEARCHABLE; 63 } 64 65 @Override displayPreference(PreferenceScreen screen)66 public void displayPreference(PreferenceScreen screen) { 67 super.displayPreference(screen); 68 69 mExecutor.execute(() -> { 70 try { 71 IKeyChainService service = KeyChain.bind(mContext).getService(); 72 mCredentialManagerPackageName = service.getCredentialManagementAppPackageName(); 73 } catch (InterruptedException | RemoteException e) { 74 Log.e(TAG, "Unable to display credential management app header"); 75 } 76 mHandler.post(() -> displayHeader(screen)); 77 }); 78 } 79 displayHeader(PreferenceScreen screen)80 private void displayHeader(PreferenceScreen screen) { 81 LayoutPreference headerPref = screen.findPreference(getPreferenceKey()); 82 ImageView appIconView = headerPref.findViewById(R.id.entity_header_icon); 83 TextView titleView = headerPref.findViewById(R.id.entity_header_title); 84 TextView summary1 = headerPref.findViewById(R.id.entity_header_summary); 85 TextView summary2 = headerPref.findViewById( 86 com.android.settingslib.widget.preference.layout.R.id.entity_header_second_summary); 87 summary2.setVisibility(View.GONE); 88 89 try { 90 ApplicationInfo applicationInfo = 91 mPackageManager.getApplicationInfo(mCredentialManagerPackageName, 0); 92 appIconView.setImageDrawable(mPackageManager.getApplicationIcon(applicationInfo)); 93 titleView.setText(applicationInfo.loadLabel(mPackageManager)); 94 summary1.setText(R.string.certificate_management_app_description); 95 } catch (PackageManager.NameNotFoundException e) { 96 appIconView.setImageDrawable(null); 97 titleView.setText(mCredentialManagerPackageName); 98 } 99 } 100 } 101