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