1 /*
2  * Copyright (C) 2015 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.systemui.qs.tiles;
18 
19 import static android.app.admin.DevicePolicyResources.Strings.SystemUi.QS_WORK_PROFILE_LABEL;
20 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Intent;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.provider.Settings;
26 import android.service.quicksettings.Tile;
27 import android.widget.Switch;
28 
29 import androidx.annotation.MainThread;
30 import androidx.annotation.Nullable;
31 
32 import com.android.internal.logging.MetricsLogger;
33 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
34 import com.android.systemui.animation.Expandable;
35 import com.android.systemui.dagger.qualifiers.Background;
36 import com.android.systemui.dagger.qualifiers.Main;
37 import com.android.systemui.plugins.ActivityStarter;
38 import com.android.systemui.plugins.FalsingManager;
39 import com.android.systemui.plugins.qs.QSTile.BooleanState;
40 import com.android.systemui.plugins.statusbar.StatusBarStateController;
41 import com.android.systemui.qs.QSHost;
42 import com.android.systemui.qs.QsEventLogger;
43 import com.android.systemui.qs.logging.QSLogger;
44 import com.android.systemui.qs.tileimpl.QSTileImpl;
45 import com.android.systemui.res.R;
46 import com.android.systemui.statusbar.phone.ManagedProfileController;
47 
48 import javax.inject.Inject;
49 
50 /** Quick settings tile: Work profile on/off */
51 public class WorkModeTile extends QSTileImpl<BooleanState> implements
52         ManagedProfileController.Callback {
53 
54     public static final String TILE_SPEC = "work";
55 
56     private final Icon mIcon = ResourceIcon.get(
57             com.android.internal.R.drawable.stat_sys_managed_profile_status);
58 
59     private final ManagedProfileController mProfileController;
60 
61     @Inject
WorkModeTile( QSHost host, QsEventLogger uiEventLogger, @Background Looper backgroundLooper, @Main Handler mainHandler, FalsingManager falsingManager, MetricsLogger metricsLogger, StatusBarStateController statusBarStateController, ActivityStarter activityStarter, QSLogger qsLogger, ManagedProfileController managedProfileController )62     public WorkModeTile(
63             QSHost host,
64             QsEventLogger uiEventLogger,
65             @Background Looper backgroundLooper,
66             @Main Handler mainHandler,
67             FalsingManager falsingManager,
68             MetricsLogger metricsLogger,
69             StatusBarStateController statusBarStateController,
70             ActivityStarter activityStarter,
71             QSLogger qsLogger,
72             ManagedProfileController managedProfileController
73     ) {
74         super(host, uiEventLogger, backgroundLooper, mainHandler, falsingManager, metricsLogger,
75                 statusBarStateController, activityStarter, qsLogger);
76         mProfileController = managedProfileController;
77         mProfileController.observe(getLifecycle(), this);
78     }
79 
80     @Override
newTileState()81     public BooleanState newTileState() {
82         return new BooleanState();
83     }
84 
85     @Override
getLongClickIntent()86     public Intent getLongClickIntent() {
87         return new Intent(Settings.ACTION_MANAGED_PROFILE_SETTINGS);
88     }
89 
90     @Override
handleClick(@ullable Expandable expandable)91     public void handleClick(@Nullable Expandable expandable) {
92         mProfileController.setWorkModeEnabled(!mState.value);
93     }
94 
95     @Override
isAvailable()96     public boolean isAvailable() {
97         return mProfileController.hasActiveProfile();
98     }
99 
100     @Override
101     @MainThread
onManagedProfileChanged()102     public void onManagedProfileChanged() {
103         refreshState(mProfileController.isWorkModeEnabled());
104     }
105 
106     @Override
107     @MainThread
onManagedProfileRemoved()108     public void onManagedProfileRemoved() {
109         mHost.removeTile(getTileSpec());
110     }
111 
112     @Override
getTileLabel()113     public CharSequence getTileLabel() {
114         DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
115         return dpm.getResources().getString(QS_WORK_PROFILE_LABEL,
116                 () -> mContext.getString(R.string.quick_settings_work_mode_label));
117     }
118 
119     @Override
handleUpdateState(BooleanState state, Object arg)120     protected void handleUpdateState(BooleanState state, Object arg) {
121         if (!isAvailable()) {
122             onManagedProfileRemoved();
123         }
124 
125         if (arg instanceof Boolean) {
126             state.value = (Boolean) arg;
127         } else {
128             state.value = mProfileController.isWorkModeEnabled();
129         }
130 
131         state.icon = mIcon;
132         state.label = getTileLabel();
133         state.contentDescription = state.label;
134         state.expandedAccessibilityClassName = Switch.class.getName();
135         state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
136         state.secondaryLabel = state.value
137                 ? ""
138                 : mContext.getString(R.string.quick_settings_work_mode_paused_state);
139     }
140 
141     @Override
getMetricsCategory()142     public int getMetricsCategory() {
143         return MetricsEvent.QS_WORKMODE;
144     }
145 }
146