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