1 /* 2 * Copyright (C) 2017 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 package com.android.settings.system; 17 18 import android.accounts.Account; 19 import android.accounts.AccountManager; 20 import android.content.Context; 21 import android.content.pm.UserInfo; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.support.v7.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.Utils; 28 import com.android.settings.core.PreferenceController; 29 30 import java.util.List; 31 32 public class FactoryResetPreferenceController extends PreferenceController { 33 /** Key of the "Factory reset" preference in {@link R.xml.reset_dashboard_fragment}. */ 34 private static final String KEY_FACTORY_RESET = "factory_reset"; 35 36 private final UserManager mUm; 37 private final AccountManager mAm; 38 39 public FactoryResetPreferenceController(Context context) { 40 super(context); 41 mUm = (UserManager) context.getSystemService(Context.USER_SERVICE); 42 mAm = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); 43 } 44 45 /** Hide "Factory reset" settings for secondary users, except demo users. */ 46 @Override 47 public boolean isAvailable() { 48 return mUm.isAdminUser() || Utils.isCarrierDemoUser(mContext); 49 } 50 51 @Override 52 public String getPreferenceKey() { 53 return KEY_FACTORY_RESET; 54 } 55 56 @Override 57 public void updateState(Preference preference) { 58 final List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId()); 59 int accountsCount = 0; 60 for (UserInfo userInfo : profiles) { 61 final int profileId = userInfo.id; 62 Account[] accounts = mAm.getAccountsAsUser(profileId); 63 accountsCount += accounts.length; 64 } 65 if (accountsCount == 0) { 66 preference.setSummary(R.string.master_clear_summary); 67 } else { 68 preference.setSummary(mContext.getResources().getQuantityString( 69 R.plurals.master_clear_with_account_summary, 70 accountsCount, accountsCount)); 71 } 72 } 73 } 74