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