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