CellularTile.java revision 899f439a29ab1e609e7ba2aea3e9de3afef35c50
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.noSim
76                ? R.drawable.stat_sys_no_sim
77                : cb.enabled && (cb.mobileSignalIconId > 0)
78                ? cb.mobileSignalIconId
79                : R.drawable.ic_qs_signal_no_signal;
80        state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled
81                ? cb.dataTypeIconId
82                : 0;
83        state.filter = state.iconId != R.drawable.stat_sys_no_sim;
84        state.activityIn = cb.enabled && cb.activityIn;
85        state.activityOut = cb.enabled && cb.activityOut;
86
87        state.label = cb.enabled
88                ? removeTrailingPeriod(cb.enabledDesc)
89                : r.getString(R.string.quick_settings_rssi_emergency_only);
90
91        final String signalContentDesc = cb.enabled && (cb.mobileSignalIconId > 0)
92                ? cb.signalContentDescription
93                : r.getString(R.string.accessibility_no_signal);
94        final String dataContentDesc = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled
95                ? cb.dataContentDescription
96                : r.getString(R.string.accessibility_no_data);
97        state.contentDescription = r.getString(
98                R.string.accessibility_quick_settings_mobile,
99                signalContentDesc, dataContentDesc,
100                state.label);
101    }
102
103    // Remove the period from the network name
104    public static String removeTrailingPeriod(String string) {
105        if (string == null) return null;
106        final int length = string.length();
107        if (string.endsWith(".")) {
108            return string.substring(0, length - 1);
109        }
110        return string;
111    }
112
113    private static final class CallbackInfo {
114        boolean enabled;
115        boolean wifiEnabled;
116        int mobileSignalIconId;
117        String signalContentDescription;
118        int dataTypeIconId;
119        String dataContentDescription;
120        boolean activityIn;
121        boolean activityOut;
122        String enabledDesc;
123        boolean noSim;
124    }
125
126    private final NetworkSignalChangedCallback mCallback = new NetworkSignalChangedCallback() {
127        private boolean mWifiEnabled;
128
129        @Override
130        public void onWifiSignalChanged(boolean enabled, int wifiSignalIconId,
131                boolean activityIn, boolean activityOut,
132                String wifiSignalContentDescriptionId, String description) {
133            mWifiEnabled = enabled;
134        }
135
136        @Override
137        public void onMobileDataSignalChanged(boolean enabled,
138                int mobileSignalIconId,
139                String mobileSignalContentDescriptionId, int dataTypeIconId,
140                boolean activityIn, boolean activityOut,
141                String dataTypeContentDescriptionId, String description, boolean noSim) {
142            final CallbackInfo info = new CallbackInfo();  // TODO pool?
143            info.enabled = enabled;
144            info.wifiEnabled = mWifiEnabled;
145            info.mobileSignalIconId = mobileSignalIconId;
146            info.signalContentDescription = mobileSignalContentDescriptionId;
147            info.dataTypeIconId = dataTypeIconId;
148            info.dataContentDescription = dataTypeContentDescriptionId;
149            info.activityIn = activityIn;
150            info.activityOut = activityOut;
151            info.enabledDesc = description;
152            info.noSim = noSim;
153            refreshState(info);
154        }
155
156        @Override
157        public void onAirplaneModeChanged(boolean enabled) {
158            // noop
159        }
160    };
161}
162