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