Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 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 | |
Jonathan Scott | ed70157 | 2019-04-15 15:36:17 +0100 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 20 | import android.app.settings.SettingsEnums; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
Jonathan Scott | ed70157 | 2019-04-15 15:36:17 +0100 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 29 | import android.provider.SearchIndexableResource; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Jonathan Scott | ed70157 | 2019-04-15 15:36:17 +0100 | [diff] [blame] | 32 | import androidx.preference.Preference; |
| 33 | import androidx.preference.PreferenceGroup; |
| 34 | import androidx.preference.PreferenceManager; |
| 35 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 36 | import com.android.settings.R; |
Ricky Wai | bf94d09 | 2017-02-01 15:26:29 +0000 | [diff] [blame] | 37 | import com.android.settings.Utils; |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 38 | import com.android.settings.dashboard.DashboardFragment; |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 39 | import com.android.settings.search.BaseSearchIndexProvider; |
Raff Tsai | 966fa01 | 2019-09-25 11:19:06 +0800 | [diff] [blame] | 40 | import com.android.settingslib.search.Indexable; |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 41 | import com.android.settingslib.search.SearchIndexable; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
Jonathan Scott | ed70157 | 2019-04-15 15:36:17 +0100 | [diff] [blame] | 45 | import java.util.Set; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 46 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 47 | /** |
| 48 | * Setting page for managed profile. |
| 49 | * FIXME: It currently assumes there is only one managed profile. |
| 50 | */ |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 51 | @SearchIndexable |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 52 | public class ManagedProfileSettings extends DashboardFragment { |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 53 | |
| 54 | private UserManager mUserManager; |
| 55 | private UserHandle mManagedUser; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 56 | |
| 57 | private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver; |
| 58 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 59 | private static final String TAG = "ManagedProfileSettings"; |
| 60 | |
| 61 | @Override |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 62 | protected String getLogTag() { |
| 63 | return TAG; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected int getPreferenceScreenResId() { |
| 68 | return R.xml.managed_profile_settings; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onAttach(Context context) { |
| 73 | super.onAttach(context); |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 74 | mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 75 | mManagedUser = getManagedUserFromArgument(); |
| 76 | if (mManagedUser == null) { |
| 77 | getActivity().finish(); |
| 78 | } |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 79 | use(WorkModePreferenceController.class).setManagedUser(mManagedUser); |
| 80 | use(ContactSearchPreferenceController.class).setManagedUser(mManagedUser); |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 81 | use(CrossProfileCalendarPreferenceController.class).setManagedUser(mManagedUser); |
Jonathan Scott | ed70157 | 2019-04-15 15:36:17 +0100 | [diff] [blame] | 82 | use(CrossProfileCalendarDisabledPreferenceController.class).setManagedUser(mManagedUser); |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onCreate(Bundle icicle) { |
| 87 | super.onCreate(icicle); |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 88 | mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver(); |
| 89 | mManagedProfileBroadcastReceiver.register(getActivity()); |
| 90 | } |
| 91 | |
| 92 | @Override |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 93 | public void onDestroy() { |
| 94 | super.onDestroy(); |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 95 | if (mManagedProfileBroadcastReceiver != null) { |
| 96 | mManagedProfileBroadcastReceiver.unregister(getActivity()); |
| 97 | } |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private UserHandle getManagedUserFromArgument() { |
| 101 | Bundle arguments = getArguments(); |
| 102 | if (arguments != null) { |
| 103 | UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER); |
| 104 | if (userHandle != null) { |
| 105 | if (mUserManager.isManagedProfile(userHandle.getIdentifier())) { |
| 106 | return userHandle; |
| 107 | } |
| 108 | } |
| 109 | } |
Ricky Wai | bf94d09 | 2017-02-01 15:26:29 +0000 | [diff] [blame] | 110 | // Return default managed profile for the current user. |
| 111 | return Utils.getManagedProfile(mUserManager); |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 114 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 115 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 116 | return SettingsEnums.ACCOUNTS_WORK_PROFILE_SETTINGS; |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Raff Tsai | ac3e0d0 | 2019-09-19 17:06:45 +0800 | [diff] [blame] | 119 | public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
yuemingw | a35990b | 2018-10-24 13:02:05 +0100 | [diff] [blame] | 120 | new BaseSearchIndexProvider() { |
| 121 | @Override |
| 122 | public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, |
| 123 | boolean enabled) { |
| 124 | final ArrayList<SearchIndexableResource> result = new ArrayList<>(); |
| 125 | final SearchIndexableResource sir = new SearchIndexableResource(context); |
| 126 | sir.xmlResId = R.xml.managed_profile_settings; |
| 127 | result.add(sir); |
| 128 | return result; |
| 129 | } |
| 130 | @Override |
| 131 | protected boolean isPageSearchEnabled(Context context) { |
| 132 | return false; |
| 133 | } |
| 134 | }; |
| 135 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 136 | private class ManagedProfileBroadcastReceiver extends BroadcastReceiver { |
| 137 | |
| 138 | @Override |
| 139 | public void onReceive(Context context, Intent intent) { |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 140 | if (intent == null) { |
| 141 | return; |
| 142 | } |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 143 | final String action = intent.getAction(); |
| 144 | Log.v(TAG, "Received broadcast: " + action); |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 145 | if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 146 | if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 147 | UserHandle.USER_NULL) == mManagedUser.getIdentifier()) { |
| 148 | getActivity().finish(); |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 153 | Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction()); |
| 154 | } |
| 155 | |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 156 | public void register(Context context) { |
| 157 | IntentFilter intentFilter = new IntentFilter(); |
| 158 | intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 159 | context.registerReceiver(this, intentFilter); |
| 160 | } |
| 161 | |
| 162 | public void unregister(Context context) { |
| 163 | context.unregisterReceiver(this); |
| 164 | } |
| 165 | } |
Tony Mak | 64f6d98 | 2016-05-03 18:55:41 +0100 | [diff] [blame] | 166 | } |