WorkModeTile.java revision ba2318eff373a729f9768b146350a001dd22e4c5
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.app.ActivityManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.pm.UserInfo;
25import android.os.UserHandle;
26import android.os.UserManager;
27import com.android.internal.logging.MetricsLogger;
28import com.android.systemui.R;
29import com.android.systemui.qs.QSTile;
30
31import java.util.LinkedList;
32import java.util.List;
33
34/** Quick settings tile: Work profile on/off */
35public class WorkModeTile extends QSTile<QSTile.BooleanState> {
36    private final AnimationIcon mEnable =
37            new AnimationIcon(R.drawable.ic_signal_workmode_enable_animation);
38    private final AnimationIcon mDisable =
39            new AnimationIcon(R.drawable.ic_signal_workmode_disable_animation);
40
41    private boolean mListening;
42
43    private UserManager mUserManager;
44    private List<UserInfo> mProfiles;
45
46    public WorkModeTile(Host host) {
47        super(host);
48        mUserManager = UserManager.get(mContext);
49        mProfiles = new LinkedList<UserInfo>();
50    }
51
52    @Override
53    protected BooleanState newTileState() {
54        return new BooleanState();
55    }
56
57    @Override
58    public void handleClick() {
59        MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
60        setWorkModeEnabled(!mState.value);
61    }
62
63    private void reloadManagedProfiles(int userHandle) {
64        synchronized (mProfiles) {
65            mProfiles.clear();
66
67            if (userHandle == UserHandle.USER_CURRENT) {
68                userHandle = ActivityManager.getCurrentUser();
69            }
70            for (UserInfo ui : mUserManager.getEnabledProfiles(userHandle)) {
71                if (ui.isManagedProfile()) {
72                    mProfiles.add(ui);
73                }
74            }
75        }
76    }
77
78    private boolean hasActiveProfile() {
79        synchronized (mProfiles) {
80            return mProfiles.size() > 0;
81        }
82    }
83
84    private boolean isWorkModeEnabled() {
85        synchronized (mProfiles) {
86            for (UserInfo ui : mProfiles) {
87                if (ui.isQuietModeEnabled()) {
88                    return false;
89                }
90            }
91            return true;
92        }
93    }
94
95    private void refreshQuietModeState(boolean backgroundRefresh) {
96        if (backgroundRefresh) {
97            refreshState(isWorkModeEnabled() ? UserBoolean.BACKGROUND_TRUE
98                    : UserBoolean.BACKGROUND_FALSE);
99        } else {
100            refreshState(isWorkModeEnabled() ? UserBoolean.USER_TRUE : UserBoolean.USER_FALSE);
101        }
102    }
103
104    @Override
105    protected void handleUpdateState(BooleanState state, Object arg) {
106        if (!hasActiveProfile()) {
107            mHost.removeTile(getTileSpec());
108            return;
109        }
110
111        final boolean userInitialized;
112        if (arg instanceof UserBoolean) {
113            state.value = ((UserBoolean) arg).value;
114            userInitialized = ((UserBoolean) arg).userInitiated;
115        } else {
116            state.value = isWorkModeEnabled();
117            userInitialized = false;
118        }
119
120        final AnimationIcon icon;
121        state.label = mContext.getString(R.string.quick_settings_work_mode_label);
122        if (state.value) {
123            icon = mEnable;
124            state.contentDescription =  mContext.getString(
125                    R.string.accessibility_quick_settings_work_mode_on);
126        } else {
127            icon = mDisable;
128            state.contentDescription =  mContext.getString(
129                    R.string.accessibility_quick_settings_work_mode_off);
130        }
131        icon.setAllowAnimation(userInitialized);
132        state.icon = icon;
133    }
134
135    @Override
136    public int getMetricsCategory() {
137        return MetricsLogger.QS_WORKMODE;
138    }
139
140    @Override
141    protected String composeChangeAnnouncement() {
142        if (mState.value) {
143            return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_on);
144        } else {
145            return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_off);
146        }
147    }
148
149    @Override
150    public void setListening(boolean listening) {
151        if (mListening == listening) {
152            return;
153        }
154        mListening = listening;
155        if (listening) {
156            reloadManagedProfiles(UserHandle.USER_CURRENT);
157
158            final IntentFilter filter = new IntentFilter();
159            filter.addAction(Intent.ACTION_USER_SWITCHED);
160            filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
161            filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
162            filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED);
163            mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null);
164        } else {
165            mContext.unregisterReceiver(mReceiver);
166        }
167    }
168
169    private void setWorkModeEnabled(boolean enabled) {
170        synchronized (mProfiles) {
171            for (UserInfo ui : mProfiles) {
172                mUserManager.setQuietModeEnabled(ui.id, !enabled);
173            }
174        }
175    }
176
177    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
178        @Override
179        public void onReceive(Context context, Intent intent) {
180            final String action = intent.getAction();
181            final int targetUser;
182            final boolean isBackgroundRefresh;
183            switch (action) {
184                case Intent.ACTION_USER_SWITCHED:
185                    targetUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
186                            UserHandle.USER_CURRENT);
187                    isBackgroundRefresh = true;
188                    break;
189                case Intent.ACTION_MANAGED_PROFILE_ADDED:
190                case Intent.ACTION_MANAGED_PROFILE_REMOVED:
191                    targetUser = UserHandle.USER_CURRENT;
192                    isBackgroundRefresh = true;
193                    break;
194                case Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED:
195                    targetUser = UserHandle.USER_CURRENT;
196                    isBackgroundRefresh = false;
197                    break;
198               default:
199                   targetUser = UserHandle.USER_NULL;
200                   isBackgroundRefresh = false;
201            }
202            if (targetUser != UserHandle.USER_NULL) {
203                reloadManagedProfiles(targetUser);
204                refreshQuietModeState(isBackgroundRefresh);
205            }
206        }
207    };
208}
209