• 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 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 
76         if (mManagedUser == null) {
77             getActivity().finish();
78         }
79     }
80 
81     @Override
onCreate(Bundle icicle)82     public void onCreate(Bundle icicle) {
83         super.onCreate(icicle);
84         mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
85         mManagedProfileBroadcastReceiver.register(getActivity());
86         replaceEnterprisePreferenceScreenTitle(
87                 MANAGED_PROFILE_SETTINGS_TITLE, R.string.managed_profile_settings_title);
88         replaceEnterpriseStringTitle("work_mode",
89                 WORK_PROFILE_SETTING, R.string.work_mode_label);
90         replaceEnterpriseStringTitle("contacts_search",
91                 WORK_PROFILE_CONTACT_SEARCH_TITLE, R.string.managed_profile_contact_search_title);
92         replaceEnterpriseStringSummary("contacts_search",
93                 WORK_PROFILE_CONTACT_SEARCH_SUMMARY,
94                 R.string.managed_profile_contact_search_summary);
95         replaceEnterpriseStringTitle("cross_profile_calendar",
96                 CROSS_PROFILE_CALENDAR_TITLE, R.string.cross_profile_calendar_title);
97         replaceEnterpriseStringSummary("cross_profile_calendar",
98                 CROSS_PROFILE_CALENDAR_SUMMARY, R.string.cross_profile_calendar_summary);
99     }
100 
101     @Override
onDestroy()102     public void onDestroy() {
103         super.onDestroy();
104         if (mManagedProfileBroadcastReceiver != null) {
105             mManagedProfileBroadcastReceiver.unregister(getActivity());
106         }
107     }
108 
getManagedUserFromArgument()109     private UserHandle getManagedUserFromArgument() {
110         Bundle arguments = getArguments();
111         if (arguments != null) {
112             UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER);
113             if (userHandle != null) {
114                 if (mUserManager.isManagedProfile(userHandle.getIdentifier())) {
115                     return userHandle;
116                 }
117             }
118         }
119         // Return default managed profile for the current user.
120         return Utils.getManagedProfile(mUserManager);
121     }
122 
123     @Override
getMetricsCategory()124     public int getMetricsCategory() {
125         return SettingsEnums.ACCOUNTS_WORK_PROFILE_SETTINGS;
126     }
127 
128     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
129             new BaseSearchIndexProvider() {
130                 @Override
131                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
132                         boolean enabled) {
133                     final ArrayList<SearchIndexableResource> result = new ArrayList<>();
134                     final SearchIndexableResource sir = new SearchIndexableResource(context);
135                     sir.xmlResId = R.xml.managed_profile_settings;
136                     result.add(sir);
137                     return result;
138                 }
139                 @Override
140                 protected boolean isPageSearchEnabled(Context context) {
141                     UserManager userManager = context.getSystemService(UserManager.class);
142                     UserHandle managedUser = Utils.getManagedProfile(userManager);
143                     return managedUser != null;
144                 }
145 
146             };
147 
148     private class ManagedProfileBroadcastReceiver extends BroadcastReceiver {
149 
150         @Override
onReceive(Context context, Intent intent)151         public void onReceive(Context context, Intent intent) {
152             if (intent == null) {
153                 return;
154             }
155             final String action = intent.getAction();
156             Log.v(TAG, "Received broadcast: " + action);
157             if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
158                 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
159                         UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
160                     getActivity().finish();
161                 }
162                 return;
163             }
164 
165             Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
166         }
167 
register(Context context)168         public void register(Context context) {
169             IntentFilter intentFilter = new IntentFilter();
170             intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
171             context.registerReceiver(this, intentFilter);
172         }
173 
unregister(Context context)174         public void unregister(Context context) {
175             context.unregisterReceiver(this);
176         }
177     }
178 }
179