/*
- * Copyright (C) 2014 The Android Open Source Project
+ * Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-package com.android.systemui.statusbar.policy;
-
-import static android.net.ConnectivityManager.TYPE_MOBILE;
-import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
-import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
-import static android.telephony.TelephonyManager.SIM_STATE_READY;
-import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
-import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
+package com.android.settingslib.net;
import android.content.Context;
import android.net.ConnectivityManager;
import java.util.Date;
import java.util.Locale;
-public class MobileDataControllerImpl implements NetworkController.MobileDataController {
+import static android.net.ConnectivityManager.TYPE_MOBILE;
+import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
+import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
+import static android.telephony.TelephonyManager.SIM_STATE_READY;
+import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
+import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
+
+public class MobileDataController {
private static final String TAG = "MobileDataController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private INetworkStatsSession mSession;
private Callback mCallback;
- private NetworkControllerImpl mNetworkController;
+ private NetworkNameProvider mNetworkController;
- public MobileDataControllerImpl(Context context) {
+ public MobileDataController(Context context) {
mContext = context;
mTelephonyManager = TelephonyManager.from(context);
mConnectivityManager = ConnectivityManager.from(context);
mPolicyManager = NetworkPolicyManager.from(mContext);
}
- public void setNetworkController(NetworkControllerImpl networkController) {
+ public void setNetworkController(NetworkNameProvider networkController) {
mNetworkController = networkController;
}
} else {
usage.warningLevel = DEFAULT_WARNING_LEVEL;
}
- if (usage != null) {
+ if (usage != null && mNetworkController != null) {
usage.carrier = mNetworkController.getMobileDataNetworkName();
}
return usage;
}
}
+ public interface NetworkNameProvider {
+ String getMobileDataNetworkName();
+ }
+
+ public static class DataUsageInfo {
+ public String carrier;
+ public String period;
+ public long limitLevel;
+ public long warningLevel;
+ public long usageLevel;
+ }
+
public interface Callback {
void onMobileDataEnabled(boolean enabled);
}
--- /dev/null
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
+ */
+
+package com.android.settingslib.wifi;
+
+import android.content.Intent;
+import android.net.NetworkInfo;
+import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
+
+import java.util.List;
+
+public class WifiStatusTracker {
+
+ private final WifiManager mWifiManager;
+ public boolean enabled;
+ public boolean connected;
+ public String ssid;
+ public int rssi;
+ public int level;
+
+ public WifiStatusTracker(WifiManager wifiManager) {
+ mWifiManager = wifiManager;
+ }
+
+ public void handleBroadcast(Intent intent) {
+ String action = intent.getAction();
+ if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
+ enabled = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
+ WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED;
+ } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
+ final NetworkInfo networkInfo = (NetworkInfo)
+ intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
+ connected = networkInfo != null && networkInfo.isConnected();
+ // If Connected grab the signal strength and ssid.
+ if (connected) {
+ // try getting it out of the intent first
+ WifiInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO) != null
+ ? (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO)
+ : mWifiManager.getConnectionInfo();
+ if (info != null) {
+ ssid = getSsid(info);
+ } else {
+ ssid = null;
+ }
+ } else if (!connected) {
+ ssid = null;
+ }
+ } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
+ // Default to -200 as its below WifiManager.MIN_RSSI.
+ rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
+ level = WifiManager.calculateSignalLevel(rssi, 5);
+ }
+ }
+
+ private String getSsid(WifiInfo info) {
+ String ssid = info.getSSID();
+ if (ssid != null) {
+ return ssid;
+ }
+ // OK, it's not in the connectionInfo; we have to go hunting for it
+ List<WifiConfiguration> networks = mWifiManager.getConfiguredNetworks();
+ int length = networks.size();
+ for (int i = 0; i < length; i++) {
+ if (networks.get(i).networkId == info.getNetworkId()) {
+ return networks.get(i).SSID;
+ }
+ }
+ return null;
+ }
+}
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-
import com.android.internal.logging.MetricsLogger;
+import com.android.settingslib.net.MobileDataController;
import com.android.systemui.R;
import com.android.systemui.qs.QSTile;
import com.android.systemui.qs.QSTileBaseView;
import com.android.systemui.qs.SignalTileView;
import com.android.systemui.statusbar.policy.NetworkController;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
-import com.android.systemui.statusbar.policy.NetworkController.MobileDataController;
-import com.android.systemui.statusbar.policy.NetworkController.MobileDataController.DataUsageInfo;
import com.android.systemui.statusbar.policy.SignalCallbackAdapter;
/** Quick settings tile: Cellular **/
final DataUsageDetailView v = (DataUsageDetailView) (convertView != null
? convertView
: LayoutInflater.from(mContext).inflate(R.layout.data_usage, parent, false));
- final DataUsageInfo info = mDataController.getDataUsageInfo();
+ final MobileDataController.DataUsageInfo info = mDataController.getDataUsageInfo();
if (info == null) return v;
v.bind(info);
return v;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
-
+import com.android.settingslib.net.MobileDataController;
import com.android.systemui.FontSizeUtils;
import com.android.systemui.R;
import com.android.systemui.qs.DataUsageGraph;
-import com.android.systemui.statusbar.policy.NetworkController;
import java.text.DecimalFormat;
R.dimen.qs_data_usage_text_size);
}
- public void bind(NetworkController.MobileDataController.DataUsageInfo info) {
+ public void bind(MobileDataController.DataUsageInfo info) {
final Resources res = mContext.getResources();
final int titleId;
final long bytes;
import android.content.Context;
import android.content.Intent;
import android.telephony.SubscriptionInfo;
-
+import com.android.settingslib.net.MobileDataController;
import com.android.settingslib.wifi.AccessPoint;
import java.util.List;
void onSettingsActivityTriggered(Intent settingsIntent);
}
}
-
- /**
- * Tracks mobile data support and usage.
- */
- public interface MobileDataController {
- boolean isMobileDataSupported();
- boolean isMobileDataEnabled();
- void setMobileDataEnabled(boolean enabled);
- DataUsageInfo getDataUsageInfo();
-
- public static class DataUsageInfo {
- public String carrier;
- public String period;
- public long limitLevel;
- public long warningLevel;
- public long usageLevel;
- }
- }
}
package com.android.systemui.statusbar.policy;
-import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.util.MathUtils;
-
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.TelephonyIntents;
+import com.android.settingslib.net.MobileDataController;
import com.android.systemui.DemoMode;
import com.android.systemui.R;
import java.util.Locale;
import java.util.Map;
+import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
+
/** Platform implementation of the network controller. **/
public class NetworkControllerImpl extends BroadcastReceiver
- implements NetworkController, DemoMode {
+ implements NetworkController, DemoMode, MobileDataController.NetworkNameProvider {
// debug
static final String TAG = "NetworkController";
static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
// SIM for most actions. This may be null if there aren't any SIMs around.
private MobileSignalController mDefaultSignalController;
private final AccessPointControllerImpl mAccessPoints;
- private final MobileDataControllerImpl mMobileDataController;
+ private final MobileDataController mMobileDataController;
private boolean mInetCondition; // Used for Logging and demo.
SubscriptionManager.from(context), Config.readConfig(context), bgLooper,
new CallbackHandler(),
new AccessPointControllerImpl(context, bgLooper),
- new MobileDataControllerImpl(context),
+ new MobileDataController(context),
new SubscriptionDefaults());
mReceiverHandler.post(mRegisterListeners);
}
SubscriptionManager subManager, Config config, Looper bgLooper,
CallbackHandler callbackHandler,
AccessPointControllerImpl accessPointController,
- MobileDataControllerImpl mobileDataController,
+ MobileDataController mobileDataController,
SubscriptionDefaults defaultsHandler) {
mContext = context;
mConfig = config;
mMobileDataController = mobileDataController;
mMobileDataController.setNetworkController(this);
// TODO: Find a way to move this into MobileDataController.
- mMobileDataController.setCallback(new MobileDataControllerImpl.Callback() {
+ mMobileDataController.setCallback(new MobileDataController.Callback() {
@Override
public void onMobileDataEnabled(boolean enabled) {
mCallbackHandler.setMobileDataEnabled(enabled);
import android.content.Context;
import android.content.Intent;
import android.net.NetworkCapabilities;
-import android.net.NetworkInfo;
-import android.net.wifi.WifiConfiguration;
-import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
-
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.AsyncChannel;
+import com.android.settingslib.wifi.WifiStatusTracker;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
-import java.util.List;
import java.util.Objects;
private final WifiManager mWifiManager;
private final AsyncChannel mWifiChannel;
private final boolean mHasMobileData;
+ private final WifiStatusTracker mWifiTracker;
public WifiSignalController(Context context, boolean hasMobileData,
CallbackHandler callbackHandler, NetworkControllerImpl networkController) {
super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI,
callbackHandler, networkController);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+ mWifiTracker = new WifiStatusTracker(mWifiManager);
mHasMobileData = hasMobileData;
Handler handler = new WifiHandler();
mWifiChannel = new AsyncChannel();
* Extract wifi state directly from broadcasts about changes in wifi state.
*/
public void handleBroadcast(Intent intent) {
- String action = intent.getAction();
- if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
- mCurrentState.enabled = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
- WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED;
- } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
- final NetworkInfo networkInfo = (NetworkInfo)
- intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
- mCurrentState.connected = networkInfo != null && networkInfo.isConnected();
- // If Connected grab the signal strength and ssid.
- if (mCurrentState.connected) {
- // try getting it out of the intent first
- WifiInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO) != null
- ? (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO)
- : mWifiManager.getConnectionInfo();
- if (info != null) {
- mCurrentState.ssid = getSsid(info);
- } else {
- mCurrentState.ssid = null;
- }
- } else if (!mCurrentState.connected) {
- mCurrentState.ssid = null;
- }
- } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
- // Default to -200 as its below WifiManager.MIN_RSSI.
- mCurrentState.rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
- mCurrentState.level = WifiManager.calculateSignalLevel(
- mCurrentState.rssi, WifiIcons.WIFI_LEVEL_COUNT);
- }
-
+ mWifiTracker.handleBroadcast(intent);
+ mCurrentState.enabled = mWifiTracker.enabled;
+ mCurrentState.connected = mWifiTracker.connected;
+ mCurrentState.ssid = mWifiTracker.ssid;
+ mCurrentState.rssi = mWifiTracker.rssi;
+ mCurrentState.level = mWifiTracker.level;
notifyListenersIfNecessary();
}
- private String getSsid(WifiInfo info) {
- String ssid = info.getSSID();
- if (ssid != null) {
- return ssid;
- }
- // OK, it's not in the connectionInfo; we have to go hunting for it
- List<WifiConfiguration> networks = mWifiManager.getConfiguredNetworks();
- int length = networks.size();
- for (int i = 0; i < length; i++) {
- if (networks.get(i).networkId == info.getNetworkId()) {
- return networks.get(i).SSID;
- }
- }
- return null;
- }
-
@VisibleForTesting
void setActivity(int wifiActivity) {
mCurrentState.activityIn = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT
import android.util.Log;
import com.android.internal.telephony.cdma.EriInfo;
+import com.android.settingslib.net.MobileDataControllerImpl;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.policy.NetworkController.IconState;
import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config;
import android.telephony.TelephonyManager;
import android.test.suitebuilder.annotation.SmallTest;
+import com.android.settingslib.net.MobileDataControllerImpl;
import org.mockito.Mockito;
@SmallTest
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.TelephonyIntents;
+import com.android.settingslib.net.MobileDataControllerImpl;
import com.android.systemui.R;
import org.mockito.ArgumentCaptor;