import android.os.UserHandle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
+import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceClickListener;
+import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
*/
public class AccountSettings extends SettingsPreferenceFragment
implements AuthenticatorHelper.OnAccountsUpdateListener,
- OnPreferenceClickListener, Indexable {
+ OnPreferenceClickListener, OnPreferenceChangeListener, Indexable {
public static final String TAG = "AccountSettings";
private static final String KEY_ACCOUNT = "account";
private static final String ADD_ACCOUNT_ACTION = "android.settings.ADD_ACCOUNT_SETTINGS";
private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
- private static final int ORDER_LAST = 1001;
- private static final int ORDER_NEXT_TO_LAST = 1000;
+ private static final int ORDER_LAST = 1002;
+ private static final int ORDER_NEXT_TO_LAST = 1001;
+ private static final int ORDER_NEXT_TO_NEXT_TO_LAST = 1000;
private UserManager mUm;
private SparseArray<ProfileData> mProfiles = new SparseArray<ProfileData>();
*/
public Preference addAccountPreference;
/**
+ * The preference that displays the button to toggle work profile.
+ */
+ public SwitchPreference workModeSwitch;
+ /**
* The preference that displays the button to remove the managed profile
*/
public Preference removeWorkProfilePreference;
return false;
}
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ // Check the preference
+ final int count = mProfiles.size();
+ for (int i = 0; i < count; i++) {
+ ProfileData profileData = mProfiles.valueAt(i);
+ if (preference == profileData.workModeSwitch) {
+ final int userId = profileData.userInfo.id;
+ mUm.setQuietModeEnabled(userId, !((boolean) newValue));
+ return true;
+ }
+ }
+ return false;
+ }
+
void updateUi() {
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.account_settings);
profileData.preferenceGroup.setSummary(workGroupSummary);
((AccessiblePreferenceCategory) profileData.preferenceGroup).setContentDescription(
getString(R.string.accessibility_category_work, workGroupSummary));
+ profileData.workModeSwitch = newWorkModeSwitchPreference(context);
+ final UserHandle userHandle = profileData.userInfo.getUserHandle();
+ profileData.workModeSwitch.setChecked(!mUm.isQuietModeEnabled(userHandle));
profileData.removeWorkProfilePreference = newRemoveWorkProfilePreference(context);
} else {
profileData.preferenceGroup.setTitle(R.string.category_personal);
preference.setTitle(R.string.add_account_label);
preference.setIcon(R.drawable.ic_menu_add);
preference.setOnPreferenceClickListener(this);
+ preference.setOrder(ORDER_NEXT_TO_NEXT_TO_LAST);
+ return preference;
+ }
+
+ private SwitchPreference newWorkModeSwitchPreference(Context context) {
+ SwitchPreference preference = new SwitchPreference(getPrefContext());
+ preference.setTitle(R.string.work_mode_label);
+ preference.setSummary(R.string.work_mode_summary);
+ preference.setOnPreferenceChangeListener(this);
preference.setOrder(ORDER_NEXT_TO_LAST);
return preference;
}
R.string.managed_profile_not_available_label);
profileData.preferenceGroup.addPreference(mProfileNotAvailablePreference);
}
+ if (profileData.workModeSwitch != null) {
+ profileData.preferenceGroup.addPreference(profileData.workModeSwitch);
+ }
if (profileData.removeWorkProfilePreference != null) {
profileData.preferenceGroup.addPreference(profileData.removeWorkProfilePreference);
}
@Override
public void onReceive(Context context, Intent intent) {
+ Log.v(TAG, "Received broadcast: " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)
|| intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_ADDED)) {
- Log.v(TAG, "Received broadcast: " + intent.getAction());
// Clean old state
stopListeningToAccountUpdates();
cleanUpPreferences();
getActivity().invalidateOptionsMenu();
return;
}
+
+ if (intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED)) {
+ // We assume there's only one managed profile, otherwise this needs to change.
+ ProfileData profileData = mProfiles.valueAt(1);
+ if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
+ UserHandle.USER_NULL) == profileData.userInfo.id) {
+ profileData.workModeSwitch.setChecked(
+ !mUm.isQuietModeEnabled(profileData.userInfo.getUserHandle()));
+ }
+ return;
+ }
Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
+ intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED);
context.registerReceiver(this, intentFilter);
listeningToManagedProfileEvents = true;
}