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
17package com.android.systemui.qs.tiles;
18
19import android.content.Intent;
20import android.provider.Settings;
21import android.widget.Switch;
22
23import com.android.internal.logging.MetricsLogger;
24import com.android.internal.logging.MetricsProto.MetricsEvent;
25import com.android.systemui.R;
26import com.android.systemui.qs.QSTile;
27import com.android.systemui.statusbar.phone.ManagedProfileController;
28
29/** Quick settings tile: Work profile on/off */
30public class WorkModeTile extends QSTile<QSTile.BooleanState> implements
31        ManagedProfileController.Callback {
32    private final AnimationIcon mEnable =
33            new AnimationIcon(R.drawable.ic_signal_workmode_enable_animation,
34                    R.drawable.ic_signal_workmode_disable);
35    private final AnimationIcon mDisable =
36            new AnimationIcon(R.drawable.ic_signal_workmode_disable_animation,
37                    R.drawable.ic_signal_workmode_enable);
38
39    private final ManagedProfileController mProfileController;
40
41    public WorkModeTile(Host host) {
42        super(host);
43        mProfileController = host.getManagedProfileController();
44    }
45
46    @Override
47    public BooleanState newTileState() {
48        return new BooleanState();
49    }
50
51    @Override
52    public void setListening(boolean listening) {
53        if (listening) {
54            mProfileController.addCallback(this);
55        } else {
56            mProfileController.removeCallback(this);
57        }
58    }
59
60    @Override
61    public Intent getLongClickIntent() {
62        return new Intent(Settings.ACTION_SYNC_SETTINGS);
63    }
64
65    @Override
66    public void handleClick() {
67        MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
68        mProfileController.setWorkModeEnabled(!mState.value);
69    }
70
71    @Override
72    public boolean isAvailable() {
73        return mProfileController.hasActiveProfile();
74    }
75
76    @Override
77    public void onManagedProfileChanged() {
78        refreshState(mProfileController.isWorkModeEnabled());
79    }
80
81    @Override
82    public void onManagedProfileRemoved() {
83        mHost.removeTile(getTileSpec());
84    }
85
86    @Override
87    public CharSequence getTileLabel() {
88        return mContext.getString(R.string.quick_settings_work_mode_label);
89    }
90
91    @Override
92    protected void handleUpdateState(BooleanState state, Object arg) {
93        if (arg instanceof Boolean) {
94            state.value = (Boolean) arg;
95        } else {
96            state.value = mProfileController.isWorkModeEnabled();
97        }
98
99        state.label = mContext.getString(R.string.quick_settings_work_mode_label);
100        if (state.value) {
101            state.icon = mEnable;
102            state.contentDescription =  mContext.getString(
103                    R.string.accessibility_quick_settings_work_mode_on);
104        } else {
105            state.icon = mDisable;
106            state.contentDescription =  mContext.getString(
107                    R.string.accessibility_quick_settings_work_mode_off);
108        }
109        state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
110                = Switch.class.getName();
111    }
112
113    @Override
114    public int getMetricsCategory() {
115        return MetricsEvent.QS_WORKMODE;
116    }
117
118    @Override
119    protected String composeChangeAnnouncement() {
120        if (mState.value) {
121            return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_on);
122        } else {
123            return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_off);
124        }
125    }
126}
127