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