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