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