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.dashboard.conditional; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.UserInfo; 22 import android.graphics.drawable.Drawable; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 27 import com.android.settings.R; 28 import com.android.settings.Settings; 29 30 import java.util.List; 31 32 public class WorkModeCondition extends Condition { 33 34 private UserManager mUm; 35 private UserHandle mUserHandle; 36 WorkModeCondition(ConditionManager conditionManager)37 public WorkModeCondition(ConditionManager conditionManager) { 38 super(conditionManager); 39 mUm = (UserManager) mManager.getContext().getSystemService(Context.USER_SERVICE); 40 } 41 updateUserHandle()42 private void updateUserHandle() { 43 List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId()); 44 final int profilesCount = profiles.size(); 45 mUserHandle = null; 46 for (int i = 0; i < profilesCount; i++) { 47 UserInfo userInfo = profiles.get(i); 48 if (userInfo.isManagedProfile()) { 49 // We assume there's only one managed profile, otherwise UI needs to change. 50 mUserHandle = userInfo.getUserHandle(); 51 break; 52 } 53 } 54 } 55 56 @Override refreshState()57 public void refreshState() { 58 updateUserHandle(); 59 setActive(mUserHandle != null && mUm.isQuietModeEnabled(mUserHandle)); 60 } 61 62 @Override getIcon()63 public Drawable getIcon() { 64 return mManager.getContext().getDrawable(R.drawable.ic_signal_workmode_enable); 65 } 66 67 @Override getTitle()68 public CharSequence getTitle() { 69 return mManager.getContext().getString(R.string.condition_work_title); 70 } 71 72 @Override getSummary()73 public CharSequence getSummary() { 74 return mManager.getContext().getString(R.string.condition_work_summary); 75 } 76 77 @Override getActions()78 public CharSequence[] getActions() { 79 return new CharSequence[] { 80 mManager.getContext().getString(R.string.condition_turn_on) 81 }; 82 } 83 84 @Override onPrimaryClick()85 public void onPrimaryClick() { 86 mManager.getContext().startActivity(new Intent(mManager.getContext(), 87 Settings.AccountDashboardActivity.class) 88 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 89 } 90 91 @Override onActionClick(int index)92 public void onActionClick(int index) { 93 if (index == 0) { 94 if (mUserHandle != null) { 95 mUm.requestQuietModeEnabled(false, mUserHandle); 96 } 97 setActive(false); 98 } else { 99 throw new IllegalArgumentException("Unexpected index " + index); 100 } 101 } 102 103 @Override getMetricsConstant()104 public int getMetricsConstant() { 105 return MetricsEvent.SETTINGS_CONDITION_WORK_MODE; 106 } 107 } 108