• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settingslib.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     }
83 
84     @Override
onCreate(Bundle icicle)85     public void onCreate(Bundle icicle) {
86         super.onCreate(icicle);
87         mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
88         mManagedProfileBroadcastReceiver.register(getActivity());
89     }
90 
91     @Override
onDestroy()92     public void onDestroy() {
93         super.onDestroy();
94         if (mManagedProfileBroadcastReceiver != null) {
95             mManagedProfileBroadcastReceiver.unregister(getActivity());
96         }
97     }
98 
getManagedUserFromArgument()99     private UserHandle getManagedUserFromArgument() {
100         Bundle arguments = getArguments();
101         if (arguments != null) {
102             UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER);
103             if (userHandle != null) {
104                 if (mUserManager.isManagedProfile(userHandle.getIdentifier())) {
105                     return userHandle;
106                 }
107             }
108         }
109         // Return default managed profile for the current user.
110         return Utils.getManagedProfile(mUserManager);
111     }
112 
113     @Override
getMetricsCategory()114     public int getMetricsCategory() {
115         return SettingsEnums.ACCOUNTS_WORK_PROFILE_SETTINGS;
116     }
117 
118     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
119             new BaseSearchIndexProvider() {
120                 @Override
121                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
122                         boolean enabled) {
123                     final ArrayList<SearchIndexableResource> result = new ArrayList<>();
124                     final SearchIndexableResource sir = new SearchIndexableResource(context);
125                     sir.xmlResId = R.xml.managed_profile_settings;
126                     result.add(sir);
127                     return result;
128                 }
129                 @Override
130                 protected boolean isPageSearchEnabled(Context context) {
131                     return false;
132                 }
133             };
134 
135     private class ManagedProfileBroadcastReceiver extends BroadcastReceiver {
136 
137         @Override
onReceive(Context context, Intent intent)138         public void onReceive(Context context, Intent intent) {
139             if (intent == null) {
140                 return;
141             }
142             final String action = intent.getAction();
143             Log.v(TAG, "Received broadcast: " + action);
144             if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
145                 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
146                         UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
147                     getActivity().finish();
148                 }
149                 return;
150             }
151 
152             Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
153         }
154 
register(Context context)155         public void register(Context context) {
156             IntentFilter intentFilter = new IntentFilter();
157             intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
158             context.registerReceiver(this, intentFilter);
159         }
160 
unregister(Context context)161         public void unregister(Context context) {
162             context.unregisterReceiver(this);
163         }
164     }
165 }
166