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