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