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