blob: 6591be488a32a0963eaae0a71c515e880390f951 [file] [log] [blame]
Tony Mak64f6d982016-05-03 18:55:41 +01001/*
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
17package com.android.settings.accounts;
18
Jonathan Scotted701572019-04-15 15:36:17 +010019import android.app.admin.DevicePolicyManager;
Fan Zhang31b21002019-01-16 13:49:47 -080020import android.app.settings.SettingsEnums;
Tony Mak64f6d982016-05-03 18:55:41 +010021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
Jonathan Scotted701572019-04-15 15:36:17 +010025import android.content.pm.PackageManager;
Tony Mak64f6d982016-05-03 18:55:41 +010026import android.os.Bundle;
27import android.os.UserHandle;
28import android.os.UserManager;
yuemingwa35990b2018-10-24 13:02:05 +010029import android.provider.SearchIndexableResource;
Tony Mak64f6d982016-05-03 18:55:41 +010030import android.util.Log;
31
Jonathan Scotted701572019-04-15 15:36:17 +010032import androidx.preference.Preference;
33import androidx.preference.PreferenceGroup;
34import androidx.preference.PreferenceManager;
35
Tony Mak64f6d982016-05-03 18:55:41 +010036import com.android.settings.R;
Ricky Waibf94d092017-02-01 15:26:29 +000037import com.android.settings.Utils;
jason_chiuc69102d2018-04-03 11:37:09 +080038import com.android.settings.dashboard.DashboardFragment;
yuemingwa35990b2018-10-24 13:02:05 +010039import com.android.settings.search.BaseSearchIndexProvider;
Raff Tsai966fa012019-09-25 11:19:06 +080040import com.android.settingslib.search.Indexable;
yuemingwa35990b2018-10-24 13:02:05 +010041import com.android.settingslib.search.SearchIndexable;
42
43import java.util.ArrayList;
44import java.util.List;
Jonathan Scotted701572019-04-15 15:36:17 +010045import java.util.Set;
Tony Mak64f6d982016-05-03 18:55:41 +010046
Tony Mak64f6d982016-05-03 18:55:41 +010047/**
48 * Setting page for managed profile.
49 * FIXME: It currently assumes there is only one managed profile.
50 */
yuemingwa35990b2018-10-24 13:02:05 +010051@SearchIndexable
jason_chiuc69102d2018-04-03 11:37:09 +080052public class ManagedProfileSettings extends DashboardFragment {
Tony Mak64f6d982016-05-03 18:55:41 +010053
54 private UserManager mUserManager;
55 private UserHandle mManagedUser;
Tony Mak64f6d982016-05-03 18:55:41 +010056
57 private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver;
58
Tony Mak64f6d982016-05-03 18:55:41 +010059 private static final String TAG = "ManagedProfileSettings";
60
61 @Override
jason_chiuc69102d2018-04-03 11:37:09 +080062 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 Mak64f6d982016-05-03 18:55:41 +010074 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
75 mManagedUser = getManagedUserFromArgument();
76 if (mManagedUser == null) {
77 getActivity().finish();
78 }
jason_chiuc69102d2018-04-03 11:37:09 +080079 use(WorkModePreferenceController.class).setManagedUser(mManagedUser);
80 use(ContactSearchPreferenceController.class).setManagedUser(mManagedUser);
yuemingwa35990b2018-10-24 13:02:05 +010081 use(CrossProfileCalendarPreferenceController.class).setManagedUser(mManagedUser);
Jonathan Scotted701572019-04-15 15:36:17 +010082 use(CrossProfileCalendarDisabledPreferenceController.class).setManagedUser(mManagedUser);
jason_chiuc69102d2018-04-03 11:37:09 +080083 }
84
85 @Override
86 public void onCreate(Bundle icicle) {
87 super.onCreate(icicle);
Tony Mak64f6d982016-05-03 18:55:41 +010088 mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
89 mManagedProfileBroadcastReceiver.register(getActivity());
90 }
91
92 @Override
Tony Mak64f6d982016-05-03 18:55:41 +010093 public void onDestroy() {
94 super.onDestroy();
jason_chiuc69102d2018-04-03 11:37:09 +080095 if (mManagedProfileBroadcastReceiver != null) {
96 mManagedProfileBroadcastReceiver.unregister(getActivity());
97 }
Tony Mak64f6d982016-05-03 18:55:41 +010098 }
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 Waibf94d092017-02-01 15:26:29 +0000110 // Return default managed profile for the current user.
111 return Utils.getManagedProfile(mUserManager);
Tony Mak64f6d982016-05-03 18:55:41 +0100112 }
113
Tony Mak64f6d982016-05-03 18:55:41 +0100114 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700115 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800116 return SettingsEnums.ACCOUNTS_WORK_PROFILE_SETTINGS;
Tony Mak64f6d982016-05-03 18:55:41 +0100117 }
118
Raff Tsaiac3e0d02019-09-19 17:06:45 +0800119 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
yuemingwa35990b2018-10-24 13:02:05 +0100120 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 Mak64f6d982016-05-03 18:55:41 +0100136 private class ManagedProfileBroadcastReceiver extends BroadcastReceiver {
137
138 @Override
139 public void onReceive(Context context, Intent intent) {
jason_chiuc69102d2018-04-03 11:37:09 +0800140 if (intent == null) {
141 return;
142 }
Tony Mak64f6d982016-05-03 18:55:41 +0100143 final String action = intent.getAction();
144 Log.v(TAG, "Received broadcast: " + action);
jason_chiuc69102d2018-04-03 11:37:09 +0800145 if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Tony Mak64f6d982016-05-03 18:55:41 +0100146 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
147 UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
148 getActivity().finish();
149 }
150 return;
151 }
152
Tony Mak64f6d982016-05-03 18:55:41 +0100153 Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
154 }
155
Tony Mak64f6d982016-05-03 18:55:41 +0100156 public void register(Context context) {
157 IntentFilter intentFilter = new IntentFilter();
158 intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Tony Mak64f6d982016-05-03 18:55:41 +0100159 context.registerReceiver(this, intentFilter);
160 }
161
162 public void unregister(Context context) {
163 context.unregisterReceiver(this);
164 }
165 }
Tony Mak64f6d982016-05-03 18:55:41 +0100166}