Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.settings; |
| 18 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
| 27 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 28 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
| 29 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 30 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 31 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
| 32 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 33 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 34 | import static android.net.NetworkTemplate.buildTemplateEthernet; |
| 35 | import static android.net.NetworkTemplate.buildTemplateMobile3gLower; |
| 36 | import static android.net.NetworkTemplate.buildTemplateMobile4g; |
| 37 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 38 | import static android.net.NetworkTemplate.buildTemplateWifiWildcard; |
Jeff Sharkey | 77dae91 | 2012-02-03 14:50:33 -0800 | [diff] [blame] | 39 | import static android.net.TrafficStats.GB_IN_BYTES; |
| 40 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 41 | import static android.net.TrafficStats.UID_REMOVED; |
| 42 | import static android.net.TrafficStats.UID_TETHERING; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 43 | import static android.telephony.TelephonyManager.SIM_STATE_READY; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 44 | import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH; |
| 45 | import static android.text.format.DateUtils.FORMAT_SHOW_DATE; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 46 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 47 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 48 | import static com.android.settings.Utils.prepareCustomPreferencesList; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 50 | import android.animation.LayoutTransition; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 51 | import android.app.AlertDialog; |
| 52 | import android.app.Dialog; |
| 53 | import android.app.DialogFragment; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 54 | import android.app.Fragment; |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 55 | import android.app.FragmentManager; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 56 | import android.app.FragmentTransaction; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 57 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 58 | import android.content.ContentResolver; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 59 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 60 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 61 | import android.content.Intent; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 62 | import android.content.Loader; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 63 | import android.content.SharedPreferences; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 64 | import android.content.pm.PackageManager; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 65 | import android.content.res.Resources; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 66 | import android.graphics.Color; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 67 | import android.graphics.drawable.ColorDrawable; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 68 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 69 | import android.net.ConnectivityManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 70 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 71 | import android.net.INetworkStatsService; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 72 | import android.net.INetworkStatsSession; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 73 | import android.net.NetworkPolicy; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 74 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 75 | import android.net.NetworkStats; |
| 76 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 77 | import android.net.NetworkTemplate; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 78 | import android.net.TrafficStats; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 79 | import android.net.Uri; |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 80 | import android.os.AsyncTask; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 81 | import android.os.Bundle; |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 82 | import android.os.INetworkManagementService; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 83 | import android.os.Parcel; |
| 84 | import android.os.Parcelable; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 85 | import android.os.RemoteException; |
| 86 | import android.os.ServiceManager; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 87 | import android.os.SystemProperties; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 88 | import android.os.UserId; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 89 | import android.preference.Preference; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 90 | import android.preference.PreferenceActivity; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 91 | import android.provider.Settings; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 92 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 93 | import android.text.TextUtils; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 94 | import android.text.format.DateUtils; |
| 95 | import android.text.format.Formatter; |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 96 | import android.text.format.Time; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 97 | import android.util.Log; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 98 | import android.util.SparseArray; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 99 | import android.util.SparseBooleanArray; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 100 | import android.view.LayoutInflater; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 101 | import android.view.Menu; |
| 102 | import android.view.MenuInflater; |
| 103 | import android.view.MenuItem; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 104 | import android.view.View; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 105 | import android.view.View.OnClickListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 106 | import android.view.ViewGroup; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 107 | import android.widget.AdapterView; |
| 108 | import android.widget.AdapterView.OnItemClickListener; |
| 109 | import android.widget.AdapterView.OnItemSelectedListener; |
| 110 | import android.widget.ArrayAdapter; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 111 | import android.widget.BaseAdapter; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 112 | import android.widget.Button; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 113 | import android.widget.CheckBox; |
| 114 | import android.widget.CompoundButton; |
| 115 | import android.widget.CompoundButton.OnCheckedChangeListener; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 116 | import android.widget.ImageView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 117 | import android.widget.LinearLayout; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 118 | import android.widget.ListView; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 119 | import android.widget.NumberPicker; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 120 | import android.widget.ProgressBar; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 121 | import android.widget.Spinner; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 122 | import android.widget.Switch; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 123 | import android.widget.TabHost; |
| 124 | import android.widget.TabHost.OnTabChangeListener; |
| 125 | import android.widget.TabHost.TabContentFactory; |
| 126 | import android.widget.TabHost.TabSpec; |
| 127 | import android.widget.TabWidget; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 128 | import android.widget.TextView; |
| 129 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 130 | import com.android.internal.telephony.Phone; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 131 | import com.android.settings.drawable.InsetBoundsDrawable; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 132 | import com.android.settings.net.ChartData; |
| 133 | import com.android.settings.net.ChartDataLoader; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 134 | import com.android.settings.net.DataUsageMeteredSettings; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 135 | import com.android.settings.net.NetworkPolicyEditor; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 136 | import com.android.settings.net.SummaryForAllUidLoader; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 137 | import com.android.settings.net.UidDetail; |
| 138 | import com.android.settings.net.UidDetailProvider; |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 139 | import com.android.settings.widget.ChartDataUsageView; |
| 140 | import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 141 | import com.android.settings.widget.PieChartView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 142 | import com.google.android.collect.Lists; |
| 143 | |
| 144 | import java.util.ArrayList; |
| 145 | import java.util.Collections; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 146 | import java.util.List; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 147 | import java.util.Locale; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 148 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 149 | import libcore.util.Objects; |
| 150 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 151 | /** |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 152 | * Panel showing data usage history across various networks, including options |
| 153 | * to inspect based on usage cycle and control through {@link NetworkPolicy}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 154 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 155 | public class DataUsageSummary extends Fragment { |
| 156 | private static final String TAG = "DataUsage"; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 157 | private static final boolean LOGD = false; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 158 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 159 | // TODO: remove this testing code |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 160 | private static final boolean TEST_ANIM = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 161 | private static final boolean TEST_RADIOS = false; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 162 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 163 | private static final String TEST_RADIOS_PROP = "test.radios"; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 164 | private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 166 | private static final String TAB_3G = "3g"; |
| 167 | private static final String TAB_4G = "4g"; |
| 168 | private static final String TAB_MOBILE = "mobile"; |
| 169 | private static final String TAB_WIFI = "wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 170 | private static final String TAB_ETHERNET = "ethernet"; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 171 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 172 | private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable"; |
| 173 | private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 174 | private static final String TAG_CONFIRM_LIMIT = "confirmLimit"; |
| 175 | private static final String TAG_CYCLE_EDITOR = "cycleEditor"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 176 | private static final String TAG_WARNING_EDITOR = "warningEditor"; |
| 177 | private static final String TAG_LIMIT_EDITOR = "limitEditor"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 178 | private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 179 | private static final String TAG_DENIED_RESTRICT = "deniedRestrict"; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 180 | private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 181 | private static final String TAG_APP_DETAILS = "appDetails"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 182 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 183 | private static final int LOADER_CHART_DATA = 2; |
| 184 | private static final int LOADER_SUMMARY = 3; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 185 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 186 | private INetworkManagementService mNetworkService; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 187 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 188 | private NetworkPolicyManager mPolicyManager; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 189 | private ConnectivityManager mConnService; |
| 190 | |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 191 | private INetworkStatsSession mStatsSession; |
| 192 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 193 | private static final String PREF_FILE = "data_usage"; |
| 194 | private static final String PREF_SHOW_WIFI = "show_wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 195 | private static final String PREF_SHOW_ETHERNET = "show_ethernet"; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 196 | |
| 197 | private SharedPreferences mPrefs; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 199 | private TabHost mTabHost; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 200 | private ViewGroup mTabsContainer; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 201 | private TabWidget mTabWidget; |
| 202 | private ListView mListView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 203 | private DataUsageAdapter mAdapter; |
| 204 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 205 | /** Distance to inset content from sides, when needed. */ |
| 206 | private int mInsetSide = 0; |
| 207 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 208 | private ViewGroup mHeader; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 209 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 210 | private ViewGroup mNetworkSwitchesContainer; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 211 | private LinearLayout mNetworkSwitches; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 212 | private Switch mDataEnabled; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 213 | private View mDataEnabledView; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 214 | private CheckBox mDisableAtLimit; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 215 | private View mDisableAtLimitView; |
| 216 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 217 | private View mCycleView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 218 | private Spinner mCycleSpinner; |
| 219 | private CycleAdapter mCycleAdapter; |
| 220 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 221 | private ChartDataUsageView mChart; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 222 | private TextView mUsageSummary; |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 223 | private TextView mEmpty; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 224 | |
| 225 | private View mAppDetail; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 226 | private ImageView mAppIcon; |
| 227 | private ViewGroup mAppTitles; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 228 | private PieChartView mAppPieChart; |
| 229 | private TextView mAppForeground; |
| 230 | private TextView mAppBackground; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 231 | private Button mAppSettings; |
| 232 | |
| 233 | private LinearLayout mAppSwitches; |
| 234 | private CheckBox mAppRestrict; |
| 235 | private View mAppRestrictView; |
| 236 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 237 | private boolean mShowWifi = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 238 | private boolean mShowEthernet = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 239 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 240 | private NetworkTemplate mTemplate; |
| 241 | private ChartData mChartData; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 242 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 243 | private AppItem mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 244 | |
| 245 | private Intent mAppSettingsIntent; |
| 246 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 247 | private NetworkPolicyEditor mPolicyEditor; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 248 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 249 | private String mCurrentTab = null; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 250 | private String mIntentTab = null; |
| 251 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 252 | private MenuItem mMenuDataRoaming; |
| 253 | private MenuItem mMenuRestrictBackground; |
| 254 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 255 | /** Flag used to ignore listeners during binding. */ |
| 256 | private boolean mBinding; |
| 257 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 258 | private UidDetailProvider mUidDetailProvider; |
| 259 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 260 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 261 | public void onCreate(Bundle savedInstanceState) { |
| 262 | super.onCreate(savedInstanceState); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 263 | final Context context = getActivity(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 264 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 265 | mNetworkService = INetworkManagementService.Stub.asInterface( |
| 266 | ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 267 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 268 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 269 | mPolicyManager = NetworkPolicyManager.from(context); |
| 270 | mConnService = ConnectivityManager.from(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 271 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 272 | mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 273 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 274 | mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 275 | mPolicyEditor.read(); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 276 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 277 | mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 278 | mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 279 | |
Jeff Sharkey | 0bc5b93 | 2012-05-03 15:02:06 -0700 | [diff] [blame] | 280 | // override preferences when no mobile radio |
| 281 | if (!hasReadyMobileRadio(context)) { |
| 282 | mShowWifi = hasWifiRadio(context); |
| 283 | mShowEthernet = hasEthernet(context); |
| 284 | } |
| 285 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 286 | setHasOptionsMenu(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 287 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 288 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 289 | @Override |
| 290 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 291 | Bundle savedInstanceState) { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 292 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 293 | final Context context = inflater.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 294 | final View view = inflater.inflate(R.layout.data_usage_summary, container, false); |
| 295 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 296 | mUidDetailProvider = new UidDetailProvider(context); |
| 297 | |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 298 | try { |
| 299 | mStatsSession = mStatsService.openSession(); |
| 300 | } catch (RemoteException e) { |
| 301 | throw new RuntimeException(e); |
| 302 | } |
| 303 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 304 | mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 305 | mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 306 | mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs); |
| 307 | mListView = (ListView) view.findViewById(android.R.id.list); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 308 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 309 | // decide if we need to manually inset our content, or if we should rely |
| 310 | // on parent container for inset. |
| 311 | final boolean shouldInset = mListView.getScrollBarStyle() |
| 312 | == View.SCROLLBARS_OUTSIDE_OVERLAY; |
| 313 | if (shouldInset) { |
| 314 | mInsetSide = view.getResources().getDimensionPixelOffset( |
| 315 | com.android.internal.R.dimen.preference_fragment_padding_side); |
| 316 | } else { |
| 317 | mInsetSide = 0; |
| 318 | } |
| 319 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 320 | // adjust padding around tabwidget as needed |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 321 | prepareCustomPreferencesList(container, view, mListView, true); |
| 322 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 323 | mTabHost.setup(); |
| 324 | mTabHost.setOnTabChangedListener(mTabListener); |
| 325 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 326 | mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 327 | mHeader.setClickable(true); |
| 328 | |
| 329 | mListView.addHeaderView(mHeader, null, true); |
| 330 | mListView.setItemsCanFocus(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 331 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 332 | if (mInsetSide > 0) { |
| 333 | // inset selector and divider drawables |
| 334 | insetListViewDrawables(mListView, mInsetSide); |
| 335 | mHeader.setPadding(mInsetSide, 0, mInsetSide, 0); |
| 336 | } |
| 337 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 338 | { |
| 339 | // bind network switches |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 340 | mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById( |
| 341 | R.id.network_switches_container); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 342 | mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 343 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 344 | mDataEnabled = new Switch(inflater.getContext()); |
| 345 | mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled); |
| 346 | mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener); |
| 347 | mNetworkSwitches.addView(mDataEnabledView); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 348 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 349 | mDisableAtLimit = new CheckBox(inflater.getContext()); |
| 350 | mDisableAtLimit.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 351 | mDisableAtLimit.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 352 | mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 353 | mDisableAtLimitView.setClickable(true); |
| 354 | mDisableAtLimitView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 355 | mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener); |
| 356 | mNetworkSwitches.addView(mDisableAtLimitView); |
| 357 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 358 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 359 | // bind cycle dropdown |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 360 | mCycleView = mHeader.findViewById(R.id.cycles); |
| 361 | mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 362 | mCycleAdapter = new CycleAdapter(context); |
| 363 | mCycleSpinner.setAdapter(mCycleAdapter); |
| 364 | mCycleSpinner.setOnItemSelectedListener(mCycleListener); |
| 365 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 366 | mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 367 | mChart.setListener(mChartListener); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 368 | mChart.bindNetworkPolicy(null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 369 | |
| 370 | { |
| 371 | // bind app detail controls |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 372 | mAppDetail = mHeader.findViewById(R.id.app_detail); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 373 | mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon); |
| 374 | mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 375 | mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart); |
| 376 | mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground); |
| 377 | mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 378 | mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 379 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 380 | mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 381 | mAppSettings.setOnClickListener(mAppSettingsListener); |
| 382 | |
| 383 | mAppRestrict = new CheckBox(inflater.getContext()); |
| 384 | mAppRestrict.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 385 | mAppRestrict.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 386 | mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 387 | mAppRestrictView.setClickable(true); |
| 388 | mAppRestrictView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 389 | mAppRestrictView.setOnClickListener(mAppRestrictListener); |
| 390 | mAppSwitches.addView(mAppRestrictView); |
| 391 | } |
| 392 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 393 | mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 394 | mEmpty = (TextView) mHeader.findViewById(android.R.id.empty); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 395 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 396 | mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 397 | mListView.setOnItemClickListener(mListListener); |
| 398 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 399 | |
| 400 | return view; |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void onResume() { |
| 405 | super.onResume(); |
| 406 | |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 407 | // pick default tab based on incoming intent |
| 408 | final Intent intent = getActivity().getIntent(); |
| 409 | mIntentTab = computeTabFromIntent(intent); |
| 410 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 411 | // this kicks off chain reaction which creates tabs, binds the body to |
| 412 | // selected network, and binds chart, cycles and detail list. |
| 413 | updateTabs(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 414 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 415 | // kick off background task to update stats |
| 416 | new AsyncTask<Void, Void, Void>() { |
| 417 | @Override |
| 418 | protected Void doInBackground(Void... params) { |
| 419 | try { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 420 | // wait a few seconds before kicking off |
| 421 | Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 422 | mStatsService.forceUpdate(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 423 | } catch (InterruptedException e) { |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 424 | } catch (RemoteException e) { |
| 425 | } |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | protected void onPostExecute(Void result) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 431 | if (isAdded()) { |
| 432 | updateBody(); |
| 433 | } |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 434 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 435 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 438 | @Override |
| 439 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 440 | inflater.inflate(R.menu.data_usage, menu); |
| 441 | } |
| 442 | |
| 443 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 444 | public void onPrepareOptionsMenu(Menu menu) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 445 | final Context context = getActivity(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 446 | final boolean appDetailMode = isAppDetailMode(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 447 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 448 | mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 449 | mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 450 | mMenuDataRoaming.setChecked(getDataRoaming()); |
| 451 | |
| 452 | mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 453 | mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode); |
| 454 | mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground()); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 455 | |
| 456 | final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g); |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 457 | split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 458 | split4g.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 459 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 460 | final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 461 | if (hasWifiRadio(context) && hasReadyMobileRadio(context)) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 462 | showWifi.setVisible(!appDetailMode); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 463 | showWifi.setChecked(mShowWifi); |
| 464 | } else { |
| 465 | showWifi.setVisible(false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 466 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 467 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 468 | final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 469 | if (hasEthernet(context) && hasReadyMobileRadio(context)) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 470 | showEthernet.setVisible(!appDetailMode); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 471 | showEthernet.setChecked(mShowEthernet); |
| 472 | } else { |
| 473 | showEthernet.setVisible(false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 475 | |
| 476 | final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered); |
| 477 | if (hasReadyMobileRadio(context) || hasWifiRadio(context)) { |
| 478 | metered.setVisible(!appDetailMode); |
| 479 | } else { |
| 480 | metered.setVisible(false); |
| 481 | } |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 482 | |
| 483 | final MenuItem help = menu.findItem(R.id.data_usage_menu_help); |
| 484 | String helpUrl; |
| 485 | if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) { |
| 486 | Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl)); |
| 487 | helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 488 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 489 | help.setIntent(helpIntent); |
| 490 | help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); |
| 491 | } else { |
| 492 | help.setVisible(false); |
| 493 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 494 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 495 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 496 | @Override |
| 497 | public boolean onOptionsItemSelected(MenuItem item) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 498 | switch (item.getItemId()) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 499 | case R.id.data_usage_menu_roaming: { |
| 500 | final boolean dataRoaming = !item.isChecked(); |
| 501 | if (dataRoaming) { |
| 502 | ConfirmDataRoamingFragment.show(this); |
| 503 | } else { |
| 504 | // no confirmation to disable roaming |
| 505 | setDataRoaming(false); |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | case R.id.data_usage_menu_restrict_background: { |
| 510 | final boolean restrictBackground = !item.isChecked(); |
| 511 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 512 | ConfirmRestrictFragment.show(this); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 513 | } else { |
| 514 | // no confirmation to drop restriction |
| 515 | setRestrictBackground(false); |
| 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | case R.id.data_usage_menu_split_4g: { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 520 | final boolean mobileSplit = !item.isChecked(); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 521 | setMobilePolicySplit(mobileSplit); |
| 522 | item.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 523 | updateTabs(); |
| 524 | return true; |
| 525 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 526 | case R.id.data_usage_menu_show_wifi: { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 527 | mShowWifi = !item.isChecked(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 528 | mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 529 | item.setChecked(mShowWifi); |
| 530 | updateTabs(); |
| 531 | return true; |
| 532 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 533 | case R.id.data_usage_menu_show_ethernet: { |
| 534 | mShowEthernet = !item.isChecked(); |
| 535 | mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply(); |
| 536 | item.setChecked(mShowEthernet); |
| 537 | updateTabs(); |
| 538 | return true; |
| 539 | } |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 540 | case R.id.data_usage_menu_metered: { |
| 541 | final PreferenceActivity activity = (PreferenceActivity) getActivity(); |
| 542 | activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null, |
| 543 | R.string.data_usage_metered_title, null, this, 0); |
| 544 | return true; |
| 545 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 550 | @Override |
Jeff Sharkey | 02b327e | 2012-05-15 11:33:59 -0700 | [diff] [blame] | 551 | public void onDestroy() { |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 552 | mDataEnabledView = null; |
| 553 | mDisableAtLimitView = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 554 | |
| 555 | mUidDetailProvider.clearCache(); |
| 556 | mUidDetailProvider = null; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 557 | |
| 558 | TrafficStats.closeQuietly(mStatsSession); |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 559 | |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 560 | if (this.isRemoving()) { |
| 561 | getFragmentManager() |
| 562 | .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| 563 | } |
Jeff Sharkey | 02b327e | 2012-05-15 11:33:59 -0700 | [diff] [blame] | 564 | |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 565 | super.onDestroy(); |
| 566 | } |
| 567 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 568 | /** |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 569 | * Build and assign {@link LayoutTransition} to various containers. Should |
| 570 | * only be assigned after initial layout is complete. |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 571 | */ |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 572 | private void ensureLayoutTransitions() { |
| 573 | // skip when already setup |
| 574 | if (mChart.getLayoutTransition() != null) return; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 575 | |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 576 | mTabsContainer.setLayoutTransition(buildLayoutTransition()); |
| 577 | mHeader.setLayoutTransition(buildLayoutTransition()); |
| 578 | mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition()); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 579 | |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 580 | final LayoutTransition chartTransition = buildLayoutTransition(); |
| 581 | chartTransition.disableTransitionType(LayoutTransition.APPEARING); |
| 582 | chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING); |
| 583 | mChart.setLayoutTransition(chartTransition); |
| 584 | } |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 585 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 586 | private static LayoutTransition buildLayoutTransition() { |
| 587 | final LayoutTransition transition = new LayoutTransition(); |
| 588 | if (TEST_ANIM) { |
| 589 | transition.setDuration(1500); |
| 590 | } |
| 591 | transition.setAnimateParentHierarchy(false); |
| 592 | return transition; |
| 593 | } |
| 594 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 595 | /** |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 596 | * Rebuild all tabs based on {@link NetworkPolicyEditor} and |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 597 | * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects |
| 598 | * first tab, and kicks off a full rebind of body contents. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 599 | */ |
| 600 | private void updateTabs() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 601 | final Context context = getActivity(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 602 | mTabHost.clearAllTabs(); |
| 603 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 604 | final boolean mobileSplit = isMobilePolicySplit(); |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 605 | if (mobileSplit && hasReadyMobile4gRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 606 | mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g)); |
| 607 | mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g)); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 608 | } else if (hasReadyMobileRadio(context)) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 609 | mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 610 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 611 | if (mShowWifi && hasWifiRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 612 | mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi)); |
| 613 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 614 | if (mShowEthernet && hasEthernet(context)) { |
| 615 | mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet)); |
| 616 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 617 | |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 618 | final boolean noTabs = mTabWidget.getTabCount() == 0; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 619 | final boolean multipleTabs = mTabWidget.getTabCount() > 1; |
| 620 | mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE); |
| 621 | if (mIntentTab != null) { |
| 622 | if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 623 | // already hit updateBody() when added; ignore |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 624 | updateBody(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 625 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 626 | mTabHost.setCurrentTabByTag(mIntentTab); |
| 627 | } |
| 628 | mIntentTab = null; |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 629 | } else if (noTabs) { |
| 630 | // no usable tabs, so hide body |
| 631 | updateBody(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 632 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 633 | // already hit updateBody() when added; ignore |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 637 | /** |
| 638 | * Factory that provide empty {@link View} to make {@link TabHost} happy. |
| 639 | */ |
| 640 | private TabContentFactory mEmptyTabContent = new TabContentFactory() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 641 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 642 | public View createTabContent(String tag) { |
| 643 | return new View(mTabHost.getContext()); |
| 644 | } |
| 645 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 646 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 647 | /** |
| 648 | * Build {@link TabSpec} with thin indicator, and empty content. |
| 649 | */ |
| 650 | private TabSpec buildTabSpec(String tag, int titleRes) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 651 | return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent( |
| 652 | mEmptyTabContent); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | private OnTabChangeListener mTabListener = new OnTabChangeListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 656 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 657 | public void onTabChanged(String tabId) { |
| 658 | // user changed tab; update body |
| 659 | updateBody(); |
| 660 | } |
| 661 | }; |
| 662 | |
| 663 | /** |
| 664 | * Update body content based on current tab. Loads |
| 665 | * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and |
| 666 | * binds them to visible controls. |
| 667 | */ |
| 668 | private void updateBody() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 669 | mBinding = true; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 670 | if (!isAdded()) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 671 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 672 | final Context context = getActivity(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 673 | final String currentTab = mTabHost.getCurrentTabTag(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 674 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 675 | if (currentTab == null) { |
| 676 | Log.w(TAG, "no tab selected; hiding body"); |
| 677 | mListView.setVisibility(View.GONE); |
| 678 | return; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 679 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 680 | mListView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 681 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 682 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 683 | final boolean tabChanged = !currentTab.equals(mCurrentTab); |
| 684 | mCurrentTab = currentTab; |
| 685 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 686 | if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab); |
| 687 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 688 | mDataEnabledView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 689 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 690 | // TODO: remove mobile tabs when SIM isn't ready |
| 691 | final TelephonyManager tele = TelephonyManager.from(context); |
| 692 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 693 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 694 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile); |
| 695 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 696 | mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 697 | |
| 698 | } else if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 699 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g); |
| 700 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit); |
| 701 | // TODO: bind mDataEnabled to 3G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 702 | mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 703 | |
| 704 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 705 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g); |
| 706 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit); |
| 707 | // TODO: bind mDataEnabled to 4G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 708 | mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context)); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 709 | |
| 710 | } else if (TAB_WIFI.equals(currentTab)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 711 | // wifi doesn't have any controls |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 712 | mDataEnabledView.setVisibility(View.GONE); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 713 | mDisableAtLimitView.setVisibility(View.GONE); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 714 | mTemplate = buildTemplateWifiWildcard(); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 715 | |
| 716 | } else if (TAB_ETHERNET.equals(currentTab)) { |
| 717 | // ethernet doesn't have any controls |
| 718 | mDataEnabledView.setVisibility(View.GONE); |
| 719 | mDisableAtLimitView.setVisibility(View.GONE); |
| 720 | mTemplate = buildTemplateEthernet(); |
| 721 | |
| 722 | } else { |
| 723 | throw new IllegalStateException("unknown tab: " + currentTab); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 726 | // kick off loader for network history |
| 727 | // TODO: consider chaining two loaders together instead of reloading |
| 728 | // network history when showing app detail. |
| 729 | getLoaderManager().restartLoader(LOADER_CHART_DATA, |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 730 | ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 731 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 732 | // detail mode can change visible menus, invalidate |
| 733 | getActivity().invalidateOptionsMenu(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 734 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 735 | mBinding = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 738 | private boolean isAppDetailMode() { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 739 | return mCurrentApp != null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /** |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 743 | * Update UID details panels to match {@link #mCurrentApp}, showing or |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 744 | * hiding them depending on {@link #isAppDetailMode()}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 745 | */ |
| 746 | private void updateAppDetail() { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 747 | final Context context = getActivity(); |
| 748 | final PackageManager pm = context.getPackageManager(); |
| 749 | final LayoutInflater inflater = getActivity().getLayoutInflater(); |
| 750 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 751 | if (isAppDetailMode()) { |
| 752 | mAppDetail.setVisibility(View.VISIBLE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 753 | mCycleAdapter.setChangeVisible(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 754 | } else { |
| 755 | mAppDetail.setVisibility(View.GONE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 756 | mCycleAdapter.setChangeVisible(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 757 | |
| 758 | // hide detail stats when not in detail mode |
| 759 | mChart.bindDetailNetworkStats(null); |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | // remove warning/limit sweeps while in detail mode |
| 764 | mChart.bindNetworkPolicy(null); |
| 765 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 766 | // show icon and all labels appearing under this app |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 767 | final int appId = mCurrentApp.appId; |
| 768 | final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 769 | mAppIcon.setImageDrawable(detail.icon); |
| 770 | |
| 771 | mAppTitles.removeAllViews(); |
| 772 | if (detail.detailLabels != null) { |
| 773 | for (CharSequence label : detail.detailLabels) { |
| 774 | mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label)); |
| 775 | } |
| 776 | } else { |
| 777 | mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label)); |
| 778 | } |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 779 | |
| 780 | // enable settings button when package provides it |
| 781 | // TODO: target torwards entire UID instead of just first package |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 782 | final String[] packageNames = pm.getPackagesForUid(appId); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 783 | if (packageNames != null && packageNames.length > 0) { |
| 784 | mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE); |
| 785 | mAppSettingsIntent.setPackage(packageNames[0]); |
| 786 | mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 787 | |
| 788 | final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null; |
| 789 | mAppSettings.setEnabled(matchFound); |
Jeff Sharkey | d92e041 | 2012-04-24 11:35:43 -0700 | [diff] [blame] | 790 | mAppSettings.setVisibility(View.VISIBLE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 791 | |
| 792 | } else { |
| 793 | mAppSettingsIntent = null; |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 794 | mAppSettings.setVisibility(View.GONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 797 | updateDetailData(); |
| 798 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 799 | if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground() |
| 800 | && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 801 | setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background); |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 802 | setPreferenceSummary(mAppRestrictView, |
| 803 | getString(R.string.data_usage_app_restrict_background_summary)); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 804 | |
| 805 | mAppRestrictView.setVisibility(View.VISIBLE); |
| 806 | mAppRestrict.setChecked(getAppRestrictBackground()); |
| 807 | |
| 808 | } else { |
| 809 | mAppRestrictView.setVisibility(View.GONE); |
| 810 | } |
| 811 | } |
| 812 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 813 | private void setPolicyWarningBytes(long warningBytes) { |
| 814 | if (LOGD) Log.d(TAG, "setPolicyWarningBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 815 | mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 816 | updatePolicy(false); |
| 817 | } |
| 818 | |
| 819 | private void setPolicyLimitBytes(long limitBytes) { |
| 820 | if (LOGD) Log.d(TAG, "setPolicyLimitBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 821 | mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 822 | updatePolicy(false); |
| 823 | } |
| 824 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 825 | /** |
| 826 | * Local cache of value, used to work around delay when |
| 827 | * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async. |
| 828 | */ |
| 829 | private Boolean mMobileDataEnabled; |
| 830 | |
| 831 | private boolean isMobileDataEnabled() { |
| 832 | if (mMobileDataEnabled != null) { |
| 833 | // TODO: deprecate and remove this once enabled flag is on policy |
| 834 | return mMobileDataEnabled; |
| 835 | } else { |
| 836 | return mConnService.getMobileDataEnabled(); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | private void setMobileDataEnabled(boolean enabled) { |
| 841 | if (LOGD) Log.d(TAG, "setMobileDataEnabled()"); |
| 842 | mConnService.setMobileDataEnabled(enabled); |
| 843 | mMobileDataEnabled = enabled; |
| 844 | updatePolicy(false); |
| 845 | } |
| 846 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 847 | private boolean isNetworkPolicyModifiable(NetworkPolicy policy) { |
| 848 | return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked(); |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | private boolean isBandwidthControlEnabled() { |
| 852 | try { |
| 853 | return mNetworkService.isBandwidthControlEnabled(); |
| 854 | } catch (RemoteException e) { |
| 855 | Log.w(TAG, "problem talking with INetworkManagementService: " + e); |
| 856 | return false; |
| 857 | } |
| 858 | } |
| 859 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 860 | private boolean getDataRoaming() { |
| 861 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 862 | return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0; |
| 863 | } |
| 864 | |
| 865 | private void setDataRoaming(boolean enabled) { |
| 866 | // TODO: teach telephony DataConnectionTracker to watch and apply |
| 867 | // updates when changed. |
| 868 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 869 | Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0); |
| 870 | mMenuDataRoaming.setChecked(enabled); |
| 871 | } |
| 872 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 873 | public void setRestrictBackground(boolean restrictBackground) { |
| 874 | mPolicyManager.setRestrictBackground(restrictBackground); |
| 875 | mMenuRestrictBackground.setChecked(restrictBackground); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | private boolean getAppRestrictBackground() { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 879 | final int appId = mCurrentApp.appId; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 880 | final int uidPolicy = mPolicyManager.getAppPolicy(appId); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 881 | return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
| 882 | } |
| 883 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 884 | private void setAppRestrictBackground(boolean restrictBackground) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 885 | if (LOGD) Log.d(TAG, "setAppRestrictBackground()"); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 886 | final int appId = mCurrentApp.appId; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 887 | mPolicyManager.setAppPolicy(appId, |
| 888 | restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 889 | mAppRestrict.setChecked(restrictBackground); |
| 890 | } |
| 891 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 892 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 893 | * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for |
| 894 | * current {@link #mTemplate}. |
| 895 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 896 | private void updatePolicy(boolean refreshCycle) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 897 | if (isAppDetailMode()) { |
| 898 | mNetworkSwitches.setVisibility(View.GONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 899 | } else { |
| 900 | mNetworkSwitches.setVisibility(View.VISIBLE); |
| 901 | } |
| 902 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 903 | // TODO: move enabled state directly into policy |
| 904 | if (TAB_MOBILE.equals(mCurrentTab)) { |
| 905 | mBinding = true; |
| 906 | mDataEnabled.setChecked(isMobileDataEnabled()); |
| 907 | mBinding = false; |
| 908 | } |
| 909 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 910 | final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate); |
| 911 | if (isNetworkPolicyModifiable(policy)) { |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 912 | mDisableAtLimitView.setVisibility(View.VISIBLE); |
| 913 | mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 914 | if (!isAppDetailMode()) { |
| 915 | mChart.bindNetworkPolicy(policy); |
| 916 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 917 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 918 | } else { |
| 919 | // controls are disabled; don't bind warning/limit sweeps |
| 920 | mDisableAtLimitView.setVisibility(View.GONE); |
| 921 | mChart.bindNetworkPolicy(null); |
| 922 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 923 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 924 | if (refreshCycle) { |
| 925 | // generate cycle list based on policy and available history |
| 926 | updateCycleList(policy); |
| 927 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 931 | * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay} |
| 932 | * and available {@link NetworkStatsHistory} data. Always selects the newest |
| 933 | * item, updating the inspection range on {@link #mChart}. |
| 934 | */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 935 | private void updateCycleList(NetworkPolicy policy) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 936 | // stash away currently selected cycle to try restoring below |
| 937 | final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 938 | mCycleAdapter.clear(); |
| 939 | |
| 940 | final Context context = mCycleSpinner.getContext(); |
| 941 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 942 | long historyStart = Long.MAX_VALUE; |
| 943 | long historyEnd = Long.MIN_VALUE; |
| 944 | if (mChartData != null) { |
| 945 | historyStart = mChartData.network.getStart(); |
| 946 | historyEnd = mChartData.network.getEnd(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 947 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 948 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 949 | final long now = System.currentTimeMillis(); |
| 950 | if (historyStart == Long.MAX_VALUE) historyStart = now; |
| 951 | if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 952 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 953 | boolean hasCycles = false; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 954 | if (policy != null) { |
| 955 | // find the next cycle boundary |
| 956 | long cycleEnd = computeNextCycleBoundary(historyEnd, policy); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 957 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 958 | // walk backwards, generating all valid cycle ranges |
| 959 | while (cycleEnd > historyStart) { |
| 960 | final long cycleStart = computeLastCycleBoundary(cycleEnd, policy); |
| 961 | Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs=" |
| 962 | + historyStart); |
| 963 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 964 | cycleEnd = cycleStart; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 965 | hasCycles = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 966 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 967 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 968 | // one last cycle entry to modify policy cycle day |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 969 | mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy)); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 970 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 971 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 972 | if (!hasCycles) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 973 | // no policy defined cycles; show entry for each four-week period |
| 974 | long cycleEnd = historyEnd; |
| 975 | while (cycleEnd > historyStart) { |
| 976 | final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4); |
| 977 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 978 | cycleEnd = cycleStart; |
| 979 | } |
| 980 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 981 | mCycleAdapter.setChangePossible(false); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 982 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 983 | |
| 984 | // force pick the current cycle (first item) |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 985 | if (mCycleAdapter.getCount() > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 986 | final int position = mCycleAdapter.findNearestPosition(previousItem); |
| 987 | mCycleSpinner.setSelection(position); |
| 988 | |
| 989 | // only force-update cycle when changed; skipping preserves any |
| 990 | // user-defined inspection region. |
| 991 | final CycleItem selectedItem = mCycleAdapter.getItem(position); |
| 992 | if (!Objects.equal(selectedItem, previousItem)) { |
| 993 | mCycleListener.onItemSelected(mCycleSpinner, null, position, 0); |
| 994 | } else { |
| 995 | // but still kick off loader for detailed list |
| 996 | updateDetailData(); |
| 997 | } |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 998 | } else { |
| 999 | updateDetailData(); |
| 1000 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1003 | private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1004 | @Override |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1005 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 1006 | if (mBinding) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1007 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1008 | final boolean dataEnabled = isChecked; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1009 | final String currentTab = mCurrentTab; |
| 1010 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1011 | if (dataEnabled) { |
| 1012 | setMobileDataEnabled(true); |
| 1013 | } else { |
| 1014 | // disabling data; show confirmation dialog which eventually |
| 1015 | // calls setMobileDataEnabled() once user confirms. |
| 1016 | ConfirmDataDisableFragment.show(DataUsageSummary.this); |
| 1017 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1018 | } |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 1019 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1020 | updatePolicy(false); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | }; |
| 1023 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1024 | private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1025 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1026 | public void onClick(View v) { |
| 1027 | final boolean disableAtLimit = !mDisableAtLimit.isChecked(); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1028 | if (disableAtLimit) { |
| 1029 | // enabling limit; show confirmation dialog which eventually |
| 1030 | // calls setPolicyLimitBytes() once user confirms. |
| 1031 | ConfirmLimitFragment.show(DataUsageSummary.this); |
| 1032 | } else { |
| 1033 | setPolicyLimitBytes(LIMIT_DISABLED); |
| 1034 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1035 | } |
| 1036 | }; |
| 1037 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1038 | private View.OnClickListener mAppRestrictListener = new View.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1039 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1040 | public void onClick(View v) { |
| 1041 | final boolean restrictBackground = !mAppRestrict.isChecked(); |
| 1042 | |
| 1043 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 1044 | // enabling restriction; show confirmation dialog which |
| 1045 | // eventually calls setRestrictBackground() once user |
| 1046 | // confirms. |
| 1047 | ConfirmAppRestrictFragment.show(DataUsageSummary.this); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1048 | } else { |
| 1049 | setAppRestrictBackground(false); |
| 1050 | } |
| 1051 | } |
| 1052 | }; |
| 1053 | |
| 1054 | private OnClickListener mAppSettingsListener = new OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1055 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1056 | public void onClick(View v) { |
Jeff Sharkey | d92e041 | 2012-04-24 11:35:43 -0700 | [diff] [blame] | 1057 | if (!isAdded()) return; |
| 1058 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1059 | // TODO: target torwards entire UID instead of just first package |
| 1060 | startActivity(mAppSettingsIntent); |
| 1061 | } |
| 1062 | }; |
| 1063 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1064 | private OnItemClickListener mListListener = new OnItemClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1065 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1066 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1067 | final Context context = view.getContext(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1068 | final AppItem app = (AppItem) parent.getItemAtPosition(position); |
Jeff Sharkey | 3da415f | 2012-05-18 14:00:10 -0700 | [diff] [blame] | 1069 | |
| 1070 | // TODO: sigh, remove this hack once we understand 6450986 |
| 1071 | if (mUidDetailProvider == null || app == null) return; |
| 1072 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1073 | final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true); |
| 1074 | AppDetailsFragment.show(DataUsageSummary.this, app, detail.label); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1075 | } |
| 1076 | }; |
| 1077 | |
| 1078 | private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1079 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1080 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 1081 | final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position); |
| 1082 | if (cycle instanceof CycleChangeItem) { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1083 | // show cycle editor; will eventually call setPolicyCycleDay() |
| 1084 | // when user finishes editing. |
| 1085 | CycleEditorFragment.show(DataUsageSummary.this); |
| 1086 | |
| 1087 | // reset spinner to something other than "change cycle..." |
| 1088 | mCycleSpinner.setSelection(0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1089 | |
| 1090 | } else { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1091 | if (LOGD) { |
| 1092 | Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end=" |
| 1093 | + cycle.end + "]"); |
| 1094 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1095 | |
| 1096 | // update chart to show selected cycle, and update detail data |
| 1097 | // to match updated sweep bounds. |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1098 | mChart.setVisibleRange(cycle.start, cycle.end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1099 | |
| 1100 | updateDetailData(); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1104 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1105 | public void onNothingSelected(AdapterView<?> parent) { |
| 1106 | // ignored |
| 1107 | } |
| 1108 | }; |
| 1109 | |
| 1110 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1111 | * Update details based on {@link #mChart} inspection range depending on |
| 1112 | * current mode. In network mode, updates {@link #mAdapter} with sorted list |
| 1113 | * of applications data usage, and when {@link #isAppDetailMode()} update |
| 1114 | * app details. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1115 | */ |
| 1116 | private void updateDetailData() { |
| 1117 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 1118 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1119 | final long start = mChart.getInspectStart(); |
| 1120 | final long end = mChart.getInspectEnd(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1121 | final long now = System.currentTimeMillis(); |
| 1122 | |
| 1123 | final Context context = getActivity(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1124 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1125 | NetworkStatsHistory.Entry entry = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1126 | if (isAppDetailMode() && mChartData != null && mChartData.detail != null) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1127 | // bind foreground/background to piechart and labels |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1128 | entry = mChartData.detailDefault.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1129 | final long defaultBytes = entry.rxBytes + entry.txBytes; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1130 | entry = mChartData.detailForeground.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1131 | final long foregroundBytes = entry.rxBytes + entry.txBytes; |
| 1132 | |
| 1133 | mAppPieChart.setOriginAngle(175); |
| 1134 | |
| 1135 | mAppPieChart.removeAllSlices(); |
| 1136 | mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a")); |
| 1137 | mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666")); |
| 1138 | |
| 1139 | mAppPieChart.generatePath(); |
| 1140 | |
| 1141 | mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes)); |
| 1142 | mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes)); |
| 1143 | |
| 1144 | // and finally leave with summary data for label below |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1145 | entry = mChartData.detail.getValues(start, end, now, null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1146 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1147 | getLoaderManager().destroyLoader(LOADER_SUMMARY); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1148 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1149 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1150 | if (mChartData != null) { |
| 1151 | entry = mChartData.network.getValues(start, end, now, null); |
| 1152 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1153 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1154 | // kick off loader for detailed stats |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1155 | getLoaderManager().restartLoader(LOADER_SUMMARY, |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1156 | SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1157 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1158 | |
| 1159 | final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0; |
| 1160 | final String totalPhrase = Formatter.formatFileSize(context, totalBytes); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1161 | final String rangePhrase = formatDateRange(context, start, end); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1162 | |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1163 | final int summaryRes; |
| 1164 | if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp) |
| 1165 | || TAB_4G.equals(mCurrentApp)) { |
| 1166 | summaryRes = R.string.data_usage_total_during_range_mobile; |
| 1167 | } else { |
| 1168 | summaryRes = R.string.data_usage_total_during_range; |
| 1169 | } |
| 1170 | |
| 1171 | mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase)); |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 1172 | |
| 1173 | // initial layout is finished above, ensure we have transitions |
| 1174 | ensureLayoutTransitions(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1177 | private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks< |
| 1178 | ChartData>() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1179 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1180 | public Loader<ChartData> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 1181 | return new ChartDataLoader(getActivity(), mStatsSession, args); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1184 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1185 | public void onLoadFinished(Loader<ChartData> loader, ChartData data) { |
| 1186 | mChartData = data; |
| 1187 | mChart.bindNetworkStats(mChartData.network); |
| 1188 | mChart.bindDetailNetworkStats(mChartData.detail); |
| 1189 | |
| 1190 | // calcuate policy cycles based on available data |
| 1191 | updatePolicy(true); |
| 1192 | updateAppDetail(); |
| 1193 | |
| 1194 | // force scroll to top of body when showing detail |
| 1195 | if (mChartData.detail != null) { |
| 1196 | mListView.smoothScrollToPosition(0); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1200 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1201 | public void onLoaderReset(Loader<ChartData> loader) { |
| 1202 | mChartData = null; |
| 1203 | mChart.bindNetworkStats(null); |
| 1204 | mChart.bindDetailNetworkStats(null); |
| 1205 | } |
| 1206 | }; |
| 1207 | |
| 1208 | private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks< |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1209 | NetworkStats>() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1210 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1211 | public Loader<NetworkStats> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 1212 | return new SummaryForAllUidLoader(getActivity(), mStatsSession, args); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1215 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1216 | public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1217 | final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy( |
| 1218 | POLICY_REJECT_METERED_BACKGROUND); |
| 1219 | mAdapter.bindStats(data, restrictedAppIds); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1220 | updateEmptyVisible(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1221 | } |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 1222 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1223 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1224 | public void onLoaderReset(Loader<NetworkStats> loader) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1225 | mAdapter.bindStats(null, new int[0]); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1226 | updateEmptyVisible(); |
| 1227 | } |
| 1228 | |
| 1229 | private void updateEmptyVisible() { |
| 1230 | final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode(); |
| 1231 | mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1232 | } |
| 1233 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1234 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1235 | @Deprecated |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1236 | private boolean isMobilePolicySplit() { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1237 | final Context context = getActivity(); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1238 | if (hasReadyMobileRadio(context)) { |
| 1239 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1240 | return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context)); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1241 | } else { |
| 1242 | return false; |
| 1243 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1246 | @Deprecated |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1247 | private void setMobilePolicySplit(boolean split) { |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1248 | final Context context = getActivity(); |
| 1249 | if (hasReadyMobileRadio(context)) { |
| 1250 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1251 | mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split); |
| 1252 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1255 | private static String getActiveSubscriberId(Context context) { |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1256 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1257 | final String actualSubscriberId = tele.getSubscriberId(); |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 1258 | return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1261 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1262 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1263 | public void onInspectRangeChanged() { |
| 1264 | if (LOGD) Log.d(TAG, "onInspectRangeChanged()"); |
| 1265 | updateDetailData(); |
| 1266 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1267 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1268 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1269 | public void onWarningChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1270 | setPolicyWarningBytes(mChart.getWarningBytes()); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1271 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1272 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1273 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1274 | public void onLimitChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1275 | setPolicyLimitBytes(mChart.getLimitBytes()); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1276 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1277 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1278 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1279 | public void requestWarningEdit() { |
| 1280 | WarningEditorFragment.show(DataUsageSummary.this); |
| 1281 | } |
| 1282 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1283 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1284 | public void requestLimitEdit() { |
| 1285 | LimitEditorFragment.show(DataUsageSummary.this); |
| 1286 | } |
| 1287 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1288 | |
| 1289 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1290 | * List item that reflects a specific data usage cycle. |
| 1291 | */ |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1292 | public static class CycleItem implements Comparable<CycleItem> { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1293 | public CharSequence label; |
| 1294 | public long start; |
| 1295 | public long end; |
| 1296 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1297 | CycleItem(CharSequence label) { |
| 1298 | this.label = label; |
| 1299 | } |
| 1300 | |
| 1301 | public CycleItem(Context context, long start, long end) { |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1302 | this.label = formatDateRange(context, start, end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1303 | this.start = start; |
| 1304 | this.end = end; |
| 1305 | } |
| 1306 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1307 | @Override |
| 1308 | public String toString() { |
| 1309 | return label.toString(); |
| 1310 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1311 | |
| 1312 | @Override |
| 1313 | public boolean equals(Object o) { |
| 1314 | if (o instanceof CycleItem) { |
| 1315 | final CycleItem another = (CycleItem) o; |
| 1316 | return start == another.start && end == another.end; |
| 1317 | } |
| 1318 | return false; |
| 1319 | } |
| 1320 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1321 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1322 | public int compareTo(CycleItem another) { |
| 1323 | return Long.compare(start, another.start); |
| 1324 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1327 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 1328 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 1329 | sBuilder, Locale.getDefault()); |
| 1330 | |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1331 | public static String formatDateRange(Context context, long start, long end) { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1332 | final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1333 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1334 | synchronized (sBuilder) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1335 | sBuilder.setLength(0); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1336 | return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null) |
| 1337 | .toString(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1341 | /** |
| 1342 | * Special-case data usage cycle that triggers dialog to change |
| 1343 | * {@link NetworkPolicy#cycleDay}. |
| 1344 | */ |
| 1345 | public static class CycleChangeItem extends CycleItem { |
| 1346 | public CycleChangeItem(Context context) { |
| 1347 | super(context.getString(R.string.data_usage_change_cycle)); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | public static class CycleAdapter extends ArrayAdapter<CycleItem> { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1352 | private boolean mChangePossible = false; |
| 1353 | private boolean mChangeVisible = false; |
| 1354 | |
| 1355 | private final CycleChangeItem mChangeItem; |
| 1356 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1357 | public CycleAdapter(Context context) { |
| 1358 | super(context, android.R.layout.simple_spinner_item); |
| 1359 | setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1360 | mChangeItem = new CycleChangeItem(context); |
| 1361 | } |
| 1362 | |
| 1363 | public void setChangePossible(boolean possible) { |
| 1364 | mChangePossible = possible; |
| 1365 | updateChange(); |
| 1366 | } |
| 1367 | |
| 1368 | public void setChangeVisible(boolean visible) { |
| 1369 | mChangeVisible = visible; |
| 1370 | updateChange(); |
| 1371 | } |
| 1372 | |
| 1373 | private void updateChange() { |
| 1374 | remove(mChangeItem); |
| 1375 | if (mChangePossible && mChangeVisible) { |
| 1376 | add(mChangeItem); |
| 1377 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1378 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1379 | |
| 1380 | /** |
| 1381 | * Find position of {@link CycleItem} in this adapter which is nearest |
| 1382 | * the given {@link CycleItem}. |
| 1383 | */ |
| 1384 | public int findNearestPosition(CycleItem target) { |
| 1385 | if (target != null) { |
| 1386 | final int count = getCount(); |
| 1387 | for (int i = count - 1; i >= 0; i--) { |
| 1388 | final CycleItem item = getItem(i); |
| 1389 | if (item instanceof CycleChangeItem) { |
| 1390 | continue; |
| 1391 | } else if (item.compareTo(target) >= 0) { |
| 1392 | return i; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | return 0; |
| 1397 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1400 | public static class AppItem implements Comparable<AppItem>, Parcelable { |
| 1401 | public final int appId; |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1402 | public boolean restricted; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1403 | public SparseBooleanArray uids = new SparseBooleanArray(); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1404 | public long total; |
| 1405 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1406 | public AppItem(int appId) { |
| 1407 | this.appId = appId; |
| 1408 | } |
| 1409 | |
| 1410 | public AppItem(Parcel parcel) { |
| 1411 | appId = parcel.readInt(); |
| 1412 | uids = parcel.readSparseBooleanArray(); |
| 1413 | total = parcel.readLong(); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | public void addUid(int uid) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1417 | uids.put(uid, true); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1420 | @Override |
| 1421 | public void writeToParcel(Parcel dest, int flags) { |
| 1422 | dest.writeInt(appId); |
| 1423 | dest.writeSparseBooleanArray(uids); |
| 1424 | dest.writeLong(total); |
| 1425 | } |
| 1426 | |
| 1427 | @Override |
| 1428 | public int describeContents() { |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | public int compareTo(AppItem another) { |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1434 | return Long.compare(another.total, total); |
| 1435 | } |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1436 | |
| 1437 | public static final Creator<AppItem> CREATOR = new Creator<AppItem>() { |
| 1438 | @Override |
| 1439 | public AppItem createFromParcel(Parcel in) { |
| 1440 | return new AppItem(in); |
| 1441 | } |
| 1442 | |
| 1443 | @Override |
| 1444 | public AppItem[] newArray(int size) { |
| 1445 | return new AppItem[size]; |
| 1446 | } |
| 1447 | }; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1450 | /** |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1451 | * Adapter of applications, sorted by total usage descending. |
| 1452 | */ |
| 1453 | public static class DataUsageAdapter extends BaseAdapter { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1454 | private final UidDetailProvider mProvider; |
| 1455 | private final int mInsetSide; |
| 1456 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1457 | private ArrayList<AppItem> mItems = Lists.newArrayList(); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1458 | private long mLargest; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1459 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1460 | public DataUsageAdapter(UidDetailProvider provider, int insetSide) { |
| 1461 | mProvider = checkNotNull(provider); |
| 1462 | mInsetSide = insetSide; |
| 1463 | } |
| 1464 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1465 | /** |
| 1466 | * Bind the given {@link NetworkStats}, or {@code null} to clear list. |
| 1467 | */ |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1468 | public void bindStats(NetworkStats stats, int[] restrictedAppIds) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1469 | mItems.clear(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1470 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1471 | final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID); |
| 1472 | final SparseArray<AppItem> knownUids = new SparseArray<AppItem>(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1473 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1474 | NetworkStats.Entry entry = null; |
| 1475 | final int size = stats != null ? stats.size() : 0; |
| 1476 | for (int i = 0; i < size; i++) { |
| 1477 | entry = stats.getValues(i, entry); |
| 1478 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1479 | final boolean isApp = UserId.isApp(entry.uid); |
| 1480 | final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid; |
| 1481 | if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) { |
| 1482 | AppItem item = knownUids.get(appId); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1483 | if (item == null) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1484 | item = new AppItem(appId); |
| 1485 | knownUids.put(appId, item); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1486 | mItems.add(item); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1487 | } |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1488 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1489 | item.total += entry.rxBytes + entry.txBytes; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1490 | item.addUid(entry.uid); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1491 | } else { |
| 1492 | systemItem.total += entry.rxBytes + entry.txBytes; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1493 | systemItem.addUid(entry.uid); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1494 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1497 | for (int appId : restrictedAppIds) { |
| 1498 | AppItem item = knownUids.get(appId); |
| 1499 | if (item == null) { |
| 1500 | item = new AppItem(appId); |
| 1501 | item.total = -1; |
| 1502 | mItems.add(item); |
| 1503 | } |
| 1504 | item.restricted = true; |
| 1505 | } |
| 1506 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1507 | if (systemItem.total > 0) { |
| 1508 | mItems.add(systemItem); |
| 1509 | } |
| 1510 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1511 | Collections.sort(mItems); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1512 | mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1513 | notifyDataSetChanged(); |
| 1514 | } |
| 1515 | |
| 1516 | @Override |
| 1517 | public int getCount() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1518 | return mItems.size(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public Object getItem(int position) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1523 | return mItems.get(position); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | @Override |
| 1527 | public long getItemId(int position) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1528 | return mItems.get(position).appId; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | @Override |
| 1532 | public View getView(int position, View convertView, ViewGroup parent) { |
| 1533 | if (convertView == null) { |
| 1534 | convertView = LayoutInflater.from(parent.getContext()).inflate( |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 1535 | R.layout.data_usage_item, parent, false); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1536 | |
| 1537 | if (mInsetSide > 0) { |
| 1538 | convertView.setPadding(mInsetSide, 0, mInsetSide, 0); |
| 1539 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | final Context context = parent.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1543 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1544 | final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1545 | final ProgressBar progress = (ProgressBar) convertView.findViewById( |
| 1546 | android.R.id.progress); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1547 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1548 | // kick off async load of app details |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1549 | final AppItem item = mItems.get(position); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1550 | UidDetailTask.bindView(mProvider, item, convertView); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1551 | |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1552 | if (item.restricted && item.total <= 0) { |
| 1553 | text1.setText(R.string.data_usage_app_restricted); |
| 1554 | progress.setVisibility(View.GONE); |
| 1555 | } else { |
| 1556 | text1.setText(Formatter.formatFileSize(context, item.total)); |
| 1557 | progress.setVisibility(View.VISIBLE); |
| 1558 | } |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1559 | |
| 1560 | final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0; |
| 1561 | progress.setProgress(percentTotal); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1562 | |
| 1563 | return convertView; |
| 1564 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1567 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1568 | * Empty {@link Fragment} that controls display of UID details in |
| 1569 | * {@link DataUsageSummary}. |
| 1570 | */ |
| 1571 | public static class AppDetailsFragment extends Fragment { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1572 | private static final String EXTRA_APP = "app"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1573 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1574 | public static void show(DataUsageSummary parent, AppItem app, CharSequence label) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1575 | if (!parent.isAdded()) return; |
| 1576 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1577 | final Bundle args = new Bundle(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1578 | args.putParcelable(EXTRA_APP, app); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1579 | |
| 1580 | final AppDetailsFragment fragment = new AppDetailsFragment(); |
| 1581 | fragment.setArguments(args); |
| 1582 | fragment.setTargetFragment(parent, 0); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1583 | final FragmentTransaction ft = parent.getFragmentManager().beginTransaction(); |
| 1584 | ft.add(fragment, TAG_APP_DETAILS); |
| 1585 | ft.addToBackStack(TAG_APP_DETAILS); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1586 | ft.setBreadCrumbTitle(label); |
Jeff Sharkey | 3235ddb | 2012-03-15 16:40:31 -0700 | [diff] [blame] | 1587 | ft.commitAllowingStateLoss(); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | @Override |
| 1591 | public void onStart() { |
| 1592 | super.onStart(); |
| 1593 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1594 | target.mCurrentApp = getArguments().getParcelable(EXTRA_APP); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1595 | target.updateBody(); |
| 1596 | } |
| 1597 | |
| 1598 | @Override |
| 1599 | public void onStop() { |
| 1600 | super.onStop(); |
| 1601 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1602 | target.mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1603 | target.updateBody(); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | /** |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1608 | * Dialog to request user confirmation before setting |
| 1609 | * {@link NetworkPolicy#limitBytes}. |
| 1610 | */ |
| 1611 | public static class ConfirmLimitFragment extends DialogFragment { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1612 | private static final String EXTRA_MESSAGE = "message"; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1613 | private static final String EXTRA_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1614 | |
| 1615 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1616 | if (!parent.isAdded()) return; |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1617 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1618 | final Resources res = parent.getResources(); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1619 | final CharSequence message; |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1620 | final long minLimitBytes = (long) ( |
| 1621 | parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1622 | final long limitBytes; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1623 | |
| 1624 | // TODO: customize default limits based on network template |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1625 | final String currentTab = parent.mCurrentTab; |
| 1626 | if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1627 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1628 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1629 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1630 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1631 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1632 | } else if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1633 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1634 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1635 | } else { |
| 1636 | throw new IllegalArgumentException("unknown current tab: " + currentTab); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1639 | final Bundle args = new Bundle(); |
| 1640 | args.putCharSequence(EXTRA_MESSAGE, message); |
| 1641 | args.putLong(EXTRA_LIMIT_BYTES, limitBytes); |
| 1642 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1643 | final ConfirmLimitFragment dialog = new ConfirmLimitFragment(); |
| 1644 | dialog.setArguments(args); |
| 1645 | dialog.setTargetFragment(parent, 0); |
| 1646 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT); |
| 1647 | } |
| 1648 | |
| 1649 | @Override |
| 1650 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1651 | final Context context = getActivity(); |
| 1652 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1653 | final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1654 | final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES); |
| 1655 | |
| 1656 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1657 | builder.setTitle(R.string.data_usage_limit_dialog_title); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1658 | builder.setMessage(message); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1659 | |
| 1660 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1661 | @Override |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1662 | public void onClick(DialogInterface dialog, int which) { |
| 1663 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1664 | if (target != null) { |
| 1665 | target.setPolicyLimitBytes(limitBytes); |
| 1666 | } |
| 1667 | } |
| 1668 | }); |
| 1669 | |
| 1670 | return builder.create(); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * Dialog to edit {@link NetworkPolicy#cycleDay}. |
| 1676 | */ |
| 1677 | public static class CycleEditorFragment extends DialogFragment { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1678 | private static final String EXTRA_TEMPLATE = "template"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1679 | |
| 1680 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1681 | if (!parent.isAdded()) return; |
| 1682 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1683 | final Bundle args = new Bundle(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1684 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1685 | |
| 1686 | final CycleEditorFragment dialog = new CycleEditorFragment(); |
| 1687 | dialog.setArguments(args); |
| 1688 | dialog.setTargetFragment(parent, 0); |
| 1689 | dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR); |
| 1690 | } |
| 1691 | |
| 1692 | @Override |
| 1693 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1694 | final Context context = getActivity(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1695 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1696 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1697 | |
| 1698 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1699 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1700 | |
| 1701 | final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false); |
| 1702 | final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day); |
| 1703 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1704 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1705 | final int cycleDay = editor.getPolicyCycleDay(template); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1706 | |
| 1707 | cycleDayPicker.setMinValue(1); |
| 1708 | cycleDayPicker.setMaxValue(31); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1709 | cycleDayPicker.setValue(cycleDay); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1710 | cycleDayPicker.setWrapSelectorWheel(true); |
| 1711 | |
| 1712 | builder.setTitle(R.string.data_usage_cycle_editor_title); |
| 1713 | builder.setView(view); |
| 1714 | |
| 1715 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1716 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1717 | @Override |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1718 | public void onClick(DialogInterface dialog, int which) { |
| 1719 | final int cycleDay = cycleDayPicker.getValue(); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1720 | final String cycleTimezone = new Time().timezone; |
| 1721 | editor.setPolicyCycleDay(template, cycleDay, cycleTimezone); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1722 | target.updatePolicy(true); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1723 | } |
| 1724 | }); |
| 1725 | |
| 1726 | return builder.create(); |
| 1727 | } |
| 1728 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1729 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1730 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1731 | * Dialog to edit {@link NetworkPolicy#warningBytes}. |
| 1732 | */ |
| 1733 | public static class WarningEditorFragment extends DialogFragment { |
| 1734 | private static final String EXTRA_TEMPLATE = "template"; |
| 1735 | |
| 1736 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1737 | if (!parent.isAdded()) return; |
| 1738 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1739 | final Bundle args = new Bundle(); |
| 1740 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1741 | |
| 1742 | final WarningEditorFragment dialog = new WarningEditorFragment(); |
| 1743 | dialog.setArguments(args); |
| 1744 | dialog.setTargetFragment(parent, 0); |
| 1745 | dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR); |
| 1746 | } |
| 1747 | |
| 1748 | @Override |
| 1749 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1750 | final Context context = getActivity(); |
| 1751 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1752 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1753 | |
| 1754 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1755 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1756 | |
| 1757 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1758 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1759 | |
| 1760 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1761 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 1762 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 1763 | |
| 1764 | bytesPicker.setMinValue(0); |
| 1765 | if (limitBytes != LIMIT_DISABLED) { |
| 1766 | bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1); |
| 1767 | } else { |
| 1768 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
| 1769 | } |
| 1770 | bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES)); |
| 1771 | bytesPicker.setWrapSelectorWheel(false); |
| 1772 | |
| 1773 | builder.setTitle(R.string.data_usage_warning_editor_title); |
| 1774 | builder.setView(view); |
| 1775 | |
| 1776 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1777 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1778 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1779 | public void onClick(DialogInterface dialog, int which) { |
| 1780 | // clear focus to finish pending text edits |
| 1781 | bytesPicker.clearFocus(); |
| 1782 | |
| 1783 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 1784 | editor.setPolicyWarningBytes(template, bytes); |
| 1785 | target.updatePolicy(false); |
| 1786 | } |
| 1787 | }); |
| 1788 | |
| 1789 | return builder.create(); |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | /** |
| 1794 | * Dialog to edit {@link NetworkPolicy#limitBytes}. |
| 1795 | */ |
| 1796 | public static class LimitEditorFragment extends DialogFragment { |
| 1797 | private static final String EXTRA_TEMPLATE = "template"; |
| 1798 | |
| 1799 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1800 | if (!parent.isAdded()) return; |
| 1801 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1802 | final Bundle args = new Bundle(); |
| 1803 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1804 | |
| 1805 | final LimitEditorFragment dialog = new LimitEditorFragment(); |
| 1806 | dialog.setArguments(args); |
| 1807 | dialog.setTargetFragment(parent, 0); |
| 1808 | dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR); |
| 1809 | } |
| 1810 | |
| 1811 | @Override |
| 1812 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1813 | final Context context = getActivity(); |
| 1814 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1815 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1816 | |
| 1817 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1818 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1819 | |
| 1820 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1821 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1822 | |
| 1823 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1824 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 1825 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 1826 | |
| 1827 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
Shuhrat Dehkanov | f9237f6 | 2012-01-26 23:04:02 +0900 | [diff] [blame] | 1828 | if (warningBytes != WARNING_DISABLED && limitBytes > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1829 | bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1); |
| 1830 | } else { |
| 1831 | bytesPicker.setMinValue(0); |
| 1832 | } |
| 1833 | bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES)); |
| 1834 | bytesPicker.setWrapSelectorWheel(false); |
| 1835 | |
| 1836 | builder.setTitle(R.string.data_usage_limit_editor_title); |
| 1837 | builder.setView(view); |
| 1838 | |
| 1839 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1840 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1841 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1842 | public void onClick(DialogInterface dialog, int which) { |
| 1843 | // clear focus to finish pending text edits |
| 1844 | bytesPicker.clearFocus(); |
| 1845 | |
| 1846 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 1847 | editor.setPolicyLimitBytes(template, bytes); |
| 1848 | target.updatePolicy(false); |
| 1849 | } |
| 1850 | }); |
| 1851 | |
| 1852 | return builder.create(); |
| 1853 | } |
| 1854 | } |
| 1855 | /** |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1856 | * Dialog to request user confirmation before disabling data. |
| 1857 | */ |
| 1858 | public static class ConfirmDataDisableFragment extends DialogFragment { |
| 1859 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1860 | if (!parent.isAdded()) return; |
| 1861 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1862 | final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment(); |
| 1863 | dialog.setTargetFragment(parent, 0); |
| 1864 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE); |
| 1865 | } |
| 1866 | |
| 1867 | @Override |
| 1868 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1869 | final Context context = getActivity(); |
| 1870 | |
| 1871 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1872 | builder.setMessage(R.string.data_usage_disable_mobile); |
| 1873 | |
| 1874 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1875 | @Override |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1876 | public void onClick(DialogInterface dialog, int which) { |
| 1877 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1878 | if (target != null) { |
| 1879 | // TODO: extend to modify policy enabled flag. |
| 1880 | target.setMobileDataEnabled(false); |
| 1881 | } |
| 1882 | } |
| 1883 | }); |
| 1884 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1885 | |
| 1886 | return builder.create(); |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1891 | * Dialog to request user confirmation before setting |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1892 | * {@link android.provider.Settings.Secure#DATA_ROAMING}. |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1893 | */ |
| 1894 | public static class ConfirmDataRoamingFragment extends DialogFragment { |
| 1895 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1896 | if (!parent.isAdded()) return; |
| 1897 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1898 | final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment(); |
| 1899 | dialog.setTargetFragment(parent, 0); |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1900 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | @Override |
| 1904 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1905 | final Context context = getActivity(); |
| 1906 | |
| 1907 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1908 | builder.setTitle(R.string.roaming_reenable_title); |
| 1909 | builder.setMessage(R.string.roaming_warning); |
| 1910 | |
| 1911 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1912 | @Override |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1913 | public void onClick(DialogInterface dialog, int which) { |
| 1914 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1915 | if (target != null) { |
| 1916 | target.setDataRoaming(true); |
| 1917 | } |
| 1918 | } |
| 1919 | }); |
| 1920 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1921 | |
| 1922 | return builder.create(); |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * Dialog to request user confirmation before setting |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1928 | * {@link INetworkPolicyManager#setRestrictBackground(boolean)}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1929 | */ |
| 1930 | public static class ConfirmRestrictFragment extends DialogFragment { |
| 1931 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1932 | if (!parent.isAdded()) return; |
| 1933 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1934 | final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment(); |
| 1935 | dialog.setTargetFragment(parent, 0); |
| 1936 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT); |
| 1937 | } |
| 1938 | |
| 1939 | @Override |
| 1940 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1941 | final Context context = getActivity(); |
| 1942 | |
| 1943 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1944 | builder.setTitle(R.string.data_usage_restrict_background_title); |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1945 | builder.setMessage(getString(R.string.data_usage_restrict_background)); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1946 | |
| 1947 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1948 | @Override |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1949 | public void onClick(DialogInterface dialog, int which) { |
| 1950 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1951 | if (target != null) { |
| 1952 | target.setRestrictBackground(true); |
| 1953 | } |
| 1954 | } |
| 1955 | }); |
| 1956 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1957 | |
| 1958 | return builder.create(); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1963 | * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND} |
| 1964 | * change has been denied, usually based on |
| 1965 | * {@link DataUsageSummary#hasLimitedNetworks()}. |
| 1966 | */ |
| 1967 | public static class DeniedRestrictFragment extends DialogFragment { |
| 1968 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1969 | if (!parent.isAdded()) return; |
| 1970 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1971 | final DeniedRestrictFragment dialog = new DeniedRestrictFragment(); |
| 1972 | dialog.setTargetFragment(parent, 0); |
| 1973 | dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT); |
| 1974 | } |
| 1975 | |
| 1976 | @Override |
| 1977 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1978 | final Context context = getActivity(); |
| 1979 | |
| 1980 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1981 | builder.setTitle(R.string.data_usage_app_restrict_background); |
| 1982 | builder.setMessage(R.string.data_usage_restrict_denied_dialog); |
| 1983 | builder.setPositiveButton(android.R.string.ok, null); |
| 1984 | |
| 1985 | return builder.create(); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /** |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1990 | * Dialog to request user confirmation before setting |
| 1991 | * {@link #POLICY_REJECT_METERED_BACKGROUND}. |
| 1992 | */ |
| 1993 | public static class ConfirmAppRestrictFragment extends DialogFragment { |
| 1994 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1995 | if (!parent.isAdded()) return; |
| 1996 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1997 | final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment(); |
| 1998 | dialog.setTargetFragment(parent, 0); |
| 1999 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT); |
| 2000 | } |
| 2001 | |
| 2002 | @Override |
| 2003 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2004 | final Context context = getActivity(); |
| 2005 | |
| 2006 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2007 | builder.setTitle(R.string.data_usage_app_restrict_dialog_title); |
| 2008 | builder.setMessage(R.string.data_usage_app_restrict_dialog); |
| 2009 | |
| 2010 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 2011 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2012 | public void onClick(DialogInterface dialog, int which) { |
| 2013 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 2014 | if (target != null) { |
| 2015 | target.setAppRestrictBackground(true); |
| 2016 | } |
| 2017 | } |
| 2018 | }); |
| 2019 | builder.setNegativeButton(android.R.string.cancel, null); |
| 2020 | |
| 2021 | return builder.create(); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | /** |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2026 | * Compute default tab that should be selected, based on |
| 2027 | * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra. |
| 2028 | */ |
| 2029 | private static String computeTabFromIntent(Intent intent) { |
Jeff Sharkey | 271ec8a | 2011-07-20 16:59:16 -0700 | [diff] [blame] | 2030 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 2031 | if (template == null) return null; |
| 2032 | |
| 2033 | switch (template.getMatchRule()) { |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2034 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2035 | return TAB_3G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2036 | case MATCH_MOBILE_4G: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2037 | return TAB_4G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2038 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2039 | return TAB_MOBILE; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2040 | case MATCH_WIFI: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2041 | return TAB_WIFI; |
| 2042 | default: |
| 2043 | return null; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /** |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2048 | * Background task that loads {@link UidDetail}, binding to |
| 2049 | * {@link DataUsageAdapter} row item when finished. |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2050 | */ |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2051 | private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> { |
| 2052 | private final UidDetailProvider mProvider; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2053 | private final AppItem mItem; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2054 | private final View mTarget; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 2055 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2056 | private UidDetailTask(UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2057 | mProvider = checkNotNull(provider); |
| 2058 | mItem = checkNotNull(item); |
| 2059 | mTarget = checkNotNull(target); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2062 | public static void bindView( |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2063 | UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2064 | final UidDetailTask existing = (UidDetailTask) target.getTag(); |
| 2065 | if (existing != null) { |
| 2066 | existing.cancel(false); |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2067 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2068 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2069 | final UidDetail cachedDetail = provider.getUidDetail(item.appId, false); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2070 | if (cachedDetail != null) { |
| 2071 | bindView(cachedDetail, target); |
| 2072 | } else { |
| 2073 | target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor( |
| 2074 | AsyncTask.THREAD_POOL_EXECUTOR)); |
| 2075 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2078 | private static void bindView(UidDetail detail, View target) { |
| 2079 | final ImageView icon = (ImageView) target.findViewById(android.R.id.icon); |
| 2080 | final TextView title = (TextView) target.findViewById(android.R.id.title); |
| 2081 | |
| 2082 | if (detail != null) { |
| 2083 | icon.setImageDrawable(detail.icon); |
| 2084 | title.setText(detail.label); |
| 2085 | } else { |
| 2086 | icon.setImageDrawable(null); |
| 2087 | title.setText(null); |
| 2088 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2089 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2090 | |
| 2091 | @Override |
| 2092 | protected void onPreExecute() { |
| 2093 | bindView(null, mTarget); |
| 2094 | } |
| 2095 | |
| 2096 | @Override |
| 2097 | protected UidDetail doInBackground(Void... params) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2098 | return mProvider.getUidDetail(mItem.appId, true); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | @Override |
| 2102 | protected void onPostExecute(UidDetail result) { |
| 2103 | bindView(result, mTarget); |
| 2104 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2107 | /** |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2108 | * Test if device has a mobile data radio with SIM in ready state. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2109 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2110 | public static boolean hasReadyMobileRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2111 | if (TEST_RADIOS) { |
| 2112 | return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile"); |
| 2113 | } |
| 2114 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2115 | final ConnectivityManager conn = ConnectivityManager.from(context); |
| 2116 | final TelephonyManager tele = TelephonyManager.from(context); |
| 2117 | |
| 2118 | // require both supported network and ready SIM |
| 2119 | return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * Test if device has a mobile 4G data radio. |
| 2124 | */ |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 2125 | public static boolean hasReadyMobile4gRadio(Context context) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2126 | if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) { |
| 2127 | return false; |
| 2128 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2129 | if (TEST_RADIOS) { |
| 2130 | return SystemProperties.get(TEST_RADIOS_PROP).contains("4g"); |
| 2131 | } |
| 2132 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2133 | final ConnectivityManager conn = ConnectivityManager.from(context); |
| 2134 | final TelephonyManager tele = TelephonyManager.from(context); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2135 | |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2136 | final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX); |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 2137 | final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE) |
| 2138 | && hasReadyMobileRadio(context); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2139 | return hasWimax || hasLte; |
| 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * Test if device has a Wi-Fi data radio. |
| 2144 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2145 | public static boolean hasWifiRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2146 | if (TEST_RADIOS) { |
| 2147 | return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi"); |
| 2148 | } |
| 2149 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2150 | final ConnectivityManager conn = ConnectivityManager.from(context); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2151 | return conn.isNetworkSupported(TYPE_WIFI); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /** |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2155 | * Test if device has an ethernet network connection. |
| 2156 | */ |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2157 | public boolean hasEthernet(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2158 | if (TEST_RADIOS) { |
| 2159 | return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet"); |
| 2160 | } |
| 2161 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2162 | final ConnectivityManager conn = ConnectivityManager.from(context); |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2163 | final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET); |
| 2164 | |
| 2165 | final long ethernetBytes; |
Jeff Sharkey | d878909 | 2012-05-29 10:19:50 -0700 | [diff] [blame] | 2166 | if (mStatsSession != null) { |
| 2167 | try { |
| 2168 | ethernetBytes = mStatsSession.getSummaryForNetwork( |
| 2169 | NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE) |
| 2170 | .getTotalBytes(); |
| 2171 | } catch (RemoteException e) { |
| 2172 | throw new RuntimeException(e); |
| 2173 | } |
| 2174 | } else { |
| 2175 | ethernetBytes = 0; |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Jeff Sharkey | d878909 | 2012-05-29 10:19:50 -0700 | [diff] [blame] | 2178 | // only show ethernet when both hardware present and traffic has occurred |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2179 | return hasEthernet && ethernetBytes > 0; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2183 | * Inflate a {@link Preference} style layout, adding the given {@link View} |
| 2184 | * widget into {@link android.R.id#widget_frame}. |
| 2185 | */ |
| 2186 | private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) { |
| 2187 | final View view = inflater.inflate(R.layout.preference, root, false); |
| 2188 | final LinearLayout widgetFrame = (LinearLayout) view.findViewById( |
| 2189 | android.R.id.widget_frame); |
| 2190 | widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 2191 | return view; |
| 2192 | } |
| 2193 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 2194 | private static View inflateAppTitle( |
| 2195 | LayoutInflater inflater, ViewGroup root, CharSequence label) { |
| 2196 | final TextView view = (TextView) inflater.inflate( |
| 2197 | R.layout.data_usage_app_title, root, false); |
| 2198 | view.setText(label); |
| 2199 | return view; |
| 2200 | } |
| 2201 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2202 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2203 | * Test if any networks are currently limited. |
| 2204 | */ |
| 2205 | private boolean hasLimitedNetworks() { |
| 2206 | return !buildLimitedNetworksList().isEmpty(); |
| 2207 | } |
| 2208 | |
| 2209 | /** |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2210 | * Build string describing currently limited networks, which defines when |
| 2211 | * background data is restricted. |
| 2212 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2213 | @Deprecated |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2214 | private CharSequence buildLimitedNetworksString() { |
| 2215 | final List<CharSequence> limited = buildLimitedNetworksList(); |
| 2216 | |
| 2217 | // handle case where no networks limited |
| 2218 | if (limited.isEmpty()) { |
| 2219 | limited.add(getText(R.string.data_usage_list_none)); |
| 2220 | } |
| 2221 | |
| 2222 | return TextUtils.join(limited); |
| 2223 | } |
| 2224 | |
| 2225 | /** |
| 2226 | * Build list of currently limited networks, which defines when background |
| 2227 | * data is restricted. |
| 2228 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2229 | @Deprecated |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2230 | private List<CharSequence> buildLimitedNetworksList() { |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2231 | final Context context = getActivity(); |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2232 | |
| 2233 | // build combined list of all limited networks |
| 2234 | final ArrayList<CharSequence> limited = Lists.newArrayList(); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2235 | |
| 2236 | final TelephonyManager tele = TelephonyManager.from(context); |
| 2237 | if (tele.getSimState() == SIM_STATE_READY) { |
| 2238 | final String subscriberId = getActiveSubscriberId(context); |
| 2239 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) { |
| 2240 | limited.add(getText(R.string.data_usage_list_mobile)); |
| 2241 | } |
| 2242 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) { |
| 2243 | limited.add(getText(R.string.data_usage_tab_3g)); |
| 2244 | } |
| 2245 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) { |
| 2246 | limited.add(getText(R.string.data_usage_tab_4g)); |
| 2247 | } |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2248 | } |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2249 | |
| 2250 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) { |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2251 | limited.add(getText(R.string.data_usage_tab_wifi)); |
| 2252 | } |
| 2253 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) { |
| 2254 | limited.add(getText(R.string.data_usage_tab_ethernet)); |
| 2255 | } |
| 2256 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2257 | return limited; |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | /** |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 2261 | * Inset both selector and divider {@link Drawable} on the given |
| 2262 | * {@link ListView} by the requested dimensions. |
| 2263 | */ |
| 2264 | private static void insetListViewDrawables(ListView view, int insetSide) { |
| 2265 | final Drawable selector = view.getSelector(); |
| 2266 | final Drawable divider = view.getDivider(); |
| 2267 | |
| 2268 | // fully unregister these drawables so callbacks can be maintained after |
| 2269 | // wrapping below. |
| 2270 | final Drawable stub = new ColorDrawable(Color.TRANSPARENT); |
| 2271 | view.setSelector(stub); |
| 2272 | view.setDivider(stub); |
| 2273 | |
| 2274 | view.setSelector(new InsetBoundsDrawable(selector, insetSide)); |
| 2275 | view.setDivider(new InsetBoundsDrawable(divider, insetSide)); |
| 2276 | } |
| 2277 | |
| 2278 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2279 | * Set {@link android.R.id#title} for a preference view inflated with |
Jeff Sharkey | 52c3f44 | 2011-06-23 00:39:38 -0700 | [diff] [blame] | 2280 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2281 | */ |
| 2282 | private static void setPreferenceTitle(View parent, int resId) { |
| 2283 | final TextView title = (TextView) parent.findViewById(android.R.id.title); |
| 2284 | title.setText(resId); |
| 2285 | } |
| 2286 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2287 | /** |
| 2288 | * Set {@link android.R.id#summary} for a preference view inflated with |
| 2289 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
| 2290 | */ |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2291 | private static void setPreferenceSummary(View parent, CharSequence string) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2292 | final TextView summary = (TextView) parent.findViewById(android.R.id.summary); |
| 2293 | summary.setVisibility(View.VISIBLE); |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2294 | summary.setText(string); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2295 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 2296 | } |