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 static android.app.admin.DevicePolicyResources.Strings.Settings.CROSS_PROFILE_CALENDAR_SUMMARY; 20 import static android.app.admin.DevicePolicyResources.Strings.Settings.CROSS_PROFILE_CALENDAR_TITLE; 21 import static android.app.admin.DevicePolicyResources.Strings.Settings.MANAGED_PROFILE_SETTINGS_TITLE; 22 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_CONTACT_SEARCH_SUMMARY; 23 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_CONTACT_SEARCH_TITLE; 24 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_SETTING; 25 26 import android.app.settings.SettingsEnums; 27 import android.content.BroadcastReceiver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.IntentFilter; 31 import android.os.Bundle; 32 import android.os.UserHandle; 33 import android.os.UserManager; 34 import android.provider.SearchIndexableResource; 35 import android.util.Log; 36 37 import com.android.settings.R; 38 import com.android.settings.Utils; 39 import com.android.settings.dashboard.DashboardFragment; 40 import com.android.settings.search.BaseSearchIndexProvider; 41 import com.android.settingslib.search.SearchIndexable; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 /** 47 * Setting page for managed profile. 48 * FIXME: It currently assumes there is only one managed profile. 49 */ 50 @SearchIndexable 51 public class ManagedProfileSettings extends DashboardFragment { 52 53 private UserManager mUserManager; 54 private UserHandle mManagedUser; 55 56 private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver; 57 58 private static final String TAG = "ManagedProfileSettings"; 59 60 @Override getLogTag()61 protected String getLogTag() { 62 return TAG; 63 } 64 65 @Override getPreferenceScreenResId()66 protected int getPreferenceScreenResId() { 67 return R.xml.managed_profile_settings; 68 } 69 70 @Override onAttach(Context context)71 public void onAttach(Context context) { 72 super.onAttach(context); 73 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); 74 mManagedUser = getManagedUserFromArgument(); 75 if (mManagedUser == null) { 76 getActivity().finish(); 77 } 78 use(WorkModePreferenceController.class).setManagedUser(mManagedUser); 79 use(ContactSearchPreferenceController.class).setManagedUser(mManagedUser); 80 use(CrossProfileCalendarPreferenceController.class).setManagedUser(mManagedUser); 81 } 82 83 @Override onCreate(Bundle icicle)84 public void onCreate(Bundle icicle) { 85 super.onCreate(icicle); 86 mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver(); 87 mManagedProfileBroadcastReceiver.register(getActivity()); 88 replaceEnterprisePreferenceScreenTitle( 89 MANAGED_PROFILE_SETTINGS_TITLE, R.string.managed_profile_settings_title); 90 replaceEnterpriseStringTitle("work_mode", 91 WORK_PROFILE_SETTING, R.string.work_mode_label); 92 replaceEnterpriseStringTitle("contacts_search", 93 WORK_PROFILE_CONTACT_SEARCH_TITLE, R.string.managed_profile_contact_search_title); 94 replaceEnterpriseStringSummary("contacts_search", 95 WORK_PROFILE_CONTACT_SEARCH_SUMMARY, 96 R.string.managed_profile_contact_search_summary); 97 replaceEnterpriseStringTitle("cross_profile_calendar", 98 CROSS_PROFILE_CALENDAR_TITLE, R.string.cross_profile_calendar_title); 99 replaceEnterpriseStringSummary("cross_profile_calendar", 100 CROSS_PROFILE_CALENDAR_SUMMARY, R.string.cross_profile_calendar_summary); 101 } 102 103 @Override onDestroy()104 public void onDestroy() { 105 super.onDestroy(); 106 if (mManagedProfileBroadcastReceiver != null) { 107 mManagedProfileBroadcastReceiver.unregister(getActivity()); 108 } 109 } 110 getManagedUserFromArgument()111 private UserHandle getManagedUserFromArgument() { 112 Bundle arguments = getArguments(); 113 if (arguments != null) { 114 UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER); 115 if (userHandle != null) { 116 if (mUserManager.isManagedProfile(userHandle.getIdentifier())) { 117 return userHandle; 118 } 119 } 120 } 121 // Return default managed profile for the current user. 122 return Utils.getManagedProfile(mUserManager); 123 } 124 125 @Override getMetricsCategory()126 public int getMetricsCategory() { 127 return SettingsEnums.ACCOUNTS_WORK_PROFILE_SETTINGS; 128 } 129 130 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 131 new BaseSearchIndexProvider() { 132 @Override 133 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 134 boolean enabled) { 135 final ArrayList<SearchIndexableResource> result = new ArrayList<>(); 136 final SearchIndexableResource sir = new SearchIndexableResource(context); 137 sir.xmlResId = R.xml.managed_profile_settings; 138 result.add(sir); 139 return result; 140 } 141 @Override 142 protected boolean isPageSearchEnabled(Context context) { 143 return false; 144 } 145 }; 146 147 private class ManagedProfileBroadcastReceiver extends BroadcastReceiver { 148 149 @Override onReceive(Context context, Intent intent)150 public void onReceive(Context context, Intent intent) { 151 if (intent == null) { 152 return; 153 } 154 final String action = intent.getAction(); 155 Log.v(TAG, "Received broadcast: " + action); 156 if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { 157 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 158 UserHandle.USER_NULL) == mManagedUser.getIdentifier()) { 159 getActivity().finish(); 160 } 161 return; 162 } 163 164 Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction()); 165 } 166 register(Context context)167 public void register(Context context) { 168 IntentFilter intentFilter = new IntentFilter(); 169 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); 170 context.registerReceiver(this, intentFilter); 171 } 172 unregister(Context context)173 public void unregister(Context context) { 174 context.unregisterReceiver(this); 175 } 176 } 177 } 178