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