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