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