Home | History | Annotate | Download | only in tiles
      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 
     17 package com.android.systemui.qs.tiles;
     18 
     19 import com.android.internal.logging.MetricsLogger;
     20 import com.android.systemui.R;
     21 import com.android.systemui.qs.QSTile;
     22 import com.android.systemui.statusbar.policy.KeyguardMonitor;
     23 import com.android.systemui.statusbar.policy.LocationController;
     24 import com.android.systemui.statusbar.policy.LocationController.LocationSettingsChangeCallback;
     25 
     26 /** Quick settings tile: Location **/
     27 public class LocationTile extends QSTile<QSTile.BooleanState> {
     28 
     29     private final AnimationIcon mEnable =
     30             new AnimationIcon(R.drawable.ic_signal_location_enable_animation);
     31     private final AnimationIcon mDisable =
     32             new AnimationIcon(R.drawable.ic_signal_location_disable_animation);
     33 
     34     private final LocationController mController;
     35     private final KeyguardMonitor mKeyguard;
     36     private final Callback mCallback = new Callback();
     37 
     38     public LocationTile(Host host) {
     39         super(host);
     40         mController = host.getLocationController();
     41         mKeyguard = host.getKeyguardMonitor();
     42     }
     43 
     44     @Override
     45     protected BooleanState newTileState() {
     46         return new BooleanState();
     47     }
     48 
     49     @Override
     50     public void setListening(boolean listening) {
     51         if (listening) {
     52             mController.addSettingsChangedCallback(mCallback);
     53             mKeyguard.addCallback(mCallback);
     54         } else {
     55             mController.removeSettingsChangedCallback(mCallback);
     56             mKeyguard.removeCallback(mCallback);
     57         }
     58     }
     59 
     60     @Override
     61     protected void handleClick() {
     62         final boolean wasEnabled = (Boolean) mState.value;
     63         MetricsLogger.action(mContext, getMetricsCategory(), !wasEnabled);
     64         mController.setLocationEnabled(!wasEnabled);
     65         mEnable.setAllowAnimation(true);
     66         mDisable.setAllowAnimation(true);
     67     }
     68 
     69     @Override
     70     protected void handleUpdateState(BooleanState state, Object arg) {
     71         final boolean locationEnabled =  mController.isLocationEnabled();
     72 
     73         // Work around for bug 15916487: don't show location tile on top of lock screen. After the
     74         // bug is fixed, this should be reverted to only hiding it on secure lock screens:
     75         // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
     76         state.visible = !mKeyguard.isShowing();
     77         state.value = locationEnabled;
     78         if (locationEnabled) {
     79             state.icon = mEnable;
     80             state.label = mContext.getString(R.string.quick_settings_location_label);
     81             state.contentDescription = mContext.getString(
     82                     R.string.accessibility_quick_settings_location_on);
     83         } else {
     84             state.icon = mDisable;
     85             state.label = mContext.getString(R.string.quick_settings_location_label);
     86             state.contentDescription = mContext.getString(
     87                     R.string.accessibility_quick_settings_location_off);
     88         }
     89     }
     90 
     91     @Override
     92     public int getMetricsCategory() {
     93         return MetricsLogger.QS_LOCATION;
     94     }
     95 
     96     @Override
     97     protected String composeChangeAnnouncement() {
     98         if (mState.value) {
     99             return mContext.getString(R.string.accessibility_quick_settings_location_changed_on);
    100         } else {
    101             return mContext.getString(R.string.accessibility_quick_settings_location_changed_off);
    102         }
    103     }
    104 
    105     private final class Callback implements LocationSettingsChangeCallback,
    106             KeyguardMonitor.Callback {
    107         @Override
    108         public void onLocationSettingsChanged(boolean enabled) {
    109             refreshState();
    110         }
    111 
    112         @Override
    113         public void onKeyguardChanged() {
    114             refreshState();
    115         }
    116     };
    117 }
    118