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