1 /* 2 * Copyright (C) 2016 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.accounts; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 import android.provider.Settings; 27 import android.support.v14.preference.SwitchPreference; 28 import android.support.v7.preference.Preference; 29 import android.util.Log; 30 31 import com.android.internal.logging.MetricsProto; 32 import com.android.settings.R; 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settingslib.RestrictedLockUtils; 35 import com.android.settingslib.RestrictedSwitchPreference; 36 37 import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH; 38 39 /** 40 * Setting page for managed profile. 41 * FIXME: It currently assumes there is only one managed profile. 42 */ 43 public class ManagedProfileSettings extends SettingsPreferenceFragment 44 implements Preference.OnPreferenceChangeListener { 45 46 private SwitchPreference mWorkModePreference; 47 private RestrictedSwitchPreference mContactPrefrence; 48 49 private UserManager mUserManager; 50 private UserHandle mManagedUser; 51 private Context mContext; 52 53 private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver; 54 55 private static final String KEY_WORK_MODE = "work_mode"; 56 private static final String KEY_CONTACT = "contacts_search"; 57 58 private static final String TAG = "ManagedProfileSettings"; 59 60 @Override onCreate(Bundle icicle)61 public void onCreate(Bundle icicle) { 62 super.onCreate(icicle); 63 addPreferencesFromResource(R.xml.managed_profile_settings); 64 mWorkModePreference = (SwitchPreference) findPreference(KEY_WORK_MODE); 65 mWorkModePreference.setOnPreferenceChangeListener(this); 66 mContactPrefrence = (RestrictedSwitchPreference) findPreference(KEY_CONTACT); 67 mContactPrefrence.setOnPreferenceChangeListener(this); 68 mContext = getActivity().getApplicationContext(); 69 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); 70 mManagedUser = getManagedUserFromArgument(); 71 if (mManagedUser == null) { 72 getActivity().finish(); 73 } 74 mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver(); 75 mManagedProfileBroadcastReceiver.register(getActivity()); 76 } 77 78 @Override onResume()79 public void onResume() { 80 super.onResume(); 81 loadDataAndPopulateUi(); 82 } 83 84 @Override onDestroy()85 public void onDestroy() { 86 super.onDestroy(); 87 mManagedProfileBroadcastReceiver.unregister(getActivity()); 88 } 89 getManagedUserFromArgument()90 private UserHandle getManagedUserFromArgument() { 91 Bundle arguments = getArguments(); 92 if (arguments != null) { 93 UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER); 94 if (userHandle != null) { 95 if (mUserManager.isManagedProfile(userHandle.getIdentifier())) { 96 return userHandle; 97 } 98 } 99 } 100 return null; 101 } 102 loadDataAndPopulateUi()103 private void loadDataAndPopulateUi() { 104 if (mWorkModePreference != null) { 105 mWorkModePreference.setChecked( 106 !mUserManager.isQuietModeEnabled(mManagedUser)); 107 } 108 109 if (mContactPrefrence != null) { 110 int value = Settings.Secure.getIntForUser(getContentResolver(), 111 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier()); 112 mContactPrefrence.setChecked(value != 0); 113 RestrictedLockUtils.EnforcedAdmin enforcedAdmin = 114 RestrictedLockUtils.checkIfRemoteContactSearchDisallowed( 115 mContext, mManagedUser.getIdentifier()); 116 mContactPrefrence.setDisabledByAdmin(enforcedAdmin); 117 } 118 } 119 120 @Override getMetricsCategory()121 protected int getMetricsCategory() { 122 return MetricsProto.MetricsEvent.ACCOUNTS_WORK_PROFILE_SETTINGS; 123 } 124 125 126 @Override onPreferenceChange(Preference preference, Object newValue)127 public boolean onPreferenceChange(Preference preference, Object newValue) { 128 if (preference == mWorkModePreference) { 129 if ((boolean) newValue) { 130 mUserManager.trySetQuietModeDisabled(mManagedUser.getIdentifier(), null); 131 } else { 132 mUserManager.setQuietModeEnabled(mManagedUser.getIdentifier(), true); 133 } 134 return true; 135 } 136 if (preference == mContactPrefrence) { 137 int value = ((boolean) newValue == true) ? 1 : 0; 138 Settings.Secure.putIntForUser(getContentResolver(), 139 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier()); 140 return true; 141 } 142 return false; 143 } 144 145 private class ManagedProfileBroadcastReceiver extends BroadcastReceiver { 146 147 @Override onReceive(Context context, Intent intent)148 public void onReceive(Context context, Intent intent) { 149 final String action = intent.getAction(); 150 Log.v(TAG, "Received broadcast: " + action); 151 if (action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) { 152 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 153 UserHandle.USER_NULL) == mManagedUser.getIdentifier()) { 154 getActivity().finish(); 155 } 156 return; 157 } 158 159 if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABLE) 160 || action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { 161 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 162 UserHandle.USER_NULL) == mManagedUser.getIdentifier()) { 163 mWorkModePreference.setChecked( 164 !mUserManager.isQuietModeEnabled(mManagedUser)); 165 } 166 return; 167 } 168 Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction()); 169 } 170 171 register(Context context)172 public void register(Context context) { 173 IntentFilter intentFilter = new IntentFilter(); 174 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); 175 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); 176 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); 177 context.registerReceiver(this, intentFilter); 178 } 179 unregister(Context context)180 public void unregister(Context context) { 181 context.unregisterReceiver(this); 182 } 183 } 184 185 } 186