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