• 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.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.provider.Settings;
27 import android.support.v14.preference.SwitchPreference;
28 import android.support.v7.preference.Preference;
29 import android.util.Log;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settings.R;
33 import com.android.settings.SettingsPreferenceFragment;
34 import com.android.settings.Utils;
35 import com.android.settingslib.RestrictedLockUtils;
36 import com.android.settingslib.RestrictedSwitchPreference;
37 
38 import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
39 
40 /**
41  * Setting page for managed profile.
42  * FIXME: It currently assumes there is only one managed profile.
43  */
44 public class ManagedProfileSettings extends SettingsPreferenceFragment
45         implements Preference.OnPreferenceChangeListener {
46 
47     private SwitchPreference mWorkModePreference;
48     private RestrictedSwitchPreference mContactPrefrence;
49 
50     private UserManager mUserManager;
51     private UserHandle mManagedUser;
52     private Context mContext;
53 
54     private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver;
55 
56     private static final String KEY_WORK_MODE = "work_mode";
57     private static final String KEY_CONTACT = "contacts_search";
58 
59     private static final String TAG = "ManagedProfileSettings";
60 
61     @Override
onCreate(Bundle icicle)62     public void onCreate(Bundle icicle) {
63         super.onCreate(icicle);
64         addPreferencesFromResource(R.xml.managed_profile_settings);
65         mWorkModePreference = (SwitchPreference) findPreference(KEY_WORK_MODE);
66         mWorkModePreference.setOnPreferenceChangeListener(this);
67         mContactPrefrence = (RestrictedSwitchPreference) findPreference(KEY_CONTACT);
68         mContactPrefrence.setOnPreferenceChangeListener(this);
69         mContext = getActivity().getApplicationContext();
70         mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
71         mManagedUser = getManagedUserFromArgument();
72         if (mManagedUser == null) {
73             getActivity().finish();
74         }
75         mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
76         mManagedProfileBroadcastReceiver.register(getActivity());
77     }
78 
79     @Override
onResume()80     public void onResume() {
81         super.onResume();
82         loadDataAndPopulateUi();
83     }
84 
85     @Override
onDestroy()86     public void onDestroy() {
87         super.onDestroy();
88         mManagedProfileBroadcastReceiver.unregister(getActivity());
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 
loadDataAndPopulateUi()105     private void loadDataAndPopulateUi() {
106         if (mWorkModePreference != null) {
107             mWorkModePreference.setChecked(
108                     !mUserManager.isQuietModeEnabled(mManagedUser));
109         }
110 
111         if (mContactPrefrence != null) {
112             int value = Settings.Secure.getIntForUser(getContentResolver(),
113                     MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
114             mContactPrefrence.setChecked(value != 0);
115             RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
116                     RestrictedLockUtils.checkIfRemoteContactSearchDisallowed(
117                             mContext, mManagedUser.getIdentifier());
118             mContactPrefrence.setDisabledByAdmin(enforcedAdmin);
119         }
120     }
121 
122     @Override
getMetricsCategory()123     public int getMetricsCategory() {
124         return MetricsProto.MetricsEvent.ACCOUNTS_WORK_PROFILE_SETTINGS;
125     }
126 
127 
128     @Override
onPreferenceChange(Preference preference, Object newValue)129     public boolean onPreferenceChange(Preference preference, Object newValue) {
130         if (preference == mWorkModePreference) {
131             if ((boolean) newValue) {
132                 mUserManager.trySetQuietModeDisabled(mManagedUser.getIdentifier(), null);
133             } else {
134                 mUserManager.setQuietModeEnabled(mManagedUser.getIdentifier(), true);
135             }
136             return true;
137         }
138         if (preference == mContactPrefrence) {
139             int value = ((boolean) newValue == true) ? 1 : 0;
140             Settings.Secure.putIntForUser(getContentResolver(),
141                     MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier());
142             return true;
143         }
144         return false;
145     }
146 
147     private class ManagedProfileBroadcastReceiver extends BroadcastReceiver {
148 
149         @Override
onReceive(Context context, Intent intent)150         public void onReceive(Context context, Intent intent) {
151             final String action = intent.getAction();
152             Log.v(TAG, "Received broadcast: " + action);
153             if (action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
154                 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
155                         UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
156                     getActivity().finish();
157                 }
158                 return;
159             }
160 
161             if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
162                     || action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
163                 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
164                         UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
165                     mWorkModePreference.setChecked(
166                             !mUserManager.isQuietModeEnabled(mManagedUser));
167                 }
168                 return;
169             }
170             Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
171         }
172 
173 
register(Context context)174         public void register(Context context) {
175             IntentFilter intentFilter = new IntentFilter();
176             intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
177             intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
178             intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
179             context.registerReceiver(this, intentFilter);
180         }
181 
unregister(Context context)182         public void unregister(Context context) {
183             context.unregisterReceiver(this);
184         }
185     }
186 
187 }
188