CellularTile.java revision ccb6b9a90f228cc4e31a9442ed28756ff474c080
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.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Resources;
23
24import com.android.systemui.R;
25import com.android.systemui.qs.QSTile;
26import com.android.systemui.qs.QSTileView;
27import com.android.systemui.qs.SignalTileView;
28import com.android.systemui.statusbar.policy.NetworkController;
29import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback;
30
31/** Quick settings tile: Cellular **/
32public class CellularTile extends QSTile<QSTile.SignalState> {
33    private static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
34            "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
35
36    private final NetworkController mController;
37
38    public CellularTile(Host host) {
39        super(host);
40        mController = host.getNetworkController();
41    }
42
43    @Override
44    protected SignalState newTileState() {
45        return new SignalState();
46    }
47
48    @Override
49    public void setListening(boolean listening) {
50        if (listening) {
51            mController.addNetworkSignalChangedCallback(mCallback);
52        } else {
53            mController.removeNetworkSignalChangedCallback(mCallback);
54        }
55    }
56
57    @Override
58    public QSTileView createTileView(Context context) {
59        return new SignalTileView(context);
60    }
61
62    @Override
63    protected void handleClick() {
64        mHost.startSettingsActivity(CELLULAR_SETTINGS);
65    }
66
67    @Override
68    protected void handleUpdateState(SignalState state, Object arg) {
69        state.visible = mController.hasMobileDataFeature();
70        if (!state.visible) return;
71        final CallbackInfo cb = (CallbackInfo) arg;
72        if (cb == null) return;
73
74        final Resources r = mContext.getResources();
75        state.iconId = cb.enabled && (cb.mobileSignalIconId > 0)
76                ? cb.mobileSignalIconId
77                : R.drawable.ic_qs_signal_no_signal;
78        state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled
79                ? cb.dataTypeIconId
80                : 0;
81        state.activityIn = cb.enabled && cb.activityIn;
82        state.activityOut = cb.enabled && cb.activityOut;
83
84        state.label = cb.enabled
85                ? removeTrailingPeriod(cb.enabledDesc)
86                : r.getString(R.string.quick_settings_rssi_emergency_only);
87
88        final String signalContentDesc = cb.enabled && (cb.mobileSignalIconId > 0)
89                ? cb.signalContentDescription
90                : r.getString(R.string.accessibility_no_signal);
91        final String dataContentDesc = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled
92                ? cb.dataContentDescription
93                : r.getString(R.string.accessibility_no_data);
94        state.contentDescription = r.getString(
95                R.string.accessibility_quick_settings_mobile,
96                signalContentDesc, dataContentDesc,
97                state.label);
98    }
99
100    // Remove the period from the network name
101    public static String removeTrailingPeriod(String string) {
102        if (string == null) return null;
103        final int length = string.length();
104        if (string.endsWith(".")) {
105            return string.substring(0, length - 1);
106        }
107        return string;
108    }
109
110    private static final class CallbackInfo {
111        boolean enabled;
112        boolean wifiEnabled;
113        int mobileSignalIconId;
114        String signalContentDescription;
115        int dataTypeIconId;
116        String dataContentDescription;
117        boolean activityIn;
118        boolean activityOut;
119        String enabledDesc;
120    }
121
122    private final NetworkSignalChangedCallback mCallback = new NetworkSignalChangedCallback() {
123        private boolean mWifiEnabled;
124
125        @Override
126        public void onWifiSignalChanged(boolean enabled, int wifiSignalIconId,
127                boolean activityIn, boolean activityOut,
128                String wifiSignalContentDescriptionId, String description) {
129            mWifiEnabled = enabled;
130        }
131
132        @Override
133        public void onMobileDataSignalChanged(boolean enabled,
134                int mobileSignalIconId,
135                String mobileSignalContentDescriptionId, int dataTypeIconId,
136                boolean activityIn, boolean activityOut,
137                String dataTypeContentDescriptionId, String description) {
138            final CallbackInfo info = new CallbackInfo();  // TODO pool?
139            info.enabled = enabled;
140            info.wifiEnabled = mWifiEnabled;
141            info.mobileSignalIconId = mobileSignalIconId;
142            info.signalContentDescription = mobileSignalContentDescriptionId;
143            info.dataTypeIconId = dataTypeIconId;
144            info.dataContentDescription = dataTypeContentDescriptionId;
145            info.activityIn = activityIn;
146            info.activityOut = activityOut;
147            info.enabledDesc = description;
148            refreshState(info);
149        }
150
151        @Override
152        public void onAirplaneModeChanged(boolean enabled) {
153            // noop
154        }
155    };
156}
157