LocationTile.java revision 83e2848c562bf951fd49d85da338eb6dc459cff2
1/*
2 * Copyright (C) 2014 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.graphics.drawable.AnimationDrawable;
20
21import com.android.systemui.R;
22import com.android.systemui.qs.QSTile;
23import com.android.systemui.statusbar.policy.LocationController;
24import com.android.systemui.statusbar.policy.LocationController.LocationSettingsChangeCallback;
25
26/** Quick settings tile: Location **/
27public class LocationTile extends QSTile<QSTile.BooleanState> {
28
29    private final LocationController mController;
30
31    public LocationTile(Host host) {
32        super(host);
33        mController = host.getLocationController();
34    }
35
36    @Override
37    protected BooleanState newTileState() {
38        return new BooleanState();
39    }
40
41    @Override
42    public void setListening(boolean listening) {
43        if (listening) {
44            mController.addSettingsChangedCallback(mCallback);
45        } else {
46            mController.removeSettingsChangedCallback(mCallback);
47        }
48    }
49
50    @Override
51    protected void handleClick() {
52        final boolean wasEnabled = (Boolean) mState.value;
53        final boolean changed = mController.setLocationEnabled(!wasEnabled);
54        if (!wasEnabled && changed) {
55            // If we've successfully switched from location off to on, close the
56            // notifications tray to show the network location provider consent dialog.
57            mHost.collapsePanels();
58        }
59    }
60
61    @Override
62    protected void handleUpdateState(BooleanState state, Object arg) {
63        final boolean locationEnabled =  mController.isLocationEnabled();
64        state.visible = true;
65        if (state.value != locationEnabled) {
66            state.value = locationEnabled;
67            final AnimationDrawable d = (AnimationDrawable) mContext.getDrawable(locationEnabled
68                    ? R.drawable.ic_qs_location_on
69                    : R.drawable.ic_qs_location_off);
70            state.icon = d;
71            mUiHandler.post(new Runnable() {
72                @Override
73                public void run() {
74                    d.start();
75                }
76            });
77        }
78        if (locationEnabled) {
79            if (state.icon == null) state.iconId = R.drawable.ic_qs_location_01;
80            state.label = mContext.getString(R.string.quick_settings_location_label);
81            state.contentDescription = mContext.getString(
82                    R.string.accessibility_quick_settings_location,
83                    mContext.getString(R.string.accessibility_desc_on));
84        } else {
85            if (state.icon == null) state.iconId = R.drawable.ic_qs_location_11;
86            state.label = mContext.getString(R.string.quick_settings_location_off_label);
87            state.contentDescription = mContext.getString(
88                    R.string.accessibility_quick_settings_location,
89                    mContext.getString(R.string.accessibility_desc_off));
90        }
91    }
92
93    private final LocationSettingsChangeCallback mCallback = new LocationSettingsChangeCallback() {
94        @Override
95        public void onLocationSettingsChanged(boolean enabled) {
96            refreshState();
97        }
98    };
99}
100