1 /* 2 * Copyright (C) 2011 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 19 import android.app.settings.SettingsEnums; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.RemoteException; 23 import android.security.IKeyChainService; 24 import android.view.View; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 import androidx.fragment.app.Fragment; 29 import androidx.viewpager2.adapter.FragmentStateAdapter; 30 import androidx.viewpager2.widget.ViewPager2; 31 32 import com.android.settings.dashboard.DashboardFragment; 33 34 import com.google.android.material.tabs.TabLayout; 35 import com.google.android.material.tabs.TabLayoutMediator; 36 import com.google.common.collect.ImmutableList; 37 38 import java.util.List; 39 40 /** 41 * Main fragment to display trusted credentials settings. 42 */ 43 public class TrustedCredentialsSettings extends DashboardFragment { 44 45 private static final String TAG = "TrustedCredentialsSettings"; 46 47 public static final String ARG_SHOW_NEW_FOR_USER = "ARG_SHOW_NEW_FOR_USER"; 48 49 static final ImmutableList<Tab> TABS = ImmutableList.of(Tab.SYSTEM, Tab.USER); 50 51 private static final String USER_ACTION = "com.android.settings.TRUSTED_CREDENTIALS_USER"; 52 53 @Override getMetricsCategory()54 public int getMetricsCategory() { 55 return SettingsEnums.TRUSTED_CREDENTIALS; 56 } 57 58 @Override onCreate(Bundle savedInstanceState)59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 getActivity().setTitle(R.string.trusted_credentials); 62 } 63 64 @Override getPreferenceScreenResId()65 protected int getPreferenceScreenResId() { 66 return R.xml.placeholder_preference_screen; 67 } 68 69 @Override getLogTag()70 protected String getLogTag() { 71 return TAG; 72 } 73 74 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)75 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 76 View tabContainer = view.findViewById(R.id.tab_container); 77 tabContainer.setVisibility(View.VISIBLE); 78 79 ViewPager2 viewPager = tabContainer.findViewById(R.id.view_pager); 80 viewPager.setAdapter(new FragmentAdapter(this)); 81 viewPager.setUserInputEnabled(false); 82 83 Intent intent = getActivity().getIntent(); 84 if (intent != null && USER_ACTION.equals(intent.getAction())) { 85 viewPager.setCurrentItem(TABS.indexOf(Tab.USER), false); 86 } 87 88 TabLayout tabLayout = tabContainer.findViewById(R.id.tabs); 89 new TabLayoutMediator(tabLayout, viewPager, false, false, 90 (tab, position) -> tab.setText(TABS.get(position).mLabel)).attach(); 91 } 92 93 private static class FragmentAdapter extends FragmentStateAdapter { FragmentAdapter(Fragment fragment)94 FragmentAdapter(Fragment fragment) { 95 super(fragment); 96 } 97 98 @NonNull 99 @Override createFragment(int position)100 public Fragment createFragment(int position) { 101 TrustedCredentialsFragment fragment = new TrustedCredentialsFragment(); 102 Bundle args = new Bundle(); 103 args.putInt(TrustedCredentialsFragment.ARG_POSITION, position); 104 fragment.setArguments(args); 105 return fragment; 106 } 107 108 @Override getItemCount()109 public int getItemCount() { 110 return TrustedCredentialsSettings.TABS.size(); 111 } 112 } 113 114 enum Tab { 115 SYSTEM(R.string.trusted_credentials_system_tab, true), 116 USER(R.string.trusted_credentials_user_tab, false); 117 118 private final int mLabel; 119 final boolean mSwitch; 120 Tab(int label, boolean withSwitch)121 Tab(int label, boolean withSwitch) { 122 mLabel = label; 123 mSwitch = withSwitch; 124 } 125 getAliases(IKeyChainService service)126 List<String> getAliases(IKeyChainService service) throws RemoteException { 127 switch (this) { 128 case SYSTEM: { 129 return service.getSystemCaAliases().getList(); 130 } 131 case USER: 132 return service.getUserCaAliases().getList(); 133 } 134 throw new AssertionError(); 135 } 136 deleted(IKeyChainService service, String alias)137 boolean deleted(IKeyChainService service, String alias) throws RemoteException { 138 switch (this) { 139 case SYSTEM: 140 return !service.containsCaAlias(alias); 141 case USER: 142 return false; 143 } 144 throw new AssertionError(); 145 } 146 } 147 } 148