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